Ozymandias icon indicating copy to clipboard operation
Ozymandias copied to clipboard

Auto formatter?

Open SandroWissmann opened this issue 2 years ago • 10 comments

I was wondering if there is some kind of auto formater for the code in the project?

If not I suggest we could add something like Clang Format file which formats you the code on save etc.

SandroWissmann avatar Sep 17 '22 10:09 SandroWissmann

On Julius/augustus we use editorconfig files.

They're compatible with at least VS2022 and VSCode by using extensions for both, I don't know about other IDEs though.

crudelios avatar Sep 17 '22 11:09 crudelios

Good to know. I'm actually using VSCode. Lets see what Banderi says.

SandroWissmann avatar Sep 17 '22 13:09 SandroWissmann

I do have Clang enabled on CLion, it helps a lot! Though I don't think I have any linter action enabled (yet), if it exists within Clang's capability.

For the time being I've just been formatting things mostly manually.

Banderi avatar Sep 20 '22 12:09 Banderi

yes there is also Clang-Format which can use Clang-Format file from the root folder to automatically format based on certain rules.

I can also add some file which adds some sane formatting. But of course It can be modified how the auto format should be.

For Clion maybe this helps: https://www.jetbrains.com/help/clion/clangformat-as-alternative-formatter.html#review-settings

SandroWissmann avatar Sep 20 '22 13:09 SandroWissmann

Coming back to this I really thing we should use some auto formatter it will save some time. I can try out some things the next day and maybe provide this clang format file with instructions how it works.

SandroWissmann avatar Sep 21 '22 18:09 SandroWissmann

For the basics I think yes, my IDE automatically formats things as I set it up (either via Clang or something else internally) e.g.:

  • function spacing: int foo(int a, int b) {...}
  • pointers spacing: buffer *buf;
  • for loops: it formats to for (int i = 0; i < asd; ++i) although when I'm manually typing I usually do i++ which should be fixed to be consistent if we want to be technical, but oh well ;)
  • functions and loops: I've been putting the braces Java style, with single-line blocks having no braces
  • etc. etc.

When it comes to more macroscopic things though I do manually; with #includes I've largely let the IDE plop them automatically but when doing things properly I do local header on top, then system, then others; I also leave no line breaks between functions that are related to help me visualize the structure, and haven't done any out-of-function multi line descriptors, just have the function named and placed in such a way that is intuitive enough (though for some functions it might be useful to have) (etc. etc. more stuff that one day I'll write down)

Banderi avatar Sep 22 '22 15:09 Banderi

Maybe in one of the next commits I can try it with a clang format file. It does not force everything (like not force on line limit) but it helps keep the code consistent.

Regarding ++i, i++ I follow K&R here and always use ++i. In theory that is more efficient because i++ creates an extra copy. But the compiler will optimize that out anyway. So not really a big deal. For more on the topic see: https://stackoverflow.com/questions/24853/what-is-the-difference-between-i-and-i It would be if you have a big complex custom object which somehow has ++i and i++ implemented.

Regarding omitting bracets on one liner ifs. I know you can omit these brakets in that case but it can lead to bugs later.

e.g.

if(a)
    b;

Later you also want to add c:

if(a)
    b;
    c;

With intention one could think b and c gets executed inside the if but it does not. So I always do

if (a) {
   b;
}

See also https://stackoverflow.com/questions/2125066/is-it-a-bad-practice-to-use-an-if-statement-without-curly-braces

SandroWissmann avatar Sep 22 '22 16:09 SandroWissmann

Yes, I realize it's unrecommended if you want to be 100% safe for posterity, but after considering everything I still decided I prefer without extra braces; it looks cleaner to me, it's less fluff and less symbols all around. The more immediate bug is almost always obvious and caught by IDE unless you're coding on notepad, and it takes just a couple seconds to fix. The original C codebase have the full brace style (which is good practice in theory!) but I still converted it largely whenever I could to no-brace single line blocks, and always do so for new lines of code.

Banderi avatar Sep 22 '22 16:09 Banderi

For me it is just a "brainfuck" because all the projects i worked on in c/c++ forbid it and also everything else needs brackets so its just natural to add them for me.

So the question is now is it optional to have them or do they need to be removed?

SandroWissmann avatar Sep 22 '22 17:09 SandroWissmann

Ahh, darn. I totally get ya.... for me it's the same, but exactly the opposite haha. I get my eyes crossed if I see them and always remove them unfortunately.

Is there a way maybe to have a linter that only shows them or hides them on one's own end/IDE?

Banderi avatar Sep 22 '22 17:09 Banderi