Hacker News new | past | comments | ask | show | jobs | submit login

Oh fun another style discussion! :-)

My own taste is for a "book style": write the code like you see in good books. I agree with most of this guide, although I'm pretty loose and case-by-case in practice. I've gradually added rubocop to lots of my projects, but then I wind up disabling tons of the rules.

I'm sad all the Ruby guides I've seen ban "and" and "or". I don't find them to be such an issue, and they are nicer to type & read. For one thing you can say `foo or raise "msg"` but need parens to say `foo || raise("msg")`. On team projects I sigh and go along with it, but it is a shame.

Speaking of "book style": I have a bunch of Python books with atrocious style, e.g. no spaces between operators or even between function arguments. How can anyone read that? I even see this from high-quality publishers like O'Reilly. What is going on with the Python world? Are their thumbs tired from all that indentation, so they leave out spaces everywhere else? ;-)

I really want to write a SQL style guide. There seems to be almost no consensus. My own formatting is idiosyncratic and not super principled, but I feel it is the most readable and expresses structure the best. It looks like this:

    SELECT  a, b
    FROM    t1
    LEFT OUTER JOIN t2
    ON      t2.t1_id = t1.id
    WHERE   t2.c = 'foo';
(EDIT: Sorry, t2.c = 'foo' is a pretty dumb thing to do in an outer join. :-)

Other people like to do one or more of these things:

    SELECT a
         , b
      FROM t1
           LEFT OUTER JOIN t2 ON t2.t1_id = t1.id
     WHERE t2.c = 'foo';
In other words:

- lead with the comma in SELECT.

- ragged left edge but line up all the single spaces in a column.

- indent the joins.

Anyway sorry for the rambling. :-) There is some observation that committees spend 90% of their time on trivial details and hardly discuss any of the important stuff, and style is no exception. But who doesn't like giving their opinion? :-)




> I'm sad all the Ruby guides I've seen ban "and" and "or". I don't find them to be such an issue, and they are nicer to type & read

I generally prefer `&&` and `||` for purely aesthetic reasons; they're much taller characters, so I can easily see the division between clauses. `and` and `or` look too much like identifiers (even with keyword syntax highlighting), so my eyes tend to gloss over them at first glance.

Out of curiosity, was the use of `&` in "type & read" intentional to try to demonstrate your point? My preference is interestingly the opposite for natural language; using tall punctuation like that feels too obtrusive for me in the middle of what usually is a bunch of lowercase letters.


> Out of curiosity, was the use of `&` in "type & read" intentional to try to demonstrate your point?

Ha ha, no! :-) I think I often type "&" as a micro-tie and "and" as a macro-tie. In other words "&" connects two words (or "a, b, & c"---and btw always with an Oxford comma, which should be obviously correct to any self-respecting programmer :-) but "and" connects clauses. This comment is probably the longest I've ever thought about it, but I do believe it is a persistent habit in my typing. Maybe to be consistent I should start using "|" too. :-)

I've never thought about tall/short for Ruby conjunctions, but it seems like a plausible motivation. Lots of typographers say people read by seeing ascenders|descenders (okay that one was on purpose :-) and can even recognize words where the letters are removed and you just draw an outline around them.

EDIT: I think I've been working too hard this week. Bikeshedding style it not normally how I'd spend my Friday morning, but it's pretty fun. :-)


A symbol for "or" would be so extremely sensible in this pattern.

And there was one.

Just as "&" abbreviated "et", a-sort-of-i-thingy abbreviated "vel"(= "or"). I have no idea why only "&" thrived and the "or" one faded. Now it's long forgotten, hiding at Unicode point U+026B


Interesting, thanks for sharing that! I once spent a summer helping a library write one-sentence summaries for medieval Latin books, and all the abbreviations and ligatures made the paleography really tough! But actually we do have a common symbol for "or". I used it once in that comment, but then I switched to "|" for fun: "/".


Those are all really interesting points! The division between clauses and lists is a really great insight; I think for me, I actually have the opposite preference ("&" for clauses and "and" for lists), although in practice I don't find myself using "&" in English very much.

> people read by seeing ascenders|descenders (okay that one was on purpose :-)

I actually totally missed that you had done that until you pointed it out :)

> and btw always with an Oxford comma

Now we've definitely found some common ground!


Funny, I've been wanting to do/working on a SQL style guide myself, with a big emphasis on using leading commas because of the readability and easy-adjustability. I do something like this:

            SELECT  
                a, b
            FROM
                t1
            LEFT OUTER JOIN t2
                ON t2.t1_id = t1.id
            WHERE   
                t2.c = 'foo'
            ;


I don't like it. :-) Splitting each clause into two lines makes my eyes stumble. I can't read it as effortlessly as I read normal text. At least you are left-aligning the keywords though. :-)

I have two spaces after my SELECT because when you press tab that's where you wind up next. Keeping things tab-aligned lets you easily type it out without a lot of fuss. I think that gets you much of the benefit of your broken-line clauses without impairing reading flow.

Also on a monitor vertical space is scarce, so I'd like to keep that reasonably compressed.

I get how leading commas makes it easy to re-order projections but I've never heard anyone say it makes them easier to read. :-) (I have no problem putting them on multiple lines if you have a lot or some are long.) And I'd rather optimize for readability over not needing to add/remove a comma.

I can see why you'd indent `ON` although I prefer to keep its conditions at the same level as WHERE.

I also do the semicolon at the bottom thing a lot.

Anyway it's fun to trade tastes. :-) SQL is surprisingly hard to rule on when it gets complicated. I doubt I am consistent once I start adding subqueries, CASE, etc.


This is also my preferred style. This means the keywords are separated from variables, and there is no code-based arbitrary space-alignment.


> I'm sad all the Ruby guides I've seen ban "and" and "or"

Well, as you no doubt know it's about the precedence - "and" and "or" are much lower, which can lead to confusing behaviour since conceptually they overlap so much with && and ||. I confess I had to go and refresh my memory of their exact behaviour, and I've been using ruby for a decade. I guess I agree with the "ban" since reading code can be confusing enough without having to think about which flavour of "and" you're using, but I guess I can see the use case.

Your (first) SQL example is exactly the style I would write, by the way! It's funny how conventions seem to arise by some emergent crowd phenomenon. I remember several years ago I realised that hey, I don't even need to capitalise the keywords - but the result looked so "wrong" after years of reading them capitalised that I still do it anyway.


I would do your SQL like this:

  select a, b
    from t1
      left outer join t2
        on t2.t1_id = t1.id
    where t2.c = 'foo'
      and t2.d = 'bar';
- Lowercase

- I don't like that use of variable number of spaces to try to align things you did in both your examples. Doing that kind of thing in code generally leads to having to readjust that spacing whenever a new line is added or removed.

- Constant-width indentation indicates structure. "left outer join" is part of the "from" clause, so it's indented further. "on" is part of the "left outer join" clause, so it's indented further.

Among languages, I imagine SQL is the one with the most varied styling among those who use it.


> I'm sad all the Ruby guides I've seen ban "and" and "or". I don't find them to be such an issue, and they are nicer to type & read. For one thing you can say `foo or raise "msg"` but need parens to say `foo || raise("msg")`.

To me your example seems to be the proper use of the `and` and `or` statements - unless the team decided that the `raise("msg") unless foo` variation was preferable.




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

Search: