why brackets are not in new line

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 867 Views - Last Post: 11 September 2017 - 04:07 PM

#1 genek  Icon User is online

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 18-August 17

why brackets are not in new line

Posted 09 September 2017 - 11:53 AM

In many code examples brackets are in the same line as method name. Why not in new line?
Why this:
void method(){
    }

and not this:
void method()
{
}

Is This A Good Question/Topic? 0
  • +

Replies To: why brackets are not in new line

#2 jon.kiparsky  Icon User is offline

  • Screw Trump (before he screws you)
  • member icon


Reputation: 10600
  • View blog
  • Posts: 18,099
  • Joined: 19-March 11

Re: why brackets are not in new line

Posted 09 September 2017 - 12:22 PM

It's just a matter of style. I suspect that that approach was popular in printed tutorials because it saves a line of space in each example, which can add up, and it also makes it easier to fit examples into the text. On the other hand many people (myself included) prefer to align opening and closing elements vertically in code that we're going to be working on, because it makes it obvious when you've slipped a bracket. People in the latter camp prefer your second example.
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5782
  • View blog
  • Posts: 19,668
  • Joined: 05-May 12

Re: why brackets are not in new line

Posted 09 September 2017 - 12:31 PM

Also, many Java folks making the transition to C# are more comfortable with the OTBS indenting style.
Was This Post Helpful? 0
  • +
  • -

#4 no2pencil  Icon User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6538
  • View blog
  • Posts: 30,605
  • Joined: 10-May 07

Re: why brackets are not in new line

Posted 09 September 2017 - 04:52 PM

Posted Image
The one on the right is correct, & the other is used by amateurs that lack panache.
Was This Post Helpful? 3
  • +
  • -

#5 Atli  Icon User is offline

  • Enhance Your Calm
  • member icon

Reputation: 4237
  • View blog
  • Posts: 7,213
  • Joined: 08-June 10

Re: why brackets are not in new line

Posted 09 September 2017 - 04:59 PM

I used to be very much in favor of the standard C# style, but after some experimenting with Java I found that the standard Java variant of the OTBS style doesn't make much difference to me in terms of readability, but can drastically cut down on line count. So I use that now, personally.

Of course, it's all basically a matter of taste. As long as people don't override project/company standards with their own, I won't complain.

Although, the Whitesmiths style (where they indent the brackets to be in line with the code-block they wrap) always makes me cringe. Triggers a little OCD in me, that one does.
Was This Post Helpful? 0
  • +
  • -

#6 no2pencil  Icon User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6538
  • View blog
  • Posts: 30,605
  • Joined: 10-May 07

Re: why brackets are not in new line

Posted 09 September 2017 - 05:10 PM

& then there is the Indian Developer style :

int main  (void)


{ 


// Copied from some other site
int i=0;for(i=0;i<10;i++)

{



printf("The value of i = %i", i); system("PAUSE");


    } }




Was This Post Helpful? 4
  • +
  • -

#7 ndc85430  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 541
  • View blog
  • Posts: 2,194
  • Joined: 13-June 14

Re: why brackets are not in new line

Posted 09 September 2017 - 09:21 PM

View Postno2pencil, on 10 September 2017 - 01:10 AM, said:

& then there is the Indian Developer style :


I'm not sure what that comment is meant to mean, but I suppose some Indians might find it somewhat offensive :(.

Anyway, staying on topic, the Linux kernel is one project where both styles are used. The coding style docs give more info.
Was This Post Helpful? 0
  • +
  • -

#8 no2pencil  Icon User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6538
  • View blog
  • Posts: 30,605
  • Joined: 10-May 07

Re: why brackets are not in new line

Posted 09 September 2017 - 09:40 PM

View Postndc85430, on 10 September 2017 - 12:21 AM, said:

View Postno2pencil, on 10 September 2017 - 01:10 AM, said:

& then there is the Indian Developer style :


I'm not sure what that comment is meant to mean,


