0 users browsing Discussion. | 45 bots  
    Main » Discussion » The Horstmann brace style
    Poll
    What is your opinion on the Horstmann style?
    that's absolutely disgusting
     4
    Meh, it's all right
     1
    that's amazing
     1
    Already use it
     1
    7 users have voted so far
    Pages: 1
    Posted on 19-02-28, 23:49
    Stirrer of Shit
    Post: #50 of 717
    Since: 01-26-19

    Last post: 1763 days
    Last view: 1761 days
    So there's a somewhat obscure brace style, called the Horstmann.

    while (x == y)
    { something();
    somethingelse();
    //...
    if (x < 0)
    { printf("Negative");
    negative(x);
    }
    else
    { printf("Non-negative");
    nonnegative(x);
    }
    }
    finalthing();


    I really like it. It saves space, making things more readable, and it also lines up the braces. I can't see any downsides, except for losing the "documentation space" you have with the Allman style, where each brace gets its own line like this:

    int some_function(int n)
    { //documentation goes here
    do_something(n);
    if (n > 12)
    {
    printf("invalid month");
    }
    return n-1;
    }

    I've been using it for a fair amount of time (with Allman for functions), and the only downside I can see is that not much software supports it.
    So why isn't it more popular? When I show people it, the reactions are either "that's absolutely disgusting, never show me it again," or "that's amazing, why haven't I heard of this before?". But nobody seems to be able to explain why they hate it, other than that it's unfamiliar.

    There was a certain photograph about which you had a hallucination. You believed that you had actually held it in your hands. It was a photograph something like this.
    Posted on 19-03-01, 01:02
    Secretly, I'm PZ Myers

    Post: #149 of 599
    Since: 10-29-18

    Last post: 196 days
    Last view: 36 min.
    Not a bad style. Probably wouldn't use it but it beats the one with the half-indented braces.
    Posted on 19-03-01, 09:55 (revision 1)
    Post: #25 of 77
    Since: 10-31-18

    Last post: 1190 days
    Last view: 1116 days
    You lose the ability to use a text editor or IDE to comment out the first line (or swap the first line in an indented block with other lines).

    https://github.com/ambv/black places trailing commas after each line in a function call or list literal, to explicitly make swapping/commenting elements easy.

    I noticed that ruamel.yaml's YAML formatting also makes it hard to comment-out or reorder the first line:

    State 1:

    orders:
    - name: Apples
    price: 1.5
    - name: Onions
    price: 2
    "2d_identity_matrix": # quotes optional
    - - 1

    Diff 2: Newline before [maps within lists]. (Neither PyCharm nor ruamel.yaml does this)

    orders:
    -
    name: Apples
    price: 1.5
    -
    name: Onions
    price: 2
    "2d_identity_matrix":
    -
    - 1

    Diff 3: indenting lists within maps. (PyCharm does this, ruamel.yaml does not)

    orders:
    -
    name: Apples
    price: 1.5
    -
    name: Onions
    price: 2
    "2d_identity_matrix":
    -
    - 1

    - All the above parse to the same tree... I think YAML is too permissive.
    - There is no "standard" formatting style. I think State 1 confuses people unfamiliar with YAML (like myself at first).
    - ruamel.yaml uses State 1.
    - The YAML 1.2 spec uses Option 3 completely. https://yaml.org/spec/1.2/spec.html#id2760193
    - Pycharm does 3 but not 2 (but it's completely configurable).
    - I dislike the first 2 are dumb because you have a Dict[List[Dict]] but the innermost dict is only indented 1 level.

    i had a longer post written, but it was off-topic (reflecting on YAML's wonky syntax). New thread maybe?
    Posted on 19-03-01, 10:33
    Custom title here

    Post: #283 of 1164
    Since: 10-30-18

    Last post: 63 days
    Last view: 14 hours
    Posted by sureanem

    So why isn't it more popular? When I show people it, the reactions are either "that's absolutely disgusting, never show me it again," or "that's amazing, why haven't I heard of this before?". But nobody seems to be able to explain why they hate it, other than that it's unfamiliar.

    Because code formatting comes down to personal preference, and people are using whatever style they're comfortable with already.


    (I happen to think naming styles is kind of silly. And naming them after specific people is incredibly vain, given most of them have been organically developed numerous times.)

    --- In UTF-16, where available. ---
    Posted on 19-03-01, 11:44 (revision 1)
    Dinosaur

    Post: #176 of 1316
    Since: 10-30-18

    Last post: 3 hours
    Last view: 3 hours
    I've never liked opening braces just before sentences. And I'm on the opinion that the closing brace should go alone on its own line.

    I'll stick to the good ol' "explain what you're about to do", that is, the opening brace on the same line as the structure declaration:
    int foo(int moo) {
    return moo + 1;
    }

    if (a + b = c) {
    // do it!
    doIt();
    }


    EXCEPTION: Constant arrays - those are always a one-liner (if possible):
    public static final int SOME_CONSTS = {0, 1, 2, 4, 8, 16, 32}


    Licensed Pirate® since 2006, 100% Buttcoin™-free, enemy of All Things JavaScript™
    Posted on 19-03-01, 13:46

    Post: #81 of 456
    Since: 10-29-18

    Last post: 44 days
    Last view: 1 day
    Posted by tomman
    EXCEPTION: Constant arrays - those are always a one-liner (if possible):
    public static final int SOME_CONSTS = {0, 1, 2, 4, 8, 16, 32}

    EXCEPTION: 2D data:
    const
    Shapes : array[0..(8 * 3 * 7)-1] of char = (
    '████████' +
    ' ' +
    ' ' +
    '██████ ' +
    ' ██ ' +
    ' ' +
    '████ ' +
    ' ████ ' +
    ' ' +
    ' ████ ' +
    '████ ' +
    ' ' +
    '████ ' +
    '████ ' +
    ' ' +
    '██ ' +
    '██████ ' +
    ' ' +
    ' ██ ' +
    '██████ ' +
    ' ');


    My current setup: Super Famicom ("2/1/3" SNS-CPU-1CHIP-02) → SCART → OSSC → StarTech USB3HDCAP → AmaRecTV 3.10
    Posted on 19-03-01, 14:11
    Custom title here

    Post: #284 of 1164
    Since: 10-30-18

    Last post: 63 days
    Last view: 14 hours
    EXCEPTION: segmentation fault

    --- In UTF-16, where available. ---
    Posted on 19-03-01, 14:16
    Stirrer of Shit
    Post: #56 of 717
    Since: 01-26-19

    Last post: 1763 days
    Last view: 1761 days
    Posted by tomman
    I've never liked opening braces just before sentences. And I'm on the opinion that the closing brace should go alone on its own line.

    I'll stick to the good ol' "explain what you're about to do", that is, the opening brace on the same line as the structure declaration:

    How do you find out which block is which if the braces don't line up? With multiple levels of nesting, that gets ugly fast.

    There was a certain photograph about which you had a hallucination. You believed that you had actually held it in your hands. It was a photograph something like this.
    Posted on 19-03-01, 16:39
    Stirrer of Shit
    Post: #57 of 717
    Since: 01-26-19

    Last post: 1763 days
    Last view: 1761 days
    Posted by jimbo1qaz
    You lose the ability to use a text editor or IDE to comment out the first line (or swap the first line in an indented block with other lines).

    No, only the latter.

    for (;;)
    { a();
    b();
    c();
    }

    becomes

    for (;;)
    { //a();
    b();
    c();
    }


    There was a certain photograph about which you had a hallucination. You believed that you had actually held it in your hands. It was a photograph something like this.
    Posted on 19-03-01, 20:47
    Dinosaur

    Post: #179 of 1316
    Since: 10-30-18

    Last post: 3 hours
    Last view: 3 hours
    Posted by sureanem
    Posted by tomman
    I've never liked opening braces just before sentences. And I'm on the opinion that the closing brace should go alone on its own line.

    I'll stick to the good ol' "explain what you're about to do", that is, the opening brace on the same line as the structure declaration:

    How do you find out which block is which if the braces don't line up? With multiple levels of nesting, that gets ugly fast.

    It's called "use a competent editor/IDE that can highlight matching braces".

    This sadly excludes most (if not all?) CLI-based editors and Notepad, but that's fine as I don't use those for coding.

    Licensed Pirate® since 2006, 100% Buttcoin™-free, enemy of All Things JavaScript™
    Posted on 19-03-01, 22:56
    Full mod

    Post: #141 of 443
    Since: 10-30-18

    Last post: 1101 days
    Last view: 172 days
    Posted by sureanem
    How do you find out which block is which if the braces don't line up? With multiple levels of nesting, that gets ugly fast.


    Indenting, obviously:
    for (x = 1; x < 10; x++) {
    if (x % 2 == 0) {
    printf("Even!");
    } else {
    printf("Odd!");
    }
    }

    If you tell me you can look up from "}" to find the aligned "{", but can't look up from "}" to find any other aligned text, I'm going to be very doubtful.

    THAT SAID, much like "which font is the most readable", the biggest factor of all is what you're already used to. If you've spent twenty years finding blocks by looking for isolated "{" and "}" characters while ignoring all the text between them, finding blocks by any other pattern is going to be more effort and feel "wrong".

    One of the happiest discoveries I've made while learning Rust is the rustfmt command for automatically formatting code. While automatic formatting can never be quite as nice as manual formatting, it's great to be able to type in any old thing, hit "save" and watch it magically become neat and tidy. Less dramatically but more significantly, I don't waste ten minutes trying to reformat something "just right" because it'll snap right back afterwards. Of course, it helps that the Standard Rust Style is pretty close to my preferred style anyway, but if more people used auto-formatting there'd be fewer threads like this one. ;)

    The ending of the words is ALMSIVI.
    Posted on 19-03-01, 22:58

    Post: #50 of 100
    Since: 10-30-18

    Last post: 1782 days
    Last view: 1347 days
    The standard rust style is wrong because it doesn't use allman braces. Fight me.
    Posted on 19-03-01, 23:11
    Secretly, I'm Patrick Stewart

    Post: #151 of 599
    Since: 10-29-18

    Last post: 196 days
    Last view: 36 min.
    Autoformat is great and I have it set up how I like in VS10.
    Posted on 19-03-02, 00:00
    Stirrer of Shit
    Post: #60 of 717
    Since: 01-26-19

    Last post: 1763 days
    Last view: 1761 days
    Posted by tomman

    It's called "use a competent editor/IDE that can highlight matching braces".

    This sadly excludes most (if not all?) CLI-based editors and Notepad, but that's fine as I don't use those for coding.

    Vim supports it, and presumably emacs too. Which CLI-based editors don't, you mean?
    The fact that you can't do it visually has always been a huge issue for me, that you actually have to move the cursor over there to check.
    Posted by Screwtape

    Indenting, obviously:
    for (x = 1; x < 10; x++) {
    if (x % 2 == 0) {
    printf("Even!");
    } else {
    printf("Odd!");
    }
    }

    If you tell me you can look up from "}" to find the aligned "{", but can't look up from "}" to find any other aligned text, I'm going to be very doubtful.

    How do you mean? It's true that in trivial examples like this, it's possible. But if you have more difficult examples, then matching them becomes considerably harder:

    if (x < foo(y, z))
    haha = bar[4] + 5;
    else {
    while (z) {
    haha += foo(z, z);
    z--;
    }
    return ++x + bar();
    }


    if (data != NULL && res > 0) {
    if (!JS_DefineProperty(cx, o, "data", STRING_TO_JSVAL(JS_NewStringCopyN(cx, data, res)), NULL, NULL, JSPROP_ENUMERATE)) {
    QUEUE_EXCEPTION("Internal error!");
    goto err;
    }
    PQfreemem(data);
    }
    else if (!JS_DefineProperty(cx, o, "data", OBJECT_TO_JSVAL(NULL), NULL, NULL, JSPROP_ENUMERATE)) {
    QUEUE_EXCEPTION("Internal error!");
    goto err;
    }

    Especially if the blocks are twice as long.

    THAT SAID, much like "which font is the most readable", the biggest factor of all is what you're already used to. If you've spent twenty years finding blocks by looking for isolated "{" and "}" characters while ignoring all the text between them, finding blocks by any other pattern is going to be more effort and feel "wrong".

    Fair enough. In practice, I'd reckon the smallest legible font (contingent on eyesight and dpi) is usually the most readable one. I suppose you eventually could get used to the 3x5 fonts though, even if your eyesight isn't good enough, by recognizing the patterns.

    One of the happiest discoveries I've made while learning Rust is the rustfmt command for automatically formatting code. While automatic formatting can never be quite as nice as manual formatting, it's great to be able to type in any old thing, hit "save" and watch it magically become neat and tidy. Less dramatically but more significantly, I don't waste ten minutes trying to reformat something "just right" because it'll snap right back afterwards. Of course, it helps that the Standard Rust Style is pretty close to my preferred style anyway, but if more people used auto-formatting there'd be fewer threads like this one. ;)

    Would there? We'd still be discussing to what to set our autoformatters to, no?
    Case in point:
    Posted by wareya
    The standard rust style is wrong because it doesn't use allman braces. Fight me.

    I use vim, which does have functionality like this (although I rarely use it, since I can't figure out how to configure it), yet I still make threads like this ;)

    There was a certain photograph about which you had a hallucination. You believed that you had actually held it in your hands. It was a photograph something like this.
    Posted on 19-03-02, 01:00 (revision 1)
    Full mod

    Post: #142 of 443
    Since: 10-30-18

    Last post: 1101 days
    Last view: 172 days
    > Vim supports it, and presumably emacs too. Which CLI-based editors don't, you mean?

    nano doesn't highlight matching brackets, although it does have a "jump to matching bracket" command.

    I know this because I was going to reply to tomman with something like "even nano highlights matching brackets!" and then decided to actually fact-check myself.

    > How do you mean? It's true that in trivial examples like this, it's possible. But if you have more difficult examples, then matching them becomes considerably harder

    This is the thing I was talking about, where we're looking at the same examples and seeing different things. I look at your examples and see something like:
    ##################
    ##################
    ######
    ###########
    ##################
    ####
    #
    ###################
    #
    ...and yeah, I can quite clearly see the edges of blocks just by looking at the shape of the left-hand margin. It gets more difficult to guess which line corresponds to a given closing-bracket with a bucket-shaped structure, where you have deeply-nested blocks and then many lines at the deepest level:
    ##################
    ##################
    ##################
    ##################
    ##################
    ##################
    ##################
    ##################
    ##################
    ##################
    ##################
    ##################
    ##################
    ##################
    #
    #
    #
    #
    ...especially if the inner-most block is taller than the screen height. Still, that's going to be taxing for *any* kind of visual recogition, since it's harder to extrapolate accurately the further you go.

    > We'd still be discussing to what to set our autoformatters to, no?

    Autoformatters aren't infinitely configurable, so the discussion only has to cover the available options, not every possible variation. Also, autoformatters have defaults, so one specific configuration is vastly easier to set up and maintain, and every alternative has additional costs. Most people get a lot less invested in their formatting choices when they have to spend an afternoon wrestling with config choices or trying to talk the maintainer into accepting their patches.

    The ending of the words is ALMSIVI.
    Posted on 19-03-02, 06:19

    Post: #28 of 49
    Since: 10-29-18

    Last post: 1901 days
    Last view: 1786 days
    As usual, JavaScript is the odd one out but for a really stupid reason this time (instead of just a regular stupid). Put the { on the same line as the (...) or risk potential automatic semicolon insertion. Same for . and any other operators with right-hand sides like + .
    Posted on 19-03-02, 11:45
    Great and Powerful

    Post: #154 of 599
    Since: 10-29-18

    Last post: 196 days
    Last view: 36 min.
    Posted on 19-03-02, 12:15
    Dinosaur

    Post: #183 of 1316
    Since: 10-30-18

    Last post: 3 hours
    Last view: 3 hours
    Posted by Screwtape
    > Vim supports it, and presumably emacs too. Which CLI-based editors don't, you mean?

    nano doesn't highlight matching brackets, although it does have a "jump to matching bracket" command.

    I know this because I was going to reply to tomman with something like "even nano highlights matching brackets!" and then decided to actually fact-check myself.

    Thanks for clearing things up. That shows how much I deal with CLI editors, AKA "nothing" - I only use nano for quick edits and little else, but it's interesting to know that it can actually deal with bracket navigation.

    Anyway, my point still stands: if you're writing some code and your editor doesn't support any kind of bracket highlighting/navigation, you should really switch to a better editor.

    Licensed Pirate® since 2006, 100% Buttcoin™-free, enemy of All Things JavaScript™
    Pages: 1
      Main » Discussion » The Horstmann brace style
      Yes, it's an ad.