-
During the modification of avrdude I stumbled upon many differently formatted source code files - you can easily see the long history of this project. The most obvious thing for me was the indentation, 2 spaces vs. 4 spaces (sometimes even mixed in one file, e.g. int
function(int a, int b)
{
...
} vs. int function(int a, int b) {
...
} So the question I have is whether you avrdudes have a preferred coding style, e.g. for creating new source code files? I did not find any hint, also not in discussions. If nothing is carved in stone, what is accepted, where is the red line? int doSomethingSpecial( int sensorInput, int parameter ) { // keep temperature below ...
if ( sensorInput >= aGlobalLimit ) { // too hot ...
parameter = 2 * parameter + anotherConstant; // increase the cooling flow ...
...
}
} vs. int f(int a, int b)
{
if (a>=123) {
b=2*b+4321;
...
}
} OTAH a no-go for me are trailing spaces in code, my normal editor setting (as well as my In one of my bigger projects (Qt based), that was forked from an abandoned old project, I did an industrial cleaning once - no code changes, just reformatting according my coding style. This was one monster commit, but then everything was consistent. Next step was squashing the hundreds of warnings that QtCreator yelled at me. What is your opinion? Should avrdude also get a session at the beauty parlour? Maybe not for 7.3, but 8.0 could be a target? Martin |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 7 replies
-
Yes, there have been many different contributors, with many different styles in the past. Well, the first and foremost rule: if you are touching an existing file, try to align with the style you can already find there. This also means: as annoying as trailing white space is (I completely agree), do not be tempted to mangle with it unless you are touching the respective code area anyway. The reason for that rule is very simple: if you later review a diff across such a change, the white-space only changes could easily obfuscate the actual code changes. Spaces after opening and before closing parentheses are not commonly used in most coding guidelines, and I think AVRDUDE code has not been using it so far – so better avoid that. For whatever historical reasons, the rule of thumb is to have a space before the opening parenthesis in an operator context (for, while, if) but not to have one in a function call context. Binary operators should always have spaces, so "a + b", not "a+b". It improves readability, and in some situations (negative sign with numbers) might even disambiguate the grammar (consider something like "a+-5", vs. "a + -5"). Do not use "Hungarian" notation in any way. If you need a global variable, rather consider prefixing it with a kind of module name or such, like "stk500_is_initialized" rather than "gIsInitialized". I think most readers prefer underscore_style over CamelCase. But that's it: just my opinions. |
Beta Was this translation helpful? Give feedback.
-
Most contributors will have their own programming style. AVRDUDE is relatively liberal in that the project has a history of accepting longer stand-alone pieces in the "handwriting" of the author. I actually find it hard to write code in a different style than the one that flows naturally as writing in a different style would be too much distraction for me. I personally have two guiding principles: a) the complexity of the code should be commensurate with the complexity of the underlying problem b) tasks should be decomposed into smaller ones that fit a screen, so that they can be read without scrolling. Both principles help maintenance and readability. I realise that the size of a screen/window has changed dramatically over the last decades, but something like
violates both principles: 10 lines of code for what can simply be expressed as I personally am partial to compact writing and using tables in code (as this often helps reigning in complexity). |
Beta Was this translation helpful? Give feedback.
-
I actually find that hard to read. I'm really a fan of the ternary operator where it improves readability, but even after decades of C programming, I would have to think about that construct, rather than being able to "just read" it. Other than really simple cases, I would restrict the use of the ternary operator to situations like macros where all has to fit into a single line. (But even then, one might consider an inline function instead.) |
Beta Was this translation helpful? Give feedback.
Yes, there have been many different contributors, with many different styles in the past.
Well, the first and foremost rule: if you are touching an existing file, try to align with the style you can already find there. This also means: as annoying as trailing white space is (I completely agree), do not be tempted to mangle with it unless you are touching the respective code area anyway.
The reason for that rule is very simple: if you later review a diff across such a change, the white-space only changes could easily obfuscate the actual code changes.
Spaces after opening and before closing parentheses are not commonly used in most coding guidelines, and I think AVRDUDE code has not been u…