Just do a quick search for "indent" on C/C++

Sorry if it may come off as offensive, but from 10+ years on this site if it's one thing that is a given, Indian students don't give a shit about doing it correctly, or cleanly, just GET IT DONE NOW. & I hate to say this, but in the work place, my experience is that that attitude follows :/
Was This Post Helpful? 1
  • +
  • -

#9 jon.kiparsky  Icon User is offline

  • Screw Trump (before he screws you)
  • member icon


Reputation: 10600
  • View blog
  • Posts: 18,099
  • Joined: 19-March 11

Re: why brackets are not in new line

Posted 09 September 2017 - 10:25 PM

Two things. First of all, your stereotype doesn't really mesh with experience. Low-bid contractors, yes, tend to produce crap code. Indian programmers, when they're working for low-bid contractors, also produce crap code - but so did the American contractors that wrote a bunch of shitty tests on our codebase before I was hired.
Frankly, if I were to rank nationalities based on the quality of code that I've seen, I'd rank Indians pretty high on the list, because I work with a guy from India and I've seen him crank out some beautiful stuff, and the various other programmers I know from the subcontinent all seem pretty solid. By comparison, I've seen some truly awful stuff produced by white dudes. Like the guy who'd reinvented the join function in PHP, except he hadn't bothered to make it a function, it was just fifteen lines of copy-paste that he'd stuck in various places in his codebase, and it was broken. Punchline: this guy was the lead developer, the head of the team, and had ten years of experience. (there are plenty of stories about this guy - like his unique and idiosyncratic method of deploying code - but he's not the only example by a long shot)

Second thing, the bit about Indians seems kind of gratuitous and out of place. I mean, if you're going to take cheap and tasteless shots at someone, let's take shots at PHP programmers or people who haven't seen the glory that is emacs, or stuff like that. Or people who use Windows, if there are still any of those.
Was This Post Helpful? 4
  • +
  • -

#10 no2pencil  Icon User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6538
  • View blog
  • Posts: 30,605
  • Joined: 10-May 07

Re: why brackets are not in new line

Posted 09 September 2017 - 10:29 PM

By all means, not all Indians are bad devs, but I've only seen that crazy random indenting style from one source. & they're always students, & they don't care to take corrective action.

Also, I bring it up because I feel it is relevant, when often the proper indentation would help them fix their error.
Was This Post Helpful? 0
  • +
  • -

#11 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5782
  • View blog
  • Posts: 19,668
  • Joined: 05-May 12

Re: why brackets are not in new line

Posted 10 September 2017 - 06:54 PM

To bring this back on topic, so with OTBS, this nicely formatted C# code:
class StudentName
{
    public string FirstName { get; set; }
    public string LastName  { get; set; }
    public int    ID        { get; set; }
}

class CollInit
{
    Dictionary<int, StudentName> students = new Dictionary<int, StudentName>()
    {
        { 111, new StudentName { FirstName="Sachin", LastName="Karnik",       ID=211 } },
        { 112, new StudentName { FirstName="Dina",   LastName="Salimzianova", ID=317 } },
        { 113, new StudentName { FirstName="Andy",   LastName="Ruth",         ID=198 } }
    };
}



will now become:
class StudentName {
    public string FirstName {
        get;
        set;
    }

    public string LastName {
        get;
        set;
    }

    public int ID { 
        get;
        set;
    }
}

class CollInit {
    Dictionary<int, StudentName> students = new Dictionary<int, StudentName>() { {
            111,
            new StudentName {
                FirstName="Sachin",
                LastName="Karnik",
                ID=211
            }
        }, { 
            112,
            new StudentName {
                FirstName="Dina",
                LastName="Salimzianova",
                ID=317
            }
        }, {
            113,
            new StudentName {
                FirstName="Andy",
                LastName="Ruth",
                ID=198
            }
        }
    };
}



