7 Common Mistakes That C Programmers Often Make

I have had the opportunity to teach C among young developers, and I have noticed that certain errors are repeated in almost every one of them. It is mainly about mistakes that, once you notice, you do not commit them again because they are easy to remember. However, without a developer is not aware of them, it can cause many problems in the efficiency of the application and in the quality of the developer software. That is why I have decided to compile the 7 most frequent mistakes of the C programmers.

 

Concatenate strings instead of using StringBuilder

 

The concatenation of strings works in such a way that every time something is added to a string, a new address is being allocated in the memory. The previous string is copied to a new location with the part you just added. This is inefficient. Remember that there is a StringBuilder, which maintains the same position in the memory without performing the copy operation. Thanks to the strings added by means of StringBuilder the process is much more efficient, especially in case you have hundreds of aggregation operations.

 

LINQ – Where with First instead of FirstOrDefault

 

Many programmers find a certain set of elements by means of ‘Where‘ and then return to the first occurrence. This is inappropriate because of the ‘First‘ method can also apply the ‘Where’ condition. It should never be taken for granted that value will always be found. If “First” is used when the value is not found, an error occurs. Therefore, it is better to use FirstOrDefault. When using FirstOrDefault, if no value has been found, the default value of this type will be returned and no exception will be thrown.

 

Casting by means of (T) instead of ace (T) when it is possibly not castable

 

It is very common for software developers to use casting (T), instead of ace (T). And in general, it has no negative influence because the casted objects are always castable. However, if there is even a slight chance that an object can be non-castable, casting as (T) should be used.

 

Do not use mappers to rewrite properties

 

There are a lot of very powerful C # mappers (for example AutoMapper). If a few lines of code simply connect rewritten properties, it is undoubtedly a place for a mapper. Even if some properties are not copied directly, but some additional logic is carried out, using a mapper is still a good option

Relaunch incorrect exceptions

 

C # programmers often forget that when they throw an exception using throw ex they lose track. This is when it becomes more difficult to debug an application and achieve the appropriate log messages. When simply using throw data is not lost and all the exception, along with the tracking, can be easily recovered.

 

Use foreach instead of for anything

 

Remember that if you want to iterate through everything that is not a collection (for example, an array), using the for loop is much more efficient than using the ‘foreach‘ loop

Recover or save data in the database in more than 1 call

 

This is a very common error, especially among junior developers and especially when you use an ORM as an Entity Framework or NHibernate. Each call to the DB consumes a certain amount of time, so it is crucial to reduce the number of calls to the DB as much as possible. There are many ways to do this, you just have to inform yourself.