The Ten Commandments for C++ Programmers

July 22nd, 2010 by Kevin | 2 Comments | Filed in programming

From the book “Practical C++ Programming” by Steve Oualline, The Ten Commandments for C++ Programmers written by Phin Straite.

1. Thou shalt not rely on the compiler default methods for construction, destruction, copy construction, or assignment for any but the simplest of classes. Thou shalt forget these “big four” methods for any nontrivial class.

2. Thou shalt declare and define thy destructor as virtual such that others may become heir to the fruits of your labors.

3. Thou shalt not violate the “is-a” rule by abusing the inheritance mechanism for thine own twisted perversions.

4. Thou shalt not rely on any implementation-dependent behavior of a compiler, operating system, or hardware environment, lest thy code be forever caged within that dungeon.

5. Thou shalt not augment the interface of a class at the lowest level without most prudent deliberation. Such ill-begotten practices imprison thy clients unjustly into thy classes and create unrest when code maintenance and extension are required.

6. Thou shalt restrict thy friendship to truly worthy contemporaries. Beware, for thou art exposing thyself rudely as from a trenchcoat.

7. Thou shalt not abuse thy implementation data by making it public or static except in the rarest of circumstances. Thy data are thine own; share it not with others.

8. Thou shalt not suffer dangling pointers or references to be harbored within thy objects. They are nefarious and precarious agents of random and wanton destruction.

9. Thou shalt make use of available class libraries as conscientiously as possible. Code reuse, not just thine own but that of thy clients as well, is the Holy Grail of OO.

10. Thou shalt forever forswear the use of the vile printf/scanf, rather favoring the flowing streams. Cast off thy vile C cloak and partake of the wondrous fruit of flexible and extensible I/O.

 

Related Posts:

Tags:

The History Of Carriage Return Line Feed

July 18th, 2010 by Kevin | No Comments | Filed in programming

Most of us developers (especially Windows developers) wonder why an end-of-line is represented by <carriage return><line feed> in Windows text files. What does <carriage return> or <line feed> exactly mean?

In the early stages before computers, there existed a device known as the TeleType. The TeleType contained a keyboard, printer, and paper tape reader/punch. It could transmit messages over telephones using a modem at a rate of 10 characters per second.

TeleType

But TeleType had a problem. It took 0.2 seconds to move the printhead from the right side to the left. 0.2 seconds is two character times. If a second character came while the printhead was in the middle of a return, that character was lost.

The TeleType people solved this problem by making end-of-line two characters: <carriage return> to position the printhead at the left margin, and <line feed> to move the paper up one line. That way the <line feed> “printed” whilethe printhead was racing back to the left margin.

When the early computers came out, some designers realized that using two characters for end-of-line wasted storage. Some picked <line feed> for their end-of-line, and some chose <carriage return>. Some of the die-hards stayed with the two-character sequence.

Unix uses <line feed> for end-of-line. The newline character \n is code 0xA (LF or <line feed>). MS-DOS/Windows uses the two characters <carriage return><line feed>. So if you are transferring text files from a Unix machine to Windows, you have to use some conversion utility eg. unix2dos for stripping the <carriage return> character from the files.

Further reading:
How do I convert between Unix and Windows text files?
Newline Character

 

Related Posts:

Tags:

C++ Pitfall: Beware of Dangling References

July 11th, 2010 by Kevin | No Comments | Filed in programming

One of the situations where C++ can trip beginner programmers is with the unconscious use of dangling references. A dangling reference is a situation where you end up with a reference to a variable that does not exist in memory any longer.

Consider the example below,

const int& min(const int& i1, const int& i2)
{
   if (i1 < i2)
   return (i1);
   return (i2);
}

int main()
{
   const int& i = min(1+2, 3+4);
   return (0);
}

Before the function min is called, a temporary variable is created to hold the value of the expression 1 + 2. A reference to this temporary variable is passed to the min function as the parameter i1. Similarly, another temporary variable is created for the parameter i2.

After the function min is executed, it returns a reference to i1. But this is a temporary variable that C++ created in main. Temporary variables are destroyed by C++ at the end of the statement where they were created. This means that in the above example, i points to something that does not exist. This is called a dangling reference and can cause problems that may be difficult to detect especially for beginner programmers. So C++ programmers beware of such dangling references.

 

Related Posts:

Tags: