Monday, 14 May 2012

What's in a Name? Part 1

One of the things that we all do on a daily basis in our work is to name objects. It’s an activity that we take for granted without ever giving it much thought. It’s also something that we often get wrong and sometimes do badly and there are a couple of reasons for this. Firstly, classes can just be given the wrong name and no one bothers to change it. Secondly, the developer used a verb to name a class rather than a noun: for example, if you see a class named ProcessXMLData you instinctively know that there’s something wrong and perhaps the developer doesn’t quite understand OO and is falling into the Functional Decomposition anti-pattern. Lastly, and this happens quite often, especially on large projects, a class starts out being named correctly, but as development progresses it gets refactored and its responsibilities change, but no one has the time or inclination to rename it so that its original name no longer fits.

So, how do you name a class? OO theory usually tells you that the name of a class should be a noun...

noun |noun|
nounGrammar
a word (other than a pronoun) used to identify any of a class of people, places, or things ( common noun ), or to name a particular one of these ( proper noun ).
DERIVATIVES
nounal |ˈnounəl|adjective
ORIGIN late Middle English: from Anglo-Norman French, from Latin nomen ‘name.’

...and never a verb...

verb |vərb|
nounGrammar
a word used to describe an action, state, or occurrence, and forming the main part of the predicate of a sentence, such as hear, become, happen .
DERIVATIVES
verbless adjective
ORIGIN late Middle English: from Old French verbe or Latin verbum ‘word, verb.’

...and the usual way of demonstrating this is to pick a common object such as a cat, and then display it as a class diagram explaining that Cat extends Animal and Cat HASA Tail and Tail has a method called wag(...)


But isn't this is somewhat simplistic? After all the systems we generally program are complex and the objects they contain are rarely common nouns, but are conceptual parts of the problem domain. Couple this to that fact that the English language has problems with common usage often mixing up verbs and nouns and the naming problem becomes even more compounded. This is not a new phenomenon, in the The Hound of the Baskervilles Dr Watson refers to there being a “drizzle of rain” using the word drizzle as a noun. To me it’s always been a verb as in ‘to drizzle’ or ‘it’s drizzling’; clearly the word has changed sides since Queen Victoria was on the throne. There are many more recent examples including the word ‘archive’ which has also become a verb, as in ‘to archive’ meaning ‘to store in an archive’ - ‘archive’ being the name of a place where you store things, rather than the act of storing.

Not only do nouns have problems with verbification, but verbs have problems with nounification. When is a verb a noun? When you prefix it with ‘a’, or postfix it with ‘er’, ‘or’, ‘tion’ or ‘sion’ or do both at the same time.

Putting all this back into a software context and applying these rules my original badly named class: ProcessXMLData, then logically all I need to do to correctly name the class is to rename it XMLDataProcessor. This, to me, is pointless as it's still a badly named class; adding an 'or' doesn't really hide the fact that this is really a verb in noun's clothing.

Next take a look at the MVC pattern; we take it for granted that the terms ‘model’, ‘view’ and ‘controller are referring to ‘a model’, ‘a view’ and ‘a controller’, but doesn't the ambiguity of the classification of these words makes the whole pattern seem a bit of a linguistic fudge as you can also naturally say ‘to model’, ‘to view’ and ‘to control’?

model |ˈmädl|
noun
1 a three-dimensional representation of a person or thing or of a proposed structure, typically on a smaller scale than the original: a model of St. Paul's Cathedral | [ as modifier ] : a model airplane.
• (in sculpture) a figure or object made in clay or wax, to be reproduced in another more durable material.
2 a system or thing used as an example to follow or imitate: the law became a model for dozens of laws banning nondegradable plastic products | [ as modifier ] : a model farm.
• a simplified description, esp. a mathematical one, of a system or process, to assist calculations and predictions: a statistical model used for predicting the survival rates of endangered species.
• (model of) a person or thing regarded as an excellent example of a specified quality: as she grew older, she became a model of self-control | [ as modifier ] : he was a model husband and father.
• (model for) an actual person or place on which a specified fictional character or location is based: the author denied that Marilyn was the model for his tragic heroine.
3 a particular design or version of a product: trading your car in for a newer model.

