Best Practices of Variable & Method Naming
codebuild.blogspot.com
codebuild.blogspot.com
---
A variable name, function, or class name, should be exactly as short as it can be and still usefully to convey its purpose to someone who is not the code's author--no longer, no shorter. Their "best practice" was backed into by counting how many words it usually takes to convey this information, but the horse goes in front of the cart.
Some new approach of tags, where every variable, class name, function name can have multiple tags describing it as Factory, Adapter, Handler, whatever, can also remove this often unnecessary information from the shorthand name. Only display it in some deep editing mode or something.
Capitalization can also be handled by tags, each word in CapitalizedCamelCase or underscored_lower_case is just a tag. The IDE should handle this, so you never have to read about inconsistent naming conventions in various languages.
Each item like a var or function gets a unique ID, so you don't have to do search replace by name and find a bunch of other things with similar names in the process.
A lot of these problems stem from still using plain text as the medium.
I have to disagree. As parameters to short functions/methods, names like "value" and "data" can be perfectly appropriate, especially if those names correspond to types.
I disagree on this one -- it's helpful to be able to quickly recognize whether a given variable in a method is a local variable or a member variable of the class.
I'm pretty on the fence about it actually. It's not really necessary because of syntax highlighting, but it is convenient to just type m_ and get a list of all the member variables from intellisense.
a naming convention from the world of C++ is the use of "m_" in front of members. This is supposed to help you tell them apart from methods, so long as you forget that "method" also starts with the letter "m".
This helps when I'm scanning code, to quickly to know whether a variable was passed in vs being local, global or part of a class.
In Rails/Ruby programming I will also usually just write self.this_method where most people would just write this_method, because I instantly know where to look for that particular method (since a method definition could be in so many different places in rails).
Works really well for me but YMMV.
Interesting Perl6 introduces option of hypenated $variable-names ala Lisp. Damian Conway gave this great comment on underscores vs hyphens on the Perl6 mailing list - http://www.nntp.perl.org/group/perl.perl6.language/2010/04/m...
Ultimately, something like τ -> τ' is much more readable than tau -> tau' and is about as easy to type in a decent text editor (e.g. \tau -> \tau'). This is especially true for heavily mathematical code.
(not like the items in the article are Holy Writ, but they're sensible guidelines, and it's kind of sad that a widely-used programming language can screw up on nearly all of them)
This depends. My classes often have side-effect-free methods that just return a value based on the state of the object. They are just properties the storage of which would be redundant. I never put "get" in front of the names of these methods. For instance, in a math Vector class, I don't have getNorm(), getUnit(), etc., I just have norm() and unit().
I struggle with what to name methods that compute, for instance, a dot product of two vectors. I don't like a.getDotProduct(b), nor a.dot(b) for that matter. The way the implicit parameter ("this") is special-cased in the notation makes everything so ugly. Nowadays I usually just have a class V with static methods that act on double[]s (so I end up with V.dot(a, b), which still sucks but at least isn't so confusing).
Is this a bad practice? It seems logical to me, but I'm wonder what other peoples thoughts are and if it may be confusing to someone else.
There are only two hard things in Computer Science: cache
invalidation and naming things.
-- Phil KarltonWrong: Cust & CustCtr
Right: CustID & CustCtr
You should be able to do a global search with any tool and get every instance of that variable with no instance of any other variable.
This pretty much kills "1. 1 char loop counters", which is a good thing.
Why do you think this is a good thing ?
* NB: I am not certain whether I agree with such rules or not, but that is another discussion point.
i ii iii j ij jj ji jjj jij jjj
One letter loop counters and their ilk are always a bad idea. But saying so angers so many people because they can't possibly ever be wrong.
I would offer that you should have tools that are smarter about your code, with a contextual awareness of what things are and how they are used. Many coding purported "best-practices" are based upon such a tool deficiency.
Let me give one example that is a serious peeve of mine -- C# allows you to define region. I have to interact with code that uses regions to define accessibility or type of members.
#region private members
#endregion
This was communicated as a "best practice" because it allows you to hide/show appropriate parts of the code.
Yet the primary tool -- Visual Studio -- has fantastic tools for exactly that use. Tools that don't muddy up the code with unvalidated, quickly out of date metadata (e.g. nothing stops me from putting a public in there).
This holds true for many, many coding conventions. We discarded hungarian notation for the same reason that many of these other naming guidelines are of dubious merit.
This is also called Pascal case.