r/Clojure • • Nov 02 '21

Everything is Wrong

https://metaredux.com/posts/2021/11/02/everything-is-wrong.html
77 Upvotes

26 comments sorted by

6

u/didibus Nov 02 '21 edited Nov 02 '21

I agree with the article, too much hyperbole saying everything is wrong with the style guide, first, style is subjective (look at Rich Hickey's Java style 😝), second I'd say the style guide is pretty good as proven by tons of people happily styling their code by its rules.

Now on to the bikeshed myself 😁

Interestingly there is an open issue about the 1 vs 2 space alignment from 2016 https://github.com/bbatsov/clojure-style-guide/issues/126

(func
 a
 b
 c

I guess I never even realized this was the indentation, because I never put the first argument on a newline.

So I'd do something more like:

(func a b
      c d)

or

(func a
      b
      c)

I also don't know who sticks with 80line anymore, you try to aim for that, but there's always places where you go beyond. Most auto-formatter won't truncate things.

I think the only style that I could see making a big claim to fame over the current one is the "indent lists with 2 spaces always no matter what", and align vectors, maps and sets.

I think it was described here: https://tonsky.me/blog/clojurefmt/

I think the simplicity of that alternate style guide for pretty descent overall results visually are appealing in a way where I could be convinced that this could take over as the new norm.

Though I'd actually break that rule and say I'd prefer indent lists with 1 space always, and align the rest.

Because with 1 space, things are always visually aligned at least, I find it more pleasing:

(+ 1
  2
  3)

See how it looks misaligned?

(+ 1
 2
 3)

And with only one space it looks better.

This actually makes me wonder.... Maybe just always align everything to the first element, simple, still looks good, super straightforward.

(defn fib
 ([n]
  (fib [0 1] n))
 ([x, n]
  (if (< (count x) n) 
   (fib
    (conj x
     (+
      (last x)
      (nth x (- (count x) 2))))
    n)
   x)))

I don't think it's that bad. Might be a little harder to see the demarcation with 1 space I guess is the downside and could be the draw to say lists are 2 spaces.

5

u/bozhidarb Nov 02 '21

I've been programming in Lisp since before Clojure existed, so I'm a bit biased and my personal take is - if something ain't broke don't fix it (that's to say that I think traditional Lisp indentation is good enough). I wrote in the past about semantic formatting and my perspective hasn't changed. You'll notice that the style guide acknowledges the alternative style (fixed indentation) as a reasonable option. As for using 1 space indentation everywhere - obviously you can do this, there's just not a lot of precedent for doing so and we optimize for real-world usage. :-)

4

u/didibus Nov 02 '21

I started to lean on the align everywhere approach because it works well with parinfer and it's what Replete uses on the mobile Android app. And I've been surprised as I use it how well it works.

But you're right, I haven't tried it on a large code base or day to day. But I'm getting tempted to try it.

I do agree that I don't really have a problem with the current formatting, but if I needed to hand format Clojure code, or when I whiteboard, I see the appeal of that alternate style. And then if you buy into the Go philosophy of consistency, that's probably where I'm curious. Though I also don't know if I like the Go philosophy, why style to your lowest common denominator? If in Emacs with Cider I can get more semantic driven, properly aligned code, why not?

4

u/NoahTheDuke Nov 02 '21

Rich Hickey's Java style

It’s truly the worst thing about Clojure (which says a lot) haha. Reading the implementation code can be so rough, so confusing and awkward.

2

u/WalterGR Nov 03 '21

Friendly feedback: on my laptop, everything above the fold is a critique of the blog post in question and reads pretty snarky. Taking the high road in situations like this reads better. :)

1

u/bozhidarb Nov 03 '21

Fair enough. Thanks for the feedback!

49

u/weavejester Nov 02 '21

I've worked in Clojure for 11 years now, and 9 of those professionally, and the Clojure Style Guide aligns with my existing expectations and preferences for formatting Clojure code. It's not as if the guide is an invention entirely made up by one person; it's a continuation of existing Lisp and Clojure practices.

When it comes to the 80 character line limit, I still find it preferable even in on larger displays. I can upgrade my hardware, but I can't upgrade what's comfortable to visually process. It's the same reason why newspapers align text in columns, or why book pages are typically taller than they are wide.

20

u/aHackFromJOS Nov 02 '21

Thank you for the style guide, it’s a great resource 🙏

15

u/jeff303 Nov 02 '21

As always, no good deed goes unpunished.

11

u/Akabander Nov 02 '21

I'm sorry that one of the rewards for service is getting the attention of people like that.

9

u/TheLastSock Nov 02 '21 edited Nov 02 '21

Ideally, Programs should be formatted on read, based on the readers personal preference. Where is this most critical? Any source control repo like github, especially when your comparing diffs. The diff should be over programmatic changes. It's damn silly to be wasting time on indentation when there are real problems to be solved.

6

u/NoahTheDuke Nov 02 '21

I too disagree with the 80 char limit and 1-space indentation for functions but you know what I do about it? Change it in my personal projects and then move on with my life. Writing screeds like this is quite unnecessary and caustic.

Thanks for all the hard work you've done, Bozhidar. It's been invaluable to many folks, myself included. I'm sorry this one got to you.

5

u/CoBPEZ Nov 02 '21

You are only ”a man who wrote some of the widely used tooling in Clojure”, how dare you host a crowd sourced style guide?

8

u/InternationalCod3155 Nov 02 '21

The main formatting issue I often see is people aligning the values in maps, it has so many more disadvantages than advantages and although the style guide implies you shouldn’t do it, it would be good if it was stated more explicitly. Preferably at the very top. In size 100 font.

Anyway bikeshedding aside, the clojure style guide is a fantastic resource and it’s a shame that it’s not more widely adopted.

4

u/TheLastSock Nov 02 '21 edited Nov 02 '21

What are the disadvantages? Clearly in some cases an outlier key length will force the others to seem odd. But then ideally i could just disable the rule on that map. I haven't come across that case in 5 years so it's fairly rare.

10

u/InternationalCod3155 Nov 02 '21

It creates more conflicts, it makes diffs harder to read, it’s much harder to undo than it is to do in the first place and it’s painful to manage when working in an editor without support for it (like temporary using vim for some quick edits)

Also it really isnt that much easier to read, especially when the keys aren’t similar lengths and you have syntax highlighting.

I guess ideally there would be an editor plugin that could visualise the values as aligned but not actually changing the white space

6

u/[deleted] Nov 02 '21 edited Nov 02 '21

[removed] — view removed comment

0

u/[deleted] Nov 02 '21

[deleted]

1

u/deaddyfreddy Nov 03 '21

and this makes the first 2 slightly easier

I wouldn't be so sure, I've seen a lot of aligned Clojure code which made my eyes bleeding

0

u/[deleted] Nov 04 '21

[deleted]

1

u/deaddyfreddy Nov 04 '21

It's just a half assed way of reintroducting tabs

definitely!

1

u/deaddyfreddy Nov 03 '21

key lengths can differ very much (Especially if you use fully qualified ones), so the potential problems are

  • the space between a key and a value is so big that it's hard to say if they are on the same line

  • you can nest more than, say, 3-4 levels, because the line became longer than 100 chars

2

u/bpiel Nov 02 '21

> Everything is wrong all the time. Everyone is wrong all the time.

Yes! If, over the looong run, everything is getting better, then even our best solutions today will be laughably backward in the future. Remembering that can help you stay humble, but also encouraged by the fact the "better" is the best you can do.

2

u/juxtajarred Nov 02 '21

I used the style guide as a base for my last company’s Clojure style guide and it served us well! I never interpreted it as prescriptive, merely a good place to start that aligns with a lot of the community. Appreciate all you’ve done for the Clojure world Bozhidar.

3

u/moxxon Nov 02 '21

The only thing I find "wrong" about the style guide is an 80 character limit... basing modern coding standards on punch card parameters is just silly.

If I'm contributing to a project and their limit is 80 I'll stick to it, but where I'm calling the shots it's somewhere between 100-120.

Overall having a consistent, and reasonable, style guide is incredibly useful, especially when you don't have developers that get anal about it and apply common sense.

1

u/bsless Nov 04 '21

Everyone always talks about the technological considerations of the 80 chars limit and no one talks about the human considerations

The limit should be 60. Humans read short lines. We visually scan paragraphs diagonally.

Look at Tonsky's blog for an example of human-friendly line width

1

u/Eno6ohng Nov 03 '21

I don't think I've ever seen a function call with a line break before the first argument.

As for the 80 width limit, I'd even argue it might be useful to reduce it further (down to 60 or something) to force avoidance of the nesting in favor of flattening refactoring