Or did I mis-apply the rules of OTBS? (Also, does OTBS apply to the '<', '>', and '(', and ')' and "/*" and "*/" pairs as well?)

I ask because I'm not an avid practictioner of OTBS simply because I hate the way if-else's are handled. (e.g.)
if (cond1)
{
    DoIt();
}
else
{
    DontDoIt();
}

if (cond2)
{
   DoIt2();
}
else if (cond3)
{
   DoIt3();
}
else
{
   DoSomething();
}


becomes:
if (cond1) {
    DoIt();
} else {
    DontDoIt();
}

if (cond2) {
   DoIt2();
} else if (cond3) {
   DoIt3();
} else {
   DoSomething();
}


Was This Post Helpful? 1
  • +
  • -

#12 Atli  Icon User is offline

  • Enhance Your Calm
  • member icon

Reputation: 4237
  • View blog
  • Posts: 7,213
  • Joined: 08-June 10

Re: why brackets are not in new line

Posted 10 September 2017 - 10:57 PM

Is there anything like the C# property shorthand syntax in OTBS typical languages, like Java? I'd personally make an exception for it, because the way VS formats it is clearly superior.

I'd also make allowances in cases where data needs to be initialized in code. Those cases always end up getting messy, somehow. - I'd generally prefer importing that data from somewhere rather than hard-code it. Even small lists. (Small lists tend to migrate into large lists over time.)


The IF-ELSE examples are really a matter of taste. I personally don't like your first example. If anything, it strikes me as less legible than the second example.
Was This Post Helpful? 0
  • +
  • -

#13 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5782
  • View blog
  • Posts: 19,668
  • Joined: 05-May 12

Re: why brackets are not in new line

Posted 11 September 2017 - 05:57 AM

Yeah, I get you regarding the IF-ELSE's and taste.

I taught myself to code in BASIC and then moved to 6502 assembly, then Forth. In college, I learned Pascal, and then C. If I had gone from BASIC straight to C, I would gone with OTBS as well because I learned C using the K&R book. Unfortunately, the time spent in assembly, Forth, and Pascal got me used to taking advantage of vertical whitespace and visually well defined blocks. So when I went from Pascal to C I kept with having the Pascal style Begin and End on separate lines, and as luck would have it, my teacher for Pascal also happened to be my teacher for C and his preferred style was Allman rather than K&R. I asked him about it once. He said his preference was due to 2 reasons: it made grading assignments easier, and it was easier to move blocks of code around in `vi` because the beginning and ends were well defined.

I suspect that if I went from BASIC straight to C, I would be an OTBS follower.
Was This Post Helpful? 0
  • +
  • -

#14 Arenlor  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 36
  • View blog
  • Posts: 414
  • Joined: 26-April 06

Re: why brackets are not in new line

Posted 11 September 2017 - 07:05 AM

Personally I use what works best for clarity.
if () {
    blah;
}
else if() {
    blah;
}
else {
    blah;
}


public class SomeClass {
    public int main(String[] args) {
        blah;
    }

    public void killAllHumans() {
        blah;
    }
}


However in a case where it's default that something is going to happen I don't put it on a new line:
try {
    something;
} catch (Exception ex) {
    handleit;
}

Having catch on a separate line, to me, makes it harder to read, as try and catch are always together.
Was This Post Helpful? 0
  • +
  • -

#15 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5782
  • View blog
  • Posts: 19,668
  • Joined: 05-May 12

Re: why brackets are not in new line

Posted 11 September 2017 - 07:25 AM

But what happens when you get rid of your Pokemon exception handling and replace it with real production catches?
try {
    Something();
} catch (OutOfMemoryException ex) {
    HandleOutOfMemory(ex);
} catch (InvalidArgumentException ex) {
    HandleInvalidArgument(ex);
} catch (StackOverflowException ex) {
    HandleStackOverflow(ex);
} catch (MeteorStrikeException ex) {
    AbandonAllHope(ex);
} finally {
    SomeCleanup();
}


Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2