• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

Are the inline conditionals to tough for a junior programmer ?

 
Ranch Hand
Posts: 918
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi People

Are the inline conditionals to tough for a junior programmer ?
by example :



How i got this idea ? I just use Eclipse with Checkstyle, they mean :


Rationale: Some developers find inline conditionals hard to read, so their company's coding standards forbids them.



How is on you guys ?


Regards M.
 
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Originally posted by Mihai Radulescu:

Are the inline conditionals to tough for a junior programmer ?



I think they are okay, however I never use them. I much prefer to look at an if/else statement even if it means a couple more lines of code. Whenever I see those ternary operators my brain has to do more gymnastics than usual to get the logic flow. Personal flavour though I suppose.

But interesting point you bring up about coding for a junior programmer. My spec says to take that into account too. I don't know what everyone elses schooling was like, but patterns are only something that I learned about and more importantly could understand their power after having worked for a couple of years writing code without them.

I'm curious as to whether junior programmers would understand patterns. For example if they saw a Factory pattern instead of a simple call creating a new object then and there ... I think it may confuse then. I am treating a junior programmer as someone who has just graduated with less than 2 years in the workplace.

I am certainly using a few patterns in my solution though.

Cheers, Jared.
 
Ranch Hand
Posts: 1847
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interesting rationale. I'd say that if a programmer finds a standard language feature too difficult he needs to start learning the language instead of complaining about it.

If they said it was harder to read than a traditional if/then/else statement I might agree, especially if you're going to nest them.
But in many situations it's a great way to make code easier to read and reducing code complexity and the need for temporary variables, by inlining an entire if/then/else into another statement.
What's easier to read:

or
 
Jared Cope
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Not an entirely fair comparison of code as there are no need for the tempory variables in an if/else either. This is how I would write it



Which to me is still easier to interpret than the ternary expression. But of course I can still understand the ternary one.

Cheers, Jared.
 
Jeroen T Wenting
Ranch Hand
Posts: 1847
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
According to Bloch (Effective Java) and others that's poor design (multiple return statements from method are not recommended).
 
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Like Jeroen, I find the reasoning to be suspect. I would go futher - if we are to throw out everything in the language that a junior programmer finds difficult then we will end up writing far more code just to get around limitations in the core language.

Having said that, I should admit that I have worked in a company that did ban inline conditionals (one area that my boss and I could not agree on - she didnt like inline conditionals, and I used them all the time). We also had a corporate standard that indentation was set at 3 spaces because it was a compromise between those who wanted 2 spaces and those who wanted 4 spaces :roll: .

I tend to use inline conditionals where something needs to be set based on some value that I don't care about:I dont really care about the sample size in the above example, however I need to adjust my expected probability based on the sample size. The code above is much easier to read (IMHO) than the alternative:Not to mention that the alternative might get warnings from the compiler anyway, simply because I didnt put an else statement in there! So to really keep the compiler happy, I guess I have to write:Bleagh! (Technical term )

Regards, Andrew
 
Jeroen T Wenting
Ranch Hand
Posts: 1847
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
and that last might also be disallowed in some companies that have a coding standard stating you must never initialise a variable in its definition (I've seen it, supposedly it protects you from forgetting that else statement, because if you did the compiler would complain).
 
Mihai Radulescu
Ranch Hand
Posts: 918
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi People

ThanX for your interest.

I also agree with you,the inline conditional must not be a problem for a junior.

Jeroen, I also agree with you


multiple return statements from method are not recommended



But I don't remember to read this in the Blochs book (Effective Java).

What I remember is Flowler - Refactoring(chapter Replace Nested Conditional with Guard Clauses - pg 250 in my edition) where I get something like


If a method has a conditional behavior that does not make clear the normal path of execution use guard clauses.



so a method like :


is easy to read if it looks like


OK, you can also break the big method in other small methods and .....

What I try to say is multiple returns are a pour technique but there are cases when, well, they can be useful.

Regards M.
 
Jeroen T Wenting
Ranch Hand
Posts: 1847
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Might have been Robert Martin, but I think I read it in a work by Bloch as well some time (could have been on the net).
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jeroen T Wenting:
multiple return statements from method are not recommended



Can anyone explain to me why they're not? I tend to use multiple returns in methods, sometimes even just to exit them. What exactly is wrong with that?
 
Mihai Radulescu
Ranch Hand
Posts: 918
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jeroen,
What you mean by Robert Martin the "Agile Software Development. Principles, Patterns, and Practices" ?

Oliver, multiple returns are a "bad yuyus" because if they are use in excess can lead on confusions.
Sometimes can be also useful (see my last post or Fowler - Refactoring).


sometimes even just to exit them



This can be also a "bad yuyu", if you need to leave a method because something just happen( ) why you don't use an Exception ?
Can you provide an example ?

Regards M.
[ July 06, 2006: Message edited by: Mihai Radulescu ]
 
Oliver Weikopf
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mihai Radulescu:
Can you provide an example ?
[ July 06, 2006: Message edited by: Mihai Radulescu ][/QB]



Sure. This is what I do when the user presses ok in the dialog that lets him enter the id of the user to book a record for:



And this one is searching something in a list, does something with it, then exits.



I know that in both cases there are other way to solve it, but I don't see anything wrong with the way I do it. I find it elegant and simple.
 
Mihai Radulescu
Ranch Hand
Posts: 918
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oliver,

I agree with you :


I know that in both cases there are other way to solve it,



We land on this cases when we pack to much in one method (you can see it in your second example - the for loop - is enought to "break" the iteration and to pack the rest in other method), but we can "refactor" this on the next code review.

The code style guides are helping you to keep a clean(& consitent) code. You can use it(them) where it make sense or not.

I am wonder what Andrew think about it ?

Regards M.
[ July 06, 2006: Message edited by: Mihai Radulescu ]
 
Jeroen T Wenting
Ranch Hand
Posts: 1847
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's the book Mihai.

Multiple return statements make code brittle, errorprone, hard to test, and hard to debug.
A method should ideally have a single path of execution, not multiple entry and exit points.
 
Andrew Monkhouse
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mihai,

No surpises here - I agree with the comments from Martin Fowler, and the generally held notion that multiple return statements are bad, 'mkay.

Regards, Andrew
reply
    Bookmark Topic Watch Topic
  • New Topic