Code Improvement Techniques In C#

Coverage Topic
  • string.empty vs. double quotes (“”)
  • string.Equals vs. ==
  • string.Intern
  • Strings vs. StringBuilder
  • When to choose StringBuilder
  • String concatenation vs. string.Format
  • Advantages of string.Format
  • Case insensitive comparisons
  • Out vs. return
  • Remove un-necessary Blank Lines
  • Linq - First vs. FirstOrDefault
  • When to use FirstOrDefault
  • Single vs. SingleOrDefault
  • Performance: FirstOrDefault vs. SingleOrDefault
  • IDisposable.Dispose vs. Using Block
Let's Drill Down the Basic Concept

It's best to follow some guidelines because:
  • It will remove extra headaches.
  • It will improve code readability.
  • Better communication programmer to programmer.
string.empty vs. double quotes (“”)
  • string.empty improves readabilty than double quotes (“”).



string.Equals vs. ==

string.Equals and “==” are same for primitive data types or value types (int, double, char, bool);Because it is content base comparison.

  • “==” compares object references; calls at compile time;
  • ‘string.Equals’ compares values; calls at runtime;
string.Equals improves readabilty than == 

 string.Intern



If we need to copy the same value of the string again and again, then better to use string.Intern to improve the performance.

Strings vs. StringBuilder

  • The term immutable means: The data value may not be changed.

The values of the variable may be changed, but the original immutable values will be discarded and a new data value will be created in memory.

  • Strings are immutable and StringBuilder is mutable.
  • If you need large amount of string manipulation, use StringBuilder.
  • Strings are immutable. So, if you change your strings, then every time a new instance will be created into the memory.

When to choose StringBuilder

If you need more than THREE times string manipulating, use StringBuilder.

String concatenation vs. string.Format

  • Do use AppendFormat for StringBuilder.
  • Do use String.format for string.

Advantages

  • Improve readability.
  • Improve performance.
  • Easy to modify.
Case insensitive comparisons



Out vs. return



The method that use return is faster than out.

Remove Un-necessary Blank Lines



Better to remove unnecessary blank lines and white spaces.
  • It improves code readability.
Linq - First vs. FirstOrDefault



First

There is one or more than one element in the result, but you need the first one and empty is not okay. It throws an exception, if list is empty;

FirstOrDefault

There is one or more than one element in the result, but you need first one or empty is okay. It throws an exception, if source-list is null.

When to use FirstOrDefault

  • When more than one results are expected and you need the first record.
  • Or if there is no result; but you want default value.
  • No exception, if result is empty.
Single vs. SingleOrDefault
  • single - If you need only 1 element(not more than 1 or zero).
  • SingleOrDefault - If you need only 1 or zero element(not more than 1).

Performance - FirstOrDefault vs. SingleOrDefault

FirstOrDefault is faster than SingleOrDefault.

IDisposable.Dispose vs. Using Block

Better to use 'Using block'. It calls dispose, although there could have an exception or return statement.

Magic Value



Don't use magic value (hard coded string, number) directly in your code block. Place all of the hard coded values into one separate class or at least on top of the class. It will improve code manageability.

Still I want to say more but I'm used to caffeine-driven development. I have already finished my coffee. So, no more words. If you maintain good practices, you are ready to move on to the next journey. Don't let yourself down.


Recommended Free Ebook
Similar Articles