Method names are as important as class names to describe what they do.

First this can be illustrated with examples.

Some real method names that never caught on.

Method

Why

doThisMethodLongTime

Nothing in the name describes what it does other than take a long time.

calculateTotal2

What differentiates this method from calculateTotal?

calculateTotalBetter(..)

What differentiates this method from calculateTotal?

w_c2fr(..)

A random sequence of characters tells the user nothing.

tellReader(..)

The word tell implies that some kind of communications is involved, but communications is such a broad area this method could be doing anything.

dataSpecial(..)

Gives no indication of what the function really does.  Every method is special to the developer.

calculateTTTTTTT(..)

The TTTTTTT was used because the developer avoided the effort of coming up with a meaningful function name.   The function probably doesn't exist for a meaningful purpose either.

validateMediaValidity(..)

Too many uses of the same word.   validateMedia() would have been enough.

hasNotLessThanBackwards

A unique function to cover an obscure case of logic which will probably never be called from anywhere else.   This probably does not need to exist.

Some method names that everybody loved.

Method

Why

canUserChangeInvoice(..)

Even without looking at the method signature, the fact that the method starts with the word “can” or “is” identifies it will probably return a boolean value.  Even the parameters that will need to be passed in can be inferred from the name of the method.

onSubmit(..)

Short and sweet.  The “on” prefix implies it is the result of a submit event.

queryInvoicesByUnpaid(..)

The query prefix implies there will be a database query when the method is executed.  Without looking at the API the user can expect a performance hit when this method is called.  The method is specific in what it does so the develop has written this into the name.  Expect to find other methods nearby like queryInvoicesByPaid and queryInvoicesByContractor.

saveHistory(..)

A short and sweet method name that gets the message across.

blog comments powered by Disqus