Code after validations


Link to this posting

Postby Ursego » 21 Feb 2013, 15:51

If a large code fragment is executed after a few validations (and is placed inside a few if-s), take them all (the fragment and the validations) into a new function and exit that function just after the first failed validation.

It will not only save your code from extra indenting but also convey the following idea:

The whole algorithm (not its part!) is executed after ALL the preliminary validations. The validations are not the part of the algorithm itself - they only govern whether or not the whole subsequent logic should be applied.

If the first line of the function is if not <condition> then return and the code is longer than one screen then the reader immediately knows that all next stuff is done only if the condition is satisfied.

But if the executed stuff is placed between if and end if (the bad way) then the developer is forced to scroll down to see if there is any code after the end if.

See how you can convert a code from monstrous to elegant:

*** BAD code: ***

Code: Select all
private void method1 () {
   [code fragment A]

   if (this.dataOk1()) {
      if (this.dataOk2()) {
         if (this.dataOk3()) {
            [code fragment with its own indents]
         }
      }

   [code fragment B]
}

*** GOOD code: ***

Code: Select all
private void method1 () {
   [code fragment A]

   this.method2();

   [code fragment B]
}

private void method2 () {
   if (!this.dataOk1()) return;
   if (!this.dataOk2()) return;
   if (!this.dataOk3()) return;

   [code fragment with its own indents]
}

Here you can ask: and what about the "single point of exit" rule? I don't want to discuss it here, but this idea produces more problems than solves. I agree with Dijkstra who was strongly opposed to the concept of a single point of exit (it can simplify debugging in particular circumstances, but why do I have suffer from everyday working with more complicated code only for the sake of simplifying possible debugging which, may be, will never happen?).

If a code fragment is executed after a few validations, but you don't want to extract it into a new function.

If a code fragment after many validations is not very long and you don't want to create a function for it, then use a Boolean flag which governs all the business locally:

*** GOOD code : ***

Code: Select all
private void method1 () {
   [code fragment A]

   Boolean ok = this.dataOk1();
   if (ok) ok = this.dataOk2();
   if (ok) ok = this.dataOk3();
   if (ok) {
      [code fragment with its own indents]
   }

   [code fragment B]
}

The methods in all the given examples return Boolean to allow a simple illustration of the idea. Of course, the chances are small that all the called methods will return Boolean :lol: . In the real life, check each method in the way it requires. Often, methods return objects (so you will call the next method only if the returned pointer is not null), or some data which defines whether or not the next method should be called (for example, a status of something). Whatever you check, do that in the same level of indent!
User avatar
Ursego
Site Admin
 
Posts: 143
Joined: 19 Feb 2013, 20:33



Ketones are a more high-octane fuel for your brain than glucose. Become a biohacker and upgrade yourself to version 2.0!



cron
Traffic Counter

eXTReMe Tracker