Top.Mail.Ru
The C++ Community's Journal -- Day
? ?
The C++ Community's Journal -- Day [entries|friends|calendar]
The C++ Community

[ userinfo | livejournal userinfo ]
[ calendar | livejournal calendar ]

some C++ commandments [15 Jan 2003|08:23pm]
[ mood | Image amused ]

Something I was inspired to write up at work today, after dealing with all of it from my co-workers for eight months.

1. Thou shalt NOT use pointers unless thou havest a damned good reason for doing so. Avoid ye the new and the delete, for unless thou know whatst thou doest, they shall lead thee or thy unfortunate kin to many hours of hell in the Debugger, with much gnashing of teeth and needless rewriting of code. The reasons for this are many and varied, and if thou needst ask what they are, thou knowest not enough to be using them. So don't!
1a. For Stroustrup created the Reference, and it was very good. Thou mayst use it to thine advantage instead of pointers in many cases.
1b. Verily, the stack has been given unto thee to do with as thou pleasest. Create thine variables there, and lettest the compiler be thine worthy slave, constructing and destructing objects as Stroustrup intended, leaving thine mind free for bigger and better things, like thy actual program logic. Note that thou mayst delay thine variable declarations until thou needst them, and may thus avoid the overhead of constructing unused objects. Also note that stack variables shall prevent memory leaks caused by exceptions, as destructors for variables on the stack will be properly called, should the Devil interfere with thine holy work and cause thy program to throw.
1c. Since thou shalt not use pointers without reason, there is little need for CObList and other lists of pointers. Givest thou preference to typed template-based containers like CList and those in the STL. Note also that thou needst not handle the deletion of objects in these containers thyself, thereby easily keeping the dreaded Memory Leak at bay.

2. Avail thyself of that angel beknown to us as SourceSafe integration, and thine immortal binary soul shall be saved from the following sins:
2a. Do not recklessly pour files into SourceSafe. Yea, there is an oasis of information on this topic available at http://support.microsoft.com/default.aspx?scid=KB;EN-US;Q156513& to help thee. Learnest thou what the different files are and which needst be put in source control, and thy brothers and sisters shall thank thee for it. Better yet, use thou SourceSafe integration and worry thyself about it not. And no, thou dost NOT needst the .clw (ClassWizard) file in SourceSafe!
2b. Thou shalt check out only those files in thy project that thou actually needst to change. SourceSafe integration can make this as simple for thee as causing the Sun to rise in the East each morning. (Coders in northern Alaska, take thou this metaphor with a grain of salt.)
2c. Check in thy work. Daily checkin is wise, for if thou screwest up and needst to back up to thine code of yesterday, it shall be awaiting thee anxiously with open bits. SourceSafe integration can make this happen automatically when thou close Visual Studio. Also remember to check in everything before thou leavest for vacation!

3. Share with thy brothers and sisters. Be not greedy and keep thine own copies of classes--code is easily shared in SourceSafe, and by doing so bugfixes may also be shared by all. When sharing something in SourceSafe with thine own project, use SourceSafe integration to do it INSTEAD of doing it directly in SourceSafe. This will both share it in the SourceSafe database AND add it to thy project, making thine life and that of thy co-workers as simple and pleasant as a walk through Paradise.

4. Avoid thou the temptation to cut and paste. For when thee or thy brother or sister who wrote the code finds a bug, as is the way of this unfathomable world, thou shall likely find thyselves out of sync and shall beat thy breasts in sorrow. For Stroustrup created the Class, and it was good, so USE it, and SHARE it with thy brothers and sisters. As the tree-huggers say, so should the coders do: Reduce, Reuse, Recycle.

5. Comment thy code as thou writest it, when everything in fresh in thy mind. Thou shalt thank thyself for it later, as will thy co-workers. Note that thou needst not comment every line, nor anything that is self-explanatory. In fact, self-explanatory code is a virtue thou shouldst strive to achieve, especially if thou art the sort of coder who habitually forgets or ignores this commandment...!

6. If programming in C++, thou shalt read Effective C++ by Scott Meyers and will thereby become enlightened.
29 comments|post comment

navigation
[ viewing | January 15th, 2003 ]
[ go | previous day|next day ]
Image