IMHO there is not any perfect coding style. In some courses at university I had learnt only to use one return statement per method, which actually leads to braceful code that is even more unreadable than the branching code above. This is due to the fact you have to carry along the method result through the whole method, making sure, your result is not modifed any more ones it was assigned.
On the opposite early exit statements like returns or throwing exception is some kind of assertion, making sure your call fulfills some conditions before entering the "main processing block". Indeed I believe if your exit condition are trivial and the exiting code does not calculate too much, early exiting is an useful pattern. If you have on the over side complexer exit bodies, it would be more readable to use a convential if-else, because both exit codes are evenly weighted in your code. br, Sven 2009/9/30 Mohammad Nour El-Din <[email protected]> > Hi Jean... > > :) I just pointed to the part of the if...else statement, but if > you went through this section of the Java Coding Standards you will > see all statements that can have code blocks. > > On Wed, Sep 30, 2009 at 10:08 AM, Monteiro Jean-Louis > <[email protected]> wrote: > > Hi Mohammad, David, > > > > I'm aware of some Java Conventions :) > > But IMO, your link only shows how to write a if/else statement and as far > I can understand, it was not really the point David referred to > > By the way, I prefer the second form cause it's easier to read and makes > the code more maintainable. > > > > Hope it helps. > > > > Jean-Louis > > > > -----Message d'origine----- > > De : Mohammad Nour El-Din [mailto:[email protected]] > > Envoyé : mercredi 30 septembre 2009 09:46 > > À : [email protected] > > Objet : Re: Style: "Flattening" code blocks > > > > Hi David... > > > > Actually the blocking style os mentioned in the Java Copding > > Standards [1]. IMHO I see it very readable than the non-braces form. > > > > > > [1] - > http://java.sun.com/docs/codeconv/html/CodeConventions.doc6.html#449 > > > > On Wed, Sep 30, 2009 at 8:00 AM, David Blevins <[email protected]> > wrote: > >> There are places in the code that have excessive nesting. For some > reason > >> books show this and everyone does it and I certainly did at first too, > but > >> having to deal with trees of logic 3-4 levels deep can really be a pain. > >> Many times the logic can be made completely linear with no else cases. > A > >> big sign of that is when you repeat your else case many times. > >> > >> I started to work an example in email, but refactoring is hard to > describe > >> once you get beyond one or two steps and decided to just show you what I > >> mean: > >> > >> > >> http://people.apache.org/~dblevins/ExcessNesting.mov<http://people.apache.org/%7Edblevins/ExcessNesting.mov> > >> > >> That video basically shows how this: > >> > >> public boolean hasBinding2(List<Class<? extends Annotation>> > >> bindingTypes, List<Annotation> annots) > >> { > >> boolean result = false; > >> > >> if (bindingTypes != null && annots != null && > (bindingTypes.size() == > >> annots.size())) > >> { > >> int i = 0; > >> for (Class<? extends Annotation> bindingType : bindingTypes) > >> { > >> if (this.interceptorBindingSet.containsKey(bindingType)) > >> { > >> Annotation target = > >> this.interceptorBindingSet.get(bindingType); > >> if (AnnotationUtil.hasAnnotationMember(bindingType, > >> annots.get(i), target)) > >> { > >> result = true; > >> } > >> else > >> { > >> return false; > >> } > >> } > >> else > >> { > >> return false; > >> } > >> > >> i++; > >> } > >> } > >> > >> return result; > >> } > >> > >> Can be turned into this: > >> > >> public boolean hasBinding(List<Class<? extends Annotation>> > bindingTypes, > >> List<Annotation> annots) > >> { > >> if (bindingTypes == null || annots == null) return false; > >> if (bindingTypes.size() != annots.size()) return false; > >> if (bindingTypes.size() == 0) return false; > >> > >> int i = 0; > >> for (Class<? extends Annotation> bindingType : bindingTypes) > >> { > >> Annotation target = > this.interceptorBindingSet.get(bindingType); > >> > >> if (target == null) return false; > >> > >> if (!AnnotationUtil.hasAnnotationMember(bindingType, > >> annots.get(i), target)) return false; > >> > >> i++; > >> } > >> > >> return true; > >> } > >> > >> > >> Not sure if people like this style of code, but figured I'd share and > see > >> what you all thought. > >> > >> > >> -David > >> > >> > > > > > > > > -- > > Thanks > > - Mohammad Nour > > - LinkedIn: http://www.linkedin.com/in/mnour > > ---- > > "Life is like riding a bicycle. To keep your balance you must keep > moving" > > - Albert Einstein > > > > > > > > Ce message et les pièces jointes sont confidentiels et réservés à l'usage > exclusif de ses destinataires. Il peut également être protégé par le secret > professionnel. Si vous recevez ce message par erreur, merci d'en avertir > immédiatement l'expéditeur et de le détruire. L'intégrité du message ne > pouvant être assurée sur Internet, la responsabilité du groupe Atos Origin > ne pourra être recherchée quant au contenu de ce message. Bien que les > meilleurs efforts soient faits pour maintenir cette transmission exempte de > tout virus, l'expéditeur ne donne aucune garantie à cet égard et sa > responsabilité ne saurait être recherchée pour tout dommage résultant d'un > virus transmis. > > > > This e-mail and the documents attached are confidential and intended > solely for the addressee; it may also be privileged. If you receive this > e-mail in error, please notify the sender immediately and destroy it. As its > integrity cannot be secured on the Internet, the Atos Origin group liability > cannot be triggered for the message content. Although the sender endeavours > to maintain a computer virus-free network, the sender does not warrant that > this transmission is virus-free and will not be liable for any damages > resulting from any virus transmitted. > > > > > > > > -- > Thanks > - Mohammad Nour > - LinkedIn: http://www.linkedin.com/in/mnour > ---- > "Life is like riding a bicycle. To keep your balance you must keep moving" > - Albert Einstein >
