Consolidate naming of member variables

Bug #1395278 reported by SirVer
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
widelands
Fix Released
Low
Unassigned

Bug Description

We use m_var, _var and var_ for member variables in various places. We should consolidate this to one: var_.

_var is just as short and is similar to python, however this might clash with reserved variables in the global scope (see http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier). m_var is 1 character longer than the other suggestions.

Related branches

Revision history for this message
GunChleoc (gunchleoc) wrote :

How do we wish to call function arguments then? There is this example in the Google style guide:

class MyClass {
 public:
  ...
  int num_entries() const { return num_entries_; }
  void set_num_entries(int num_entries) { num_entries_ = num_entries; }

 private:
  int num_entries_;
};

but it will cause a "declaration of ‘num_entries’ shadows a member of 'this'" warning in the compiler for the setter function.

Revision history for this message
SirVer (sirver) wrote :

I think the warning is great since you should really not reuse a similar name in an argument. For setters (like in your example) I always use value:

  void set_num_entries(int value) { num_entries_ = value; }

Revision history for this message
GunChleoc (gunchleoc) wrote :

I also think the warning is great - it has saved me from a bug or two already methinks.

Your idea works great for setters, but I just thought of another use case - what about constructors, where we have more than one argument that we need to name? The conflict here will be between the getter function name and the argument name. We have solved a few of these in the code base by adding _ at the end or the argument, which we will now use for the member variables instead.

Revision history for this message
SirVer (sirver) wrote :

For structs (where you are not using anything to separate members) I always use init_<name> in the constructor.

Revision history for this message
Klaus Halfmann (klaus-halfmann) wrote :

I started to work on this, but this looks like a ... longer story :-)

Revision history for this message
SirVer (sirver) wrote :

Is this not mechanical?

for i in $(find . -name '*.cc' -or -name '*.h'); do
   sed -i s/\bm_([a-zA-z0-9_]+)/\1_/g;
end

Revision history for this message
SirVer (sirver) wrote :

Well, that was a bad attempt, but this should get you really close:

for i in $(find . -name '*.cc' -or -name '*.h'); do
  echo $i;
  cat $i | perl -pe 's/\bm_([a-zA-Z0-9_]+)/\1_/g' > $i.tmp;
  mv $i.tmp $i;
done

Revision history for this message
GunChleoc (gunchleoc) wrote :

Found another old naming convention in ui_basic/window.h:

private:
 bool _is_minimal;
 uint32_t _oldh; // if it is, this is the old height
 bool _dragging, _docked_left, _docked_right, _docked_bottom;
 int32_t _drag_start_win_x, _drag_start_win_y;
 int32_t _drag_start_mouse_x, _drag_start_mouse_y;

_is_minimal should be is_minimal_ etc.

Revision history for this message
SirVer (sirver) wrote : Re: [Bug 1395278] Re: Consolidate naming of member variables

These are especially bad since identifiers starting with _ are by the c++ standard reserved identifiers that compiler vendors can define and must not be used in regular code.

> Am 07.02.2016 um 06:53 schrieb GunChleoc <email address hidden>:
>
> Found another old naming convention in ui_basic/window.h:
>
> private:
> bool _is_minimal;
> uint32_t _oldh; // if it is, this is the old height
> bool _dragging, _docked_left, _docked_right, _docked_bottom;
> int32_t _drag_start_win_x, _drag_start_win_y;
> int32_t _drag_start_mouse_x, _drag_start_mouse_y;
>
> _is_minimal should be is_minimal_ etc.
>
> --
> You received this bug notification because you are subscribed to the bug
> report.
> https://bugs.launchpad.net/bugs/1395278
>
> Title:
> Consolidate naming of member variables
>
> Status in widelands:
> Confirmed
>
> Bug description:
> We use m_var, _var and var_ for member variables in various places. We
> should consolidate this to one: var_.
>
> _var is just as short and is similar to python, however this might
> clash with reserved variables in the global scope (see
> http://stackoverflow.com/questions/228783/what-are-the-rules-about-
> using-an-underscore-in-a-c-identifier). m_var is 1 character longer
> than the other suggestions.
>
> To manage notifications about this bug go to:
> https://bugs.launchpad.net/widelands/+bug/1395278/+subscriptions

Revision history for this message
GunChleoc (gunchleoc) wrote :

panel.h also needs fixing - it has a bunch of these, and 2 s_ names for static variables.

I'd like to take care of the logic dir, map_io and game_io. These are pretty intertwined, so using the automated script will give us either a bunch of compiler errors or a huge diff. So, I will refactor those with QTCreator.

GunChleoc (gunchleoc)
Changed in widelands:
milestone: none → build19-rc1
GunChleoc (gunchleoc)
Changed in widelands:
status: Confirmed → Fix Committed
GunChleoc (gunchleoc)
Changed in widelands:
status: Fix Committed → Fix Released
Revision history for this message
GunChleoc (gunchleoc) wrote :

Fixed in build19-rc1.

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.