Telerik blogs

I am a huge proponent of writing quality code, a view that is shared by many of my colleagues. Unfortunately, I do encounter those who do not share my enthusiasm. Their view is often one of “Get It Done,” whereas I take the position of “Get It Done Right.” When discussing refactoring, the “Get It Done” developer often brushes it aside as a waste of time.

I want to address that crowd, and anyone else who eschews refactoring, with my top 5 reasons why you should refactor your code.

Enjoy!

5. Your Code Sucks

Derelict Bridge
Broken Bridge / jollyUK / CC BY

How often do you write an essay and have the draft come out perfect? Sure, your writing is probably more understandable than an email from a Nigerian prince but would you publish it without revision?

Test-driven development emphasizes a sequence of activities: Red – Green – Refactor. The first step is similar to outlining your arguments and conclusion. The second step is like drafting your essay to make the arguments and reach the conclusion. Finally, the revision step is to make the essay clear and concise.

Make sure your code doesn’t come off like a teenager’s text message; take the time to revise it.

Telerik JustCode features blazing-fast, on-the-fly code analysis that finds many more issues than Visual Studio alone. Think of it as your enhanced spellchecker.

4. Debts Accrue Interest

Cash
Mixed Cash / stopnlook / CC BY

You are not truly free until you’re debt free. In the context of software development, I'm talking about technical debt. Without incorporating refactoring into your development process, technical debt accrues until someone takes the time to pay it. It continues to grow even without active development as developers become further removed from the code they wrote.

If you’re not convinced this is a problem, you’ve likely never worked in a legacy code base. Reduce the amount of technical debt owed to ensure your system doesn’t become a maintenance nightmare!

Including Telerik JustCode in your development process is like having a technical debt financial adviser.

3. Repetition Is Dangerous

Tigers Fighting
Tigers Playing / Todd Ryburn / CC BY

The DRY (Don’t Repeat Yourself) principle, put forth in The Pragmatic Programmer, states that “every piece of knowledge must have a single, unambiguous, authoritative representation within a system.” The converse of this is the WET principle, or Write Everything Twice.

What happens when you repeat yourself in code?

You create another maintenance nightmare, where a single change requires visiting more than one piece of code. Good luck if you miss a spot.

One of the dangers associated with consolidating code is not identifying all usages of the code being removed. With the Find Usages (Alt+F7) and Find Extended Usages (Shift+F12) in Telerik JustCode, you can be assured that no code is left behind.

2. Spaghetti Is Good to Eat, Bad to Read

Spaghetti
spaghetti / gotosira / CC BY

You’re always welcome to invite me out for spaghetti, but please do not make me read your spaghetti code. Even in procedural languages, control structures can be simplified to be more easily understood. However, if your language has the ability to abstract control structures into another form (e.g. classes), then you should do so. I’m not saying you must join the Anti-If Campaign, but you should at least extract your nested if statements and never, ever use a goto statement. Yes, it does exist in C#.

Deconstruct a control structure with Telerik JustCode by using extraction refactorings to encapsulate individual units. Then extract classes and interfaces to take advantage of polymorphism for differing behaviors.

1. Littering Is Rude

Litter
#195 Garbage from Göteborgsvarvet / Mikael Miettinen / CC BY

How would you like it if someone sat at your desk while you were on vacation and left empty cups and candy wrappers lying around? I bet you would be justifiably annoyed.

Codebases are like shared workspaces. Even though you may be solely responsible for a particular set of code today, someone else will one day sit in your spot. You wouldn’t like it if someone left a mess for you, so don’t leave a mess for another developer. Clean up your code so other people aren’t stuck cleaning up your garbage.

Telerik JustCode has a code cleaning feature that automates many cleaning tasks such as removing unused private members. Cleaning profiles can be set up either through options or by choosing Clean Code from the Visual Aid. To clean with the default profile, press Ctrl+Shift+Alt+F. You can also clean groupings of files or an entire solution by right-clicking an item in Solution Explorer and selecting Just Clean All.

download_JC_trial

Chris is a technical evangelist for JustCode and a C# MVP. As a southerner, he had to endure years of people exclaiming “git-r-done,” often with no discernible reason.


About the Author

Chris Eargle

is a Microsoft C# MVP with over a decade of experience designing and developing enterprise applications, and he runs the local .NET User Group: the Columbia Enterprise Developers Guild. He is a frequent guest of conferences and community events promoting best practices and new technologies. Chris is a native Carolinian; his family settled the Dutch Form region of South Carolina in 1752. He currently resides in Columbia with his wife, Binyue, his dog, Laika, and his three cats: Meeko, Tigger, and Sookie. Amazingly, they all get along... except for Meeko, who is by no means meek.

Comments

Comments are disabled in preview mode.