Java/C#/C++/[insert language here] brackets

Page 3 - Seeking answers? Join the AnandTech community: where nearly half-a-million members share solutions and discuss the latest tech.

torpid

Lifer
Sep 14, 2003
11,631
11
76
I prefer 2, but I often find when looking back that I did 1 and get mad at myself.
 

Kilrsat

Golden Member
Jul 16, 2001
1,072
0
0
Originally posted by: Jumpem
Originally posted by: Kilrsat
1

2 is just annoying and 1 also happens to be the standard we use at work.


You have those backwards.

The indenting will show you what all belongs to the same block. Wasting a line on a { just means you see less useful code on the screen at any point in time.
 

SagaLore

Elite Member
Dec 18, 2001
24,036
21
81
Originally posted by: CrackaLackaZe

2. public method
{
...
}

I like this method because it is easier to track when you have multiple inside each other.
 

Maverick

Diamond Member
Jun 14, 2000
5,900
0
76
I used to exclusively use #2 but now since I've started to learn Java I've picked up #1. This is mainly because most of the coding examples in Java programming books use the #1 style of backets.

I tried switching years ago and failed miserably. The reason I've been successful now is that I took a hiatus from backeting all together since my programming has been in VB :)

#2 is easier to read, but someone who uses #2 exclusively will have trouble reading code that is written in #1.

Better to know #1 and still be able to read #2 than only know #2 and have a tough time reading #1.
 

jaybert

Diamond Member
Mar 6, 2001
3,523
0
0
i usually use method 1 but am gradually switching to 2. the architect of the project I am working on at IBM makes it a coding standard to use 2, since its easier to match up braces since they always line up.
 

DougK62

Diamond Member
Mar 28, 2001
8,035
6
81
#1 is much easier to read. Like someone else said, #2 makes code much longer than necessary.

 

Mark R

Diamond Member
Oct 9, 1999
8,513
16
81
#2 - it's so much easier to read.

Although for mundane stuff I'll often do things like this:
finally { myStream.Close(); }
 
Jun 6, 2005
194
0
0
Originally posted by: Mark R
#2 - it's so much easier to read.

Although for mundane stuff I'll often do things like this:
finally { myStream.Close(); }

it's easier for YOU to read perhaps, but not for the second half.
i used to do #2 and switched over, can't remember why, but ever since i switched over, it's been much easier for me to read. and i think

{

}

doesn't make it easier to debug, because your line of codes aren't going to be just 1 or 2, so you won't be able to sit there and match which bracket goes with which by eyeballing it.

it all comes down to the programmer's preference of which looks nicer, because they're both clean.

on the other hand this wouldn't ble clean

if (clean) { blah(); blah2(); ............................................ blah3(); blahend(); }

i do however do those if i only have 2 or 3 statements, i just rather not waste lines, and it shouldnt be hard to read