|
I prefer simpler. It has the potential for less security holes, maintentance, and performance problems. But this all has to do with what the application is and the objective.
Blah blah blah... say your prays...
|
|
|
|
|
"Keep Code Left" (KCL) is the name I've given to a coding style I believe improves code readability and maintainability in almost any procedural language. I've been coding for many (many) years, and reviewing code as a dev manager for much of that time.
Over the years I've come to the conclusion that programmers are abusing if statements by using the then clause to contain the meat of a method, rather than to contain the exception cases. This is most egregiously demonstrated by short examples found in many internet articles which are written like this:
private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
someControl.val = 1;
}
}
The fundamental premise of KCL is that the "meat" of the method should be as un-indented as possible, and indented code is for cases which are errors or uninteresting due to starting data conditions. Most often, these exception cases end in a return statement as there is no more work to be done for that case. This leads to a simple rewrite whose implications are far-reaching (really!).
private void Page_Load(object sender, System.EventArgs e)
{
if (IsPostBack)
return;
someControl.val = 1;
}
Almost by definition this reduces the complexity of the code and increases the maintainability. If your job is to manage any significant body of code, then surely these two goals are front and center for you, each and every working day?
So what about those cockroaches else statements? By following these two points, I've found it is possible to remove over 50% of the else statements from much of the code I read; this reduces or eliminates the indentation, and even in some cases exposes bugs just by this simple refactoring (don't believe me? Try it!).
Code like this:
public IIdentity Identity {
get
{
if (this._identities.Count > 0)
return this._identities[0];
else
return null;
}
}
becomes this:
public IIdentity Identity {
get
{
if (this._identities.Count <= 0)
return null;
return this._identities[0];
}
}
The more lines of code there are in a method, the clearer the code becomes.
I've created a presentation on this topic which builds and expands on these ideas. I've delivered it at a couple of user groups over the past several months, and it's remarkable how many people have approached me at other technical gatherings since then to let me know how much they think the ideas presented have helped improve their code. This has encouraged me to share with the wider community.
Although the presentation is more fun given "live", with interactive Q&A featuring many code samples and discussion, I'd like to share it here, and solicit feedback and perhaps start a lively online discussion.
http://www.slideshare.net/MichaelMickAndrew/keep-code-left-10-1114
|
|
|
|
|
This is hardly new (although it is in direct contradiction to Steve McConnell's advice about only having one exit point from a method). You do know that tools like Resharper actually provide this ability as standard and utilities like NDepends provide this analysis and recommendation for free.
|
|
|
|
|
Pete O'Hanlon wrote: although it is in direct contradiction to Steve McConnell's advice about only
having one exit point from a method Worst advice ever if you work in a OO-language.
How can a "goto endOfMethod" be more readble than a "return"? That is only true if you need multiple statements as a return-block (say, cleanup code), where it indeed would be more difficult to maintain since it means having duplicate code inside your method.
No, limiting the exitpoints of a method to "1" does not improve readability. Yes, I've also heard the argument that it makes it easier to locate where something is returned without having to read the entire function. Well, without reading the entire function you won't know what happened either, and if it is that much reading work then you have a method that is too large.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Some KCL prescriptive anti-pattern constructs, such as “else return” or “else throw” are great candidates for automated tools such as Resharper, and I would think NDepend too. I agree that the points I am making may well be “not new”; I wrote the discussion article because I’ve not seen these simple concepts discussed plainly anywhere else, and it’s the fact that they are so simple yet have such a radical effect on code structure (in what I think is a good way), which encouraged me to write it down.
The “only one exit point from a method” rule is one which has been argued about for many years; my view is that for languages with try-finally syntax, this allows for having your cake and eating it. Simply code a try-finally around the entire content in any method which has one or more return or throw statements (or calls methods which might throw exceptions), and you have the one exit point which McConnell is a strong proponent of.
|
|
|
|
|
So what's your question?
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
The question to the community, in a superficial sense, is simply “What do you think?”.
To be more specific:
Do you program this way? Have you seen other articles that cover this ground? I haven’t, and would like to join that conversation. I think KCL has a lot to contribute.
Does the reader think this a “good” programming style or a “bad” programming style, or somewhere in between. Why? I think we all agree that we should use “good” style/technique, but it is often hard to come to agreement about what constitutes “good” versus “bad”. There is a still a lot of “art” in programming, and one person’s art is another person’s trash.
The thing I like about applying KCL is that it is highly prescriptive and not subjective; this is a good thing for automation and both coders and reviewers don’t need to be coding super heroes to apply it.
And finally a question for a few weeks after reading this: Did you try it and what do you think now?
|
|
|
|
|
mickan wrote: The question to the community, in a superficial sense, is simply “What do you think?”. I think the programming forums are not the place for this type of discussion; it belongs in the Lounge or Soapbox.
|
|
|
|
|
mickan wrote: Over the years I've come to the conclusion that programmers are abusing if statements by using the then clause to contain the meat of a method
Over the years I have come to the conclusion that simple syntax idioms have nothing to do with the real problems that developers face.
Myself I haven't worked anywhere for years where the higher ups have not always been over promising features and even while making the actual promise not even understanding what they are promising. Even worse those they are making the promise to don't know what they want either.
Solving that problem would have a vast impact on software development.
Might note that I have worked at places with explicit requirements as part of the contract and even then they can't get it right.
|
|
|
|
|
I'd love to hear any feed back regarding design, style and architecture.
You can find the source at http://github.com/corvusoft/restbed.
Asynchronous RESTful framework
#include <memory>
#include <cstdlib>
#include <restbed>
using namespace std;
using namespace restbed;
void get_method_handler( const shared_ptr< Session >& session )
{
const auto request = session->get_request( );
size_t content_length = 0;
request->get_header( "Content-Length", content_length );
session->fetch( content_length, [ ]( const shared_ptr< Session >& session,
const Bytes& body )
{
fprintf( stdout, "%.*s\n", ( int ) body.size( ), body.data( ) );
session->close( OK, "Hello, World!", { { "Content-Length", "13" } } );
} );
}
int main( const int, const char** )
{
auto resource = make_shared< Resource >( );
resource->set_path( "/resource" );
resource->set_method_handler( "GET", get_method_handler );
auto settings = make_shared< Settings >( );
settings->set_port( 1984 );
settings->set_default_header( "Connection", "close" );
Service service;
service.publish( resource );
service.start( settings );
return EXIT_SUCCESS;
}
Was told to remove this from the C++ threads. Please let me know if this is still not the right place for this?
|
|
|
|
|
|
What most people here would be interested in, and what would generate the most feedback, is an article/set of articles, going into depth about your code. Show the type of challenges you faced, the decisions you took, why you took them, what you rejected and so on. The forums really aren't the place to try and get the type of feedback you're after.
|
|
|
|
|
Thanks. This sounds like a great idea, will look into it this week.
|
|
|
|
|
|
Hello
Is it possible to get real time data feeds of futures and options from NSE Web site or any other source and i need to create a web application with few logic built to it.
|
|
|
|
|
Yes. Go to the website and find out what information they provide and in what format.
|
|
|
|
|
I'm not sure if this is the correct forum to post this in but here it goes.
I'm working on designing a more robust ETL process for our Enterprise Data Warehouse. The idea is, there would be two servers set up as primary and secondary dispatchers along with (n) number of client listeners.
I would like to be able to have each listener perform some SQL Query and/or a stored procedure using the resources on that specific machine in an effort to maximize both ETL process speed and resource utilization.
My question is:
Is this a situation where I could/should use a Parallel Data Warehouse architecture?
I wouldn't really want, let's say, 20+ computers all performing rather large queries against the same SQL Server instance... that would negate any performance gains I may get from a Distributed architecture.
Thank you for reading this and I appreciate any information you could provide.
I hate users. Not all of them, just the ones who talk.CP member: Al Einstien
|
|
|
|
|
After adding Code Snippets, I am having trouble with Syntax Error in WordPress. Can anyone tell me the solution to solve this issue?
The Error looks like:
Parse error - syntax error, unexpected $end in /public_html/site1/wp-content/themes/my-theme/functions.php on line xxx
|
|
|
|
|
Please,
I don´t finished to read the article:
======================================
Singleton Pattern - Different Flavors
http://www.codeproject.com/Articles/895977/Singleton-Pattern-Different-Flavors
Abhishek Bose - Professional Profile
@abhishek-bose89
First Posted 19 may 2015
We're sorry, but the article you are trying to view was deleted at 20 May 2015.
... WHY?!?!?
======================================
How is possible recover that information?
Is possible contact to Abhishek Bose?
I tried translate to Brasilian Portuguese too.
Please...
Thanks a lot for anything information,
Fabio I.
|
|
|
|
|
I suspect it was deleted because people viewed the content as plagiarized. You can find the content that he rehashed here[^].
|
|
|
|
|
Good Morning Pete,
Thank you.
I try translate this article in "Duolingo" (https://www.duolingo.com/).
Good book.
=================================
http://www.livrariacultura.com.br/p/c-in-depth-11099769# in depth&ref=11099769&_=0.22851076163351536
C# IN DEPTH
Autor: SKEET, JON
Idioma: INGLÊS
Editora: OREILLY & ASSOC
Assunto: Programação
Edição: 1
Ano de Edição: 2013
Ano: 2013
=================================
Thanks,
Fabio Ingenito.
|
|
|
|
|
I am writing an application(C# winforms) that counts calories.
One aspect the the application I would appreciate some ideas or suggestions on is the reporting aspect(a user control within the application).
The reporting control has a graph and a selection of reports that one can run for selected dates or all of the data.
What I am looking to do is to be able to have the report definitions sit outside of the application so that on startup the application fetches the available reports.
This will allow me to distribute the application and reports separately - allowing me to release new reports in the future without releasing a new version of the application.
The report definitions need to contain the logic for how the data is fetched as well as the definition of the chart.
The class holding the data is serialised and loaded on startup and saved to at various points.
So I am open to ideas and suggestions on what I could do.
In essence I am looking for dependency injection with regards to the reporting user control.
Thanks in advance for your ideas
[Edit] Having thought this through a bit more, maybe linking to a DLL dynamically at runtime may be a solution clickety[^] - anyone have any experience of this and/or opinions on it?
Solution
[Edit number 2]I have decided to use a DLL and by making use of reflection query the DLL on the charts available. The DLL will be dynamically loaded at runtime.
The sources of my decision were these two excellent codeproject articles:
Article 1[^]
Article 2[^]
The only issue being that I cannot get generic lists of objects from the assembly due to using .Net 4 so I am using arraylists - heck there may even be a tip(article) in this.
“That which can be asserted without evidence, can be dismissed without evidence.”
― Christopher Hitchens
modified 13-May-15 6:52am.
|
|
|
|
|
how make conect two computer in c#
|
|
|
|
|
Your question is very vague. There are multiple ways and you would have to pick one which suits your needs. So, your first step would be to formalize your requirement. Then you can get a more concrete answer.
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
You need a network cable to connect two computers.
|
|
|
|