5 Tips For Programming With C++

Faizan Ahmad
By -
C++ has been one of the best languages to program in for years. This is a relatively easy programming language to learn and if you know how to code in the C language you can quickly learn C++.

Here are five tips to help your programming and make coding just a little bit easier for you.

1. Don’t Confuse Assign (=) with Test-for-Equality (==)

The first tip might seem like a simple one but it is something that a lot of people get wrong when they start coding with C++.

If you make this mistake you can end up with an error that is very hard to find so it’s important to get in the habit of using the right operators from the start.

So keep in mind that in C++ just like in the C language a = b isn’t a test for equality. You need to use == for testing equality.

2. Use <iostream> and <iostream.h> The Right Way

A lot of C++ programmerstend to use the older <iostream.h> version but you should always use the new <iostream>. This is because the old version was deprecated and using deprecated features in your code can create unpredictable errors.

There are plenty of subtle changes that have been made to the library and therefore it is important to use the right version. There is a good guide to the differences at the Gamedev.net that you can read to understand more about the change.

3. Don’t Use Magic Numbers

There are certain numbers that you may need to use to identify a file format sometimes or it may just be the number of rows and columns of a structure. These are called magic numbers and you need to avoid using them.

It might feel convenient to use them at the start but in the long run it’s always better to use a variable in case you have to change that number at some point, for example.

Programming With C++
Image Licensed Under Attribution

Therefore always use a variable like max_rows when you code instead of just using a number like 42. A lot of the reason why the use of these magic numbers still persists is that in the old days programming was all about raw bit patterns.

4. Try To Use Local Variables

There is really only one time when you should use global variables and that is when you are communicating between functions. For example, if you are sharing some information between functions you would have to use a global variable instead of a local one. Another option is to share the information through a parameter.

The key is to try and opt for using local variable as much as you can. This is to prevent the global variables from interfering with the internal actions of other functions.

5. Don’t Overuse Classes And Objects

When you start learning C++ a lot of people will tell you to make every data structure into a class. They then tell you to make each function a class member as well and this is largely due to the object-oriented programming style that started in the 1990s.

But things are changing a bit now and if you are going for short console-output oriented programs, such as apps, you don’t need to use this functionality to this extent.
It is much better to just focus on learning the Standard Template Library (STL) because it will help simplify a lot of your programming tasks.

    Dan

About the Author:

Dan is passionate about programming and he likes to find tips to help C++ developers get started. When he isn’t reading about coding he likes to spend time playing MMORPGs with his friends.