There is an argument over naming of a method. I will not tell you on which side I am. Please don’t look this up on the internet for the sake of being objective.
void Get_Contents()
{
for (const auto& item : m_contents)
{
cout << item << " ";
}
}
Side A: Get_Contents
is misleading name because it actually prints and does not return any value.
Side B: You say so because it’s the naming convention you are accustomed to, the code is not defective. Get
functions are many times getters but often they are not. You can do PR though.
Would you consider Get_Contents
name here misleading and therefore the code defective?
Maybe you should try asking such questions on StackOverflow or CPP forums ;)
JFTR, this should be called
print_contents
because I don’t like uppercase variable names and it’s notget
ting anything, just printing. So, there you have your option C :)Mmm… nice. I think I’ll get there if there will be not enough answers here… since I already started here. I guess next time this kind of question goes straight there.
It’s probably too opinionated for Stack Overflow; it’s likely to get closed there.
… also makes sense
The Getter returning
void
already gives some info that it’s not actually getting anything.Regarding which side I’m on. I think the discussion can be avoided by renaming the function to
Print_Contents
, which more accurately reflects the actual behaviour. I also think you should rename it toprint_contents
orPrintContents
but I’m trying to be consistent.Side B claims it’s not a getter. Side A and you thinking it’s getter because of the naming convention you are accustomed to.
A lot of C++ code I’ve dealt with does not use
Get*
prefix for getters and instead uses lower case returning const or writable references depending on const modifier of the member function. I don’t know the origination ofGet*
andSet*
–I know it’s extremely prevalent in Java but not in the C++ code I’ve dealt with. It seems to be a workaround for languages not having writable references, providing reflection-discoverable consistent access to data-like objects (I think is what JavaBeans used it for), or, more generally, just not implementing the universal access principle, which most languages don’t do.If I saw a function with
Get
anything, I’d expect it to return some sort of value or use an out parameter, because it’s getting something and putting it somewhere.The more important question, is why this is implemented as
Get_Contents
when it is writing tocout
and not asstd::ostream& operator<<(std::ostream& os, const MyType& t)
.