Computer Science Canada java indentation |
Author: | cool dude [ Wed Jul 05, 2006 8:15 pm ] |
Post subject: | java indentation |
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 ![]() |
Author: | wtd [ Wed Jul 05, 2006 9:33 pm ] |
Post subject: | |
Found easily on Sun's website: http://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html |
Author: | cool dude [ Wed Jul 05, 2006 9:57 pm ] |
Post subject: | |
i noticed how easily you find things on the sun's website all the time! how do you do it? |
Author: | Hikaru79 [ Wed Jul 05, 2006 10:24 pm ] | ||||
Post 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
![]() |
Author: | Cervantes [ 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:
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. |
Author: | Cervantes [ Thu Jul 06, 2006 8:06 am ] |
Post 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. ![]() |
Author: | wtd [ Thu Jul 06, 2006 11:19 am ] |
Post subject: | |
Cervantes wrote: wtd, please don't shoot me.
![]() I would never do anything nearly so merciful. ![]() |
Author: | Aziz [ Thu Jul 13, 2006 5:23 pm ] | ||
Post subject: | |||
Okay, so then, is it bad convention to be more organized with braces, like so:
(note: the braces of lines of there own) |
Author: | Aziz [ Thu Jul 13, 2006 7:12 pm ] |
Post subject: | |
(btw add in those final braces with your imagination, lol, sorry) |
Author: | wtd [ Thu Jul 13, 2006 8:08 pm ] |
Post 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. |
Author: | Aziz [ Fri Jul 14, 2006 8:27 am ] |
Post 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. ![]() |
Author: | wtd [ Fri Jul 14, 2006 8:44 am ] |
Post 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. |
Author: | [Gandalf] [ Fri Jul 14, 2006 6:44 pm ] |
Post 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. ![]() |
Author: | McKenzie [ Fri Jul 14, 2006 8:38 pm ] |
Post 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? |
Author: | Aziz [ Fri Jul 14, 2006 10:56 pm ] |
Post 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 ![]() |
Author: | wtd [ Fri Jul 14, 2006 11:11 pm ] |
Post subject: | |
An argument in favor of the Java convention is that if you're indenting properly, the indentation should indicate levels of scope reasonably well. But mostly it's... "pick your battles." |