Click here to Skip to main content
15,896,522 members
Home / Discussions / Design and Architecture
   

Design and Architecture

 
GeneralRe: Complex Architecture? Pin
Eddy Vluggen10-Jul-15 7:17
professionalEddy Vluggen10-Jul-15 7:17 
GeneralRe: Complex Architecture? Pin
jschell10-Jul-15 14:27
jschell10-Jul-15 14:27 
AnswerRe: Complex Architecture? Pin
jschell10-Jul-15 14:25
jschell10-Jul-15 14:25 
AnswerRe: Complex Architecture? Pin
Dominic Burford17-Jul-15 3:07
professionalDominic Burford17-Jul-15 3:07 
GeneralRe: Complex Architecture? Pin
Chris Quinn17-Jul-15 3:46
Chris Quinn17-Jul-15 3:46 
GeneralRe: Complex Architecture? Pin
Dominic Burford17-Jul-15 5:05
professionalDominic Burford17-Jul-15 5:05 
AnswerRe: Complex Architecture? Pin
Kaaron7-Oct-15 14:25
Kaaron7-Oct-15 14:25 
Suggestion'else' statements: the cockroaches of programming? Pin
mickan4-Jul-15 8:35
professionalmickan4-Jul-15 8:35 
"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:
C#
private void Page_Load(object sender, System.EventArgs e)
{
    if (!IsPostBack)
    {
        // do work
        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!).
C#
private void Page_Load(object sender, System.EventArgs e)
{
    if (IsPostBack)
        return;

    // do work with no indents, and no "complex" logic to figure out
    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:
C#
public IIdentity Identity {
    get 
    {
	if (this._identities.Count > 0)
	     return this._identities[0];
	else 
             return null;
    }
}

becomes this:
C#
public IIdentity Identity {
    get
    {
        if (this._identities.Count <= 0)
            return null;  // uninteresting case, get outta here!
        
        // real work - who needs an else anyway?
        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
GeneralRe: 'else' statements: the cockroaches of programming? Pin
Pete O'Hanlon4-Jul-15 10:52
mvePete O'Hanlon4-Jul-15 10:52 
GeneralRe: 'else' statements: the cockroaches of programming? Pin
Eddy Vluggen5-Jul-15 0:38
professionalEddy Vluggen5-Jul-15 0:38 
GeneralRe: 'else' statements: the cockroaches of programming? Pin
mickan5-Jul-15 8:01
professionalmickan5-Jul-15 8:01 
AnswerRe: 'else' statements: the cockroaches of programming? Pin
Richard Andrew x644-Jul-15 11:11
professionalRichard Andrew x644-Jul-15 11:11 
GeneralRe: 'else' statements: the cockroaches of programming? Pin
mickan5-Jul-15 8:06
professionalmickan5-Jul-15 8:06 
GeneralRe: 'else' statements: the cockroaches of programming? Pin
Richard MacCutchan5-Jul-15 21:38
mveRichard MacCutchan5-Jul-15 21:38 
GeneralRe: 'else' statements: the cockroaches of programming? Pin
jschell10-Jul-15 14:32
jschell10-Jul-15 14:32 
QuestionJust released the latest version of my open source project... Pin
Corvusoft21-Jun-15 21:51
Corvusoft21-Jun-15 21:51 
AnswerRe: Just released the latest version of my open source project... Pin
Richard MacCutchan21-Jun-15 22:19
mveRichard MacCutchan21-Jun-15 22:19 
AnswerRe: Just released the latest version of my open source project... Pin
Pete O'Hanlon21-Jun-15 22:51
mvePete O'Hanlon21-Jun-15 22:51 
GeneralRe: Just released the latest version of my open source project... Pin
Corvusoft21-Jun-15 23:44
Corvusoft21-Jun-15 23:44 
GeneralRe: Just released the latest version of my open source project... Pin
Pete O'Hanlon21-Jun-15 23:54
mvePete O'Hanlon21-Jun-15 23:54 
QuestionReal time RTDS Feeds from nse SIte Pin
Member 1178089120-Jun-15 23:11
Member 1178089120-Jun-15 23:11 
AnswerRe: Real time RTDS Feeds from nse SIte Pin
Richard MacCutchan21-Jun-15 1:35
mveRichard MacCutchan21-Jun-15 1:35 
QuestionDistributed Parallel ETL Load processing Pin
OMalleyW16-Jun-15 2:59
OMalleyW16-Jun-15 2:59 
QuestionStruggling to Fix the Syntax Error in WordPress Pin
Lucy Barret11-Jun-15 2:50
professionalLucy Barret11-Jun-15 2:50 
QuestionSingleton Pattern - Different Flavors Pin
FabioI21-May-15 1:05
FabioI21-May-15 1:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.