• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

Style query (omitting "else")

 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 9103
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<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>
 
Adam Price
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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?
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
     
    Adam Price
    Ranch Hand
    Posts: 95
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    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
    Posts: 18671
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 9103
    12
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 95
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    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
    Posts: 9103
    12
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Correct.
     
    Adam Price
    Ranch Hand
    Posts: 95
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Originally posted by Marilyn de Queiroz:
    Correct.



    Sooo.....about that last cattledrive assignment I sent in...*shuffle*...there's one little change......
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic