Dak Posted May 16, 2007 Posted May 16, 2007 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.
timo Posted May 16, 2007 Posted May 16, 2007 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).
Klaynos Posted May 16, 2007 Posted May 16, 2007 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.
Dak Posted May 16, 2007 Author Posted May 16, 2007 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?)
bascule Posted May 16, 2007 Posted May 16, 2007 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
bluesmudge Posted May 16, 2007 Posted May 16, 2007 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! :-)"); } } Personally I like this^^^
Dak Posted May 16, 2007 Author Posted May 16, 2007 Personally all this crap about braces makes me long for languages without them, heh ever tried lisp 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
Cap'n Refsmmat Posted May 16, 2007 Posted May 16, 2007 Argh. I go for Atheist's way in my PHP and Java endeavours. Python style is nice though...
Aeternus Posted May 16, 2007 Posted May 16, 2007 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 ) Yes, LISP and Python are cool (I really should code some more lisp, never really did all that much with it...)
bascule Posted May 22, 2007 Posted May 22, 2007 ever tried lisp 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
tadzio Posted June 5, 2007 Posted June 5, 2007 I have to agree with Atheist and Bluesmudge ( though I use two char tab indents
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now