Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto formatter? #8

Open
SandroWissmann opened this issue Sep 17, 2022 · 10 comments
Open

Auto formatter? #8

SandroWissmann opened this issue Sep 17, 2022 · 10 comments
Labels
question Further information is requested

Comments

@SandroWissmann
Copy link

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 SandroWissmann added the question Further information is requested label Sep 17, 2022
@crudelios
Copy link

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.

@SandroWissmann
Copy link
Author

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

@Banderi
Copy link
Owner

Banderi commented Sep 20, 2022

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.

@SandroWissmann
Copy link
Author

SandroWissmann commented Sep 20, 2022

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
Copy link
Author

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.

@Banderi
Copy link
Owner

Banderi commented Sep 22, 2022

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)

@SandroWissmann
Copy link
Author

SandroWissmann commented Sep 22, 2022

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

@Banderi
Copy link
Owner

Banderi commented Sep 22, 2022

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.

@SandroWissmann
Copy link
Author

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?

@Banderi
Copy link
Owner

Banderi commented Sep 22, 2022

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants