[
Date Prev][
Date Next][
Thread Prev][
Thread Next][
Date Index][
Thread Index]
[
List Home]
RE: [stellation-res] Audit: overly complicated expression
|
> >-----Original Message-----
> >From: stellation-res-admin@xxxxxxxxxxxxxxx
> >[mailto:stellation-res-admin@xxxxxxxxxxxxxxx]On Behalf Of Florin Iucha
> >Sent: September 4, 2002 12:23 AM
> >To: stellation
> >Subject: [stellation-res] Audit: overly complicated expression
> >
> >
> >org.eclipse.stellation.workspace.List#listVersions contains the
> >following snippet:
> >
> > String branchName =
> > getArguments().size() > 0 ? getArguments().remove(0)
> > : getWorkspace().getProject() != null ?
> >getWorkspace().getProject().getBranchName()
> > : null;
> >
> > if (branchName==null) branchName = "main";
> >
> >Personally I find ?: expressions where the alternatives are something
> >other than constants or literals hard to parse. When I scan code my eyes
> >get "stuck" to that expression...
> >
> >I think this should be reworked as:
> >
> > String branchName = "main";
> > if (getArguments().size() > 0)
> > {
> > branchName = getArguments().remove(0);
> > }
> > else if (getWorkspace().getProject() != null)
> > {
> > branchName = getWorkspace().getProject().getBranchName();
> > }
> >
> >florin
> >
An alternative formatting using conditionals is:
String branchName =
(getArguments().size() > 0) ? getArguments().remove(0)
: ((getWorkspace().getProject() != NULL)
?
getWorkspace().getProject().getBranchName()
: null);
The guidelines that enhance readibility and reliability are
a) Enclose the condifional in parentheses to avoid tripping over operator
precedence.
b) Align the two action clauses for each conditional under each other.
c) Enclose entire nested conditionals in parentheses to avoid possible
problems with operator precedence.
d) Indent nested conditionals.
This is a good example, though, of how hard it is to find style idioms that
work well for everyone. Thus I find the fragment using conditionals easier
to read than the "if/else" style because I can see the nested structure more
clearly but others will undoubtedly prefer Florin's proposal.
Regards
Jonathan
Personal Email
jgossage@xxxxxxxx
Business Email
jonathan@xxxxxxxxxxxxxx