aspose file tools*
The moose likes Beginning Java and the fly likes inconsistency in style of code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "inconsistency in style of code" Watch "inconsistency in style of code" New topic
Author

inconsistency in style of code

Thomas Whalen
Ranch Hand

Joined: Aug 26, 2001
Posts: 123
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).]


if you don't know, then ask. if you do know, then share. love is knowledge.
shweta agarwal
Greenhorn

Joined: Aug 30, 2001
Posts: 10
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

Joined: Feb 26, 2001
Posts: 4757
    
    7

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).]


Junilu - [How to Ask Questions] [How to Answer Questions]
Thomas Whalen
Ranch Hand

Joined: Aug 26, 2001
Posts: 123
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

Joined: Feb 26, 2001
Posts: 4757
    
    7

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

Joined: Aug 26, 2001
Posts: 123
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

Joined: Mar 16, 2001
Posts: 986

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.)


Please ignore post, I have no idea what I am talking about.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9047
    
  10
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).]


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

Joined: Aug 26, 2001
Posts: 123
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

Joined: Nov 05, 2001
Posts: 14
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.
 
Don't get me started about those stupid light bulbs.
 
subject: inconsistency in style of code