c++ - Why does Google name accessors and mutators after member variables? -
c++ - Why does Google name accessors and mutators after member variables? -
http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml?showone=function_names#function_names
regular functions have mixed case; accessors , mutators match name of variable: myexcitingfunction(), myexcitingmethod(), my_exciting_member_variable(), set_my_exciting_member_variable().
isn't whole point of encapsulation hide implementation details user he/she not aware of whether accessor/mutator method returns/modifies fellow member variable or not? if alter variable name or alter way it's stored within object?
edit:
if have instance variable int foo_ seems straightforward
int foo() const { homecoming foo_; } but if add together method returns foo_ + 2, should name if bar or getbar?
int bar() const { homecoming foo_ + 2; } int getbar() const { homecoming foo_ + 2; } if take getbar , later decide cache returned value in fellow member variable bar_, have rename method bar?
actually, point of encapsulation hide inner workings of class, not hide names of things. name of fellow member variable doesn't matter; it's level of indirection accessor or mutator provides.
having accessor gives ability alter inner workings of class (including names of fellow member variables) without breaking class's interface outside world. user of class need not concern himself implementation details, including things named within class, on the behavior of class, seen outside.
to set way, users of class should not rely on google's style guide determine whether or not modifying fellow member variable.
c++ oop coding-style encapsulation google-style-guide
Comments
Post a Comment