Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Good chained function name?

Reply
Thread Tools

Good chained function name?

 
 
Nephi Immortal
Guest
Posts: n/a
 
      02-14-2013
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.
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      02-14-2013
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
 
Reply With Quote
 
 
 
 
Tiib
Guest
Posts: n/a
 
      02-14-2013
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.

 
Reply With Quote
 
Stefan Ram
Guest
Posts: n/a
 
      02-14-2013
Tiib <(E-Mail Removed)> 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 );

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      02-14-2013
On 2/14/2013 8:14 AM, Stefan Ram wrote:
> Tiib <(E-Mail Removed)> 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
 
Reply With Quote
 
Tiib
Guest
Posts: n/a
 
      02-14-2013
On Thursday, 14 February 2013 15:14:34 UTC+2, Stefan Ram wrote:
> �� Tiib <(E-Mail Removed)> 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;

 
Reply With Quote
 
Jeff Flinn
Guest
Posts: n/a
 
      02-14-2013
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

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      02-14-2013
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
 
Reply With Quote
 
Jeff Flinn
Guest
Posts: n/a
 
      02-14-2013
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

 
Reply With Quote
 
Tiib
Guest
Posts: n/a
 
      02-14-2013
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() );
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Chained RDP over chained VPN Frank Winkler Cisco 2 06-20-2008 08:02 AM
Chained Comparisons Sathyaish Python 15 03-20-2006 05:52 PM
Multiple "cmp"s chained one after another Volker Grabsch Python 11 05-16-2005 01:16 PM
Can abbreviated printStackTrace() output be easily prevented for chained exceptions? Mike H Java 1 02-27-2004 04:09 PM
NAT and chained subnet bomba Cisco 6 10-29-2003 02:09 PM



Advertisments