You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@openwebbeans.apache.org by David Blevins <da...@visi.com> on 2009/09/30 08:00:50 UTC
Style: "Flattening" code blocks
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
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
Re: Style: "Flattening" code blocks
Posted by David Blevins <da...@visi.com>.
Great. Good to know where everyone's head is at style wise.
I made the described change. Broke the single line ifs into multi
line braced style as Mark suggested -- both styles are used in the
code now, but I don't have a problem throwing them in.
I like single line simple ifs for returns, but wish java allowed you
to put them at the left side of the statement where they are more
visible -- something I like about Perl.
public boolean hasBinding(List<Class<? extends Annotation>>
bindingTypes, List<Annotation> annots)
{
return false if (bindingTypes == null || annots == null);
return false if (bindingTypes.size() != annots.size());
return false if (bindingTypes.size() == 0);
int i = 0;
for (Class<? extends Annotation> bindingType : bindingTypes)
{
Annotation target =
this.interceptorBindingSet.get(bindingType);
return false if (target == null);
return false if (!
AnnotationUtil.hasAnnotationMember(bindingType, annots.get(i), target));
i++;
}
return true;
}
Works out sort of like a ternary but without the else case, which is
even shorter. A guy can dream. Maybe we'll see it in Java 11 (or
some far off version).
-David
On Sep 30, 2009, at 3:17 AM, Gurkan Erdogdu wrote:
> Hi David;
>
> Actually both styles (brace like codes and fail-early like codes)
> exist in
> the current implementation.
>
> IMO, parameter/-error condition- early checking at the start of
> method is
> always a good practice. For example, we use "Asserts.notNull()" like
> methods
> in several utility methods.
>
> --Gurkan
>
> 2009/9/30 David Blevins <da...@visi.com>
>
>> 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
>>
>>
>
>
> --
> Gurkan Erdogdu
> http://gurkanerdogdu.blogspot.com
Re: Style: "Flattening" code blocks
Posted by Gurkan Erdogdu <cg...@gmail.com>.
Hi David;
Actually both styles (brace like codes and fail-early like codes) exist in
the current implementation.
IMO, parameter/-error condition- early checking at the start of method is
always a good practice. For example, we use "Asserts.notNull()" like methods
in several utility methods.
--Gurkan
2009/9/30 David Blevins <da...@visi.com>
> 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
>
>
--
Gurkan Erdogdu
http://gurkanerdogdu.blogspot.com
Re: Style: "Flattening" code blocks
Posted by Sven Linstaedt <sv...@googlemail.com>.
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 <no...@gmail.com>
> 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
> <je...@atosorigin.com> 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:nour.mohammad@gmail.com]
> > Envoyé : mercredi 30 septembre 2009 09:46
> > À : openwebbeans-dev@incubator.apache.org
> > 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 <da...@visi.com>
> 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
>
Re: Style: "Flattening" code blocks
Posted by Mohammad Nour El-Din <no...@gmail.com>.
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
<je...@atosorigin.com> 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:nour.mohammad@gmail.com]
> Envoyé : mercredi 30 septembre 2009 09:46
> À : openwebbeans-dev@incubator.apache.org
> 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 <da...@visi.com> 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
>>
>> 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
RE: Style: "Flattening" code blocks
Posted by Monteiro Jean-Louis <je...@atosorigin.com>.
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:nour.mohammad@gmail.com]
Envoyé : mercredi 30 septembre 2009 09:46
À : openwebbeans-dev@incubator.apache.org
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 <da...@visi.com> 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
>
> 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.
Re: Style: "Flattening" code blocks
Posted by Mohammad Nour El-Din <no...@gmail.com>.
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 <da...@visi.com> 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
>
> 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
Re: Style: "Flattening" code blocks
Posted by Mark Struberg <st...@yahoo.de>.
gnn, was to quick at pressing the send button
I still like it but would prefer to have bracelets even for 1-line statements. This makes maintaining the code a lot easier later.
LieGrue,
strub
--- On Wed, 9/30/09, Mark Struberg <st...@yahoo.de> wrote:
> From: Mark Struberg <st...@yahoo.de>
> Subject: Re: Style: "Flattening" code blocks
> To: openwebbeans-dev@incubator.apache.org
> Date: Wednesday, September 30, 2009, 12:12 PM
> I like it
> +1
> :)
>
> LieGrue,
> strub
>
> --- On Wed, 9/30/09, David Blevins <da...@visi.com>
> wrote:
>
> > From: David Blevins <da...@visi.com>
> > Subject: Style: "Flattening" code blocks
> > To: openwebbeans-dev@incubator.apache.org
> > Date: Wednesday, September 30, 2009, 8:00 AM
> > 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
> >
> > 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
> >
> >
>
>
>
>
Re: Style: "Flattening" code blocks
Posted by Mark Struberg <st...@yahoo.de>.
I like it
+1
:)
LieGrue,
strub
--- On Wed, 9/30/09, David Blevins <da...@visi.com> wrote:
> From: David Blevins <da...@visi.com>
> Subject: Style: "Flattening" code blocks
> To: openwebbeans-dev@incubator.apache.org
> Date: Wednesday, September 30, 2009, 8:00 AM
> 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
>
> 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
>
>