Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> Of course, conventions in Java of, e.g., giving each curly brace its own line aren't helping its case in line counts, either.

My comment is basically a free-association from this.

In C and Java both (and, um, CSS), the people I've encountered seem to strongly prefer giving each matched pair of curly braces one line (that is, the opening { doesn't get its own line, but the closing } does).

In lisps (to my knowledge) it's more normal to have all your closing )s stuck together at the end of the previous line. Using a clump of )))))s to discern exactly which scope you're in afterwards is tricky (I'm pretty sure that's exactly the "problem" that the C/Java style is trying to solve), so you look at the indentation instead.

Python formalized the indentation-is-scope paradigm, which means a malicious programmer can't trick you into believing you're looking at a different scope than you are, but my Python code always ends up being a lot taller than I feel it should be, so I have a lingering sense that Python took it a little too far. It turns out that I occasionally appreciate the option you get with lisps to put a short bit of code all on one line and mark the parse tree with parens instead of with line-breaks-plus-indentation.

(All that said, I do like Python a lot.)



Using a clump of )))))s to discern exactly which scope you're in afterwards is tricky (I'm pretty sure that's exactly the "problem" that the C/Java style is trying to solve), so you look at the indentation instead.

Another handy tool is an editor with rainbow delimiters. This feature changes the colour of each matched pair of parentheses so that they can be seen at a glance.


Seems to me that if you have a clump of )))), you should refactor that into individual functions instead. This would be akin to having a 500 line Java method.

There's really no excuse not to, since refactoring functional code tends to be as simple as taking a node in a tree and moving it out.


Since you use () for more or less everything in Lisp, from declaring functions or records(structs) to calling functions or doing comparisons (<, >, =, etc. are functions in Lisp), you will always get a clump of ) at the end of something.

In Java, if you have a class containing a function which itself contains an if-statement containing multiple lines. You will have three lines at the end of the file containing nothing but }. There is no reason you couldn't clump those together and thus end the last statement in your function with }}}.

The difference is, in Java it's good practise to give each } a seperate line. In Lisp, most people prefer to clump their ) together.

Also, you could easily end a function with alot of )))))).

(defn unique-large-squares [list-of-nums] (count (unique (filter #(> % 100) (map #(* % %) list-of-nums)))))

Of course, if the clump of ) are off-putting, you could always use ->

(defn unique-large-squares [list-of-nums] (-> list-of-nums (map #(* % %)) (filter #(> % 100)) (unique) (count)))


I imagine yogthos was referring to this, but the clump of `)))))` itself is only a symptom of the real problem: that you've got too much nested logic.

`)))))` at the end of a function doesn't matter. The function's over. Even if you needed to make a change that would break the clump up, you generally navigate it from the opening side.

But if it's at the end of a fat true-expression of an if-statement, you probably have to add a `;; x is false` comment to remind yourself what's going on by the time you get to the false-expression.

Just like in Python when you get to an `else:` yet you have to scroll up just to find the matching `if` because somebody decided to roll their own SOAP client on the true path.


Dunno. There's just a kind of a bad design smell to a language where one needs, or sorely tempted to use colors to discern the basic syntax.

Aside from the fact that if you're color blind then apparently you're stuck with all the )))))))s.


As we're already off-topic, I'll chip in with my view.

I personally hate the opening { stuck on the end of the line in C/Java. For me it goes on a line on its own pretty much every time.

I've lost count of the amount of times there's been a subtle bug introduced by incorrect scope as a result of a { being missed from the end of a line and not being noticed due to the code being formatted as if it was there.

With a { sat on its own, directly lining up with the }, that's pretty much impossible to miss.


I've always put it on the end of the line and found it easier to parse visually than when its on a separate line. To me that just seems like a waste of space.


How do you quickly check to make sure that all of your closing braces match up with the relevant opening brace, rather than, for example, an if statement that you forgot to put the { on?

Hopefully the formatting will work for this -

For example,

    if (isOK)
        for(thing in list
        {
            doSomething(thing);
        }

    }
Screams out to me at very first glance that I've missed a {,

whereas

    if (isOK)
        for(thing in list){
            doSomething(thing);
        }
    }
needs me to scan to the end of each conditional line to see where the problem is.


For me, it's the other way around.. the second instance is easier to spot because the visual cue of associating the block with the statement is more obvious if you assume beforehand that each statement has to end with the brace.

As for closing braces, I associate them with the start of the statement itself (the if and for) and not the opening brace, assuming the indentation is consistent.

I'm not arguing one is necessarily more right than the other - just that I prefer the way considered less right by consensus.


I get the impression that the { on the end of the line is the one that most people prefer, so I think you're more with consensus than me.

The argument about taking up less space, I get - although given the massive monitors that we all have these days, that seems far less of a consideration than when I used to work on 20-line terminals.

What I just genuinely struggle to understand is how it's clearer (and if I managed to understand, it might help me parse other peoples' code).

Let me turn my code into something that actually has a matching amount of braces

    while (readLine){
        if (isOK)
            loadSomething();
            for(thing in list){
                doSomething(thing);
        }
    }

versus

    while(readLine)
    {
        if (isOK)
            loadSomething();
            for(thing in list)
            {
                doSomething(thing);
        }
    }
If I was quickly parsing the first code, I'd assume everything was fine and that the first } was closing the if(ok). To spot the defect, I have to scroll up, find the matching control statement and check to the end of its line to see whether it had a {.

With second one, even the most rudamentary scan tells me that there's an opening and a closing brace that don't match up. All I have to do is scroll up and see whether there's a matching { at the same indentation.

I'm not saying you're wrong - if you are, most people who write coding articles, or code that I have to look at, are also wrong. But I would love to know what it is that other people can see easier/quicker in the first code than they can in the second in.


> I personally hate the opening { stuck on the end of the line in C/Java.

It's an abomination.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: