| 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 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.