Main » Discussion » The Horstmann brace style » New reply
    New reply
    Post help

    Presentation

    [b]…[/b] — bold type
    [i]…[/i] — italic
    [u]…[/u] — underlined
    [s]…[/s] — strikethrough
    [code]…[/code] — code block
    [spoiler]…[/spoiler] — spoiler block
    [spoiler=…]…[/spoiler]
    [source]…[/source] — colorcoded block, assuming C#
    [source=…]…[/source] — colorcoded block, specific language[which?]
    [abbr=…]…[/abbr] — abbreviation

    [franpared] [franpaorange] [franpayellow] [franpablue] [franpagreen] - you know what these are.
    [color=…]…[/color] — set text color
    [jest]…[/jest] — you're kidding
    [sarcasm]…[/sarcasm] — you're not kidding

    Links

    [img]http://…[/img] — insert image
    [url]http://…[/url]
    [url=http://…]…[/url]
    >>… — link to post by ID
    [user=##] — link to user's profile by ID

    Quotations

    [quote]…[/quote] — untitled quote
    [quote=…]…[/quote] — "Posted by …"
    [quote="…" id="…"]…[/quote] — ""Post by …" with link by post ID

    Embeds

    [youtube]…[/youtube] — video ID only please
    Thread review
    CaptainJistuce Truer words have rarely been drawn.
    Kawaoneechan I refer again to my previous post in this thread.
    CaptainJistuce Man, did you really create a throwaway account to bump a six-year-old thread by a banned user just so you could do drive-by coding style evangelism?
    Tony G The Horstmann style is the best thing invented after the hot water. Personally, I use it with slight modification:

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

    The 1-space before brace visually improves clarity - see how 'else' operator stands out like a wall you can't pass unnoticed. Also it helps to see the thing in braces as "owned" by the previous symbol.
    I've been using it since I discovered it. Usually I set tab=4 spaces, code aligns on tabs, first tab after a brace has automatic less positions, better seen in picture (PHP code):

    The image is invisible, URL is https://imgur.com/u5dtgVX
    tomman
    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.
    Kawaoneechan
    neologix 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 + .
    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.

    > 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.
    ‮strfry("emanresu")
    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 ;)
    Kawaoneechan Autoformat is great and I have it set up how I like in VS10.
    wareya The standard rust style is wrong because it doesn't use allman braces. Fight me.
    Screwtape
    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. ;)
    tomman
    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.
    ‮strfry("emanresu")
    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();
    }
    ‮strfry("emanresu")
    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.
    CaptainJistuce EXCEPTION: segmentation fault
    creaothceann
    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 = (
    '████████' +
    ' ' +
    ' ' +
    '██████ ' +
    ' ██ ' +
    ' ' +
    '████ ' +
    ' ████ ' +
    ' ' +
    ' ████ ' +
    '████ ' +
    ' ' +
    '████ ' +
    '████ ' +
    ' ' +
    '██ ' +
    '██████ ' +
    ' ' +
    ' ██ ' +
    '██████ ' +
    ' ');
    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:
    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}
    CaptainJistuce
    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.)
    nyanpasu64 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?
      Main » Discussion » The Horstmann brace style » New reply
      [Your ad here? Why not!]