Banderi / Ozymandias

An open source re-implementation of Pharaoh (1999) in the Julius/Augustus engine
GNU Affero General Public License v3.0
110 stars 10 forks source link

Auto formatter? #8

Open SandroWissmann opened 1 year ago

SandroWissmann commented 1 year ago

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.

crudelios commented 1 year ago

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 commented 1 year ago

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

Banderi commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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.:

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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?