Wednesday, 27 May 2015

NINE COMMON MISTAKES THAT PROGRAMMERS MAKE

As a new programmer, every single aspect of this act may seem difficult. And we won’t lie, it

maybe is. However, many of your problems can be solved if you just avoid committing these 9

deadly sins that hold up your entire program from being executed. These common mistakes, if

avoided, can make programming for a beginner much easier.

1. Undeclared Variables

Probably the biggest mistake of new programmers is using a variable in your coding that

hasn’t been declared anywhere. If your compiler doesn’t know what x means, it is bound

to throw an error when it comes across this alien symbol.

2. Uninitialized Variables

Ensure that you have initialized every variable that you use, because in C++ no variable

is automatically initialized to zero. Hence, an uninitialized variable could be of any value

in the range of int.

3. Setting a Variable to an Uninitialized Value

C++ unfortunately has a very simple mind. Assignment of a variable (x) to equal the

result of an operation (like addition) on other variables does not mean that whenever

those variables change, the value of the variable x will also change. Rather, once you

assign a value to a variable, this value will remain in place until a new value is

reassigned. The solution is to set every variable to a value that has been initialized, and

not simply declared in the beginning.

4. Undeclared Functions

It’s super important to put in place either a prototype for the function that you need or the

entire definition of the function before the first time that it is used in the code. Otherwise,

the compiler won’t know what to do with the function menu() if it hasn’t been declared

earlier.

5. Using a single equal sign to check equality.

Doesn’t this happen every single time? All you need to have is a presence of mind while

programming. Make sure that you always use a double equal sign (==) to check equality.

Otherwise, what a single equal sign (=) does is that it assigns the value on the right side

of the expression to the variable on the left hand side.

For example, in a while loop, an x=y means that the value of x is now Y, while an x==y

means that the loop will check whether the value of x is equal to the value of Y or not and

proceed accordingly.

6. Extra Semicolons 

It’s easy to press the semicolon button twice by mistake at one place or end a line of code

with an unneeded semicolon at another. For example, in a while loop, your code may say

“while(x==y);” You must remember that there can’t be semicolons after if statements,

loops or function definitions.

7. Overstepping Array Boundaries

The rule about arrays is that they begin their indexing at 0 and end at length-1. A 10

element array will start at 0 and end at 9. New programmers may tend to forget this and

attempt to end their arrays with the length.

8. Forgetting to Put a Break in a Switch Statement

C++ doesn’t break out of a switch statement, unless a case is encountered. To avoid

errors put a break, to break out of the switch.

9. Integer Division

Most of the binary operations in C++ need both the operands to be of the same type. In

case the types are different, one operands is promoted to match the type of the other. This

can be an issue sometimes and the value of such operations may change as a result.

Using Hungarian Notation can help prevent the confusion for the programmer. A

//integer division or //floating point division can make life much simpler.

No comments:

Post a Comment