A code formatter is a must-have tool in my workflow
I'm a very opinionated developer when things come to how to write code. I think
code consistency really matters a lot when we write code1. This time I want
to talk about some of my experiences after using
mix format (the official code
formatter shipped with Elixir 1.6) for several weeks.
Code formatter can be a great learning tool
When I first started learning Elixir, I tried to find some style guides2 because I think they can help me learn how to write more idiomatic Elixir code.
With the help of
mix format, I can get instance feedback3 on how this
code should be formatted and I know it would be the same even if another
developer come and implements it again.
Code formatter can be a real-time reviewer of your code
I've also been a rubocop user for a long time. I always think rubocop as a reviewer of my code.
Using rubocop (with flycheck in emacs) is like having a pair of eyes watching me writing code all the time. It provides suggestions on how well my code is written and helps me write better code.
rubocop teaches me how to write cleaner code
mix format improves this guidance to another level.
- I can see my code automatically updated after I save a file (I use mix-format6 in emacs). And it really feels different. It's like not only a person reviews your code in real-time, but also corrects it for you.
- But I have to say that , since
mix formatis still in its early stage, it has less features than rubocop. And
mix formatcannot change your code's logic (but only its style). Elixir may still need a linter that can provide suggestions on how well this code is written (like using Abc Metric to compute a method's complexity)
Focus on the things that really matter
mix format automatically formats my code when I save a file, I can
actually focus on things that really matter to me.
- Before using
mix format, I spent some part of my time formatting my code manually (Adding spaces here and there, indenting lines, splitting lines into shorter ones to fit the screen size, etc.). Even with emacs/vim's help, it's still a lot work to do.
- After using
mix format, I can be more forgivable on how I struct my code. Missing a space in a curly braces pair? No problem, just save the file and it's fixed. It's a huge relax on how I write my code (and I didn't even realize it before I used a formatter).
- Maybe I'll also try to let rubocop automatically format my code when I save my files.
行为主义学派认为学习的两大要点是 self-pacing 和 immediate feedback -- from https://twitter.com/ConanChou/status/956004158741667842