Code Optimization Technique in C#
Readers of the Topic – Developers
- string.empty vs. double quotes (“”)
- string.Equals vs. ==
- 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
Better to follow some guidelines,
- It will remove extra headache.
- It will improve code readability.
- It will improve code performance.
- Better communication programmer to programmer.
string.empty vs. double quotes (“”)
- Double quotes create an object.
- String.empty doesn’t create an object.
Performance: string.empty is faster than double quotes (
string.Equals vs. ==
==” compares object references; calls at compile time;
- ‘string.Equals’ compares values; calls at runtime;
Performance: string.Equals is faster than
Few More Points:
- string.Equals and “==” are same for primitive data types or value types (int, double, char, bool);Because it is content base comparison.
- String is non-primitive; but string.Equals and “==” are similar for it. It takes content base comparison. If the contents of the two string variables are same, then they are pointing to the same memory location and value.
- If one object or both are null, then string.Equals will throw NullReferenceException; but == will not throw exception.
If we need to copy same value of the string again and again, then better to use string.Intern to improve the performance.
Strings vs. StringBuilder
- The term immutable mean: 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.
- Improve readability.
- Improve performance.
- Easy to modify.
Case insensitive comparisons
Out vs. return
Performance: The method that use return, is faster than out.
Remove Un-necessary Blank Lines
- Better to remove un-necessary 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 first one or empty is not okay. Throws 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 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.
One More Point:
- 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 you down.