lainchan / λ

Tools for refactoring source code to conform to a coding style.

Cypher · 2021-03-10 16:11
I'd like to ask you for every tool you know of, for any language, that is able to modify source code to conform to an established coding style. And any tools you use to enforce a particular coding style for your source code - either at the time of writing, or afterwards. I will list what I have found so far. >Java https://github.com/google/google-java-format (Apache-2.0 License) >Python https://github.com/hhatto/autopep8 (MIT License) >Elm https://github.com/avh4/elm-format (BSD-3-Clause License) >Ruby https://github.com/rubocop/rubocop (MIT License) >Bash https://arachnoid.com/python/beautify_bash_program.html >C++ https://github.com/xuy/google-astyle (No license and seems dead) >Go https://github.com/po3rin/gofmtmd (Apache-2.0 License) >Language agnostic/Multiple languages https://clang.llvm.org/docs/ClangFormat.html http://astyle.sourceforge.net/
Knuth · 2021-03-10 16:28
https://www.gnu.org/software/indent/ is a classic but also basically useless in currentyear when clang-format exists. >D https://github.com/dlang-community/dfmt That Go one is just a wrapper for gofmt, which is part of the distribution. I think using things like this is generally misled, a style should be simple enough that it's easy to conform to without such tools, and also makes for more useful `git blame`s, though that's not always a problem if you enforce style from the beginning.
aliased · 2021-03-10 18:56
>>24806 This is not the correct way to approach this issue. Language servers are the future, and work on every major platform right now. gopls (go) and clangd (C and C++) are much better than the alternatives you posted, and both allow developers to be forced to conform to a specific style. With go specifically, you should be using gofmt on save at the minimum. Also with C - why major open source projects do not have a .clang-format as part of their repo absolutely confounds me. Are people just this unorganized? Are all of the major players decades behind?
Void · 2021-03-10 20:34
I use C-x h C-M-\ in Emacs
Drifter · 2021-03-11 13:38
>>24810 >Language servers are the future Language servers are a waste of time. They were made to solve microsoft's problems with vscode, i.e. not having to interact with plugin maintainers. If you thought "how can I get jump to definition and autocomplete" (even these are dubiously useful, anything else is definitely a joke) and came up with a holistic solution it would not resemble language servers in any way. A protocol for fuarrrk's sake. >Also with C - why major open source projects do not have a .clang-format as part of their repo absolutely confounds me. Are people just this unorganized? Are all of the major players decades behind? Because you don't need it. Not only do lots of projects have different submodules with different maintainers who have different preferences, it's also a stupid idea if you ever get "changed formatting" in your `git blame` output, which would happen if you applied this retroactively no a project which had existed for years before clang-format. And for what benefit? As long as it's readable and the things that matter are consistent then it's fine. >>24807 >a style should be simple enough that it's easy to conform to without such tools Pretty much. The amount of options clang-format has is longer than writing a short document about style.
Spike Spiegel · 2021-03-11 16:59
>>24814 >>24807 The point of autoformatters is to offload formatting from the programmer. This does make it easier to focus on what's important, even if the style is simple.
J.C. Denton · 2021-03-11 17:04
>>24817 It also prevents pissing matches between engineers when you have that one guy who doesn't understand he's using tabs in 2/4 spaces per tab code.
Ein · 2021-03-11 17:24
>>24806 for go the formatting tool is go fmt + gofmt. What you linked to seems to be some random tooling for formatting within markdown.
St. IGNUcius · 2021-03-11 20:14
>>24817 It shouldn't be any effort in the first place. >>24818 >It also prevents pissing matches between engineers when you have that one guy who doesn't understand he's using tabs in 2/4 spaces per tab code. If you're in this situation then you lose automatically. I don't see stuff like this happen in open source projects. It sounds like when you're forced to work with someone who's a complete idiot in some company. That's a different environment and recommendations are different, so it's probably useful in that situation. Same goes for overly strict standards/conventions which are necessary because you have to make do with what (who) you have whereas free software devs will just tell you to piss off.
Zhora · 2021-03-11 21:03
>>24825 > That's a different environment and recommendations are different, so it's probably useful in that situation I agree. A large part of why go is good (and I guess why the open source community doesn't get go) is because it enforces things that would not normally be enforced in corporate environments. That coworker I have which doesn't seem to understand he's using tabs instead of spaces (or probably doesn't care) has no issues using gofmt.
Tank · 2021-03-13 10:39
>>24825 >It shouldn't be any effort in the first place. Easier said than done. I have yet to see a no-effort standard that results in consistent code. Especially if you work on different projects with different standards, this becomes difficult to manually do correctly. Why /not/ automate something the computer can do for you?
let · 2021-03-13 20:44
>>24861 I have, it's called go fmt.
katy · 2021-03-14 01:25
>>24861 There is 1 format specification for go and if you're not using go fmt or gopls you're doing something wrong. The most similar language in this regard is python, but most python users don't know what they're doing and don't know how to use pyls or autopep8 / think that language server warning can just be ignored/suppressed because they're hacks.
Splicer · 2021-03-14 17:53
>>24871 >>24878 Isn't go fmt just proof that autoformatters are useful? Also, I can't find the specification for go fmt. Can you link to it?
Bob Arctor · 2021-04-15 12:48
>>24806 The is default in Emacs, it supports many languages in many different styles. For example, when I open a C program I have Emacs automatically refactor everything into GNU style, but since people hate it so much I have it refactor back to K&R when I save. The refactoring is totally transparent to me.
FILLER · 2021-07-19 23:55
For Python: https://github.com/psf/black