views:

177

answers:

8

I seem to use bland words such as node, property, children (etc) too often, and I fear that someone else would have difficulty understanding my code simply because the parts' names are vague, common words.

How do you find creative names for classes and components to make them more memorable?

I am particularly having trouble with generic tools which have no real description except their rather generic functional purpose. I would like to know if others have found creative ways to name things rather than simply naming them by their utility, such as AnonymousFunctionWrapperCallerExecutorFactory.

+4  A: 

It's hard to answer. I find them just because they seem to 'fit'.

What I do know, however, is that I find it basically impossible to move on writing code unless something is named correctly, and it 'feels' good. If it isn't named right, I find it hard to use, and the code is generally confusing.

I'm not too concerned about something being 'memorable', only 'accurate'.

I have been known to sit around thinking out loud about what to name something. Take your time, and make sure you are really happy with the name. don't be afraid of using common/simple words.

Noon Silk
I agree and would go further to say that bland names are actually a good thing as they are likely to quickly convery their purpose. Creative names should be kept for characters in crime novels. Simple, succinct and meaningful names are for code.
sipwiz
+3  A: 

Using 'metaphors' is a common theme in agile (and pattern) literature.

'Children' (in your question) is an example of a metaphor that is extensively used and for good reasons.

So, I'd encourage the use of metaphors, provided they are applicable and not a stretch of the imagination.

Metaphors are everywhere in computing. From files to bugs to pointers to streams... you can't avoid them.

Leon Bambrick
+1  A: 

Names that reflect the purpose of the class, method or property are more memorable than creative ones. Modern IDEs make it easier to use longer names so feel fee to be descriptive. Getting creative won't help as much as getting accurate.

Mike Two
+1  A: 

I recommend to pick nouns from a specific application domain. E.g. if you are putting cars in a tree, call the node class Car - the fact that it is also a node should be apparent from the API. Also, don't try to be too generic in your implementation - don't put all attributes of the car into a hashtable named properties, but create separate attributes for make, color, etc.

Martin v. Löwis
http://irrationalayush.files.wordpress.com/2009/06/car_in_tree.jpg
cwap
If only I had something as descriptive cars to work with. I am building a general-purpose tree data-structure which could be used for anything. :-S
too much php
If the tree structure is really general purpose, why are you writing it - it surely has been written many times before. If there is a reason to write it, try to think what the reason is, and put that in the name.
Martin v. Löwis
+2  A: 

I believe that for the purpose of standardization and communication, it's good to use a common vocab, like in the same case for design patterns. I have a problem with a programmer who keeps 'inventing' his own terms and I have trouble understanding him. (He kept using the term 'events orchestrating' instead of 'scripting' or 'FCFS process'. Kudos for creativity though!)

Those common vocab describe stuff we are used to. A node is a point, somewhere in a graph, in a tree, or what-not. One way is to be specific to the domain. If we are doing a mapping problem, instead of 'node', we can use 'location'. That helps in a sense, at least for me. So I find there is a need to balance being able to communicate with other programmers, and at the same time keeping the descriptor specific enough to help me remember what it does.

Extrakun
A: 

A lot of languages and coding styles like to use all sorts of descriptive prefixes. In PHP there are no clear types, so this may help greatly. Instead of doing

$isAvailable = true;

try

$bool_isAvailable = true;

It is admittedly a pain, but usually well worth the time.

I also like to use long names to describe things. It may seem strange, but is usually easier to remember, especially when I go back to refactor my code

$leftNode->properties < $leftTreeNode->arrayOfNodeProperties;

And if all else fails. Why not fall back on a solid star wars themed program.

$luke->lightsaber($darth[$ewoks]);

And lastly, in college I named my classes after my professor, and then my class methods all the things I wanted to do to that jerk.

$Kube->canEat($myShorts, $withKetchup);
jW
Horrible advices.. But fun nevertheless :) I once created a tiny app using only animals names for variables. 2 hours into writing I was so lost. It's a good exercise to understand good naming though..
cwap
I am looking for that kind of creativity, but in a way that is still meaningful.
too much php
unfortunately with any amount of creativity, you are moving away from being descriptive about what the program is doing and what the variables mean. I think that is the inherent problem with making creative variable names. As Meeh stated, and as the post is meant to joke about is that as the names become less obvious and descriptive, they become less usable.
jW
Perhaps we should use foreign language equivalents. It might help us realise the additional effort non-english-speakers have to use when programming in a language with English keywords.
pavium
+2  A: 

I think node, children, and property are great names. I can already guess the following about your classes, just by their "bland" names:

  • Node - this class is part of a graph of objects
  • children - this variable holds a list of nodes belonging to the containing node.

I don't think "node" is either vague or common, and if you're coding a generic data structure, it's probably ok to have generic names! (With that being said, if you are coding up a tree, you could use something like TreeNode to emphasize that the node is part of a tree.) One way you can make the life of developers who will use your API easier is to follow the naming conventions of your platform's built in libraries. If everyone calls a node a node, and an iterator an iterator, it makes life easy.

Peter Recore
+3  A: 

I don't really have an answer, but three things for you to think about.

  1. The late Phil Karlton famously said: "There are only two hard problems in computer science. Cache Invalidation and Naming Things." So, the fact that you are having trouble coming up with good names is entirely normal and even expected.
  2. OTOH, having trouble naming things can also be a sign of bad design. (And yes, I am perfectly aware, that #1 and #2 contradict each other. Or maybe one should think of it more like balancing each other.) E.g., if a thing has too many responsibilities, it is pretty much impossible to come up with a good name. (Witness all the "Service", "Util", "Model" and "Manager" classes in bad OO designs. Here's an example Google Code Search for "ManagerFactoryFactory".)
  3. Also, your names should map to the domain jargon used by subject matter experts. If you can't find a subject matter expert, that's a sign that you are currently worrying about code that you're not supposed to worry about. (Basically, code that implements your core business domain should be implemented and designed well, code in ancillary domains should be implemented and designed so-so, and all other code should not be implemented or designed at all, but bought from a vendor, where what you are buying is their core business domain. [Please interpret "buy" and "vendor" liberally. Community-developed Free Software is just fine.])

Regarding #3 above, you mentioned in another comment that you are currently working on implementing a tree data structure. Unless your company is in the business of selling tree data structures, that is not a part of your core domain. And the reason that you have trouble finding good names could be that you are working outside your core domain. Now, "selling tree data structures" may sound stupid, but there are actually companies that do that. For example, the BCL team inside Microsoft's developer division: they actually sell (well, for certain definitions of "sell", anyway) the .NET framework's Base Class Libraries, which include, among others, tree data structures. But note that for example Microsoft's C++ compiler team actually (literally) buys their STL from a third-party vendor – they figure that their core domain is writing compilers, and they leave the writing of libraries to a company who considers writing STLs their core domain. (And indeed, AFAIK, that company does nothing but write and sell STL implementations. That's their sole product.)

If, however, selling tree data structures is your core domain, then the names you listed are just fine. They are the names that subject matter experts (programmers, in this case) use when talking about the domain of tree data structures.

Jörg W Mittag