1

I am reading on Coding Complete book the following statement:

To name a procedure, use a strong verb followed by an object A procedure with functional cohesion usually performs an operation on an object. The name should reflect what the procedure does, and an operation on an object implies a verb-plusobject name. PrintDocument(), CalcMonthlyRevenues(), CheckOrderlnfo(), and RepaginateDocument() are samples of good procedure names.

In my code I apply this principle but some routines have an adjective in front the noun example:

Currently I am using

  _getFirstView()
  _getLastView()
  _getActiveView()
  _getActiveMasterView()

I am wondering if this naming could be improved for example using the following variant (in some how how found them more visually organized)

  _getViewFirst()
  _getViewLast()
  _getViewActive()
  _getViewMasterActive()
...

What is a good practice which makes sense in this scenario?

  • Did it also include the advice to use noun phrases for non-mutating methods and verb phrases for mutating methods? I would drop the 'get' and just use 'firstView()'. Yes, the data has to be fetched, but that's an implementation detail that is below the object level. – kevin cline Apr 28 '17 at 17:30
8

No, you shouldn't change the order of words away from their natural order in natural language.

The point of the guidelines you are citing is to ensure that method names are readable and informative. No amount of gain in informativeness that you could achieve by making method names consistent with each other can outweigh the loss of readability from such reordering. getFirstView() is perfectly fine (and getViewFirst() is actually misleading, since it implies that it's important to call this before some other method).

4

I am no English Language expert, but by my interpretation of the advice from the Code Complete book, it seems that your existing method names already adhere to that convention.

As I read it, Code Complete uses the phrase "an object" which suggests to me that the method naming advice is about the description of a specific identifiable object rather than just an object's noun.

Of course, a noun is usually the most important part of an object's description, and when only one such object exists it would be enough on its own. However, when multiple objects exist within the same context all sharing the same noun, then a noun alone is not sufficient to identify just one object.

So, assuming that the advice is covering cases for both singular objects and objects among collections/lists/etc, the phrase 'an object' still sounds like a single object which may or may not be identified by a combination of its noun and one or more adjectives describing attributes of that noun.

For example, the HappyDwarf is clearly distinct from the GrumpyDwarf, the DopeyDwarf or the BashfulDwarf, but the Dwarf is ambiguous and could refer to any of them.

2

Your naming already conforms to the specified convention.

The conventions say verb + object. The verb in your example is "get", the object is "first view", "last view" etc.

Note that "object" in grammatical terminology is not necessary a single noun, but can be a clause including adjectives, or even a whole sentence.

The rule as specified does not say anything about adjectives, but the example CalcMonthlyRevenues() puts the adjective before the noun it modifies - exactly like in regular English and like in your examples.

-1

Currently I am using

 _getFirstView()
 _getLastView()
 _getActiveView()
 _getActiveMasterView()

These presumably aren't procedures. They are instead functions. The difference between the two: A procedure alters state, and preferably returns nothing, while a function returns something and preferably leaves state unchanged. In an ideal world, the two never meet. In the real world, they sometimes do meet. A pop() function, for example, both alters state and returns a value.

With regard to your functions, do you need that get prefix? What value does it add? In many of cases of getThis(), getThat(), and getTheOtherThing() functions, the get prefix is superfluous, misleading, or both. This leads to the widely used naming convention of using noun phrases (e.g., firstView(), lastView(), etc.) for function names, verb phrases for procedure names (e.g., PrintDocument(), CalcMonthlyRevenues(), etc.).

  • I disagre. firstView() is not better than getFirstView(). – R Sahu Apr 28 '17 at 4:15
  • I use the prefix get, set, find to better indicate what the function does. example findView(id) or acivateView(id). I also find more easy to ready a more natural language style as suggested by Kilian Foth. – Radex Apr 28 '17 at 5:46
  • 3
    I don't like noun phrases as function names. When I was taught Object-Oriented Analysis and Design, class names were noun phrases, while functions and procedures were verb phrases. The class name tells you what it is, while the function name tells you what it does. – Simon B Apr 28 '17 at 9:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.