3 ms·
Other "forced" examples: result = a==1 || b==2 || c==3 ; Vs result = a==1 || b==2 || c==3 ;
by TrianguloY 4y ago
Other "forced" examples:
result = a==1
|| b==2
|| c==3
;
Vs
result = a==1 ||
b==2 ||
c==3 ;
and
result = data
.getA()
.getB()
.getC()
;
Vs
result = data.
getA().
getB().
getC();
Personally I prefer the first examples, because for me it's easier to "group" the lines.
- dspillett 4y agoI tend to use that sort of layout, particularly the comma-first example shown in the article for SQL and JSON. It makes moving things around less error prone (no need to worry about where the ; is then you reorder or add to or remove from the end of a list) and makes diffs cleaner between versions for the same reason. And I feel it just seems clearer. Some people seem to hate it with a passion though.
- TrianguloY 4y agoThe issue with that layout (and with my examples) is that the first element is the odd one, and you can't reorder it. I think it's better this way (usually the first different is special, and also you usually add things at the end) but I prefer a trailing comma over the comma at the left. A leading comma, like the article discuss, would be an nice addition indeed
- tasty_freeze 4y agoI write a lot of verilog code, and most of its operators are the same a for C. Your first example doesn't highlight the real benefit of its style because all of your items are the same length. Here is a better example: foo = here_is_some_logical_condition || and_another || (this_term && that_term) || exception_case; vs foo = here_is_some_logical_condition || and_another || (this_term && that_term) || exception_case;
- sweetsocks21 4y agoThis delimiter first pattern is used a lot in Ocaml code as well: Records: https://github.com/janestreet/base/blob/master/src/avltree.ml#L21 https://github.com/janestreet/base/blob/master/src/avltree.m... If statements: https://github.com/janestreet/base/blob/master/src/string.ml#L84 https://github.com/janestreet/base/blob/master/src/string.ml... Lists: https://github.com/janestreet/base/blob/master/src/string.ml#L175 https://github.com/janestreet/base/blob/master/src/string.ml... Function calls: https://github.com/janestreet/base/blob/master/src/string.ml#L219 https://github.com/janestreet/base/blob/master/src/string.ml... Type definitions: https://github.com/janestreet/base/blob/master/src/array.ml#L61 https://github.com/janestreet/base/blob/master/src/array.ml#... It's not always used, shorter snippets tend to be all on one line.