

Best Practices of Variable & Method Naming  - javinpaul
http://codebuild.blogspot.com/2012/02/15-best-practices-of-variable-method.html

======
ericb
> Use short enough and long enough variable names in each scope of code.
> Generally length may be 1 char for loop counters, 1 word for condition/loop
> variables, 1-2 words for methods, 2-3 words for classes, 3-4 words for
> globals.

\---

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.

------
Detrus
Best practices are best automated. Syntax highlighting helps tell apart vars
and funcs so you don't need to offload it to naming conventions.

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.

------
mistercow
>Use specific names for variables, for example "value", "equals", "data", ...
are not valid names for any case.

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.

------
bostonpete
> Don't start variables with o_, obj_, m_ etc. A variable does not need tags
> which states it is a variable.

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.

~~~
RandallBrown
Tagging your variables isn't really needed anymore thanks to syntax
highlighting in modern text editors.

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.

~~~
to3m
Which modern text editors do this?

------
trustfundbaby
Something I started doing a while ago that was surprisingly helpful was naming
arguments to methods or functions like so ... params_durations or params_user
etc.

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.

------
RegEx
Most of the Ruby code I've read through almost never uses camelCase for
variables; it's almost always some_variable. I prefer to use lowercase +
underscores for method names and camelCase for variables. I find this
particularly helpful when dealing with argument-less methods that return
strings since () is not conventionally used.

~~~
draegtun
Yes Ruby follows the Perl tradition of camelCase for _Module::OrClassNames_
and instead using underscore in _variable_names_.

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...](http://www.nntp.perl.org/group/perl.perl6.language/2010/04/msg33511.html)

------
tikhonj
I think using Unicode is fine if your language guarantees support for it.
(E.g. it is in the language standard.)

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.

------
sedev
If you're feeling uncharitable, you can go look at the source code of PHP and
see how many times they run afoul of these guidelines.

(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)

------
koningrobot
_12\. Use meaningful names for methods. The name must specify the exact action
of the method and for most cases must start with a verb. (e.g.
createPasswordHash)_

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).

~~~
TravisPe
I've recently been jumping around in my naming conventions. I'm a student and
haven't quite settled in one one particular convention. For the current
project I'm working on (C++) I've been using function overloading. So for
example, I have a class called BankAccount. I have a private and a public
method that will set the balance or return the balance (set is private, get is
public) of the account, I've overloaded this so that if no parameter is passed
it returns the balance, and if a parameter is passed it will set the balance
accordingly. I use this in my deposit and withdrawal methods. I do the same
thing for other setters/getters. I picked up this convention from seeing it
used in other languages like Ruby and Javascript.

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.

------
rplnt
These are all great tips but still won't help me figuring out how should I
name my variable. Sometimes it's obvious but sometimes I struggle. I hope it
isn't an indicator of bad code :)

~~~
riffraff
obligatory:

    
    
        There are only two hard things in Computer Science: cache     
        invalidation and naming things.
        -- Phil Karlton

~~~
sp332
I like this version: "There are two hard things in CS: cache invalidation,
naming things, and off-by-one errors."

~~~
nazar
My students often miss 0 element of arrays.

------
edw519
16\. No variable name should be fully self-contained within any other variable
name:

Wrong: 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.

~~~
Raphael_Amiard
> This pretty much kills "1. 1 char loop counters", which is a good thing.

Why do you think this is a good thing ?

~~~
mattdeboard
"for node in nodes" is preferred, at least IMO, to "for i in nodes"

~~~
inerte
"i" as in a counter is almost a standard for any code in any programming
language. "i", "j" and "k" (comes from math). If you have the loop counter as
"node", then you're doing it wrong, but if you have an actual node, then it's
ok.

~~~
kstenerud
Or, if you want to be sure that a search/replace of a name won't go awry, you
could use names such as customerIdx, nodeIdx, accountIdx. It also helps keep
things clear in the few cases where you find yourself with nested iterations
(so that you don't accidentally call customers[j] when it should have been
customers[i]).

------
functionoid
use Camel Case (aka Upper Camel Case) for classes: VelocityResponseWriter

This is also called Pascal case.

------
ranit8
RunTimeException or RuntimeException? As a non-native English speaker, I hate
CamelCase.

