How to avoid a name collision between a boolean and a method which returns it

289 Views Asked by At

Let's say that I've got a class which handles animations. Inside of this class is a boolean called isLooped and a function which returns this boolean. My question is, what can I do to avoid calling the function and variable the same thing?

I have thought about adding a prefix to my class variables, but that would mean making changes across my entire code base, which I don't desire.

2

There are 2 best solutions below

0
On

This is entirely a matter of naming conventions, here are two trivial options:

Option #1 You could for example use UpperCamelCase for method names, and LowerCamelCase for member names.

class Object {
    bool IsLooped() { return isLooped; }
    bool isLooped;
}

Option #2 You could use LowerCamelCase for method names and member names, and use some kind of a prefix/suffix for member names, such as m_, or just _

class Object {
    bool isLooped() { return m_isLooped; }
    bool m_isLooped;
}

I prefer option number #2, since it makes life easier when trying to understand which objects are members.

2
On

Forewarning I am not super aware of the popular conventions in C++.

I would keep isLooped the same and rename your method to getIsLooped(). I think it makes the relationship quite clear. If public GetIsLooped() is more appropriate. But obviously the convention used in the rest of the code base is going to influence that decision.