
-----------------------------------
cool dude
Wed Jul 05, 2006 8:15 pm

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  :)

-----------------------------------
wtd
Wed Jul 05, 2006 9:33 pm


-----------------------------------
Found easily on Sun's website:

http://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html

-----------------------------------
cool dude
Wed Jul 05, 2006 9:57 pm


-----------------------------------
i noticed how easily you find things on the sun's website all the time! how do you do it?

-----------------------------------
Hikaru79
Wed Jul 05, 2006 10:24 pm


-----------------------------------
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 site:java.sun.com. For example, searching site:java.sun.com code convention will return the relevant site on the first hit :)

-----------------------------------
Cervantes
Thu Jul 06, 2006 7:50 am

Re: java indentation
-----------------------------------
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:

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
Thu Jul 06, 2006 8:06 am


-----------------------------------
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. :P

-----------------------------------
wtd
Thu Jul 06, 2006 11:19 am


-----------------------------------
wtd, please don't shoot me. :P

I would never do anything nearly so merciful.  ;)

-----------------------------------
Aziz
Thu Jul 13, 2006 5:23 pm


-----------------------------------
Okay, so then, is it bad convention to be more organized with braces, like so:

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)

-----------------------------------
Aziz
Thu Jul 13, 2006 7:12 pm


-----------------------------------
(btw add in those final braces with your imagination, lol, sorry)

-----------------------------------
wtd
Thu Jul 13, 2006 8:08 pm


-----------------------------------
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
Fri Jul 14, 2006 8:27 am


-----------------------------------
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. :lol:

-----------------------------------
wtd
Fri Jul 14, 2006 8:44 am


-----------------------------------
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]
Fri Jul 14, 2006 6:44 pm


-----------------------------------
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
Fri Jul 14, 2006 8:38 pm


-----------------------------------
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
Fri Jul 14, 2006 10:56 pm


-----------------------------------
"]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 :lol:

-----------------------------------
wtd
Fri Jul 14, 2006 11:11 pm


-----------------------------------
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."
