Naming issue

Discussion in 'C++' started by Martin Eisenberg, Dec 16, 2005.

  1. I have a class that holds a sequence of data items. Modifying those
    items is the sole domain of the class itself. However, the user has
    read access to the data, e.g. to gather statistics. This access is
    via an iterator range. While there is one unrelated place I might add
    a write hook later, I'm positive that write access through the range
    interface will never make sense. Given that, can I call the iterator
    type just "iterator" for convenience or should it be "const_iterator"
    for conceptual accuracy? What do you think?


    Martin

    --
    Quidquid latine scriptum sit, altum viditur.
    Martin Eisenberg, Dec 16, 2005
    #1
    1. Advertising

  2. After I asked (and got good answers, thanks to Victor Bazarov) a few
    similar questions to yours now I'd say something like this:

    "Ask yourself if such name would be clear for you and if you'd be able
    infer from such name it's character - the constness - easily or not?"

    Cheers
    --
    Mateusz Loskot
    http://mateusz.loskot.net
    Mateusz Loskot, Dec 16, 2005
    #2
    1. Advertising

  3. Mateusz Loskot wrote:

    > After I asked (and got good answers, thanks to Victor Bazarov) a
    > few similar questions to yours now I'd say something like this:
    >
    > "Ask yourself if such name would be clear for you and if you'd
    > be able infer from such name it's character - the constness -
    > easily or not?"


    In view of your posts in that thread, I'm not sure if the above is
    supposed to be a real or a rhetoric question. I do believe the answer
    to the first part would be Yes for *me* once I'd grasped what the
    class does, but that's a hazardous way to write code for others to
    use. Regarding the second part, I want to know what the cognitive
    burden on *other* people is when the overall semantics of an
    interface takes precedence over conventional connotations of a member
    name.


    Martin

    --
    Quidquid latine scriptum sit, altum viditur.
    Martin Eisenberg, Dec 16, 2005
    #3
  4. I think, after your second post, I get your point better.
    It seems that "overall semantics interface of your class" is more
    importand than semantics of individual members of that interface. I
    also think right answer will be much subjective.

    As such, I still think semantics of individual members of interface is
    very important and should be as clear as possible, or even predictable.
    So, "conceptual accuracy" is also very important at the level of
    individual member functions.

    Consequently, if semantic of access is const in manner of whole object
    - provides read-only access to data managed by object - I'd use
    const_iterator + member function declared as const.

    Cheers
    --
    Mateusz Loskot
    http://mateusz.loskot.net
    Mateusz Loskot, Dec 16, 2005
    #4
  5. Mateusz Loskot wrote:

    > It seems that "overall semantics interface of your class" is
    > more importand than semantics of individual members of that
    > interface. I also think right answer will be much subjective.
    >
    > As such, I still think semantics of individual members of
    > interface is very important and should be as clear as possible,
    > or even predictable.


    I guess you're right. Since this was the last decision to make before
    relabeling the thing from beta to 1.0 and no others have given their
    opinion, I've decided to assume that they're silently shaking their
    heads and go for const_iterator. If you want to see what this was
    about anyway, look here: http://www.aphoria.de/code/diffevol-1.0.zip


    Martin

    --
    Quidquid latine scriptum sit, altum viditur.
    Martin Eisenberg, Dec 17, 2005
    #5
  6. Martin Eisenberg

    homsan toft Guest

    Martin Eisenberg wrote:

    > I have a class that holds a sequence of data items. Modifying those
    > items is the sole domain of the class itself. However, the user has
    > read access to the data, e.g. to gather statistics. This access is
    > via an iterator range. While there is one unrelated place I might add
    > a write hook later, I'm positive that write access through the range
    > interface will never make sense. Given that, can I call the iterator
    > type just "iterator" for convenience or should it be "const_iterator"
    > for conceptual accuracy? What do you think?



    You should have both, and typedef them to the same type.
    Then you should document the behaviour of course.
    I'd guess there's not a strong expectation that Type::iterator
    must be mutable (think set::iterator, or a container passed by
    const reference to some function), but users would expect
    both declarations to compile:
    YourClass::const_iterator it = a.begin();
    YourClass::iterator it2 = b.begin();


    homsan
    homsan toft, Dec 17, 2005
    #6
  7. Mateusz Loskot, Dec 17, 2005
    #7
  8. homsan toft wrote:

    > Martin Eisenberg wrote:
    >
    >> Given that, can I call the iterator type just "iterator" for
    >> convenience or should it be "const_iterator" for conceptual
    >> accuracy? What do you think?

    >
    > You should have both, and typedef them to the same type.


    That's interesting, thanks!


    Martin

    --
    Quidquid latine scriptum sit, altum viditur.
    Martin Eisenberg, Dec 17, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. C

    Strong Naming

    C, Nov 5, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    392
    Steve C. Orr [MVP, MCSD]
    Nov 6, 2003
  2. Manuel Lopez

    Strong Naming an ASP.Net Project

    Manuel Lopez, Dec 1, 2003, in forum: ASP .Net
    Replies:
    6
    Views:
    1,394
    Chris Jackson
    Dec 2, 2003
  3. Replies:
    2
    Views:
    6,472
  4. Harman
    Replies:
    1
    Views:
    2,494
    Moiristo
    Jul 28, 2006
  5. Author #1
    Replies:
    8
    Views:
    321
    Registered User
    Aug 12, 2009
Loading...

Share This Page