0

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.

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
  • Does the function and the member have the same access control? – NathanOliver Sep 28 '17 at 13:44
  • 3
    What is the actual name of the function returing the "isLooped" field ? I'm not sure to understand correctly the question but basically the answer to "what can I do to avoid calling the function and variable the same thing?" is just : give them two different names – M. Yousfi Sep 28 '17 at 13:45
  • They don't have the same access control. – Andrei Jardan Sep 28 '17 at 13:46
  • 1
    Have a different naming convention for member functions and member variables. – acraig5075 Sep 28 '17 at 13:46
  • I was going to call the function "isLooped" as well, because that's how I usually call functions which return booleans. I can obiviously call it something else, but it wouldn't be consistent with my style. – Andrei Jardan Sep 28 '17 at 13:47
  • 1
    Then rename the boolean to `is_looped` or something. There are no solutions but to rename one of the two. – HolyBlackCat Sep 28 '17 at 13:49
  • Isn't this completely a matter of opinion? Anyways, some like `mIsLooped` as member variable, some like `isLooped_`, some like to give it completely different names. – Passer By Sep 28 '17 at 14:39
  • Personally, I would call the Boolean `looped` and the method `is_looped` (or `isLooped` if you're attached to the camel case convention). But that's me. – Silvio Mayolo Sep 28 '17 at 17:01

2 Answers2

0

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.

Daniel Trugman
  • 8,186
  • 20
  • 41
0

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.

Licht
  • 1,079
  • 1
  • 12
  • 27
  • GetIsLooped is awkward. It seems much more natural to ask to call the function IsLooped() and get an answer to the question. – Philipp Sep 28 '17 at 14:23
  • 2
    One of my most hated conventions: putting `get` before everything. It doesn't even read correctly, would you rather `std::complex z{40, 2}; auto r = z.real();` or `auto r = z.GetReal();`. Talk about getting real. – Passer By Sep 28 '17 at 14:43