Prev: Customizing compare in bsearch()
Next: const is an overrated concept that is a source of extra typing and maintenance
From: James Kanze on 24 Mar 2010 15:06 On Mar 24, 7:24 pm, mattb <matthew.b...(a)l-3com.com> wrote: > I have recently heard the above, along with a further > statement along the lines of - > 'const is there to stop amateur slip ups. Professionals > should know what a function is expecting and should use that.' It sounds like the person saying this is implying that professionals never make mistakes. (Certainly, anyone *should* know what a function does before using it.) > Could I please have some comments on these statements. Regardless of anyone's personal opinions on the value of const, it's part of the C++ type system, and you can't avoid it. -- James Kanze -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Thiago A. on 24 Mar 2010 15:18 > 'const is there to stop amateur slip ups. Professionals should know > what a function is expecting and should use that.' > > Could I please have some comments on these statements. Const is a very useful concept. Together with references allow us to write safe, expressive and efficient code. For instance: Vector Normalize(const Vector& v) { const auto len = v.length(); return Vector(v.x / len, v.y / len); } We can read this code as: "Given a vector called v, with his length called len" Without const we would have to read as: "given a variable referring to a vector object, and given a variable "len" with the state of vector length at line 1 ..." If you think well, you will realize two different situations. They are conceptually different. The code is simpler using const. We don't need to think about variables. In that scope v is the vector's name and len is the vector's length doesn't matter when and where. The problem has been simplified using const representing exactly what we want in a safe and efficent way. We also gave tips to the compiler about immutability. (Before someone say that references are unnecessary too, I will remind that the logic in this code would be far different and worst using pointers.) -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Maciej Sobczak on 24 Mar 2010 15:11 On 24 Mar, 20:24, mattb <matthew.b...(a)l-3com.com> wrote: > 'const is there to stop amateur slip ups. Professionals should know > what a function is expecting and should use that.' > > Could I please have some comments on these statements. It is difficult to comment on something that is nothing else but a loose opinion with no references. It is worth to remember that all production bugs were made by professionals. In particular by those who "should know". In other words, people - no matter how much professional - are not perfect and need assistance. Const, like other features of a static type system, assists the professional programmer in his work. If somebody claims that he does not need this feature, ask him for his bug history. -- Maciej Sobczak * http://www.inspirel.com YAMI4 - Messaging Solution for Distributed Systems http://www.inspirel.com/yami4 -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Dan McLeran on 24 Mar 2010 15:19 On Mar 25, 3:24 am, mattb <matthew.b...(a)l-3com.com> wrote: > I have recently heard the above, along with a further statement along > the lines of - > > 'const is there to stop amateur slip ups. Professionals should know > what a function is expecting and should use that.' > > Could I please have some comments on these statements. { edits: quted sig & banner removed. please keep readers in mind when quoting. -mod } i think it's foolish to avoid using some compiler feature meant to protect you from yourself. any programmer who thinks their too smart for that kind of protection is someone i would not want writing my code. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Chris Uzdavinis on 24 Mar 2010 15:18
On Mar 24, 3:24 pm, mattb <matthew.b...(a)l-3com.com> wrote: > I have recently heard the above, along with a further statement along > the lines of - > > 'const is there to stop amateur slip ups. Professionals should know > what a function is expecting and should use that.' > > Could I please have some comments on these statements. Hardhats are an overrated concept that is a source of neck pain, heat, and needless rubbing of hair against the scalp. Hardhats are for amateur construction workers who haven't learned how to avoid getting hit over the head with falling objects. Professionals should know where not to stand. (After all, *most* of the time you really don't need one...) Chris -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |