Hi there! A few weeks passed by since my last blog post.
Well, the holiday time was on and I wish you all the best in 2020. I hope you all had a great time. But it’s time to move on. So, buckle up your seat belts and let’s move on as we have a lot to cover in 2020.
Before we proceed I would like to inform you that I’m currently doing a lot of work in the background. Soon, I will add more content and I do plan to redesign, completely, my website during 2020.
I want it to become more appealing and more content-rich for you!
So stay tuned for upcoming upgrades.
Let’s see top 10 quick refactoring tips for C#:
Tip 1: CAPITALIZATION
Read only strings should be CAPITALIZED.
Tip 2: IF STATEMENT
When using IF statements always use brackets. It’s easy to get lost if not using brackets.
If somethigIsTrue
doSomethingElse;
vs
If somethigIsTrue {
doSomethingElse;
}
Tip 3: STRING CONCATENATION
When concatenating strings, use String.Concat(“”,””,””);
It’s more code efficient.
If you use “+” you create multiple objects which consumes more memory but when using Concat you create only one object.
Tip 4: CONST vs READONLY
CONST is faster but needs to be recompiled (re-build) if it’s used in some DLL.
READONLY doesn’t need to be recompiled.
Tip 5: IDISPOSABLE USAGE
If class has IDISPOSABLE then ALWAYS use “USING”.
Tip 5: ENUM INITIALIZER
When dealing with ENUMS, always use 0 as invalid or none or unknown.
Tip 6: CONVERTING VALUES (TryParse vs Convert)
When converting values always use TryParse, as it won’t throw an error but will return false if it’s not parseable. This way you avoid writing try – catch block
Tip 7: INHERITANCE
Don’t do excessive inheritance and try to simplify your code.
Make it 1 class = 1 file.
Tip 8: PROPS
Use auto properties.
Tip 9: OVERRIDES
Use virtual to override methods
Tip 10: LAZY LOAD
Disable lazy loading in Entity Framework.
CONCLUSION
These would be fast refactoring tips you should try to implement when the time comes for your code to be refactored.
If you have any other quick tips please, leave a comment.
Thank you.