Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 java indentation
Index -> Programming, Java -> Java Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
cool dude




PostPosted: 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 Smile
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Wed Jul 05, 2006 9:33 pm   Post subject: (No subject)

Found easily on Sun's website:

http://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html
cool dude




PostPosted: 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




PostPosted: 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 Smile
Cervantes




PostPosted: 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




PostPosted: 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. Razz
wtd




PostPosted: Thu Jul 06, 2006 11:19 am   Post subject: (No subject)

Cervantes wrote:
wtd, please don't shoot me. Razz


I would never do anything nearly so merciful. Wink
Aziz




PostPosted: 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:

Java:
public class Monkey
{
   private int amountOfShit;
   private boolean shitInHand;

   public Monkey()
   {
      amountOfShit = 0;
      shitInHand = false;
   }

   public void eat(int amountOfFood)
   {
      amountOfShit += amountOfFood;
   }

   public void shitIntoHand()
   {
      amountOfShit -= 10;
      shitInHand = true;
   }

   public boolean throwShit(Person p)
   {
      if (!shitInHand)
      {
         return false;
      }
      else if (!p.isLaughingAt(this))
      {
         return false;
      }

      shitInHand = false;
      p.hitInFaceWithMonkeyShit();
      return true;


(note: the braces of lines of there own)
Sponsor
Sponsor
Sponsor
sponsor
Aziz




PostPosted: Thu Jul 13, 2006 7:12 pm   Post subject: (No subject)

(btw add in those final braces with your imagination, lol, sorry)
wtd




PostPosted: 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




PostPosted: 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. Laughing
wtd




PostPosted: 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]




PostPosted: 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. Sad
McKenzie




PostPosted: 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




PostPosted: 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. Sad


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 Laughing
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 16 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: