• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

inconsistency in style of code

 
Thomas Whalen
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why do i see code where braces are used to define the body of the class, yet in the functions braces are not used this way? the Deitel & Deitel book does this same thing, and i see it over at SourceForge.
thomas
[This message has been edited by Thomas Whalen (edited November 15, 2001).]
 
shweta agarwal
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
u always use the curly braces {} to indicate a block of code,and they mean the same in class bodies or functions.
 
Junilu Lacar
Bartender
Posts: 7486
50
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thomas,
Are you talking about things like if-then or while that control only one statement? You only need curlies if you have more than one statement in the block, otherwise the curlies are optional. It is good practice, however, to put them anyway because it will make it less likely for bugs to creep into your code if another statement needs to be added to the block at a later time.
<pre>
// legal
if (condition)
doSomething();

// a little safer for maintenance purposes
if (condition) {
doSomething();
}

// because it's less likely for this type of
// bug to creep in:
if (condition)
doSomething();
doThisToo(); // should be done only if condition is true
</pre>
------------------
Junilu Lacar
Sun Certified Programmer for the Java� 2 Platform
[This message has been edited by JUNILU LACAR (edited November 15, 2001).]
 
Thomas Whalen
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i should have been more clear. even though i am still rather new to programming (aka NO REAL PROGRAMS YET), i understand the reason behind the braces, both for the compiler and for readability. what i don't understand is why i see code that is indented properly for the class, but for the function the left brace is right after the function's parameter list.
 
Junilu Lacar
Bartender
Posts: 7486
50
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, THAT. Well, they don't have Marilyn to nit-pick them
Seriously though, "opening brace for classes on a separate line; opening brace for methods right after the parameter list" may not be a very common convention but as long as it's made known and followed, it doesn't really matter that much. Maybe some find that it emphasizes the body of the class a little more, I don't know.
Junilu
 
Thomas Whalen
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i know, i know. i tend to go overboard about some things
for me, learning programming is a totally different culture than working in a factory. right now i am just trying to establish standards for myself, everything from having Marcus' picture on my computer desktop to drinking water so that i can function halfway decently (you should see me on soda), to slicking my hair back so that i can walk the talk....er, i mean talk the walk...hehe
 
William Barnes
Ranch Hand
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The left curly "{" can be on the same line, or the next line. It is a style thing. Code works the same. Just like the number of spaces you use to indent, only a style thing, code works the same.
(In Java they are called methods not functions.)
 
Marilyn de Queiroz
Sheriff
Posts: 9065
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out this Style Guide. Most companies each have their own style guide. Sun's style guide uses the K&R style braces where the opening brace is at the end of the line of code. Many others use the style required by the JavaRanch Style Guide.

Consistant use of a style guide makes your code more readable.

[This message has been edited by Marilyn deQueiroz (edited November 15, 2001).]
 
Thomas Whalen
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oops, yeah, they are called methods. that's what i get for learning the other languages. thanks for the link Marilyn, this ought to be interesting reading.
 
Bigglesworth Naruki
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look here instead: http://watson-net.com/jargon/lexicon.asp
The key is consistency. Find a style you like and stick with it. The idea of doing it one way for methods and another way for control blocks is just going to confuse everybody.
If your company has a style guide, follow that. If not, try using the 1TBS (my fave), since that's what Sun seems to prefer. Check it out here: http://java.sun.com/docs/codeconv/html/CodeConventions.doc10.html#186.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic