• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

inconsistency in style of code

 
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).]
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1067
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.)
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Oh, sure, you could do that. Or you could eat some pie. While reading this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic