Optimizing Your Code: Are you willing to be a good code writer?

Good developers always try to write a clean/readable code that runs quickly. Writing unnecessary code is undoubtedly bad for work efficiency. However, it’s important to realize different situations needs.

This blog post will give you some tips for writing code that performs well for real world applications using timing tool to find bottlenecks. The following code presents the Stopwatch class, a simple timer to test parts of your code that may have poor performance.

var watch = new System.Diagnostics.Stopwatch();

watch.Start(); //start watch

Thread.Sleep(5000); // the thread won’t be executed for 5 seconds

watch.Stop(); //stop watch

var elapsedS = watch.Elapsed.Seconds;

var elapsedMS = watch.ElapsedMilliseconds;

// Output:

// ElapsedS =  Elapsed in Seconds: 5

// ElapsedMS = Elapsed in Milliseconds: 4999

It’s a good benchmark tool to check the time spent of a particular function in you code, you are finding yourself needing less and less to use it as time goes on.

Properties, Methods, and Classes

You may consider to use a property from inside a class, access field directly if possible and don’t call it properties repeatedly without store the value in a variable.

Consider refactoring your code to new classes if you have long codes or repetition of functions. You should avoid methods with more than 25 lines of code, if you do, try to split it into separate methods.

Do not use ‘whatever names’ for methods, it should be obvious and specific, telling you what it does. Consequently, you don’t need to write comments in all the lines. Well written code needs just a few comments.

You can use conventional adapt for Pascal or Camel casing. With Pascal, the first character of all words is upper case and the other characters are lower case. For Camel casing, the first character of all words, except the first word, is uppercase and other characters are lowercase.

When you are comparing strings, always make sure they are all in lowercase before you compare them. In .NET you will find a method called “String.ToLowerInvariant()”, that convert all the characters  in a string to lowercase.

Use constants for numbers and resource files for strings instead of hardcoding them. Whatever required, numbers or strings to pass them to methods, use “enum” or declare local variables  instead of indicating discrete values.

Objects and Value Types

They use a lot of memory from the heap, and every object created need to be destroyed, and being destroyed takes more time than to be created. Sometimes the object is not destroyed during the garbage collector, so it may affect performance due wasted of memory.

Whenever need to use a Try/Catch, write a friendly message to the user and log the actual error with all possible details such as productId, SKU, date/time, method and class name. Never do a “Catch” exception empty and always get specific exception instead of generic ones.

I hope you enjoyed these little wonders. Hopefully you found one you didn’t know about or had forgotten about and can put it to good use!

Cheers!