srry if this was asked before, but i haven't really come across it. i'm just wondering about the indentations. for example, after the class name would you put "{" on the same line or the next line, and if the same line do you leave a space? does this apply to whenever you use "{" i.e. for loop, if statements, etc. Also when you write comments do you have them on the same line as the code or on line on top? also is there any advantage of using switch (cases) over if statements?
Any more insightful information on style would be appreciated. thanks
Sponsor Sponsor
wtd
Posted: Wed Jul 05, 2006 9:33 pm Post subject: (No subject)
Posted: Wed Jul 05, 2006 9:57 pm Post subject: (No subject)
i noticed how easily you find things on the sun's website all the time! how do you do it?
Hikaru79
Posted: Wed Jul 05, 2006 10:24 pm Post subject: (No subject)
cool dude wrote:
i noticed how easily you find things on the sun's website all the time! how do you do it?
Try prefixing all your Google searches with
code:
site:java.sun.com
. For example, searching
code:
site:java.sun.com code convention
will return the relevant site on the first hit
Cervantes
Posted: Thu Jul 06, 2006 7:50 am Post subject: Re: java indentation
cool dude wrote:
also is there any advantage of using switch (cases) over if statements?
Sure is.
Both are a form of conditional, but they work differently. One of the main advantages of a switch construct is that you only write the condition once. If you were using an if .. else if .. type construct, you'd have to repeat that condition for every if conditional.
Of course, the disadvantage to only writing the conditional once is that that is the conditional for the whole construct. It's less flexible this way.
The other advantage is you can use this "fall through" behaviour in Java. If your first case succeeds, you don't necessarily have to exit the entire construct (but you can if you put a break at the end of that case). This would be like the following structure using if statements:
code:
if condition
{
// ...
}
if condition
// ...
}
// ...
But with far less typing.
I suspect a switch construct is faster if your condition is large and gangly. I suspect it must store the evaluation of the condition in memory and compare that spot in memory to the cases. Of course, you could do this with an if structure too, by first evaluating your condition and storing it in a variable. But then that's still more work.
Cervantes
Posted: Thu Jul 06, 2006 8:06 am Post subject: (No subject)
Sorry about the lack of proper indentation (and missing a brace) in that code. This of all threads would have been the thread to format code properly. Can you tell I don't work in [Java Help]?
wtd, please don't shoot me.
wtd
Posted: Thu Jul 06, 2006 11:19 am Post subject: (No subject)
Cervantes wrote:
wtd, please don't shoot me.
I would never do anything nearly so merciful.
Aziz
Posted: Thu Jul 13, 2006 5:23 pm Post subject: (No subject)
Okay, so then, is it bad convention to be more organized with braces, like so:
Posted: Thu Jul 13, 2006 7:12 pm Post subject: (No subject)
(btw add in those final braces with your imagination, lol, sorry)
wtd
Posted: Thu Jul 13, 2006 8:08 pm Post subject: (No subject)
With regards to Java, I would say that such use of braces is improper, but in the grand scheme of things, a minor fault if you get indentation right.
Aziz
Posted: Fri Jul 14, 2006 8:27 am Post subject: (No subject)
Really? Interesting. I always thought it was much more organized that way. Well, I guess it's as good a time as every to change right now, huh? I'm gonna read that Java Conventions tutorial, later. My lady just bought me Harvest Moon and I think Java's set aside for a bit.
wtd
Posted: Fri Jul 14, 2006 8:44 am Post subject: (No subject)
It largely has to do with the style preferred by the community at large. Sometimes it's not worth fighting everyone else out there.
[Gandalf]
Posted: Fri Jul 14, 2006 6:44 pm Post subject: (No subject)
wtd wrote:
It largely has to do with the style preferred by the community at large. Sometimes it's not worth fighting everyone else out there.
Maybe you remember my take on this... Anyway, I personally condone the style Aziz showed before. It'll do you more good if you ever try out C#, C++, or any of the like. And it is neater, cleaner, more organized, better.
That said, I am now a hypocrite since I reverted to the "evil style" a while ago. Only in Java, that is.
McKenzie
Posted: Fri Jul 14, 2006 8:38 pm Post subject: (No subject)
wtd,
The guys at JavaRanch feel that it is poor style to put constants in all capitals because, obviously it goes against the principle of abstraction. When it's Sun's language, I follow their guide. I notice that guide was made in 1999, has the preferred style for constants changed, or are the guys at JavaRanch just code snobs?
Aziz
Posted: Fri Jul 14, 2006 10:56 pm Post subject: (No subject)
[Gandalf] wrote:
wtd wrote:
It largely has to do with the style preferred by the community at large. Sometimes it's not worth fighting everyone else out there.
Maybe you remember my take on this... Anyway, I personally condone the style Aziz showed before. It'll do you more good if you ever try out C#, C++, or any of the like. And it is neater, cleaner, more organized, better.
That said, I am now a hypocrite since I reverted to the "evil style" a while ago. Only in Java, that is.
True, I've always thought it was cleaner, and it led to more readable code. You can see what's inside a method or condition by glance (for example, when you're scrolling through a long source file). Though, the other way seems for effecient, using less lines. Does that really affect filesize? I don't think much, eh, since a newline is just one character, correct? Anywho, I'm pretty sure that's how RTP indents. I was so use to the auto-indent in Turing and kept getting pissed of when my JCreator did nothing with F2