peter@mit-amt.MEDIA.MIT.EDU (Peter Schroeder) (09/23/89)
Is it desirable from an efficiency point of view to use the const modifier wherever applicable? I find myself hardly using it, although many (most?) of my functions do not change any of their arguments. I am wondering whether the compiler (cfront) will actually generate "better" code if I give it these hints. I understand the implications of its use as far as protecting myself, but mostly functions are so small that I don't usually screw myself in this way (type checking). On the other hand it is a nice hint to see a declaration such as form(const char* ...) which tells me that a certain argument will not be changed by the function.... Any advice/thoughts on this matter? Peter peter@media-lab.media.mit.edu
marc@dumbcat.UUCP (Marco S Hyman) (09/23/89)
In article <740@mit-amt.MEDIA.MIT.EDU> peter@mit-amt.MEDIA.MIT.EDU (Peter Schroeder) writes:
Is it desirable from an efficiency point of view to use the const modifier
wherever applicable?
It's good for programmer efficiency. Eight months after you've written the
code will you remember which operations modify the state of an object?
Which parameters are modified on operaiton calls? If const is used in the
class definitmon you won't have to take the time to go digging thru the
code. IMHO it helps make code reusable with a smaller learning curve.
--marc
--
// Marco S. Hyman {ames,pyramid,sun}!pacbell!dumbcat!marc
jima@hplsla.HP.COM (Jim Adcock) (09/27/89)
> Is it desirable from an efficiency point of view to use the const modifier > wherever applicable? > > I find myself hardly using it, although many (most?) of my functions do not > change any of their arguments. I am wondering whether the compiler (cfront) > will actually generate "better" code if I give it these hints. > > I understand the implications of its use as far as protecting myself, but > mostly functions are so small that I don't usually screw myself in this > way (type checking). On the other hand it is a nice hint to see a > declaration such as form(const char* ...) which tells me that a certain > argument will not be changed by the function.... Const is important, and you need to learn to use it. When first exposed to 2.0 compatible compilers, const seems real strange, but after a while it starts to become natural. Without declaring functions "const" you force the compiler to introduce many "unnecessary" temporary variables. At least on AT&T's compiler you can see this by turning on the warning option. The const'ness of an object or a function is a very important attribute.