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.
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. :-)
SELECT
a, b
FROM
t1
LEFT OUTER JOIN t2
ON t2.t1_id = t1.id
WHERE
t2.c = 'foo'
;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.
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.
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.
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.
"This is the style established in both "The Ruby Programming Language" and "Programming Ruby". Historically it is derived from the fact that case and switch statements are not blocks, hence should not be indented, and the when and else keywords are labels (compiled in the C language, they are literally labels for JMP calls)."
1: https://github.com/rubocop-hq/ruby-style-guide/issues/273
I blame Rust, and macros.
Now that the guide has more editors I'm optimistic that we won't end up in a situation with that many non-triaged tickets down the road.
When reading code, my eye will immediately notice the `return` keyword and will have less to think about in terms of code execution.
The `return` keyword makes for great readability.
Maybe JS's implicit-return-in-arrow-functions is a better compromise.
https://github.com/testdouble/standard
Lightning talk about it here:
https://www.youtube.com/watch?v=uLyV5hOqGQ8
Edit: I posted it on its own if you want to discuss:
The guide has received ample feedback and edits/additions over the years, with an emphasis on reaching a consensus amongst senior ruby developers and open source contributors.
I believe that adding justifications or reasons for each guideline would make the guide harder to read and search. The way it is written now is succinct enough to pass on to junior developers, and allows a company to centralize style around a well-established set of rules rather than reinventing the wheel.
* Split panes in editors. With 80 chars, you can usually have two panes on screen along with a file tree, on most laptop screens. This is pretty nice to have.
* Code reviews in a web interface like Github/Gitlab - 80 chars means usually there's no need to pan, even when your browser window isn't full screen
It will definitely make people use weird variable names instead of meaningful ones.
is_included = true
could turn into, inc = true #included or increment?
That's a far worse side effect than other points raised in this thread.* Too long lines are hard to read. For text 11 words per line are recommended, otherwise your eyes will loose track. 11 Words is roughly 60 characters, but code is indented so you need a bit more.
* Some people look at your code in an 80-column terminal, if you use more, this would be rude.
* Some people use a GUI diff tool that displays the code side by side, so that's already 160 characters.
- Unless that's your team's preference, you're not helping anyone.
- Same as above.