Are formatters better than linters?

栏目: IT技术 · 发布时间: 4年前

内容简介:So, I just watchedMichał Muskała’s talk at CodeBEAMSF, and I have something to say…As you might know, I’m also working withJuan Bono,Particularly I want to look at the following slide…

So, I just watchedMichał Muskała’s talk at CodeBEAMSF, and I have something to say…

Erlang is getting pretty!

As you might know, I’m also working withJuan Bono, Diego Calero ,Facundo Olano, and others on our own formatter for Erlang (just like Daniel Tipping is working on steamroller ). I could write an article on how our formatter differs fromMichał’s but this time I want to focus our attention on a different topic.

Particularly I want to look at the following slide…

Are formatters better than linters?

Formatters are more than Linters

Are Formatters “Linters on Steroids”?

Michał presents a concept in his talk that is somewhat widespread and I don’t think it’s correct: The idea that if linters can tell you where you need to improve your code, formatters are better because they can just fix it.

Having worked on both Elvis (a linter) and rebar3_format (a formatter), I do believe that they are complementary but independent tools. They do have some intersection, but the really interesting parts are the ones where they don’t intersect with each other. And I believe this happens as well in other languages, for instance with Credo and mix format for Elixir.

There are things that linters can detect, but formatters can’t fix. To name a few Elvis rules like that:

  • Nesting Level : The linter can detect that your code is nested too deeply, but what will the formatter do to fix that? It could theoretically move some lines out to a different function but I don’t think that’s something you would expect your formatter to do, right?
  • No debug calls : The linter can tell you that you left some io:format ’s in your production code, which is not good. I don’t think the formatter should just remove them . Leaving aside the fact that sometimes it’s not as easy as just removing the calls.
  • DRY : My favorite Elvis rule of all time: The linter can identify pieces of code that are repeated. What will the formatter do here? A tool that would actually refactor your code to extract them to a separate function would be amazing but I don’t think the formatter should be that tool.

If only we had a tool to automatically perform all those tasks… MaybeSimonThompson knows one ?

On the other hand, there are things that a formatter can fix and a linter shouldn’t deal with, like:

  • Consistent indentation of clauses. The linter can warn you on whether you’re using tabs or spaces for indentation but figuring out if you properly indented all your code using the right number of spaces/tabs is not worth the effort, particularly if you have a tool like a formatter that just fixes that for you .
  • Personalization. As I mentioned in my article linked above, I believe that formatters should have room for personalization, so I can read the code in the way I understand it best and you can read the same code in the way that you like it best. We don’t need to fight over comma-first ROK-style, indentation levels, tabs-vs-spaces… As long as we share a format for the code once its pushed, each one of us can work using his/her own style. That’s something that the linter can’t allow since the idea is that the rules enforced by the linter are respected by all of us.

Conclusion

The linter and the formatter are different tools that serve different purposes. The linter will make sure your code is well written and follows the best practices defined by the community or your team. The formatter will make sure that your code is nicely written and easy to understand by any other developer.

A formatter will happily make some linter rules obsolete (which is great!) but it won’t remove the need for it, at all.

In other words…

Don’t stop using a linter when you start using a formatter!


以上所述就是小编给大家介绍的《Are formatters better than linters?》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Spark

Spark

Bill Chambers、Matei Zaharia / O′Reilly / 2017-10-31 / GBP 39.99

Learn how to use, deploy, and maintain Apache Spark with this comprehensive guide, written by the creators of the open-source cluster-computing framework. With an emphasis on improvements and new feat......一起来看看 《Spark》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具