Click here to Skip to main content
Click here to Skip to main content

Tagged as

Preventing nested code blocks

, 11 Mar 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
The easiest readable and maintainable code, is code that contains no nested blocks.Although sometimes unavoidable, preventing nested code blocks should be part of any coding guideline. Two basic rules:1) Exit First Rule2) DelegateRule 1 simply means that first any condition must be...
The easiest readable and maintainable code, is code that contains no nested blocks.
 
Although sometimes unavoidable, preventing nested code blocks should be part of any coding guideline.
Two basic rules:
1) Exit First Rule
2) Delegate
 
Rule 1 simply means that first any condition must be checked that may lead to an early exit of the method. e.g. consider (pseudo code):
 
void MyMethod(...)
{
   if (x > 0)
   {
      // do your stuff here
   }
}
this can be refactored to:
 
void MyMethod(...)
{
   if (x <= 0)
       return; // exit early, or throw exception

   // do your stuff here

}
 

Rule 2 Delegate means that any loop that contains another loop should be delegated to and executed by another method.
 
e.g. consider (pseudo code):
 
// method will calculate the faculty of x (non-recursive)
int MyMethod(int x)
{
    if (x < 1) return 0;  // exit first rule
    if (x == 1) return 1;  // exit first rule

    int result = 0;
    while (x < max)
    {
        for (int i = 0; i < x; i++)
        {
           //do stuff here;
        }
        x++;
    }
}
 
The inner loop should be put in a separate method. this will not only increase readability but also increase code reuse, maintainability, and reduce code complexity, like so:
 
// method will calculate the faculty of x (non-recursive)
int MyMethod(int x)
{
    if (x < 1) return 0;  // exit first rule
    if (x == 1) return 1;  // exit first rule

    int result = 0;
    while (x < max)
        result += MySubCalculation(x);
 
    x++;
}
 
int MySubCalculation(int x)
{
   // put any exit firsts here

  for (int i = 0; i < x; i++) 
  {
      //do stuff here;
  }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Herre Kuijpers
Architect Capgemini
Netherlands Netherlands
Currently Herre Kuijpers is employed at Capgemini Netherlands for over 10 years, where he developed skills with all kinds of technologies, methodologies and programming languages such as c#, ASP.Net, Silverlight, VC++, Javascript, SQL, UML, RUP, WCF. Currently he fulfills the role of software architect in various projects.
 
Herre Kuijpers is a very experienced software architect with deep knowledge of software design and development on the Microsoft .Net platform. He has a broad knowledge of Microsoft products and knows how these, in combination with custom software, can be optimally implemented in the often complex environment of the customer.

Comments and Discussions

 
GeneralCode output Pinmemberkornman0014-Mar-10 3:37 
GeneralRe: Code output PinmemberHerre Kuijpers14-Mar-10 9:20 
GeneralTechniques are sometimes useful, but not without drawbacks Pinmembersupercat912-Mar-10 4:42 
GeneralRe: Techniques are sometimes useful, but not without drawbacks PinmemberHerre Kuijpers12-Mar-10 5:35 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.141022.2 | Last Updated 12 Mar 2010
Article Copyright 2010 by Herre Kuijpers
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid