PDA

View Full Version : C++ vs Ruby: setters and getters




toddburch
Nov 6, 2007, 10:20 AM
I fairly versed in Ruby in regards to class and method definitions, and I'm learning C++ (as I implement a project with it).

In Ruby, you can define instance method setters and getters inside a class for an instance variable, or define an accessor for it to reduce the typing need if all you need is basic setting and getting of a non-public instance variable.

Long way in Ruby with a setter and getter:

class MyClass

def mysetter(var)
@var = var ;
end # method mysetter

def mygetter()
return @var
end # method mygetter

end # class MyClass

me = MyClass.new()
me.mysetter("MacRumors Rocks")
puts me.mygetter()


Ruby, short way:


class MyClass
attr_accessor :var
end # class

me = MyClass.new()
me.var = "MacRumors Rocks"
puts me.var



For C++, I'm looking for a short way for defining member functions where all I need is Ruby "attr_accessor" type of specification. Is there a way to do this? Or, should I just delcare the C++ member variable "var" as public and be done with it?

Thanks, Todd



Lyle
Nov 6, 2007, 11:49 AM
For C++, I'm looking for a short way for defining member functions where all I need is Ruby "attr_accessor" type of specification. Is there a way to do this?No, there's not a way in standard C++ to simply declare an attribute and get getter and setter methods for free (as you can with Ruby's attr_accessor). You must write those accessor methods "by hand".

Or, should I just delcare the C++ member variable "var" as public and be done with it?You could do that, but it's a bad habit to get into. There are a number of advantages to encapsulation (hiding the data behind accessor methods), such as being able to set a debugger breakpoint inside the setter method to determine when the variable's value is being changed. My advice is to take the time to do it the right way.

toddburch
Nov 6, 2007, 11:54 AM
You could do that, but it's a bad habit to get into. There are a number of advantages to encapsulation (hiding the data behind accessor methods), such as being able to set a debugger breakpoint inside the setter method to determine when the variable's value is being changed. My advice is to take the time to do it the right way.

Excellent advice. Thanks. Will do.

Todd

toddburch
Nov 6, 2007, 12:35 PM
OK, I'm coding up these setters and getters. What do people use for a namining convention?

For instance,

class File {
string name ;
public:
void setname(string s) ;
string getname() ;
} ;

void setname(string s) {
name = s ;
}

string getname() {
return name ;
}


Now, I have 15 other variables to add, so that's 30 member functions to declare and 30 member functions to write and 30 function names to derive in a meaning full manner.

Surely there's an less knuckle dragging way to do this. :(

Or, is this just death by encapsulation?

Todd

Lyle
Nov 6, 2007, 01:22 PM
OK, I'm coding up these setters and getters. What do people use for a naming convention?If it were Java code, the established convention would be to use "CamelCased" method names like getName() and setName(). For C++ there's not really a standard per se; I'd go either with the Java style, or with words separated by underscores, e.g. get_name() and set_name().

Now, I have 15 other variables to add, so that's 30 member functions to declare and 30 member functions to write and 30 function names to derive in a meaning full manner.

Surely there's an less knuckle dragging way to do this. :(

Or, is this just death by encapsulation?If you're using a programming editor worth its salt, you should be able to create a macro of some kind that will let you type in the attribute name (e.g. "name") and then it will generate the code for you.

As your design evolves, you might also consider whether some of those fifteen data members can be extracted into separate classes. See "Extract Class" (http://www.refactoring.com/catalog/extractClass.html) and "Introduce Parameter Object" (http://www.refactoring.com/catalog/introduceParameterObject.html) in Martin Fowler's Refactoring.

sord
Nov 6, 2007, 01:27 PM
As far as naming convensions, I typically stick to first word lowercase, the rest with a first capital letter (so setName, getName, setPhoneNumber, getPhoneNumber, etc) -- though this is of course just a matter of opinion.

As far as needing to create all of those accessors, it depends on what tool you are using. Some IDEs have support for generating them for you (such as Eclipse does with Java).
As a worst case scenario, you could always fire up Ruby or something, make an array of your variable names, and have it generate the code for you for the getters/setters. Then just copy your output and drop it in your C++ program.

Also, I noticed your code for your accessors wasn't actually in the class, you want to do:

void File::setName(string s) {
name = s ;
}

string File::getName() {
return name ;
}


EDIT: darn - was beat to it. I second the suggestion that if you have that many members you may want to look into refactoring.

toddburch
Nov 6, 2007, 01:40 PM
If it were Java code, the established convention would be to use "CamelCased" method names like getName() and setName(). For C++ there's not really a standard per se; I'd go either with the Java style, or with words separated by underscores, e.g. get_name() and set_name().

Underscores it is.

If you're using a programming editor worth its salt, you should be able to create a macro of some kind that will let you type in the attribute name (e.g. "name") and then it will generate the code for you.

Xcode. Is it worth it's salt?


As your design evolves, you might also consider whether some of those fifteen data members can be extracted into separate classes. See "Extract Class" (http://www.refactoring.com/catalog/extractClass.html) and "Introduce Parameter Object" (http://www.refactoring.com/catalog/introduceParameterObject.html) in Martin Fowler's Refactoring.

Yes, some of them are classes themselves I have defined. Others are vectors, and other types and containers.

I'll look into the Refactoring book. Thanks for the links.

Todd

toddburch
Nov 6, 2007, 01:42 PM
As far as needing to create all of those accessors, it depends on what tool you are using. Some IDEs have support for generating them for you (such as Eclipse does with Java).
As a worst case scenario, you could always fire up Ruby or something, make an array of your variable names, and have it generate the code for you for the getters/setters. Then just copy your output and drop it in your C++ program.

True on the ruby hack. I'm using XCode for the C++.

Also, I noticed your code for your accessors wasn't actually in the class, you want to do:...

Oops. Typo. I should have cut and pasted my code instead of reinventing and getting it wrong.

Thanks. Todd

Lyle
Nov 6, 2007, 02:06 PM
Xcode. Is it worth its salt?I've not used Xcode all that much, so I suppose that is an exercise left to the reader. ;)