6 Most Common Programming Errors

6 Most Common Programming Errors

6 most common programming errors:

1. Missing type information in method signature

2. Overly verbose or cryptic code

3. Using default values instead of explicitly specifying them

4. Not using static typing correctly

5. Misusing exceptions incorrectly

6. Incorrect use of return value optimization

The first mistake is not mentioning the required type information in your method signature. For example, if you have a function like this one:

public void DoSomething(int x) { Console.WriteLine(“You entered number : ” + x); }

6 Most Common Programming Errors from our website

Then, you need to specify the required type in your signature. If you don’t do it then the compiler will complain and throw an exception.

Here is an example of required type information:

public void DoSomething(int x) { Console.WriteLine(“You entered number : ” + x); }

The second mistake is writing overly verbose or cryptic code. This doesn’t mean that you need to write very short methods with a single line of code, but you shouldn’t write a method which contains dozens of lines of code which are very difficult to understand especially when you come back to it after a few months.

For example, look at the following code:

if(!arr.SequenceEqual(arr2, 0, count, arr.Count – 1, true))

This is an example of overly verbose code. There is no need to write so many characters for a simple sequence check.

A better version of the same code can be:

arr.SequenceEqual(arr2, count – 1, 0, arr.Count – 1, true)

The third mistake is using default values instead of explicitly specifying them. Let’s say we have a complex type like this one:

6 Most Common Programming Errors at

public struct ComplexType


public int m_IntValue;

public double m_DoubleValue;

public float m_FloatValue;

public char m_CharValue;

public byte m_ByteValue;


If you want a particular value to be of type int, then the default value for other types (char, float, double etc. ..) will be 0.

Here is an example of this mistake:

6 Most Common Programming Errors at

new ComplexType().m_DoubleValue = 0.0f;

It’s better to always specify the required type like this one:

new ComplexType().m_DoubleValue = (double)0.0;

The fourth mistake is using static typing incorrectly. You should always try to use the more restrictive type.

In most cases, you should use a class instead of a struct. Also, you can use a struct if your struct contains only fields of built-in types (int, double etc. ..) and no custom objects.

The last mistake is using the return value optimization. It means that you can explicitly check if a method succeeded or failed and ignore the result in both cases.

This is an example:

int result = SomeMethod();

if(result == 0)

6 Most Common Programming Errors - Image

Console.WriteLine(“Error occured.”);

Here, SomeMethod can fail, but we are not interested to know if it failed. If you use this approach in your code then it’s possible that if SomeMethod is changed in the future, to return a non-zero value if it succeeds, then you will no longer ignore the result and your code will fail.

Chapter 7:The Most Common Mistakes in C#

In this last chapter about common mistakes, you can read about the most common mistakes concerning C# language.

1. Not using static methods or variables

You should always try to use static variables and methods when possible because they make the code more readable and save you the trouble of creating multiple instances of a class. For example, instead of writing a constructor for your class which initiates all the properties, you should consider writing a static method which will do this job and simply call it from your main class.

2. Not using constants

Using const all the time can make your code more robust and save you from mistakes. For example, instead of writing something like this:

if(DateTime.Now.DayOfWeek == DayOfWeek.Tuesday)

you should write:

6 Most Common Programming Errors |

DayOfWeek today = DayOfWeek.Monday;

if(today == DayOfWeek.Tuesday)

It’s more readable and if you need to modify the code in the future, you won’t accidentally compare the day of week with Tuesday, but with Monday.

3. Not using assertions

If you think that your code must adhere to a certain rules, then you should use an assertion to verify this. For example, let’s say you have a Vector2 class and one of the properties is m_Speed.

You know that the speed of any object in motion is always positive. Because of this, you can add an assertion to the set method of m_Speed like this:

Vector2 V = new Vector2();

V.m_Speed = 100;

V.m_Speed = -100;

6 Most Common Programming Errors at

Visual Studio 2017 will tell you that there is an error in your code and the reason is: Test failed: m_speed should be always positive.

4. Not using string.Empty or “”.PadRight() or .PadLeft()

If you need a string with a specific length you can simply use string.Empty and then use the PadRight or PadLeft methods to fill it with spaces or zeroes.

It’s better than writing a loop and using string.Concat or another method which manipulates the .Length property.

5. Not using the switch statement for enums

The switch statement is in fact faster than using if-else statements for enums. Because of this, you should use the switch statement instead of if-else statements when it’s possible.

6. Not initializing variables

If you don’t initialize a variable explicitly, then C# will initialize it for you, but probably not the way you want. In fact, it will set the type for this variable to ‘Object’ and initialize it as null.

This may cause bugs because you will be using this variable as an integer although it contains a null reference.

7. Relying on default parameters in user code

The default value for a parameter in the method signature is always ‘null’ for reference types and 0 for integral types. This means that if you call a method and this method has parameters with default values, then you can pass null or 0 without specifying it in the call.

But problem arises when the compiler does not know if you are referring to the parameter or the default value. To avoid this, you can use named arguments when calling a method and ignore the default ones completely.

6 Most Common Programming Errors from our website

8. Using a loop counter instead of enumerators

This is something I learned from reading ‘Code Complete’. A lot of people use a variable to count from 1 to 100 and so on.

Although it’s faster to use int.MaxValue, it’s even faster to use the C# Enum methods like Max(), Last(), Next() and so on. Using these methods is equally fast to writing a ‘for’ loop with an integer counter from 1 to 100.

9. Using ‘break’ without ‘goto’

This is just wrong and as result your code will do something else that you didn’t intend to. You should always use ‘goto’ when using ‘break’.

10. Relying on type conversion rules

When you convert from integer to float, the compiler will throw this annoying error if you try to convert a number that won’t fit into the float type: “Possible loss of precision”. This means you should know the type conversion rules to avoid this.

11. Using ‘break’ with ‘switch’

Unlike C and C++, in C# you cannot use ‘break’ with ‘switch’ statements. You can only use it with loops like while, do..while, for and foreach.

This is a limitation of the language and you have to respect it.

What is your opinion on these statements? Did I miss anything?

Leave your thoughts in the comment section.

Sources & references used in this article: