Curly Brackets Go Where?

Please compose all posts in Emacs.

Moderators: phlip, Moderators General, Prelates

How do you do your bracketing?

Same line
88
48%
New line
88
48%
OTTERS!
7
4%
 
Total votes: 183

User avatar
Qoppa
Posts: 694
Joined: Sat Nov 24, 2007 9:32 pm UTC
Location: Yes.

Curly Brackets Go Where?

Postby Qoppa » Sun Mar 01, 2009 5:01 pm UTC

Same line:

Code: Select all

if (a == 1) {
    //stuff
}


New line:

Code: Select all

if (a == 1)
{
    // stuff
}


I prefer a new line, though I'm starting to be more tolerant of using the same line. I used to hate it A LOT.

Code: Select all

_=0,w=-1,(*t)(int,int);a()??<char*p="[gd\
~/d~/\\b\x7F\177l*~/~djal{x}h!\005h";(++w
<033)?(putchar((*t)(w??(p:>,w?_:0XD)),a()
):0;%>O(x,l)??<_='['/7;{return!(x%(_-11))
?x??'l:x^(1+ ++l);}??>main(){t=&O;w=a();}

Rysto
Posts: 1459
Joined: Wed Mar 21, 2007 4:07 am UTC

Re: Curly Brackets Go Where?

Postby Rysto » Sun Mar 01, 2009 5:23 pm UTC


User avatar
Qoppa
Posts: 694
Joined: Sat Nov 24, 2007 9:32 pm UTC
Location: Yes.

Re: Curly Brackets Go Where?

Postby Qoppa » Sun Mar 01, 2009 5:32 pm UTC

Figures. But it has no poll!

Code: Select all

_=0,w=-1,(*t)(int,int);a()??<char*p="[gd\
~/d~/\\b\x7F\177l*~/~djal{x}h!\005h";(++w
<033)?(putchar((*t)(w??(p:>,w?_:0XD)),a()
):0;%>O(x,l)??<_='['/7;{return!(x%(_-11))
?x??'l:x^(1+ ++l);}??>main(){t=&O;w=a();}

User avatar
Dobblesworth
Dobblesworth, here's the title you requested over three years ago. -Banana
Posts: 1429
Joined: Wed May 30, 2007 12:06 pm UTC
Contact:

Re: Curly Brackets Go Where?

Postby Dobblesworth » Sun Mar 01, 2009 5:52 pm UTC

This poll lacks sufficient DUCKS!!
I go with spamming my { & } on a new line, helps fragment the code portions up on the screen from being walls of text and makes it a nicer read to highlight the syntax.

User avatar
pavel
Posts: 25
Joined: Mon Feb 23, 2009 5:44 pm UTC

Re: Curly Brackets Go Where?

Postby pavel » Sun Mar 01, 2009 5:56 pm UTC

I don't like {'s being on a new line. I can tell the level by indentation, anyway, and a { on its own line just wastes space, imo. The only time I'd put a { on its own line is if the function declaration or conditional statement itself spans several lines (hate doing this, but if it's a huge honking thing, it has to be done for readability), and even then I indent the { to the next level.

User avatar
Berengal
Superabacus Mystic of the First Rank
Posts: 2707
Joined: Thu May 24, 2007 5:51 am UTC
Location: Bergen, Norway
Contact:

Re: Curly Brackets Go Where?

Postby Berengal » Sun Mar 01, 2009 6:27 pm UTC

Brackets are for people writing in deprecated languages. Modern languages use unambiguous but mostly unrestricted layout.
It is practically impossible to teach good programming to students who are motivated by money: As potential programmers they are mentally mutilated beyond hope of regeneration.

User avatar
Xanthir
My HERO!!!
Posts: 5311
Joined: Tue Feb 20, 2007 12:49 am UTC
Location: The Googleplex
Contact:

Re: Curly Brackets Go Where?

Postby Xanthir » Sun Mar 01, 2009 6:52 pm UTC

Indentation contains all the information you need. Brackets are superfluous, so you should limit their effect on layout.
(defun fibs (n &optional (a 1) (b 1)) (take n (unfold '+ a b)))

Comic JK
Posts: 270
Joined: Wed Feb 18, 2009 6:08 pm UTC

Re: Curly Brackets Go Where?

Postby Comic JK » Sun Mar 01, 2009 8:35 pm UTC

Xanthir wrote:Indentation contains all the information you need. Brackets are superfluous, so you should limit their effect on layout.

Indentation is great, but until they get around to compiling Python into machine code, some of us still need to write in C++.

I like to put my brackets on a new line, unless the whole block is small, in which case:

Code: Select all

if(boolean) {stuff; more stuff;}

if(long conditional statement == too long to do the whole thing on one line)
     {stuff; more stuff; all the remaining stuff;}

if(large block of code within brackets)
{
     the ordinary way;
     ...lots of code here...
     is sometimes best;
}
Image
A webcomic funnier than life itself. Updated Monday-Friday.

User avatar
r1chard
Posts: 281
Joined: Thu Dec 06, 2007 2:17 am UTC
Location: Melbourne, AU
Contact:

Re: Curly Brackets Go Where?

Postby r1chard » Sun Mar 01, 2009 9:08 pm UTC

Comic JK wrote:
Xanthir wrote:Indentation contains all the information you need. Brackets are superfluous, so you should limit their effect on layout.
Indentation is great, but until they get around to compiling Python into machine code, some of us still need to write in C++.

But that's not what Xanthir was saying. And I agree, indentation tells us enough so just get them out of the way, "limit their effect on layout." Certainly they should not get a new line of their own.

User avatar
Xanthir
My HERO!!!
Posts: 5311
Joined: Tue Feb 20, 2007 12:49 am UTC
Location: The Googleplex
Contact:

Re: Curly Brackets Go Where?

Postby Xanthir » Sun Mar 01, 2009 9:34 pm UTC

Comic JK wrote:
Xanthir wrote:Indentation contains all the information you need. Brackets are superfluous, so you should limit their effect on layout.

Indentation is great, but until they get around to compiling Python into machine code, some of us still need to write in C++.

I don't use Python, and indentation conveys all the necessary information in C++. Brackets are superfluous (to a human) in *all* of the C-like languages.
(defun fibs (n &optional (a 1) (b 1)) (take n (unfold '+ a b)))

User avatar
Mach1ne
Posts: 35
Joined: Tue Feb 24, 2009 5:20 pm UTC
Location: This exact location but 3 minutes from now

Re: Curly Brackets Go Where?

Postby Mach1ne » Sun Mar 01, 2009 11:36 pm UTC

I always do newline brackets. I guess its a JAVA thing?

User avatar
b.i.o
Green is the loneliest number
Posts: 2519
Joined: Fri Jul 27, 2007 4:38 pm UTC
Location: Hong Kong

Re: Curly Brackets Go Where?

Postby b.i.o » Mon Mar 02, 2009 12:06 am UTC

Comic JK wrote:Indentation is great, but until they get around to compiling Python into machine code, some of us still need to write in C++.


That's not what he was saying. The indentation of the code inside the block means that this:

Code: Select all

if(large block of code within brackets) {
     this way;
     ...lots of code here...
     is better;
}

is at least as readable and takes up less space, besides. Even if you're in a language that doesn't require whitespace, you indent inner blocks of code. The brackets are superfluous from a human perspective--if you're doing things properly, your code should be completely readable without them.

I always do newline brackets. I guess its a JAVA thing?

???
No. There's no more reason to do it in Java than in any other C-like language.

Rysto
Posts: 1459
Joined: Wed Mar 21, 2007 4:07 am UTC

Re: Curly Brackets Go Where?

Postby Rysto » Mon Mar 02, 2009 12:44 am UTC

Mach1ne wrote:I always do newline brackets. I guess its a JAVA thing?

The Java style is actually more strict about it then K&R. In K&R, the brace to open a function goes on its own line, but not in Java style. You probably use Allman style, which is popular in C/C++ code.

User avatar
Mach1ne
Posts: 35
Joined: Tue Feb 24, 2009 5:20 pm UTC
Location: This exact location but 3 minutes from now

Re: Curly Brackets Go Where?

Postby Mach1ne » Mon Mar 02, 2009 2:34 am UTC

Rysto wrote:
Mach1ne wrote:I always do newline brackets. I guess its a JAVA thing?

The Java style is actually more strict about it then K&R. In K&R, the brace to open a function goes on its own line, but not in Java style. You probably use Allman style, which is popular in C/C++ code.


Hmm actually I'm mistaken, I use the same line style, which goes in line with what you're saying. My mistake.

As to the other post, I was never saying it was more necessary than C/C++...just saying that from my experience JAVA users tend to do more same line than C/C++ users, I could be mistaken though.

User avatar
OOPMan
Posts: 314
Joined: Mon Oct 15, 2007 10:20 am UTC
Location: Cape Town, South Africa

Re: Curly Brackets Go Where?

Postby OOPMan » Mon Mar 02, 2009 7:13 am UTC

I tend to towards newline braces but my given a choice I go for Python's indentation delimiting anyday.
Image

Image

User avatar
Xanthir
My HERO!!!
Posts: 5311
Joined: Tue Feb 20, 2007 12:49 am UTC
Location: The Googleplex
Contact:

Re: Curly Brackets Go Where?

Postby Xanthir » Mon Mar 02, 2009 2:42 pm UTC

Actually, I just switched from

Code: Select all

function name (args) {
   code;
   code;
}

to

Code: Select all

function name (args) {
   code;
   code; }

If you're a Pythonista, it should look more familiar. To me, though, it's a Lisp indentation style. I think it actually makes my code *even clearer*, because now I don't have that annoying just-a-curly-brace line at the end of my blocks either. As stated previously, indentation makes it completely obvious to the eye what's going on, so the braces don't add anything, even at the end. I have a function in front of me with some heavily-nested if and foreach statements, and it's super-clear what's going on. The reduction in vertical space certainly helps too.

Now I really do wish I could just get rid of the braces entirely. In Lisp they're an absolute necessity, because they define the list structure that programs are built in, but in a line-oriented, non-rewritable language (like the PHP I hack in for work), they don't add a single thing, and force me to do useless brace-matching when writing. What I'm saying is that I need a Python module for PHP. (If Python was just a *little bit easier* for webdev, I'd probably switch over now.)
(defun fibs (n &optional (a 1) (b 1)) (take n (unfold '+ a b)))

User avatar
Vault
Posts: 169
Joined: Mon Nov 10, 2008 5:00 pm UTC
Location: Just past the event horizon
Contact:

Re: Curly Brackets Go Where?

Postby Vault » Mon Mar 02, 2009 5:01 pm UTC

For me it depends on what language I'm using. Actually it matters if I'm using objective-C. Objective-C gets brackets like this:

Code: Select all

- (NSString*)doSomethingWithVariable:(int)x andOtherVariable:(NSArray*)y
{
     //stuff
}


Java and everything else gets this:

Code: Select all

String doSomehting(int x, ArrayList y) {
    //stuff
}

User avatar
You, sir, name?
Posts: 6983
Joined: Sun Apr 22, 2007 10:07 am UTC
Location: Chako Paul City
Contact:

Re: Curly Brackets Go Where?

Postby You, sir, name? » Mon Mar 02, 2009 10:22 pm UTC

Generally

Code: Select all

if(foo) {
  code;
  code;
  code;
}


There is an exception though, and that is

Code: Select all

if((an & (obscenely || long) ^ expression) ||
    (that && lasts) || ((more >> 2 & than) & one) || line)
 {
  code;
  code;
  code;
}
I edit my posts a lot and sometimes the words wrong order words appear in sentences get messed up.

User avatar
OOPMan
Posts: 314
Joined: Mon Oct 15, 2007 10:20 am UTC
Location: Cape Town, South Africa

Re: Curly Brackets Go Where?

Postby OOPMan » Tue Mar 03, 2009 7:13 am UTC

Xanthir wrote:Actually, I just switched from

Code: Select all

function name (args) {
   code;
   code;
}

to

Code: Select all

function name (args) {
   code;
   code; }

If you're a Pythonista, it should look more familiar. To me, though, it's a Lisp indentation style. I think it actually makes my code *even clearer*, because now I don't have that annoying just-a-curly-brace line at the end of my blocks either. As stated previously, indentation makes it completely obvious to the eye what's going on, so the braces don't add anything, even at the end. I have a function in front of me with some heavily-nested if and foreach statements, and it's super-clear what's going on. The reduction in vertical space certainly helps too.

Now I really do wish I could just get rid of the braces entirely. In Lisp they're an absolute necessity, because they define the list structure that programs are built in, but in a line-oriented, non-rewritable language (like the PHP I hack in for work), they don't add a single thing, and force me to do useless brace-matching when writing. What I'm saying is that I need a Python module for PHP. (If Python was just a *little bit easier* for webdev, I'd probably switch over now.)


I've been thinking of trying to float a Python project through for web dev as well. As for Python in PHP, look no further:

PiP

I have no idea how well it works though ;-)
Image

Image

User avatar
r1chard
Posts: 281
Joined: Thu Dec 06, 2007 2:17 am UTC
Location: Melbourne, AU
Contact:

Re: Curly Brackets Go Where?

Postby r1chard » Tue Mar 03, 2009 8:39 am UTC

Xanthir wrote:If Python was just a *little bit easier* for webdev, I'd probably switch over now.

This is probably a question for other sections of the forum, but in what way is it hard now?

0xBADFEED
Posts: 687
Joined: Mon May 05, 2008 2:14 am UTC

Re: Curly Brackets Go Where?

Postby 0xBADFEED » Tue Mar 03, 2009 2:12 pm UTC

Regardless of which line the braces occupy I hope we can all agree that GNU-style is an abomination.

User avatar
Mach1ne
Posts: 35
Joined: Tue Feb 24, 2009 5:20 pm UTC
Location: This exact location but 3 minutes from now

Re: Curly Brackets Go Where?

Postby Mach1ne » Tue Mar 03, 2009 5:20 pm UTC

GNU style? I don't know style names, give me an example :D

User avatar
bratwurst
Posts: 73
Joined: Fri Dec 19, 2008 11:37 pm UTC

Re: Curly Brackets Go Where?

Postby bratwurst » Tue Mar 03, 2009 5:46 pm UTC

Mach1ne wrote:GNU style? I don't know style names, give me an example :D


If I recollect correctly, GNU style is:

Code: Select all

if (foo == bar)
  {
    widget();
    cthulhu();
  }

0xBADFEED
Posts: 687
Joined: Mon May 05, 2008 2:14 am UTC

Re: Curly Brackets Go Where?

Postby 0xBADFEED » Tue Mar 03, 2009 6:27 pm UTC

Mach1ne wrote:GNU style? I don't know style names, give me an example :D

You can find a nice rundown of different brace styles and their associated names here.

User avatar
Xanthir
My HERO!!!
Posts: 5311
Joined: Tue Feb 20, 2007 12:49 am UTC
Location: The Googleplex
Contact:

Re: Curly Brackets Go Where?

Postby Xanthir » Tue Mar 03, 2009 6:31 pm UTC

r1chard wrote:
Xanthir wrote:If Python was just a *little bit easier* for webdev, I'd probably switch over now.

This is probably a question for other sections of the forum, but in what way is it hard now?

In comparison with PHP. PHP is the be-all-end-all of easy webdev. You have files. People request those files. You get faster service than vanilla CGI. You don't need to escape *anything* in the HTML/CSS/JS/whatever, if you just surround it with ?> <?php tags (I usually prefer to think of it that way, rather than the <?php ?> tags surrounding your code).

@OOPMan:
Not really looking to do Python *in* PHP. I'd prefer to keep my server-side languages apart. Honestly, though, I'm going to be giving Hunchentoot (a lisp webserver) a good, hard look once I get an extra machine. They just put out a big new release. Alternately: Lisp as CGI. CL-WHO makes it really easy to write html in Lisp, but I'm not sure how well it responds to writing JS or CSS.

@bratwurst:
::shudder::
(defun fibs (n &optional (a 1) (b 1)) (take n (unfold '+ a b)))

User avatar
r1chard
Posts: 281
Joined: Thu Dec 06, 2007 2:17 am UTC
Location: Melbourne, AU
Contact:

Re: Curly Brackets Go Where?

Postby r1chard » Tue Mar 03, 2009 9:09 pm UTC

Xanthir wrote:
r1chard wrote:
Xanthir wrote:If Python was just a *little bit easier* for webdev, I'd probably switch over now.

This is probably a question for other sections of the forum, but in what way is it hard now?

In comparison with PHP. PHP is the be-all-end-all of easy webdev. You have files. People request those files. You get faster service than vanilla CGI. You don't need to escape *anything* in the HTML/CSS/JS/whatever, if you just surround it with ?> <?php tags (I usually prefer to think of it that way, rather than the <?php ?> tags surrounding your code).

Oh, yeah. My area is more the larger, complex web applications so I tend to forget about the simpler websites where PHP really does rock :)

User avatar
Xanthir
My HERO!!!
Posts: 5311
Joined: Tue Feb 20, 2007 12:49 am UTC
Location: The Googleplex
Contact:

Re: Curly Brackets Go Where?

Postby Xanthir » Tue Mar 03, 2009 10:17 pm UTC

r1chard wrote:Oh, yeah. My area is more the larger, complex web applications so I tend to forget about the simpler websites where PHP really does rock :)

Yup. Though I am gradually building up to the point where I might be hitting diminishing returns with PHP. So far I've been able to cope via ruthless refactoring and the construction of a homebrew framework.
(defun fibs (n &optional (a 1) (b 1)) (take n (unfold '+ a b)))

User avatar
Area Man
Posts: 256
Joined: Thu Dec 25, 2008 8:08 pm UTC
Location: Local

Re: Curly Brackets Go Where?

Postby Area Man » Wed Mar 04, 2009 6:46 am UTC

Either style is fine with me -- java was the first lang used in college, and now I only code C/C++; GNU, whitesmith and the other styles are pretty gross.
I default to ANSI style (and have even experimented with Horstmann style in my youth).
I find it more pleasing and consistent to line up the brackets vertically, and no exceptions have to be made for "You, sir, name?"'s second case, or anonymous scopes.
Bisquick boxes are a dead medium.

Comic JK
Posts: 270
Joined: Wed Feb 18, 2009 6:08 pm UTC

Re: Curly Brackets Go Where?

Postby Comic JK » Wed Mar 04, 2009 9:57 pm UTC

Xanthir wrote:
Comic JK wrote:
Xanthir wrote:Indentation contains all the information you need. Brackets are superfluous, so you should limit their effect on layout.

Indentation is great, but until they get around to compiling Python into machine code, some of us still need to write in C++.

I don't use Python, and indentation conveys all the necessary information in C++. Brackets are superfluous (to a human) in *all* of the C-like languages.

But I try to think like the compiler will when I'm programming; that means that in Python I only see whitespace, but in C and Java, I look for brackets and ignore whitespace.

Of course I write my code with indented blocks anyway, but I still like the encasing brackets to be very clearly visible. I find the "if(boolean) {" style makes the bracket harder to spot, so if the block is long, I give each bracket its own line.

Short blocks go all on one indented line with brackets flanking { like(); this(); }, as I mentioned before. I wouldn't mind seeing that style spread.
Image
A webcomic funnier than life itself. Updated Monday-Friday.

User avatar
kriel
Posts: 922
Joined: Thu Feb 07, 2008 2:58 pm UTC
Location: Somewhere I'm not.
Contact:

Re: Curly Brackets Go Where?

Postby kriel » Sat Mar 21, 2009 12:47 am UTC

Just out of curiosity; what do y'all consider to be a line? I know 80 characters used to be the norm; but uh...

Code: Select all

--------------------------------------------------------------------------------
// 80 hyphens.
if (stuff) single_function();
if (otherstuff) {I can fit this all on one line;}

if (omgwtf)
{
    morestuff;
    and stuff;
    and maybe a bit more;
}

if (somebody really had no sense and decided to put all this
    goddamned if stuff in a single if statement this is such a run-on)
{
    then for some reason i decided to make a really long line
        that takes up a lot of space for whatever reason;
    do;
    stuff;
}


... Yeah.
Last edited by kriel on Sat Mar 21, 2009 3:26 am UTC, edited 1 time in total.

Philwelch
Posts: 2904
Joined: Tue Feb 19, 2008 5:33 am UTC
Location: RIGHT BEHIND YOU

Re: Curly Brackets Go Where?

Postby Philwelch » Sat Mar 21, 2009 2:31 am UTC

Xanthir wrote:
Comic JK wrote:
Xanthir wrote:Indentation contains all the information you need. Brackets are superfluous, so you should limit their effect on layout.

Indentation is great, but until they get around to compiling Python into machine code, some of us still need to write in C++.

I don't use Python, and indentation conveys all the necessary information in C++. Brackets are superfluous (to a human) in *all* of the C-like languages.


Good luck with machine-generated code.

In my view?

pavel wrote:I don't like {'s being on a new line. I can tell the level by indentation, anyway, and a { on its own line just wastes space, imo. The only time I'd put a { on its own line is if the function declaration or conditional statement itself spans several lines (hate doing this, but if it's a huge honking thing, it has to be done for readability), and even then I indent the { to the next level.


I LOVE doing this.

Compare:

Code: Select all

const int limit = 22;

my_function(int arg1, string arg2, int arg3 = 14){
    if ((arg3 + arg1) < limit){
        foo()
    }
    else if ((arg3) > limit){
        bar()
    }
    else{
        baz()
    }
}

Code: Select all

const int limit = 22;

my_function(
           int arg1,
           string arg2,
           int arg3 = 14
           )
{
    if (
        (arg3 + arg1)
        <
        limit
       )
    {
        foo()
    }
    else if (
            (arg3)
            >
            limit
            )
    {
        bar()
    }
    else
    {
        baz()
    }
}


Which is more legible?

Don't whine to me about wasting whitespace, we aren't using teletypes anymore. Whitespace is cheap.
Fascism: If you're not with us you're against us.
Leftism: If you're not part of the solution you're part of the problem.

Perfection is an unattainable goal.

User avatar
kriel
Posts: 922
Joined: Thu Feb 07, 2008 2:58 pm UTC
Location: Somewhere I'm not.
Contact:

Re: Curly Brackets Go Where?

Postby kriel » Sat Mar 21, 2009 3:21 am UTC

tbh; the first one is more legible to me. I like a level of terseness, though.

also:

Code: Select all

if (foo)
{
   bar;
}else{
   more bar;
}

0xBADFEED
Posts: 687
Joined: Mon May 05, 2008 2:14 am UTC

Re: Curly Brackets Go Where?

Postby 0xBADFEED » Sat Mar 21, 2009 3:26 am UTC

Philwelch wrote:

Code: Select all

const int limit = 22;
my_function(
           int arg1,
           string arg2,
           int arg3 = 14
           )
{
    if (
        (arg3 + arg1)
        <
        limit
       )
    {
        foo()
    }
    else if (
            (arg3)
            >
            limit
            )
    {
        bar()
    }
    else
    {
        baz()
    }
}


ewwwww... That layout makes my eyes bleed.
What's wrong with

Code: Select all

const int limit = 22;
my_function(int arg1, string arg2, int arg3 = 14)
{
    if (arg3 + arg1 < limit)
        foo()
    else if (arg3 > limit)
        bar()
    else   
        baz()
}

If brackets aren't required I don't use them.

Philwelch
Posts: 2904
Joined: Tue Feb 19, 2008 5:33 am UTC
Location: RIGHT BEHIND YOU

Re: Curly Brackets Go Where?

Postby Philwelch » Sat Mar 21, 2009 3:31 am UTC

Splitting your conditionals into separate lines *does* help when you have complicated nesting conditionals with boolean operators in it.

And it's better looking as you get used to it, I suppose.
Fascism: If you're not with us you're against us.
Leftism: If you're not part of the solution you're part of the problem.

Perfection is an unattainable goal.

User avatar
kriel
Posts: 922
Joined: Thu Feb 07, 2008 2:58 pm UTC
Location: Somewhere I'm not.
Contact:

Re: Curly Brackets Go Where?

Postby kriel » Sat Mar 21, 2009 3:36 am UTC

Putting
conditionals
like
that
is
like
typing
like
this.
Philwelch wrote:Don't whine to me about wasting whitespace, we aren't using teletypes anymore. Whitespace is cheap.


Though, I can see if you've got a really really complicated conditional;
with many parts and phrases to it;
that make it really hard to understand;
how it might be a little bit easier to split it up on multiple lines.

Just make sure it's actually complicated enough to warrant multiple lines.

0xBADFEED
Posts: 687
Joined: Mon May 05, 2008 2:14 am UTC

Re: Curly Brackets Go Where?

Postby 0xBADFEED » Sat Mar 21, 2009 3:43 am UTC

Philwelch wrote:Splitting your conditionals into separate lines *does* help when you have complicated nesting conditionals with boolean operators in it.

It can, but usually if I have a conditional that's complicated enough that it's going to be broken up over multiple lines I'll factor it out to a function. Like:

Code: Select all

bool complicated_test(object v1, object v2, object v3)
{
    return v1.some_func1() && v2.some_func2() ||
             !(v3.x && v2.y) || (v1.z && v2.w) || !v3.some_really_long_function_name();
}

void func()
{
    object o1,o2,o3;
    ...
    if(complicated_test(o1,o2,o3)){
    ...
    }
}

This tends to keep the conditionals clean and the decision structure clear, with the added bonus that the function name gives everyone else some added information as to the purpose behind the complicated test.
Last edited by 0xBADFEED on Sat Mar 21, 2009 3:27 pm UTC, edited 2 times in total.

User avatar
Xanthir
My HERO!!!
Posts: 5311
Joined: Tue Feb 20, 2007 12:49 am UTC
Location: The Googleplex
Contact:

Re: Curly Brackets Go Where?

Postby Xanthir » Sat Mar 21, 2009 3:50 am UTC

Philwelch wrote:Good luck with machine-generated code.

Um, what? I have no idea what that is supposed to mean as a response to my comment.

Don't whine to me about wasting whitespace, we aren't using teletypes anymore. Whitespace is cheap.

Um, no. Whitespace is *expensive* in terms of comprehension. The more you can comprehensibly fit into your eye, the better. Stretching a function out over a whole page when it can be compressed into less than half that space without losing comprehensibility is a crime.
(defun fibs (n &optional (a 1) (b 1)) (take n (unfold '+ a b)))

Philwelch
Posts: 2904
Joined: Tue Feb 19, 2008 5:33 am UTC
Location: RIGHT BEHIND YOU

Re: Curly Brackets Go Where?

Postby Philwelch » Sat Mar 21, 2009 5:09 am UTC

Xanthir wrote:
Philwelch wrote:Good luck with machine-generated code.

Um, what? I have no idea what that is supposed to mean as a response to my comment


It's easy to machine-generate brace-delimited blocks. Machine-generating whitespace-delimited blocks is more of a challenge: it turns your code generator (which is one project) into a code generator with a pretty-printer attached (which is two projects).

Xanthir wrote:
Don't whine to me about wasting whitespace, we aren't using teletypes anymore. Whitespace is cheap.

Um, no. Whitespace is *expensive* in terms of comprehension. The more you can comprehensibly fit into your eye, the better. Stretching a function out over a whole page when it can be compressed into less than half that space without losing comprehensibility is a crime.


Yes, but that's a question-begging way of putting it. "Without losing comprehensibility" is exactly the question here. And the better question is, "when and where does whitespace enhance comprehensibility"?

For instance:

Code: Select all

function(a, b, c){foo(a); bar(b); baz(c);}foo(x){fred(x+10)}fred(a){function(a, a, a);}


has no whitespace, and is thus unreadable, even though it fits within 80 columns or so.

Code: Select all

function(a, b, c)
{
    foo(a);
    bar(b);
    baz(c);
}

foo(x)
{
    fred(x+10)
}

fred(a)
{
    function(a, a, a);
}


Now look over BOTH blocks of code and tell me: where is the missing semicolon more apparent? (Yes, even if your compiler helpfully informs you what column your missing semicolon is on, and not just what line.)

Also note: this problem is exacerbated when using languages like Perl, where an absence of whitespace will elicit the classic "explosion at the ASCII factory" appearance.

Also:

kriel wrote:Putting
conditionals
like
that
is
like
typing
like
this.
Philwelch wrote:Don't whine to me about wasting whitespace, we aren't using teletypes anymore. Whitespace is cheap.


Though, I can see if you've got a really really complicated conditional;
with many parts and phrases to it;
that make it really hard to understand;
how it might be a little bit easier to split it up on multiple lines.

Just make sure it's actually complicated enough to warrant multiple lines.


I don't know about that.

Consider:

Phil's shopping list wrote:Bagels
Dishwasher soap
Bratwurst
K-Y Jelly


vs.
kriel's shopping list wrote:Bagels, dishwasher soap, bratwurst, and K-Y jelly.


In code:

Code: Select all

if (
    gotBagels() &&
    gotDishwasherSoap() &&
    gotBratwurst() &&
    gotKYJelly() &&
    )
  leaveStore();


vs.

Code: Select all

if (gotBagels() && gotDishwasherSoap() && gotBratwurst() && gotKYJelly())
  leaveStore();
Fascism: If you're not with us you're against us.
Leftism: If you're not part of the solution you're part of the problem.

Perfection is an unattainable goal.

User avatar
Berengal
Superabacus Mystic of the First Rank
Posts: 2707
Joined: Thu May 24, 2007 5:51 am UTC
Location: Bergen, Norway
Contact:

Re: Curly Brackets Go Where?

Postby Berengal » Sat Mar 21, 2009 10:11 am UTC

Philwelch wrote:It's easy to machine-generate brace-delimited blocks. Machine-generating whitespace-delimited blocks is more of a challenge: it turns your code generator (which is one project) into a code generator with a pretty-printer attached (which is two projects).

If you're generating code in python it's much easier to work on the object level instead of the text level anyway.

Whitespace are the most important characters there is. In haskell, space is function application, in lisp it's cons and in python it's structure.
It is practically impossible to teach good programming to students who are motivated by money: As potential programmers they are mentally mutilated beyond hope of regeneration.

User avatar
kriel
Posts: 922
Joined: Thu Feb 07, 2008 2:58 pm UTC
Location: Somewhere I'm not.
Contact:

Re: Curly Brackets Go Where?

Postby kriel » Sat Mar 21, 2009 2:11 pm UTC

FT: So far the types of formatting we've dealt with:

  • if/then/else blocks
  • if with an extremely large conditional
  • if with an extremely small 'body' (what's the word for this? Function?)
  • a block with a single function
  • a single 'line' that takes more than one line of visible space.

Did I miss anything?


Return to “Religious Wars”

Who is online

Users browsing this forum: No registered users and 14 guests