Jump to content

really quick java/C question; code readability


Dak

Recommended Posts

I've just started learning java, and i'm finding the indentation of the {}s a bit hard to follow (i.e., i can easily imagine missing closing a {}).

 

basically, for the following:

 

/**
*quick learny thingy
*/

class TestApp {	
public static void main (String[] args) {
	System.out.println("I have written a thingy! :-)"); 
     } 
}

 

I have a really strong inclination to write as:

 

/**
*quick learny thingy
*/

class TestApp 	{
public static void main (String[] args) {
	System.out.println("I have written a thingy! :-)");
					} 
	}

 

My question is: for someone who's experienced in java/C, would the latter make it harder to read or not really matter?

 

If it makes it harder to read for most people, i'd rather not get into the bad habbit of doing it. otherwize, i'll use indentation so it's a bit clearer to me from the outset.

Link to comment
Share on other sites

Don't do so, it looks terrible. Write

 

/**
*quick learny thingy
*/

class TestApp 
{	
public static void main (String[] args) 
       {
	System.out.println("I have written a thingy! :-)"); 
       } 
}

if you absolutely need the parentheses to be in the same column (I consider it a waste of lines but that's just my personal preference).

Link to comment
Share on other sites

Out of the 3 options I see here. I say the first one is the easiest to read. I find the one Atheist posted marginally annoying but not unusable, and the second one posted by Dak to be pretty much impossible to read as I'm so not used to it.

Link to comment
Share on other sites

how about a slight alteration of Atheists' way:

 

/**
*quick learny thingy
*/

class TestApp 
       {	
public static void main (String[] args) 
               {
	System.out.println("I have written a thingy! :-)"); 
               } 
       }

 

?

 

I'd quite like a logical human-readable representation of the code blocks, but obviously without pissing other people off, as i eventually will probably start some open source work for practice.

 

having said that, Atheist's suggestion tipped me off that the close-} is (in the first example) on the same indentation level as the block that you're returning to, and the open-{ seems to be pretty ignorable (from a human pov).

 

It seems odd to me, as i'd be more inclined to put the block start/stop marks at the indentation of the block you're starting/closing (as in my example above), or just use indentation to mark the blocks and put the open/close {}s inline with each other so you can easily see if you've missed any.

 

I'll probably be able to get used to the standard way now that i've spotted the reasoning, which is what i'll try to do if the above is also confusing to people used to java (now i see the reasoning, i'd assume that it is quite odd and hard to read for you?)

Link to comment
Share on other sites

There's several brace styles. I prefer what's known as 1TBS (one true brace style) which looks a little something like

 

void function() [or class]
{
   if(1tbs) {
       puts("All other braces are inline");
   }
} 

 

What Dak showed looks a lot like GNU style.

 

Personally all this crap about braces makes me long for languages without them, heh

Link to comment
Share on other sites

Personally all this crap about braces makes me long for languages without them, heh

 

ever tried lisp :D

 

or python for one without, but i'm sure your aware of both.

 

----

 

checking out "gnu style" i found this, so i'll probably pick a style from there, as i doubt any of them are too objectionable.

 

thinking about it, if you're going to indent, you don't really need to convey any human-readable info with the {}s, so i'll probably go for:

 

/**
*quick learny thingy
*/

class TestApp{
public static void main (String[] args){
	System.out.println("I have written a thingy! :-)");}}

 

look how small it is! my 800*600 res screen will thank me for it.

 

cheers muchly guys :)

Link to comment
Share on other sites

For Java I'd say the first one.

 

See http://java.sun.com/docs/codeconv/html/CodeConventions.doc10.html#182

 

For C, I'd say do whatever the hell you like, just be consistent and if writing with others or on a project, stick to their standards (don't for the love of god go changing their code to suit your ideology unless it's already been decided by the group :P)

 

Yes, LISP and Python are cool :D (I really should code some more lisp, never really did all that much with it...)

Link to comment
Share on other sites

ever tried lisp :D

 

Yes, a great language conceptually but too many parens

 

or python for one without, but i'm sure your aware of both.

 

I've still not used Python. I use Ruby.

 

Right now I'm learning Erlang, which is also brace-free

Link to comment
Share on other sites

  • 2 weeks later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.