The easiest and best way to fix errors in C++ is to avoid introducing them into your programs in the first place. Part of this is just a matter of experience, but adopting a clear and consistent programming style helps.
Humans have a limited amount of CPU power between their ears. You need to direct your CPU cycles towards creating a working program. You shouldn’t be distracted by things like indentation.
This makes it important to be consistent in how you name your variables, where you put the opening and closing parentheses, how much they are indented, etc. This is called your coding style. Develop a style and stick to it.
After a while, your coding style becomes second nature. You’ll find that you can code your programs in less time – and can read the resulting programs with less effort – if your coding style is clear and consistent. This translates to fewer coding errors.
When you’re working on a program with multiple programmers, it’s also important that you all use the same style to avoid the Tower of Babel effect with conflicting and confusing methods. Every project needs a coding guide that explains (sometimes in painful detail) exactly how the if statement will be placed, how much the case should be indented, and whether a blank line should be placed after the break statements, to name a few.
C++ does not care about indentation. All distances are the same for her. The indentation is there to make it easier to read and understand the resulting program.
Create variable naming conventions
There is more debate about naming the variants than there is about how many angels fit into a pinhead. Use the following rules when naming variables:
- The first letter is lowercase and indicates the type of the variable. n for int, c for char, b for bool. This is very useful when using a variable because you know its type right away.
- Variable names are descriptive. There are no variables with ambiguous names like x or y. You need something that you can recognize when you try to read your own show tomorrow, next week, or next year.
- Multiple word names use capital letters at the beginning of each word with no underscores between the words.