Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Good chained function name? (http://www.velocityreviews.com/forums/t957588-good-chained-function-name.html)

Nephi Immortal 02-14-2013 04:43 AM

Good chained function name?
 
I ask you for your opinion what chained function name look like. I do not need to post class definition since you know what I am talking about.

Lets say for example I have 'A' class.

A a( 1 ), b( 2 ), c( 3 );
int data = a.get_value();
a.set_value( 2 );

What if I am going to write like this below?

a.set_value( b.get_value() + c.get_value() );

They can be to include operator=, operator+, and operator cast. It looksvery readable than getter and setter.

a = b + c;

What if I choose to use chained function name?

a.value() = b.value() + c.value();

Is that readable?

If I name value1() and value2(), I may not want to place them in the main class. I rather prefer to place them in the nested class.

a.Inner().value1() = b.Inner().value1() + c.Inner().value2();

What if I dont like Inner()? It should be removed and operator()() is used.

a().value1() = b().value1() + c().value2();

Do chained function name sound good?

Lets give some ideas.

int data1, data2, data3;

data1 = a.Get().Bytes();
data2 = b.Get().Kilobytes();
data3 = c.Get().Megabytes();

a.Set().Bytes( 1 );
b.Set().Kilobytes( 2 );
c.Set().Megabytes( 3 );

data1 = a().Bytes();
data2 = b().Kilobytes();
data3 = c().Megabytes();

a().Bytes() = 1;
b().Kilobytes() = 2;
c().Megabytes() = 3;

a().Bytes() = b().Kilobytes() + c().Megabytes();

Please comment if my code looks readable.

Victor Bazarov 02-14-2013 11:59 AM

Re: Good chained function name?
 
On 2/13/2013 11:43 PM, Nephi Immortal wrote:
> I ask you for your opinion what chained function name look like. I
> do

not need to post class definition since you know what I am talking about.
>
> Let’s say for example I have 'A' class.
>
> A a( 1 ), b( 2 ), c( 3 );
> int data = a.get_value();
> a.set_value( 2 );
>
> What if I am going to write like this below?
>
> a.set_value( b.get_value() + c.get_value() );


<shrug> I've seen it done.

> They can be to include operator=, operator+, and operator cast. It looks very readable than getter and setter.
>
> a = b + c;


Looks OK to me, as well. Supposedly, the type A is a "value type" that
defines addition as one of its operations.

> What if I choose to use chained function name?


Never heard this term, so not sure what the meaning of it is.

>
> a.value() = b.value() + c.value();
>
> Is that readable?


Readability is subjective. To me it's no more readable than a=b+c. Of
course, if each A has different, independently settable values, then it
would make sense to separate them by means of their own accessors.

a.value_blah() = b.value_blah() * c.value_droo();

> If I name value1() and value2(), I may not want to place them in the

main class. I rather prefer to place them in the nested class.
>
> a.Inner().value1() = b.Inner().value1() + c.Inner().value2();


I don't see any reason to do so.

> What if I don’t like “Inner()”? It should be removed and operator()() is used.
>
> a().value1() = b().value1() + c().value2();
>
> Do chained function name sound good?


What's "chained function"?

> Let’s give some ideas.
>
> int data1, data2, data3;
>
> data1 = a.Get().Bytes();
> data2 = b.Get().Kilobytes();
> data3 = c.Get().Megabytes();
>
> a.Set().Bytes( 1 );
> b.Set().Kilobytes( 2 );
> c.Set().Megabytes( 3 );
>
> data1 = a().Bytes();
> data2 = b().Kilobytes();
> data3 = c().Megabytes();
>
> a().Bytes() = 1;
> b().Kilobytes() = 2;
> c().Megabytes() = 3;
>
> a().Bytes() = b().Kilobytes() + c().Megabytes();
>
> Please comment if my code looks readable.


Since the type of 'Bytes', etc., is all the same, it seems to be OK to
add them together. It's not proper from the POV of dimensioned values,
though. In this particular case 'Bytes' and 'Kilobytes' don't look good
as function names, but rather dimensioned types. "Storage", "Volume",
or "Capacity", are better names. Whether the values returned by those
functions are in bytes or kilobytes, can be easily decided, and then you
can easily add those together to yield a value of the more granulated
type, IOW if bytes and kilobytes are added, you get bytes in which
kilobytes are multiplied correspondingly.

So, in short, no, I don't think your code is readable because it does
not reflect anything that would make sense to me. But, again, it can be
all in the eye of the beholder. If you don't intend your code to be
read by anybody else but you, make it readable to *you*, nothing else is
of any importance.

V
--
I do not respond to top-posted replies, please don't ask

Tiib 02-14-2013 12:40 PM

Re: Good chained function name?
 
On Thursday, 14 February 2013 06:43:24 UTC+2, Nephi Immortal wrote:
> I ask you for your opinion what chained function name look like.
> I do not need to post class definition since you know what I am talking about.



We can imagine how to achieve that.

> Lets say for example I have 'A' class.
>
> A a( 1 ), b( 2 ), c( 3 );
>
> int data = a.get_value();
> a.set_value( 2 );
>
> What if I am going to write like this below?
>
> a.set_value( b.get_value() + c.get_value() );


You see yourself that it is too much code bloat. Viewing classes as storage
of values of other types is common but too primitive usage. It
exposes internals and makes error handling hard.

> They can be to include operator=, operator+, and operator cast. It
> looks very readable than getter and setter.
>
> a = b + c;


Yes, that is done often and is good. Full externally observable "value" of
instance participates (we call it "state"). That state is usually not
representable as single value of some simpler type (like int) that you can
"get".

> What if I choose to use chained function name?
>
> a.value() = b.value() + c.value();


If that value is full state, then previous was better. If it is some property
then setter feels wrong, getters look fine:

a.length() = b.length() + c.length(); // worse
a.resize( b.length() + c.length() ); // better

> Is that readable?
>
> If I name value1() and value2(), I may not want to place them in the
> main class. I rather prefer to place them in the nested class.
>
> a.Inner().value1() = b.Inner().value1() + c.Inner().value2();


Now you expose internals of internals, that endangers your design to grow
into terrible spaghetti.

> What if I dont like Inner()? It should be removed and operator()() is
> used.
>
> a().value1() = b().value1() + c().value2();


Unreadable. Confusing usage of function call operator. Reserve function
call operator for objects that behave like functions.

> Do chained function name sound good?
>
> Lets give some ideas.
>
> int data1, data2, data3;
>
> data1 = a.Get().Bytes();
> data2 = b.Get().Kilobytes();
> data3 = c.Get().Megabytes();
> a.Set().Bytes( 1 );
> b.Set().Kilobytes( 2 );
> c.Set().Megabytes( 3 );
> data1 = a().Bytes();
> data2 = b().Kilobytes();
> data3 = c().Megabytes();
> a().Bytes() = 1;
> b().Kilobytes() = 2;
> c().Megabytes() = 3;


I like none. These are names of units of measurement otherwise plural names
feel like container accessors for me.

Use 'discA.size().asKiloBytes()' ... or actually I don't understand your
example. Try to come up with examples closer to practice.

> a().Bytes() = b().Kilobytes() + c().Megabytes();
>
> Please comment if my code looks readable.


Adding kilobytes to megabytes and getting bytes? It feels as readable as
any too obvious logic error can be. If I read it I feel that either I don't
understand something major or it is unbelievably wrong. I am confused by
such code.


Stefan Ram 02-14-2013 01:14 PM

Re: Good chained function name?
 
Tiib <ootiib@hot.ee> writes:
> a.length() = b.length() + c.length(); // worse
> a.resize( b.length() + c.length() ); // better


Should be

a.length( b.length() + c.length() )

according with the standard library:

const int prec = ::std::cout.precision();
::std::cout.precision( prec );


Victor Bazarov 02-14-2013 01:29 PM

Re: Good chained function name?
 
On 2/14/2013 8:14 AM, Stefan Ram wrote:
> Tiib <ootiib@hot.ee> writes:
>> a.length() = b.length() + c.length(); // worse
>> a.resize( b.length() + c.length() ); // better

>
> Should be
>
> a.length( b.length() + c.length() )
>
> according with the standard library:
>
> const int prec = ::std::cout.precision();
> ::std::cout.precision( prec );
>
> .


The Standard library is but an example of a committee's understanding of
readability. And we know quite well what a horse designed by a
committee looks like. Choosing a noun to also play the role of setting
the value (along with getting it) is not the best approach to concur
readability IMNSHO. Of course to many whose native tongue is not
English it might not matter (no offense is intended). After all, it's
an idiom. The case like this ('blah' without an argument is a getter,
and 'blah' *with an argument* is a setter) might be one case where C++
overloading shines, but I'm reminded that all that glitters is not gold.

V
--
I do not respond to top-posted replies, please don't ask

Tiib 02-14-2013 01:39 PM

Re: Good chained function name?
 
On Thursday, 14 February 2013 15:14:34 UTC+2, Stefan Ram wrote:
> �� Tiib <ootiib@hot.ee> writes:
> > a.length() = b.length() + c.length(); // worse
> > a.resize( b.length() + c.length() ); // better

>
> Should be
>
> a.length( b.length() + c.length() )
>
> according with the standard library:
>
> const int prec = ::std::cout.precision();
> ::std::cout.precision( prec );


Standard library is pile of subtle inconsistencies that can't me edited
because of backward compatibility. The input/output library is notably
different from rest of it AND it is the part that is most frequently
bashed for inefficiency and inconvenience. Less bashed container library
is more consistent with what I wrote:

typedef std::vector<int> Ints;
Ints a( 3, 7 );
Ints b( 5 );
Ints c;

// properties are changed with operations whose name is verb, not noun
c.resize( a.size() + b.capacity() );
c.shrink_to_fit();
// accessors to non-properties are nouns providing references
c.front() = 42;


Jeff Flinn 02-14-2013 01:47 PM

Re: Good chained function name?
 
On 2/13/2013 11:43 PM, Nephi Immortal wrote:
> I ask you for your opinion what chained function name look like. I do not need to post class definition since you know what I am talking about.
>
> Lets say for example I have 'A' class.
>
> A a( 1 ), b( 2 ), c( 3 );
> int data = a.get_value();
> a.set_value( 2 );
>
> What if I am going to write like this below?
>
> a.set_value( b.get_value() + c.get_value() );
>
> They can be to include operator=, operator+, and operator cast. It looks very readable than getter and setter.
>
> a = b + c;
>
> What if I choose to use chained function name?
>
> a.value() = b.value() + c.value();
>
> Is that readable?


Well, the function named 'value' would lead me to believe that it, well
returns by value, and not by reference. So I would expect the above
expression to have no lasting effect as the temporary returned by value
gets it's value set, and then is gone at the ;

Jeff


Victor Bazarov 02-14-2013 01:52 PM

Re: Good chained function name?
 
On 2/14/2013 8:47 AM, Jeff Flinn wrote:
> On 2/13/2013 11:43 PM, Nephi Immortal wrote:
>> I ask you for your opinion what chained function name look like. I do
>> not need to post class definition since you know what I am talking about.
>>
>> Lets say for example I have 'A' class.
>>
>> A a( 1 ), b( 2 ), c( 3 );
>> int data = a.get_value();
>> a.set_value( 2 );
>>
>> What if I am going to write like this below?
>>
>> a.set_value( b.get_value() + c.get_value() );
>>
>> They can be to include operator=, operator+, and operator cast. It
>> looks very readable than getter and setter.
>>
>> a = b + c;
>>
>> What if I choose to use chained function name?
>>
>> a.value() = b.value() + c.value();
>>
>> Is that readable?

>
> Well, the function named 'value' would lead me to believe that it, well
> returns by value, and not by reference. So I would expect the above
> expression to have no lasting effect as the temporary returned by value
> gets it's value set, and then is gone at the ;


So, would you then recommend to have a 'reference' as a function name,
as in

a.reference() = b.value() + c.value();

? 8-[

May I remind you that 'value' *can* mean an L-value just as much as an
R-value? Especially when it appears on the *L*eft side of the
assignment... Just sayin'... ;-)

V
--
I do not respond to top-posted replies, please don't ask

Jeff Flinn 02-14-2013 02:27 PM

Re: Good chained function name?
 
On 2/14/2013 8:52 AM, Victor Bazarov wrote:
> On 2/14/2013 8:47 AM, Jeff Flinn wrote:
>> On 2/13/2013 11:43 PM, Nephi Immortal wrote:
>>> I ask you for your opinion what chained function name look like. I do
>>> not need to post class definition since you know what I am talking
>>> about.
>>>
>>> Lets say for example I have 'A' class.
>>>
>>> A a( 1 ), b( 2 ), c( 3 );
>>> int data = a.get_value();
>>> a.set_value( 2 );
>>>
>>> What if I am going to write like this below?
>>>
>>> a.set_value( b.get_value() + c.get_value() );
>>>
>>> They can be to include operator=, operator+, and operator cast. It
>>> looks very readable than getter and setter.
>>>
>>> a = b + c;
>>>
>>> What if I choose to use chained function name?
>>>
>>> a.value() = b.value() + c.value();
>>>
>>> Is that readable?

>>
>> Well, the function named 'value' would lead me to believe that it, well
>> returns by value, and not by reference. So I would expect the above
>> expression to have no lasting effect as the temporary returned by value
>> gets it's value set, and then is gone at the ;

>
> So, would you then recommend to have a 'reference' as a function name,
> as in
>
> a.reference() = b.value() + c.value();
>
> ? 8-[


I would avoid the whole approach. I'm assuming the OP's example is just
that: a paired down example. Without a clearer def of the class in
question it's difficult to say what approach should be used, indeed are
these accessor/mutator pairs even needed.

Jeff


Tiib 02-14-2013 03:14 PM

Re: Good chained function name?
 
On Thursday, 14 February 2013 16:27:29 UTC+2, Jeff Flinn wrote:
> On 2/14/2013 8:52 AM, Victor Bazarov wrote:
> > On 2/14/2013 8:47 AM, Jeff Flinn wrote:
> >> On 2/13/2013 11:43 PM, Nephi Immortal wrote:
> >>> What if I choose to use chained function name?
> >>>
> >>> a.value() = b.value() + c.value();
> >>>
> >>> Is that readable?
> >>
> >> Well, the function named 'value' would lead me to believe that it, well
> >> returns by value, and not by reference. So I would expect the above
> >> expression to have no lasting effect as the temporary returned by value
> >> gets it's value set, and then is gone at the ;

> >
> > So, would you then recommend to have a 'reference' as a function name,
> > as in
> >
> > a.reference() = b.value() + c.value();
> >
> > ? 8-[

>
> I would avoid the whole approach. I'm assuming the OP's example is just
> that: a paired down example. Without a clearer def of the class in
> question it's difficult to say what approach should be used, indeed are
> these accessor/mutator pairs even needed.


Try to be more constructive? Your answer feels like Nephi should just
drop dead and stop trying. It is easy to imagine something what can be
under question. Then naming can be discussed. I try to demonstrate:

'value' (as noun) can not be considered non-property of something whose
'value' it is. I never provide properties as L-Values for any by-passer
to store references at and change at will. I feel it error-prone.

I prefer immutable (after construction) properties. I feel it is safer
It can be LOT more efficient if property's value is even set
compile-time. Consider such things about every property.

If there is certain need to mutate a property during object's life-time
then it deserves command-like operations named by verbs. It is better
to make such operations as safe and complete as possible. Keep invariant
of whole object under its own control.

'value' is still hard example since it is fitting verb too. On such cases
I prefer to use some synonymous verb (like 'assess', 'estimate', 'evaluate')
to reduce the possible confusion:

a.revalue( b.price() + c.net_value() );


All times are GMT. The time now is 07:08 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.