aspose file tools*
The moose likes Cattle Drive and the fly likes Style query (omitting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » This Site » Cattle Drive
Bookmark "Style query (omitting "else")" Watch "Style query (omitting "else")" New topic
Author

Style query (omitting "else")

Adam Price
Ranch Hand

Joined: Nov 11, 2005
Posts: 95
If I a method that does something like this:

I could leave out the "else" and the associated brackets and get the same output. I haven't done so in my assignments because it felt sloppy, but I wonder if the stylists here agree - I can't find it addressed in the style guide.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
<pre>3 - Coding
3.1 - Constructs to Avoid
Never use do..while. examples and reasoning
Never use "return" in the middle of a method. (reasoning)

</pre>


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Adam Price
Ranch Hand

Joined: Nov 11, 2005
Posts: 95
Originally posted by Marilyn de Queiroz:
<pre>3 - Coding
3.1 - Constructs to Avoid
Never use do..while. examples and reasoning
Never use "return" in the middle of a method. (reasoning)

</pre>


Is there anything besides "return" and exceptions that would prevent the completion of a method?
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Ummm...

  • System.exit(0);
  • while (true) {} (or any infinite loop)
  • Thread.sleep(Long.MAX_VALUE);
  • Synchronization deadlock
  • Turning your computer off


  • Does that answer your question? I'm not sure how it would relate to the earlier part of this thread.


    "I'm not back." - Bill Harding, Twister
    Adam Price
    Ranch Hand

    Joined: Nov 11, 2005
    Posts: 95
    Originally posted by Jim Yingst:


    Does that answer your question? I'm not sure how it would relate to the earlier part of this thread.

    It relates because Marilyn pointed out that the style guide says [bold]return[/bold] shouldn't be in the middle of a method, and I was wondering if there was anything else I could substitute for return that would maintain the spirit of te original question.



    -Adam

    p.s.: Jim - you might enjoy these
    Jim Yingst
    Wanderer
    Sheriff

    Joined: Jan 30, 2000
    Posts: 18671
    If your method has a return type other than void, then there's no way to get your code to compile without at least one return, unless the method always throws an exception instead. My last post gives a bunch of ways that the return or exception might not actually happen, but that seems unrelated to the intent of the question.
    Marilyn de Queiroz
    Sheriff

    Joined: Jul 22, 2000
    Posts: 9044
        
      10
    The intent of prohibiting return in the middle of the method is to prevent the very thing you are trying to do, i.e. create more than one exit point to the method.

    Reasoning: Using "return" in the middle of a method makes it difficult to later break the method into smaller methods. It also forces the developer to consider more than one exit point to a method.
    Adam Price
    Ranch Hand

    Joined: Nov 11, 2005
    Posts: 95
    Originally posted by Marilyn de Queiroz:
    The intent of prohibiting return in the middle of the method is to prevent the very thing you are trying to do, i.e. create more than one exit point to the method.

    Reasoning: Using "return" in the middle of a method makes it difficult to later break the method into smaller methods. It also forces the developer to consider more than one exit point to a method.


    So even the way I originally wrote it is in bad form, then. Am I right that it would be preferable to have:

    [ April 28, 2006: Message edited by: Adam Price ]
    Marilyn de Queiroz
    Sheriff

    Joined: Jul 22, 2000
    Posts: 9044
        
      10
    Correct.
    Adam Price
    Ranch Hand

    Joined: Nov 11, 2005
    Posts: 95
    Originally posted by Marilyn de Queiroz:
    Correct.


    Sooo.....about that last cattledrive assignment I sent in...*shuffle*...there's one little change......
     
    wood burning stoves
     
    subject: Style query (omitting "else")