www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Re: poll for properties

reply Dimitar Kolev <DimitarRosenovKolev hotmail.com> writes:
Steven Schveighoffer Wrote:

 Please respond to this poll before reading other responses.
 
 Read the following function:
 
 void foo(Box b)
 {
    if(b.xxx)
      b.fill(5);
    else
      b.yyy;
 }
 
 Assuming you have no idea what type Box is, what of the following options  
 looks most natural for xxx in order to test to see if b has an element in  
 it?
 
 a) if(b.empty)
 b) if(b.empty())
 c) if(b.clear)
 d) if(b.clear())
 
 Answer:
 =============
 
None. I would call it isEmpty or isEmpty(). All of your options are bad names for functions or variables to check the state of an object.
 
 =============
 
 What would you guess looks most natural for yyy in order to remove all  
 elements from b?
 
 a) b.empty;
 b) b.empty();
 c) b.clear;
 d) b.clear();
 
 Answer:
 =============
All of them as all indicate functions and not states.
 
 
 =============
 
 Which of the following functions looks incorrect?
 
 void a(Box box)
 {
     if(box.empty())
       box.fill(5);
     else
       box.clear();
 }
 
 void b(Box box)
 {
     if(box.empty)
       box.fill(5);
     else
       box.clear;
 }
 
 void c(Box box)
 {
      if(box.clear)
         box.fill(5);
      else
         box.empty;
 }
 
 void d(Box box)
 {
      if(box.clear())
         box.fill(5);
      else
         box.empty();
 }
 
 
 Answer:
 ==============
I do not find a correct looking one. As I stated you should not make state checking an object with names like that.
 
 
 ==============
 
 You read the documentation for Box, and it looks like this:
 
 /**
   * check to see if a box is clear
   */
   bool clear();
 
 /**
   * empty a box, returning true if the box had contents before emptying
   */
   bool empty();
This should return true if it emptied the box not if the box was empty before.
 
 Now knowing what the actual meaning of clear and empty are, indicate which  
 version(s) of the function in the previous question would surprise you if  
 it compiled.
 
 Here are the functions again for reference:
 
 void a(Box box)
 {
     if(box.empty())
       box.fill(5);
     else
       box.clear();
 }
 
 void b(Box box)
 {
     if(box.empty)
       box.fill(5);
     else
       box.clear;
 }
 
 void c(Box box)
 {
      if(box.clear)
         box.fill(5);
      else
         box.empty;
 }
 
 void d(Box box)
 {
      if(box.clear())
         box.fill(5);
      else
         box.empty();
 }
 
 Answer:
 ==============
This whole example surprises me.
 
 
 ==============
 
 Do you think the meaning of a symbol with parentheses suggests something  
 different than that same symbol without parentheses for the following  
 symbols:
 
 a) select
 b) rock
 c) keyboard
 d) elevate
 
 Answer:
 ==============
a) select - action. b) rock - could be both but you would know if the application has anything to do with rocks (from the earth) c) keyboard - I do not think you can say he keyboards. Guess what it should be. d) elevate - action.
 
 
 ==============
 
 Thank you for taking the poll.  I tried to be as objective as possible, if  
 you don't think I was, please indicate why:
I do not find anything objective here. Choosing right names is important and I am sorry but your example does not provide correct naming. void a(Box box) { if(box.isEmpty()) box.fill(5); else box.clear(); } void b(Box box) { if(box.isEmpty) box.fill(5); else box.clear; } I do not find anything wrong with my example. Both cases are perfectly readable and understandable.
Jul 28 2009
parent reply bearophile <bearophileHUGS lycos.com> writes:
Dimitar Kolev:
 None. I would call it isEmpty or isEmpty().
This may interest Andrei: lot of people here (me too) seem to think that the name "isEmpty" is better than "empty" for that purpose. Bye, bearophile
Jul 29 2009
parent reply Kagamin <spam here.lot> writes:
bearophile Wrote:

 Dimitar Kolev:
 None. I would call it isEmpty or isEmpty().
This may interest Andrei: lot of people here (me too) seem to think that the name "isEmpty" is better than "empty" for that purpose.
The very properties discussion comes from D living in the context of C family languages and their good practices. Python somehow managed to rebel from those...
Jul 29 2009
parent reply Dimitar Kolev <DimitarRosenovKolev hotmail.com> writes:
Kagamin Wrote:

 bearophile Wrote:
 
 Dimitar Kolev:
 None. I would call it isEmpty or isEmpty().
This may interest Andrei: lot of people here (me too) seem to think that the name "isEmpty" is better than "empty" for that purpose.
The very properties discussion comes from D living in the context of C family languages and their good practices. Python somehow managed to rebel from those...
The properties discussion arises from the people who can not think of good names for variables and functions. So they get confused by things like empty. It should be isEmpty the state and empty the function then there will be no confusion which does what and people can finally realize that () are optional. This is just an example but good names are the reason for all this weirdness. Or bad names.
Jul 29 2009
parent Bill Baxter <wbaxter gmail.com> writes:
On Wed, Jul 29, 2009 at 4:35 AM, Dimitar
Kolev<DimitarRosenovKolev hotmail.com> wrote:
 Kagamin Wrote:

 bearophile Wrote:

 Dimitar Kolev:
 None. I would call it isEmpty or isEmpty().
This may interest Andrei: lot of people here (me too) seem to think that the name "isEmpty" is better than "empty" for that purpose.
The very properties discussion comes from D living in the context of C family languages and their good practices. Python somehow managed to rebel from those...
The properties discussion arises from the people who can not think of good names for variables and functions.
Andrei decided to use empty over isEmpty because he wanted names as short as possible for the fundamental ops for ranges. It had nothing to do with not being able "think of" isEmpty. He thought of it and rejected it as too cumbersome for this purpose. And it shouldn't be particularly ambiguous if understood to be an attribute of the range and not an action on the range. Unfortunately we have no way in the language to specify that distinction.
 So they get confused by things like empty. It should be isEmpty the state and
empty the function then there will be no confusion which does what and people
can finally realize that () are optional.
If you don't have dedicated property syntax, then I agree, you probably should avoid the ambiguity and resort to isEmpty.
 This is just an example but good names are the reason for all this weirdness.
Or bad names.
I think having dedicated property syntax makes it easier to choose good names because there are fewer ambiguity landmines to watch out for. --bb
Jul 29 2009