-
-
Notifications
You must be signed in to change notification settings - Fork 21.1k
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
Let's talk about the inconsistencies in the code-style #16521
Comments
This could make the code more human readable but I think that it is more important to optimize the code in matters of performance (e. g. link-time optimization) rather than reaching a more aethetical look. |
@LinuxUserGD I don't see how the suggestions above would harm performance, they are purely aesthetic as far as I know. Besides, I don't think anyone should go straight optimization > readability, but instead, a fine balance between the two. |
Honestly this is not going to happen. Let just people use the code style they want provided that they respect the given clang formatting. We're a community driven project that involves a lot of different people with different tastes regarding code style. Let's not force people to adopt one as it does not really impact the tm project maintainability. |
Following that logic, we should just drop clang-format and let people write however they like as long as it's legible and compiles. |
Well, following yours, we should start including a code quality static checks. But we're not making a plane onboard controller, we're making a game engine. IMO, the workflow is good as is. We ensure that merging is not getting messy with too many aesthetic changes while still letting people make their own decision on the code they are working on... It's a question of balance between code quality and being welcoming to developpers that have different tastes regarding code style. Also, to illustrate my point, I generally don't agree with your proposals. They are some places where brackets are just too verbose, one-line functions are approriate (in header files sometimes). Also, regarding the comments, I actually don't care. So I would not force people to adopt a rule on that. It's already hard to make good code I'm not going to bother people so that they put a capital and a point in their comments. |
Both are quite complex and need to be readable, maintainable, because they need to function long-term.
Agreed. But:
As a potential new contributor, "we have no style" sounds more offputting to me than "we enforce a style that you don't like". I can live with doing formatting according to your whims, if it is consistent, and actually would prefer that over a wild mix of styles all over the place. And I disagree strongly on the braces. I already saw bugs that wouldn't have been possible with braces in conditionals, and I already debugged one I caused myself because I modified existing code and missed the missing braces. Comments and stuff are nitpicking, yes, but for braces, always having them trades a little more verbosity in some cases against eliminating a whole class of bugs. Far worth it, IMO. |
I think we can enforce a more strict style, we already do to some extent (more than the clang-format, PR-reviewers usually point other things too). Regarding single line // Simple enough to be a single line.
if (!is_inside_tree()) return;
// Not on the same line, use braces.
if (something && something_else || something_other) {
do_something_more();
} |
Why not if we can automate the formating / testing for that. |
Ideally, you don't have to ask them because the CI already fails if code isn't formatted correctly. |
I'd like to throw one under the comments header. The engine code needs quite a bit more commenting in the source code, so new people (we're aiming to allow anybody to contribute, right?) can look at a section of code and go "That's what that's doing". Right now though, I've noticed (and maybe I'm not looking enough, but) a big lack of comments within the code describing what everything's doing, which forces people reading the code to have to look at it and make a guess as to what's happening. Now I won't say from now on everything has to be commented, but I'd like there (at least) to come times where we go through and see what needs some in-depth comments. And no, I don't believe self-documenting function names are enough in this case. Just to clarify, this is in regards to the engine's source code, not the documentation for GDscript over at godot-docs. I'd like to see at least simple one-liners describing certain sections of code. |
A switch is similar to making use of gotos. If you are writing code like
you are basically telling the compiler to jump to the line "case 2" if x is 2. This works fine with imperative code, but it is a problem with declarative code like this:
That's why some people (including me) like to write switches like this:
because now you are allowed to declare temporary variables inside a case since every case has its own scope. In theory it would be fine to only use braces when you do use temporary variables, but most people think of braces as a formatting element rather than a scope definition. I mean it's very rare that braces are added just to limit a scope, usually they directly follow a "header line" like a function signature, an if, a while etc. |
I agree we should drop new lines for blocks inside functions. However, I think we should allow single line statements to be bracket-less. If one of the blocks in an
IMO we should follow K&R's style of placing the opening brace of functions in a new line. If we are going to add a new line any way, better do it in a way that improves readability. Example: void Foo::bar()
{
if (cond) {
a();
} else {
X res = x();
b(res);
}
if (another_cond)
c();
} |
Ifs: Agree, braces are nice. Functions: I have to agree with @neikeq, if we're going to take up that vertical space anyway, we should use it! I haven't raised an issue about this before because I figured it'd be a lot to change. Comments: Capitalization is good. Additionally, they should always start with spaces. Comments would probably be easiest to fix without upsetting any contributors who prefer their code looks a certain way. I think everyone would prefer either |
There's also a small inconsistency when it comes to property hints. Some of them use spaces after commas, but most don't: godot/core/io/file_access_network.cpp Line 515 in 5441aaf
Line 783 in 5441aaf
We should probably pick one style or the other. I'm not sure which style would be best; they're more readable when separated with spaces, but this makes them look like an actual string that would be displayed to the user. On the other hand, not using spaces makes it clearer that the property hint string is only used for internal purposes. This inconsistency is also present in FileDialog filters (notice the space before the semicolon): godot/editor/editor_feature_profile.cpp Line 897 in 5441aaf
godot/editor/project_manager.cpp Line 376 in 5441aaf
|
About property hints: I would suggest spamming warnings when the "wrong" style is used, so that contributors have a reason to switch to the "right" style. |
I tend to prefer the compact form without spaces, which makes it clear that the string is a property hint, and not several extra arguments to the |
In addition to C++ style, we should also adopt some rules for texts that appear in the editor. Currently:
In addition to the current doc writing guidelines, we could also take inspiration from the Fluent writing style guidelines. |
I am wraping my head around and trying to understand - can someone explain what are the origins of offsetting function's/method's body by one line? I can't find similar code style in any other open source software and I am mostly confused while skimming the code (but I am gradually accustomed to it). |
@dreamsComeTrue I think it's for readability. If that's the case, it doesn't make much sense; better place the brace in a new line, right? |
Closing as superseded by #33027. If some items mentioned here are not reflected in the other issue, please add a comment there. |
Ok, I will try to tackle the issue without sounding like a nitpicky asshat (no promises):
The code-style in Godot's code base is kinda all over the place. Yes, we do have clang-format that makes sure some rules are obeyed, like starting brackets be in the same line as the
if
statement for example, but that are a lot of other things that don't have a proper rule on how they should be formatted, which results in very different code-styles in different files (sometimes even in the same file!).Here a list of the most occurred inconsistencies in the code:
If's:
Functions:
Switches:
Comments (Yeah, yeah, getting into really deep nitpick territory, but stick with me):
And that's mostly it, but there are probably some others thing that I either forgot or didn't notice.
I'm not saying that anyone should go hunting for these, but trying to maintain some consistency while writing/editing new code could be a good idea.
The text was updated successfully, but these errors were encountered: