Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. Clever Code
  4. subtle bug magnet --> std::vector's clear() and empty()

subtle bug magnet --> std::vector's clear() and empty()

Scheduled Pinned Locked Moved Clever Code
c++comgraphicsadobehelp
22 Posts 15 Posters 10 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    Warren Stevens
    wrote on last edited by
    #1

    Bring me the head of the person who decided on the names clear() and empty() for std::vector ! :mad: The two functions work as they are documented, but having such confusing names is a recipe for creating bugs - I'm sure I'm not the only person who has written empty() when I should have written clear() (i.e. when I'm trying to get rid of the contents of the vector - but instead just return a bool into thin air) According to the dictionary: empty[^] can be both an adjective "2. vacant; unoccupied: an empty house." and a verb "12. to make empty; deprive of contents; discharge the contents of: to empty a bucket" so if you were reading some unknown code and saw a function ABC.empty() you would be perfectly correct to interpret the meaning as either a test or an action, which is why this is a bad choice for a function name. What should the STL have done? used MakeEmpty() and IsEmpty() or at least Empty() and IsEmpty() like the CString designers did. Rant over.:sigh:


    www.IconsReview.com[^] Huge list of stock icon collections (both free and commercial)

    R C G M A 8 Replies Last reply
    0
    • W Warren Stevens

      Bring me the head of the person who decided on the names clear() and empty() for std::vector ! :mad: The two functions work as they are documented, but having such confusing names is a recipe for creating bugs - I'm sure I'm not the only person who has written empty() when I should have written clear() (i.e. when I'm trying to get rid of the contents of the vector - but instead just return a bool into thin air) According to the dictionary: empty[^] can be both an adjective "2. vacant; unoccupied: an empty house." and a verb "12. to make empty; deprive of contents; discharge the contents of: to empty a bucket" so if you were reading some unknown code and saw a function ABC.empty() you would be perfectly correct to interpret the meaning as either a test or an action, which is why this is a bad choice for a function name. What should the STL have done? used MakeEmpty() and IsEmpty() or at least Empty() and IsEmpty() like the CString designers did. Rant over.:sigh:


      www.IconsReview.com[^] Huge list of stock icon collections (both free and commercial)

      R Offline
      R Offline
      Rama Krishna Vavilala
      wrote on last edited by
      #2

      I got confused first time but that was before reading the docs. I will not call it a subtle bug though


      Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -Brian Kernighan

      W P 2 Replies Last reply
      0
      • R Rama Krishna Vavilala

        I got confused first time but that was before reading the docs. I will not call it a subtle bug though


        Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -Brian Kernighan

        W Offline
        W Offline
        Warren Stevens
        wrote on last edited by
        #3

        Rama Krishna Vavilala wrote:

        I will not call it a subtle bug though

        It's not a bug in itself, but it's a fertile ground for writing your own (e.g. clearing out a vector that is usually, but not always, empty). It lets you write code that reads like it will work, and might not be noticed all of the time - which is my definition of a "subtle bug" (but I can understand if you disagree). Warren


        www.IconsReview.com[^] Huge list of stock icon collections (both free and commercial)

        1 Reply Last reply
        0
        • W Warren Stevens

          Bring me the head of the person who decided on the names clear() and empty() for std::vector ! :mad: The two functions work as they are documented, but having such confusing names is a recipe for creating bugs - I'm sure I'm not the only person who has written empty() when I should have written clear() (i.e. when I'm trying to get rid of the contents of the vector - but instead just return a bool into thin air) According to the dictionary: empty[^] can be both an adjective "2. vacant; unoccupied: an empty house." and a verb "12. to make empty; deprive of contents; discharge the contents of: to empty a bucket" so if you were reading some unknown code and saw a function ABC.empty() you would be perfectly correct to interpret the meaning as either a test or an action, which is why this is a bad choice for a function name. What should the STL have done? used MakeEmpty() and IsEmpty() or at least Empty() and IsEmpty() like the CString designers did. Rant over.:sigh:


          www.IconsReview.com[^] Huge list of stock icon collections (both free and commercial)

          C Offline
          C Offline
          Chris Meech
          wrote on last edited by
          #4

          Are you saying that Plain English ® has failed you? Heretic, I say. ;P

          Chris Meech I am Canadian. [heard in a local bar] I agree with you that my argument is useless. [Red Stateler] Hey, I am part of a special bread, we are called smart people [Captain See Sharp] The zen of the soapbox is hard to attain...[Jörgen Sigvardsson] I wish I could remember what it was like to only have a short term memory.[David Kentley]

          W 1 Reply Last reply
          0
          • C Chris Meech

            Are you saying that Plain English ® has failed you? Heretic, I say. ;P

            Chris Meech I am Canadian. [heard in a local bar] I agree with you that my argument is useless. [Red Stateler] Hey, I am part of a special bread, we are called smart people [Captain See Sharp] The zen of the soapbox is hard to attain...[Jörgen Sigvardsson] I wish I could remember what it was like to only have a short term memory.[David Kentley]

            W Offline
            W Offline
            Warren Stevens
            wrote on last edited by
            #5

            Yeah, on second thought this C++ thing isn't working out for me. I guess I'll start porting everything to Plain English after lunch... :-D


            www.IconsReview.com[^] Huge list of stock icon collections (both free and commercial)

            1 1 Reply Last reply
            0
            • W Warren Stevens

              Bring me the head of the person who decided on the names clear() and empty() for std::vector ! :mad: The two functions work as they are documented, but having such confusing names is a recipe for creating bugs - I'm sure I'm not the only person who has written empty() when I should have written clear() (i.e. when I'm trying to get rid of the contents of the vector - but instead just return a bool into thin air) According to the dictionary: empty[^] can be both an adjective "2. vacant; unoccupied: an empty house." and a verb "12. to make empty; deprive of contents; discharge the contents of: to empty a bucket" so if you were reading some unknown code and saw a function ABC.empty() you would be perfectly correct to interpret the meaning as either a test or an action, which is why this is a bad choice for a function name. What should the STL have done? used MakeEmpty() and IsEmpty() or at least Empty() and IsEmpty() like the CString designers did. Rant over.:sigh:


              www.IconsReview.com[^] Huge list of stock icon collections (both free and commercial)

              G Offline
              G Offline
              Guffa
              wrote on last edited by
              #6

              Reminds me of a thread somewhere here not long ago, when someone was asking what properties was good for. This is one thing that properties is good for. If it was a .NET class (for example), Empty would have been a (read-only) property instead of a method (and also named IsEmpty instead). Being a property, one would not expect it to change the data in the class. But, as some people in the thread argued, as the properties are just another way of writing getters and setters, we could just as well have a getter for the value, named get_empty(). Hmm... would it be logical that a method by that name would return the status of the emptiness of the class, or would it be more logical that it would factor a new empty vector? ;)

              --- b { font-weight: normal; }

              W 1 Reply Last reply
              0
              • W Warren Stevens

                Yeah, on second thought this C++ thing isn't working out for me. I guess I'll start porting everything to Plain English after lunch... :-D


                www.IconsReview.com[^] Huge list of stock icon collections (both free and commercial)

                1 Offline
                1 Offline
                123 0
                wrote on last edited by
                #7

                Warren Stevens wrote:

                I guess I'll start porting everything to Plain English after lunch...

                Where you can say:

                Clear the desk.
                Empty the desk.
                If the desk is empty...
                If the desk is clear...

                and a variety of similar things that will all work in the obvious (and intended way).

                W T A 3 Replies Last reply
                0
                • 1 123 0

                  Warren Stevens wrote:

                  I guess I'll start porting everything to Plain English after lunch...

                  Where you can say:

                  Clear the desk.
                  Empty the desk.
                  If the desk is empty...
                  If the desk is clear...

                  and a variety of similar things that will all work in the obvious (and intended way).

                  W Offline
                  W Offline
                  Warren Stevens
                  wrote on last edited by
                  #8

                  Hey, if it really works as well as that, then you'll have a lot of converts in the end.


                  www.IconsReview.com[^] Huge list of stock icon collections (both free and commercial)

                  1 Reply Last reply
                  0
                  • G Guffa

                    Reminds me of a thread somewhere here not long ago, when someone was asking what properties was good for. This is one thing that properties is good for. If it was a .NET class (for example), Empty would have been a (read-only) property instead of a method (and also named IsEmpty instead). Being a property, one would not expect it to change the data in the class. But, as some people in the thread argued, as the properties are just another way of writing getters and setters, we could just as well have a getter for the value, named get_empty(). Hmm... would it be logical that a method by that name would return the status of the emptiness of the class, or would it be more logical that it would factor a new empty vector? ;)

                    --- b { font-weight: normal; }

                    W Offline
                    W Offline
                    Warren Stevens
                    wrote on last edited by
                    #9

                    Guffa wrote:

                    someone was asking what properties was good for.

                    Properties rock! Since I usually write in c++, I'm really jealous of the properties in C#, and a bunch of other things too (like being able to write everything in one file, instead of having to maintain function names and parameters in two places (.h and .cpp)).


                    www.IconsReview.com[^] Huge list of stock icon collections (both free and commercial)

                    1 Z 2 Replies Last reply
                    0
                    • W Warren Stevens

                      Guffa wrote:

                      someone was asking what properties was good for.

                      Properties rock! Since I usually write in c++, I'm really jealous of the properties in C#, and a bunch of other things too (like being able to write everything in one file, instead of having to maintain function names and parameters in two places (.h and .cpp)).


                      www.IconsReview.com[^] Huge list of stock icon collections (both free and commercial)

                      1 Offline
                      1 Offline
                      123 0
                      wrote on last edited by
                      #10

                      Warren Stevens wrote:

                      I'm really jealous of the properties in C#, and a bunch of other things too (like being able to write everything in one file, instead of having to maintain function names and parameters in two places (.h and .cpp)).

                      Yeah, that is a bit primitive for the 21st century. How does "no headers at all and the sequence in which you define things doesn't matter" sound to you?

                      1 Reply Last reply
                      0
                      • W Warren Stevens

                        Bring me the head of the person who decided on the names clear() and empty() for std::vector ! :mad: The two functions work as they are documented, but having such confusing names is a recipe for creating bugs - I'm sure I'm not the only person who has written empty() when I should have written clear() (i.e. when I'm trying to get rid of the contents of the vector - but instead just return a bool into thin air) According to the dictionary: empty[^] can be both an adjective "2. vacant; unoccupied: an empty house." and a verb "12. to make empty; deprive of contents; discharge the contents of: to empty a bucket" so if you were reading some unknown code and saw a function ABC.empty() you would be perfectly correct to interpret the meaning as either a test or an action, which is why this is a bad choice for a function name. What should the STL have done? used MakeEmpty() and IsEmpty() or at least Empty() and IsEmpty() like the CString designers did. Rant over.:sigh:


                        www.IconsReview.com[^] Huge list of stock icon collections (both free and commercial)

                        M Offline
                        M Offline
                        Michael Dunn
                        wrote on last edited by
                        #11

                        That is a constant annoyance for me, the naming scheme is bad in subtle ways that can trip up newbies. My 2nd most-hated is begin() and end(): methods whose names are verbs but which don't actually do any actions.

                        --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ

                        1 Reply Last reply
                        0
                        • 1 123 0

                          Warren Stevens wrote:

                          I guess I'll start porting everything to Plain English after lunch...

                          Where you can say:

                          Clear the desk.
                          Empty the desk.
                          If the desk is empty...
                          If the desk is clear...

                          and a variety of similar things that will all work in the obvious (and intended way).

                          T Offline
                          T Offline
                          toxcct
                          wrote on last edited by
                          #12

                          perfect idiots never understand when we explain them to stop advertise their Plain english crap :( reported as Abusive ! :suss:


                          Don't know where to start ?
                          Refer the Forums Guidelines and ask a friend

                          [VisualCalc 3.0][Flags Beginner's Guide]

                          P 1 Reply Last reply
                          0
                          • W Warren Stevens

                            Bring me the head of the person who decided on the names clear() and empty() for std::vector ! :mad: The two functions work as they are documented, but having such confusing names is a recipe for creating bugs - I'm sure I'm not the only person who has written empty() when I should have written clear() (i.e. when I'm trying to get rid of the contents of the vector - but instead just return a bool into thin air) According to the dictionary: empty[^] can be both an adjective "2. vacant; unoccupied: an empty house." and a verb "12. to make empty; deprive of contents; discharge the contents of: to empty a bucket" so if you were reading some unknown code and saw a function ABC.empty() you would be perfectly correct to interpret the meaning as either a test or an action, which is why this is a bad choice for a function name. What should the STL have done? used MakeEmpty() and IsEmpty() or at least Empty() and IsEmpty() like the CString designers did. Rant over.:sigh:


                            www.IconsReview.com[^] Huge list of stock icon collections (both free and commercial)

                            A Offline
                            A Offline
                            Antony M Kancidrowski
                            wrote on last edited by
                            #13

                            Ah, ambiguity in programming contexts. Yup, I still have to stop and think about these.

                            Ant. I'm hard, yet soft.
                            I'm coloured, yet clear.
                            I'm fruity and sweet.
                            I'm jelly, what am I? Muse on it further, I shall return!
                            - David Walliams (Little Britain)

                            1 Reply Last reply
                            0
                            • R Rama Krishna Vavilala

                              I got confused first time but that was before reading the docs. I will not call it a subtle bug though


                              Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -Brian Kernighan

                              P Offline
                              P Offline
                              peterchen
                              wrote on last edited by
                              #14

                              Rama Krishna Vavilala wrote:

                              I will not call it a subtle bug though

                              No, but a subtle-bug-source.


                              Developers, Developers, Developers, Developers, Developers, Developers, Velopers, Develprs, Developers!
                              We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP
                              Linkify!|Fold With Us!

                              1 Reply Last reply
                              0
                              • W Warren Stevens

                                Bring me the head of the person who decided on the names clear() and empty() for std::vector ! :mad: The two functions work as they are documented, but having such confusing names is a recipe for creating bugs - I'm sure I'm not the only person who has written empty() when I should have written clear() (i.e. when I'm trying to get rid of the contents of the vector - but instead just return a bool into thin air) According to the dictionary: empty[^] can be both an adjective "2. vacant; unoccupied: an empty house." and a verb "12. to make empty; deprive of contents; discharge the contents of: to empty a bucket" so if you were reading some unknown code and saw a function ABC.empty() you would be perfectly correct to interpret the meaning as either a test or an action, which is why this is a bad choice for a function name. What should the STL have done? used MakeEmpty() and IsEmpty() or at least Empty() and IsEmpty() like the CString designers did. Rant over.:sigh:


                                www.IconsReview.com[^] Huge list of stock icon collections (both free and commercial)

                                N Offline
                                N Offline
                                Nemanja Trifunovic
                                wrote on last edited by
                                #15

                                Warren Stevens wrote:

                                What should the STL have done? used MakeEmpty() and IsEmpty() or at least Empty() and IsEmpty() like the CString designers did.

                                is_empty() if you ask me, but I agree - STL names are very strange.


                                Programming Blog utf8-cpp

                                1 Reply Last reply
                                0
                                • 1 123 0

                                  Warren Stevens wrote:

                                  I guess I'll start porting everything to Plain English after lunch...

                                  Where you can say:

                                  Clear the desk.
                                  Empty the desk.
                                  If the desk is empty...
                                  If the desk is clear...

                                  and a variety of similar things that will all work in the obvious (and intended way).

                                  A Offline
                                  A Offline
                                  Andy Brummer
                                  wrote on last edited by
                                  #16

                                  The Grand Negus wrote:

                                  a

                                  So the first one is if nothing is on the desk, and the second one means that you can see through it? ;P

                                  The pig go. Go is to the fountain. The pig put foot. Grunt. Foot in what? ketchup. The dove fly. Fly is in sky. The dove drop something. The something on the pig. The pig disgusting. The pig rattle. Rattle with dove. The dove angry. The pig leave. The dove produce. Produce is chicken wing. With wing bark. No Quack. - Thedailywtf.com

                                  1 Reply Last reply
                                  0
                                  • T toxcct

                                    perfect idiots never understand when we explain them to stop advertise their Plain english crap :( reported as Abusive ! :suss:


                                    Don't know where to start ?
                                    Refer the Forums Guidelines and ask a friend

                                    [VisualCalc 3.0][Flags Beginner's Guide]

                                    P Offline
                                    P Offline
                                    Paul Conrad
                                    wrote on last edited by
                                    #17

                                    toxcct wrote:

                                    perfect idiots never understand when we explain them to stop advertise their Plain english crap

                                    :laugh:


                                    Some people have a memory and an attention span, you should try them out one day. - Jeremy Falcon

                                    1 Reply Last reply
                                    0
                                    • W Warren Stevens

                                      Bring me the head of the person who decided on the names clear() and empty() for std::vector ! :mad: The two functions work as they are documented, but having such confusing names is a recipe for creating bugs - I'm sure I'm not the only person who has written empty() when I should have written clear() (i.e. when I'm trying to get rid of the contents of the vector - but instead just return a bool into thin air) According to the dictionary: empty[^] can be both an adjective "2. vacant; unoccupied: an empty house." and a verb "12. to make empty; deprive of contents; discharge the contents of: to empty a bucket" so if you were reading some unknown code and saw a function ABC.empty() you would be perfectly correct to interpret the meaning as either a test or an action, which is why this is a bad choice for a function name. What should the STL have done? used MakeEmpty() and IsEmpty() or at least Empty() and IsEmpty() like the CString designers did. Rant over.:sigh:


                                      www.IconsReview.com[^] Huge list of stock icon collections (both free and commercial)

                                      M Offline
                                      M Offline
                                      Monty2
                                      wrote on last edited by
                                      #18

                                      I fell for that recently, not much damange done though :)


                                      OK, so what's the speed of dark?

                                      1 Reply Last reply
                                      0
                                      • W Warren Stevens

                                        Guffa wrote:

                                        someone was asking what properties was good for.

                                        Properties rock! Since I usually write in c++, I'm really jealous of the properties in C#, and a bunch of other things too (like being able to write everything in one file, instead of having to maintain function names and parameters in two places (.h and .cpp)).


                                        www.IconsReview.com[^] Huge list of stock icon collections (both free and commercial)

                                        Z Offline
                                        Z Offline
                                        Zac Howland
                                        wrote on last edited by
                                        #19

                                        Warren Stevens wrote:

                                        like being able to write everything in one file, instead of having to maintain function names and parameters in two places (.h and .cpp)).

                                        You don't HAVE to put things in 2 files ... it is done out of convience for the most part. You CAN put everything for a class in a .h file if you really wanted to.

                                        If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

                                        W 1 Reply Last reply
                                        0
                                        • Z Zac Howland

                                          Warren Stevens wrote:

                                          like being able to write everything in one file, instead of having to maintain function names and parameters in two places (.h and .cpp)).

                                          You don't HAVE to put things in 2 files ... it is done out of convience for the most part. You CAN put everything for a class in a .h file if you really wanted to.

                                          If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

                                          W Offline
                                          W Offline
                                          Warren Stevens
                                          wrote on last edited by
                                          #20

                                          Zac Howland wrote:

                                          You don't HAVE to put things in 2 files ... it is done out of convience for the most part. You CAN put everything for a class in a .h file if you really wanted to.

                                          Only in the most trivial cases. As soon as your project gets to a decent size, you do NOT want to touch a widely used .h file (or it takes forever to compile), which is why the split is needed. Also, if you have two classes that use functions of each other (which isn't that uncommon) the only way to do so is to use a forward declaration and two .h and .cpp files. :)


                                          www.IconsReview.com[^] Huge list of stock icon collections (both free and commercial)

                                          Z 1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups