Question

I have seen examples of methods whose name ended with a preposition applying to the first (and most often only) parameters. But other examples don't use this kind of naming convention. Which do you think is more readable? For example, should I make a method like this:

getColorFor(Player player)

or like this?

getColor(Player player)

In this scenario, the method would return a color that represents the player supplied as parameter.

Was it helpful?

Solution

If it adds meaningful clarification or fits the "ethos" ... Yes.

.NET's OrderBy() and ElementAt() might be good examples.

Personally, I like code that reads pretty much like English. It takes a ton of guesswork, hovering and digging, and arcane knowledge out of reading someone's code when it tells me unambiguously what it does.

And as far as I can tell, I'm not the only one who feels this way.

OTHER TIPS

While shorter is better, sometimes the best fit for a short word requires disambiguation. A humorous example:

var translator = new Translator();
var result = translator.French("sister");
//throws TargetIsMarriedException

This is better:

var translator = new Translator();
var result = translator.FrenchFor("sister");
//sets result to "soeur"

This is probably the reasoning behind some of Microsoft's own examples of compound names, for example:

@html.TextBoxFor( m => m.Field );

It can be especially helpful if the first word could be either a noun or a verb, for example:

graphics.Color(x,y);

might be either

graphics.ColorOf(x,y);

or

graphics.SetColor(x,y);

Although it seems an interesting idea, I think it's overkill. I have never seen it used.

There is no need to re-form complete English language syntax in a name, especially if it adds no value - the shorter name will be perfectly clear to even beginner programmers.

An interesting question.

I agree with @Aganju that it is overkill.

The ()'s alone mean something like "with" or "by" or "in the context of" these arguments, so no need for that in the text of the identifier that names the function. 

In many languages, if you use the identifier getColor without the ()'s then that refers to the function or method itself rather than an invocation of it; for example, if you want to pass the function itself as a parameter somewhere for someone else to use.  I suppose that formally the ()'s means "apply" the arguments to that function.  This is seen sometimes in functional programming oriented languages, and also JavaScript, for example, has bind, and apply.

If you use OOP, then we might see: player.getColor() which makes it read somewhat clear that getColor is being done by player (possibly in the context of ("with") other arguments, if it takes them).

Further, the notion of adding a preposition probably does not scale well for more parameters.

Some, but not all, languages offer naming actual arguments at the call site (by the formal parameter name) so you can do getColor(for:=player), and, with that you'd get to provide descriptive names (that could be prepositions or more) for multiple parameters.

Licensed under: CC-BY-SA with attribution
scroll top