Question

I have this personal rule to start all function/method names with a verb. My verb of choice for functions or methods that get a value based on some data structure or object is get. I'm wondering if that's a good idea.

The good thing about get is that it doesn't really say anything about how that value is being retrieved, just get it. Something like calculate (apart from being awfully long) may be too much information.

This applies to both functions and methods, i.e.:

float get_magnitude(Vector2d vector)
{
    return sqrt(pow(vector.x, 2), pow(vector.y, 2));
}

or

float Vector2d::get_magnitude() const
{
    return sqrt(pow(x, 2), pow(y, 2));
}

On the other hand, I sometimes end up with getter methods for read-only properties. It may make sense to use a more meaningful verb for anything that doesn't just return some property, and may not run in linear time.

Is there a case to be made for either approach or is it just a matter of taste?

No correct solution

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