verb ( models, modeling , modeled ; Brit. models, modelling, modelled ) [ with obj. ]
1 fashion or shape (a three-dimensional figure or object) in a malleable material such as clay or wax: use the icing to model a house.
• (in drawing or painting) represent so as to appear three-dimensional: the body of the woman to the right is modeled in softer, riper forms.
• (model something on/after) use (esp. a system or procedure) as an example to follow or imitate: the research method will be modeled on previous work.
• (model oneself on) take (someone admired or respected) as an example to copy: he models himself on rock legend Elvis Presley.
• devise a representation, esp. a mathematical one, of (a phenomenon or system): a computer program that can model how smoke behaves.
2 display (clothes) by wearing them.
• [ no obj. ] work as a model by displaying clothes or posing for an artist, photographer, or sculptor.

view |vyo͞o|
noun
1 the ability to see something or to be seen from a particular place: the end of the tunnel came into view | they stood on the bar to get a better view.
• a sight or prospect, typically of attractive natural scenery, that can be taken in by the eye from a particular place: a fine view of the castle.
• a work of art depicting such a sight.
• the visual appearance or an image of something when looked at in a particular way: an aerial view of the military earthworks.
• an inspection of things for sale by prospective purchasers, esp. of works of art at an exhibition.
• Law (in court proceedings) a formal inspection by the judge and jury of the scene of a crime or property mentioned in evidence.
2 a particular way of considering or regarding something; an attitude or opinion: strong political views.
verb
1 [ with obj. ] look at or inspect (something): the public can view the famous hall with its unique staircase.
• watch (something) on television.
• Hunting see (a fox) break cover.
2 [ with obj. ] regard in a particular light or with a particular attitude: farmers are viewing the rise in rabbit numbers with concern.

control |kənˈtrōl|
noun
1 the power to influence or direct people's behavior or the course of events: the whole operation is under the control of a production manager | the situation was slipping out of her control .
• the ability to manage a machine, vehicle, or other moving object: he lost control of his car | improve your ball control.
• the restriction of an activity, tendency, or phenomenon: pest control.
• the power to restrain something, esp. one's own emotions or actions: give children time to get control of their emotions.
• (often controls) a means of limiting or regulating something: growing controls on local spending.
• a switch or other device by which a machine is regulated: the volume control.
• the place where a particular item is verified: passport control.
• the base from which a system or activity is directed: communications could be established with central control | mission control.
• Bridge a high card that will prevent opponents from establishing a particular suit.
• Computing short for control key.
2 Statistics a group or individual used as a standard of comparison for checking the results of a survey or experiment: they saw no difference between the cancer patients and the controls.
3 a member of an intelligence organization who personally directs the activities of a spy.

verb ( controls, controlling , controlled )
1 [ with obj. ] determine the behavior or supervise the running of: he was appointed to control the company's marketing strategy.
• maintain influence or authority over: you shouldn't have dogs if you can't control them.
• limit the level, intensity, or numbers of: he had to control his temper.
• (control oneself) remain calm and reasonable despite provocation: he made an effort to control himself.
• regulate (a mechanical or scientific process): the airflow is controlled by a fan.
• (as adj. controlled) (of a drug) restricted by law with respect to use and possession: a sentence for possessing controlled substances.
2 [ no obj. ] Statistics (control for) take into account (an extraneous factor that might affect results) when performing an experiment: no attempt was made to control for variations | (as adj. controlled) : a controlled trial.
check; verify.

Asserting that MVC, one of todays crucial patterns, is a bit of a fudge seems way-out, whacky idea, but this is software development where precision is everything, so the question needs asking.

Given all this fudging and laxness in the English language1, it’s not surprising that classes are sometimes badly named, so are there any techniques that can be used to create meaningful names? Several have been documented over the years including

  • Classical Classification
  • CRC cards
  • Informal English Descriptions

but more on that another day...


1 I can’t really speak for code written in other languages here, not being multilingual.

2 comments:

Anonymous said...

I find "nounification" no problem at all. The important thing is that one know it's about some "thing", not "doing some thing". Although the main purpose of a processor is processing, it's still an object so that we can change it's state, pass it around and remove it. It make no sense if one change, pass or delete "a processing".

Roger Hughes said...

Thanks for the comment. I quite agree that there’s nothing wrong with nounification or verbification after all spoken languages are versatile, dynamic entities. I’m really trying to point out that even if you have a rule like ‘the name of an object must be a noun’, it’s still possible to come up with bad names.

Also, ‘Processor’ apart from being ‘a verb in noun’s clothing’ is a bad choice for a name mainly because it’s a weasel word as it doesn’t reveal an objects intent.