-
Notifications
You must be signed in to change notification settings - Fork 15
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
Inconsistent / Poor code style #13
Comments
Hi! I agree that this is a important issue. I agree with the code style you proposed on most parts.
Personally, I agree with this answer on SO. I think it's easier to read pointer variables by declaring each one in a separate line. What do you think?
I prefer Thanks for opening this issue, it's a really needed one :) |
I think The most important thing should be consistency. That said,
I agree with this. I think one declaration per line should be used since it makes the code more readable. As for the asterisk placement I don't have a preference about that. I normally use |
Good point. I agree with the pro-consistency argument (original code received contributions from varying sources, with no agreed upon conventions). If I were to opine, and being that a standard C program, I would go for the tradition (lowercase identifiers etc.). Most important, we should choose if the program will be strict ansi (c90), or else c99 compatible (this will affect portability). It is already POSIX, though (what about cross compiling to Win32, eventuall?) I am perhaps among the few old-school nerds who likes GNU coding style [1], and that's why the example comes with all-caps macro constants, lowercase function-like macros, other-line curly braces, Emacs-like indentation, some weird spacing between parenthesis and so on. I know it's a quasi-religious field but I'll do my best to hear the community (might I have at least the next-line block opening?) It has also some suggestion for source comments. That said, I like |
Next-link block opening is the Allman style? If so, I completely agree! Furthermore, I would suggest to always use braces in compound statements, even for one-line statements. That is, I would suggest that we use
instead of
IMO it's easy to make a mistake in the second version, if it happens that we need to add something to the if-block afterwards. |
Great issue! I agree that consistency is important, but personally I'd prefer:
I'm 200% for that, although I think |
Maybe we should pin this issue? |
I don't have a really strong personal preference about the subject so I think I won't give suggestion about the choice we should make. But maybe we should create a document with the standard we set?
I think so, since we cannot code if we don't set how we should code. I think we also need to make this issue |
I disagree, I'll side with @monacofj on this one :P I think it's worse for read/understand the code.
I agree about asterisk placement (
I disagree about keeping the block on the same line as the condition, even for one-liners; it's harder to maintain IMO. I would suggest to use
or at least (if the K&R Style bracing style is adopted):
|
I agree with @Float07. This is a topic I have a strong opinion on, I feel like next line opening curly brackets are confusing and I would much rather use same liners.
Neither here nor there on this one, I´ll go with what makes more sense to everyone in regards to how many variables per line. I usually use type* var, but would not mind to switch for the project.
I agree with @math-araujo on this one, not a fan of one line conditions, I would stick with: Finally, when it comes to indentation, I believe one tab would be the best way to do it. It´s easier to do and I like the space it provides. |
We should also set a max width for the lines. I like 80 characters |
Oh boy, this will probably be the longer thread in the whole project.... and likely never resolved, rs. |
Yeah, it's probably worth it to make a poll to decide on each item? Don't really think we're gonna be able to convince each other on the subject hahaha |
I'm down for a poll. Can we run one in here? |
We can with this app. I can add it to the repo and create one if everybody is ok with that. What are all the options we have to vote? |
So, I think our options are as follow. Am I forgetting something? If y'all agree I can create a poll. Variables/functions formatting
Structs formatting
Indentation
Blocks
If-like statements
Pointer asterisks
Declare multiple variable in same line
One-liners
if (condition) {
variable = value;
} Line's max-width
|
After deciding which style to follow, we can use some auto-formatter tool like Clang-Formatter with a pre-commit hook to ensure that everyone will follow with no effort needed |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
There we go. I don't think I can close the polls, so we should set a deadline for voting. What do you all think? Unfortunately, if you're using a dark theme for GitHub you may have to disable it to better read the poll's options (they're incompatible and I have no control over it). You can do that in settings > appearance. Also, this comment has some examples for the options. Variables/functions formattingStructs formattingIndentationBlocksIf-like statementsPointer asterisksDeclare multiple variable in same lineOne-linersLine's max-width |
Refactor existing code to follow voted code style
Fixing read_scenes() typo *scene_array_ptr
TL;DR
Code style is currently inconsistent, and some choices are questionable (e.g. functions names).
I think we should stablish a fixed code style to avoid messy code in the future.
Feel free to give suggestions in the comments.
Detailed explanation
I know code style is something that really depends on the team's / person's preferences, but there are a lot of lines that most people would agree are not part of any code style.
Here are some lines of code (of many), just to exemplify what I'm trying to say:
Mix of Spaces and Tabs (not visible on github code preview)
Inconsistent use of curly brackets
Right after function declaration:
snaskii21/src/ttsnake.c
Lines 292 to 294 in 33eb585
On the next line:
snaskii21/src/ttsnake.c
Lines 307 to 311 in 33eb585
Inconsistent positioning of the * character on pointers
snaskii21/src/ttsnake.c
Lines 335 to 337 in 33eb585
snaskii21/src/ttsnake.c
Lines 359 to 361 in 33eb585
snaskii21/src/ttsnake.c
Line 375 in 33eb585
snaskii21/src/ttsnake.c
Line 228 in 33eb585
snaskii21/src/ttsnake.c
Line 123 in 33eb585
Proposed standard coding style
Based on the majority of lines already written, I've observed the following pattern:
snake_case
thislonglongfunctionwouldbesomethinglikethat();
ALL_CAPS_WITH_UNDERSCORE
snake_case
like variables, but its typedef issnake_case_t
(_t
at the end)Here is my opinion on those topics:
As for functions and macros being
everythnigtogheterprettyunreadable
, I really think it's a bad idea to follow this code style, although I see where this came from (ncurses functions use this style). I also think mixing pointer asterisk placement can be confusing for some people, so I would suggest sticking to only one in all scenarios.All the other stuff I've listed before are pretty standard, but are not consistent throughout the code.
For the pointer placement, my personal preference is to use
type *pointer1, *pointer2;
for declarations of pointers,void *func(type *pointer);
For function names, maybe we should use
snake_case
, just likepthread_create
.snaskii21/src/ttsnake.c
Line 459 in 33eb585
Variables can still be
snake_case
as well, but I like to change them tocammelCase
in my codes to differentiate them (just personal preference).I think this needs discussion, and I want to emphasize I am not imposing any code style and I'm opening this issue so that we can discuss a code style to stick to throughout the development of this project.
The text was updated successfully, but these errors were encountered: