Friday, 12 September 2008

Agent nouns are code smells

Class names ending in agent nouns are a code smell. An agent noun is:
any noun that denotes someone or something carrying out the verb's action, typically words ending in -er or -or
Classes with agent nouns are, the majority of the time, a sign of procedural thinking - especially agent nouns such as "manager", "helper" etc.   As the definition says it denotes the class is carrying out the verbs action.   This is contrary to good OOP where the verbs belong to the nouns themselves; classes which represent things are responsible for their own doings, not someone else. This reflects the real world where I am a Person able to do Programmer tasks, the verbs of programming are executed by me not some CodeProgrammer object which sits next to my desk.

Agent nouns are useful for describing roles which makes them good for interface names. A SpellChecker interface on a Dictionary class gives clear definition of the role (to check spelling) and allows the Dictionary to implement the verbs (check_spelling "word"), likewise a SynonymProvider sits well on a Thesaurus class.

So beware agent nouns; they are a language trick which fools you into believing that a class is a first class concept when really it's stealing someone's verb.

2 comments:

Wynona said...

People should read this.

Anonymous said...

Well, I did read it. To understand it needs some background experience. That may be too much asked from (all) people but it made me happy as a rainbow

Keshet

Blog Archive

About Me

My Photo
Peter Gillard-Moss
West Malling, Kent, United Kingdom
I am a ThoughtWorker and general Memeologist living in the UK. I have worked in IT since 2000 on many projects from public facing websites in media and e-commerce to rich-client banking applications and corporate intranets. I am passionate and committed to making IT a better world.
View my complete profile