Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Most people like to look at a method and assume a single point of exit. When you interpolate returns in the middle of loops it breaks up the flow a bit and jars this assumption. That's why many will make the "not clean" argument. There's really nothing dirty about it, but it forces the "human" reader of the program to dig deeper into the method to get a clearer understanding of its flow. Anything you can do to reduce the amount of mental deducing people have to do to understand the flow of your logic is considered a good thing. </p> <p>If I could glance at a method without reading through its internals while getting an overall general understanding of what it does and where it exits then the author did a good job. Trouble arises when a method, which means well initially, grows over time and forces maintainers to juggle multiple states mentally in an exercise to understand it.</p> <p>A good example is the classic guard clause which is considered good practice by some. It's a way of checking the state or a parameter for validity before continuing. If a method is given the wrong stuff then we don't want to continue. See this fake code:</p> <pre><code>public void doSomethingCool(Object stuff) { if(weWereNotExpectingThis(stuff)) return; //guard clause while(somethingHappens()) { doSomethingCool(); } return; } </code></pre> <p>Somebody comes along and says, "hey cool! We it's ok to use return statements in the middle of methods!" They make changes and it becomes:</p> <pre><code>public void doSomethingCool(Object stuff) { if(weWereNotExpectingThis(stuff)) return; //guard clause while(somethingHappens()) { if(somthingHasChanged()) return; //because my team likes early exits, Yay! else { doSomethingCool(); } } return; } </code></pre> <p>Still not too shabby, but more and more changes go in and you end up with this:</p> <pre><code>public void doSomethingCool(Object stuff) { if(weWereNotExpectingThis(stuff)) return; //guard clause while(somethingHappens()) { if(somthingHasChanged()) return; else if(weDetectNewInput()) { doSomethingCool(); } else { doSomethingBoring(); if(weDetectCoolInput()) { doSomethingCool(); continue; } return; } } return; } </code></pre> <p>Now you see where this is going. It is difficult to understand where we jump or what's going on at a single glance.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload