>Encoding the type of a function into the name (so-called Hungarian notation) is brain damaged - the compiler knows the types anyway and can check those, and it only confuses the programmer. No wonder MicroSoft makes buggy programs. "Making Wrong Code Look Wrong" by Joel Spolsky is a must-read and contains an explanation of Apps Hungarian (the original, thoughtful one) vs Systems Hungarian http://www.joelonsoftware.c…
C is not a strongly typed language and it does not allow function overloading. C projects should allow for some flexibility in naming notations to make up for those language design decisions. Also, any project that uses int return codes shouldn't be leaning too heavily on type safety.
Linux kernel coding style
61–70 of 102 posts
Re: Linux kernel coding style
#62Mostly good advice, sometimes even great, but the part about typedefs is total BS. Any non-trivial program will use values that have clearly different meanings but end up being the same C integer type. One's an index, one's a length, one's a repeat count, one's an enumerated value ("enum" was added to the language to support this very usage), and so on. It's stupid that C compilers don't distinguish between any two t…
I agree with you. I think structs help readability specially we using function pointers within structures. Would it be out of line to suggest a new naming convention for struct typedefs and pointer typedefs i.e _t for typedegs and _tp for typedeg pointers
I'm not going to argue against pointer typedefs, though I personally don't use them. I'm just saying that I can't make a strong argument for them as I believe I can for other cases.
Re: Linux kernel coding style
#63I am glad that for Go there is `go fmt` which predefines some of the issues mentioned in the article. Thus there is "one global coding style for Go". It's another matter if one likes it or not.
I don't see why there couldn't be a `kernel fmt` tool. In this day and age, we should really be beyond having to worry about things like hmm, what was the brace style in this project again, and should all if/while/for have mandatory braces? .
[0] https://github.com/torvalds/linux/blob/master/scripts/checkp...
Re: Linux kernel coding style
#64Earlier quoted context omitted.
I find it hard to agree with a lot of this, but it'd obviously be someone who's written a lot of code, thought a lot about how to write code, and reads a lot of code - even if we didn't know who it was. There's a lot to learn from reading stuff like this, if you take it all with a grain of salt... or if you're contributing.
I think it was written by Torvalds and other kernel hackers. It is part of the Linux source code, under the Documentation directory.
Re: Linux kernel coding style
#65> Do not unnecessarily use braces where a single statement will do. if (condition) action(); > and if (condition) do_this(); else do_that(); The Apple SSL bug ( https://nakedsecurity.sophos.com/2014/02/24/anatomy-of-a-got... ) makes me wonder if this is really worth the potential for introducing bugs.
IMO, the bug was a much deeper issue then simply not putting braces on if statements. It doesn't matter if the code becomes this: if (condition) { goto fail; } goto fail; if nobody looks at the commit. Don't get me wrong though, braces on if's do help for making cleaner patches, so there is a valid reason to request braces. You should never rely on them to fix these types of bugs though, that's bound to come back and…
To me, though, requiring braces would make it much easier to spot any such problem at any point in the development process (writing, debugging, reviewing, maintenance) such that the extra line per conditional would be well worth it in all cases, not to mention making edits easier.
Re: Linux kernel coding style
#66"Get a decent editor and don't leave whitespace at the end of lines." Trailing whitespace always raises a huge red flag for me whenever I look at someone's code. It's not just sloppy, it often makes diff output so noisy you can't detect real changes to the code.
Re: Linux kernel coding style
#67"Get a decent editor and don't leave whitespace at the end of lines." Trailing whitespace always raises a huge red flag for me whenever I look at someone's code. It's not just sloppy, it often makes diff output so noisy you can't detect real changes to the code.
Who cares? Get a decent editor that doesn't give a crap if there's invisible whitespace at the end of a line.
Re: Linux kernel coding style
#68"Get a decent editor and don't leave whitespace at the end of lines." Trailing whitespace always raises a huge red flag for me whenever I look at someone's code. It's not just sloppy, it often makes diff output so noisy you can't detect real changes to the code.
Who cares? Get a decent editor that doesn't give a crap if there's invisible whitespace at the end of a line.
Re: Linux kernel coding style
#69"Get a decent editor and don't leave whitespace at the end of lines." Trailing whitespace always raises a huge red flag for me whenever I look at someone's code. It's not just sloppy, it often makes diff output so noisy you can't detect real changes to the code.
Who cares? Get a decent editor that doesn't give a crap if there's invisible whitespace at the end of a line.
Re: Linux kernel coding style
#70Earlier quoted context omitted.
Most of that section is concerned with hiding structs or pointers as typedefs: "In general, a pointer, or a struct that has elements that can reasonably be directly accessed should _never_ be a typedef." Say you are reading a function, and see a local variable declared: "something_t variable_name;". Is it a struct, a pointer, or a basic type? Now compare with "struct something * variable_name;", which is clearly a po…
> it's item (c): "when you use sparse to literally create a > _new_ type for type-checking." The problem is that this is presented as an exception that must be (strongly) justified. I think that using typedefs for integer types should be acceptable by default , and there should be specific rules for when to avoid them. The burden of proof is being put on the wrong side. Even for structs, the argument for typedefs is…
The parent provided a very good example: a structure takes up a lot more space than a single int/pointer type, and passing them by value is usually an unnecessary copy.
and need to be changed if the API ever changes.
If the API changes then changing the declarations is likely to be trivial in comparison to the other changes that would need to be made to all the code using it.
Exposing too much reduces modularity and future flexibility.
...and exposing too little reduces understanding of the details, which I think is far more important especially for a kernel.