ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Coding style is very personal, and I won't _force_ my
views on anybody, but this is what goes for anything that I have to be
able to maintain, and I'd prefer it for most other things too. Please
at least consider the points made here.
...
C is a Spartan language, and so should your naming be. Unlike Modula-2
and Pascal programmers, C programmers do not use cute names like
ThisVariableIsATemporaryCounter. A C programmer would call that
variable "tmp", which is much easier to write, and not the least more
difficult to understand.
HOWEVER, while mixed-case names are frowned upon, descriptive names for
global variables are a must. To call a global function "foo" is a
shooting offense.
...
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.
LOCAL variable names should be short, and to the point. If you have
some random integer loop counter, it should probably be called "i".
Calling it "loop_counter" is non-productive, if there is no chance of it
being mis-understood. Similarly, "tmp" can be just about any type of
variable that is used to hold a temporary value.
...
If you have some random integer loop counter, it should probably be called "i".
No. No no no. No.
If you call something "i", and you use your text editor to find all occurrences of that variable, you'll also stop at every variable whose name inclues "i", and every "if", and who knows how many places in your comments.
I like to follow the rule that no variable's name should be found as part of any other name. It makes searching for all occurrences of a variable far, far easier.
Nonsense! "i" is a *perfect* name for a temporary index ... for a loop ... that's only going to span a couple of lines anyway.
If your loop is out-of-control to the point you *need* a text editor to find the index ... then you've got bigger problems than naming conventions for variables.
I prefer ThisIsAStruct, THIS_IS_A_CONSTANT, this_is_a_variable, and ThisIs_aPseudoMemberFunctionOf(ThisIs*).
At work, CThisIsAClass, ThisIsAFunction, THIS_IS_A_STRUCT_s, THIS_IS_AN_ENUM_e, conjAnd pnThese vAre nVariables, because our code used to be Windows-only and that's what MFC does.
If you call something "i", and you use your text editor to find all occurrences of that variable, you'll also stop at every variable whose name inclues "i", and every "if", and who knows how many places in your comments.
I like to follow the rule that no variable's name should be found as part of any other name. It makes searching for all occurrences of a variable far, far easier.
I tend to follow the rule "When in Rome, do as the Romans do." If I'm working in a project started by someone else or as part of a team, I'll follow their naming conventions (and brace placement, and tabs vs. spaces, etc.). But on this I have to chime in.
This is a very weak excuse at best for not using "i" as a loop variable name. If you're searching occurrences of variable names, typically you're searching for global variables (or member variables, in C++) which should have longer, more descriptive names anyway. If you want to find the loops themselves, look for "for" or "while". If you want to find every occurrence of "i" within that loop code, you should be able to eyeball it. If you can't eyeball it, there is too much code in the loop and it needs to be broken up or re-written.
I tend to follow the rule "When in Rome, do as the Romans do." If I'm working in a project started by someone else or as part of a team, I'll follow their naming conventions (and brace placement, and tabs vs. spaces, etc.).
I agree completely!
Let's face it: the whole POINT of "coding conventions" is to instill "consistency". A code base that's "consistent" is easier to read; a code base that's easier to read helps the team and helps the project. An all around good thing.
If I've got a "better idea" and start violating those conventions (position my braces differently, name my variables differently - whatever), then I'm injecting INCONSISTENCIES. Regardless of how one feels about names or braces ... doing it DIFFERENTLY ... is worse than no coding conventions at all.
Let's face it: the whole POINT of "coding conventions" is to instill "consistency". A code base that's "consistent" is easier to read; a code base that's easier to read helps the team and helps the project. An all around good thing.
If I've got a "better idea" and start violating those conventions (position my braces differently, name my variables differently - whatever), then I'm injecting INCONSISTENCIES. Regardless of how one feels about names or braces ... doing it DIFFERENTLY ... is worse than no coding conventions at all.
In general, this is a good idea, and I guess in principle I'll have to agree with it. (You'll note that my dissent in this thread had to do with an entirely different issue.)
I would just point out that there have been times where I've looked at some modification to some C code and asked myself, "What's going on here?" And then I looked at the braces, the commenting style, and so forth, and thought, "Wait, that's Bryan's handwriting, I'll go ask him."
This might make you gag, and it makes me gag, too, but it's helped from time to time.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.