Welcome!

.NET Authors: Pieter Van Heck, Pat Romanski, Shelly Palmer, Jayaram Krishnaswamy, Chris Pollach

Related Topics: .NET

.NET: Article

Why Code Snippets Are Not My Cup of Tea

But how about a few good refactoring options in the Visual Studio menu

Call me a purist but I like to make my code as object-oriented and well structured as possible. I like to tweak the design and to work toward the overall quality of the code. My refactoring olfactory sense is quick to react to any offending odor in the code and it’s becoming more sensitive and less tolerant with time.

One of the worst odors you can come across in code is duplicated code. Duplication can appear as a result of different practices. Sometimes the system is so large that the programmer can’t easily find his way around. He’s simply not aware that the same problem has already been solved only a few namespaces away. Other times it’s the result of that typical resolve to get the job done as quickly as possible. In those cases, the programmer desperately relies on fast-growing LOC numbers as crucial evidence of progress. Finally, probably any burgeoning programmer has enjoyed the sudden surge in productivity that comes as the result of the most basic and unfortunate reuse mechanism called copy-paste. In those cases, duplication is so evident it borders on being rude. As a novice, one can be pardoned (after all, we have all been there once), but it is not commendable for any self-respecting, experienced programmer.

Duplicated code is difficult to maintain. To fix a single thing, you have to perform change in more than one place. Such code is difficult to understand, since there’s no clear relation between the domain it represents and the code structure. It leads to more code overall, and the list of the adverse effects of code duplication just goes on.

In order not to duplicate code, we rely on structure. Instead of repeatedly writing the same few lines of code, we can isolate those lines inside a procedure. Then, we can make a procedure call whenever we need the functionality contained in those few lines. As a higher-order structuring mechanism, we can bring data and functionality that operates on that data in the form of a class. Then, we can instantiate an object and delegate some work to it. On another occasion, it’s appropriate to inherit the class and thus reuse existing code in an object-oriented, structured manner.

With Visual Basic, I have witnessed the constant progress in language and tool capabilities that let you be more productive and reuse your code better. With the advent of VB .NET, you finally got inheritance and a full object-oriented language.

More Stories By Danijel Arsenovski

Danijel Arsenovski is a software developer from Santiago, Chile. He works as a Product and Solutions Architect at Excelsys S.A, designing Internet banking solutions for numerous clients in the region. He started experimenting with refactoring while overhauling a huge banking system, and he hasn't lost interest in refactoring ever since. He pioneered the use of refactoring as a vehicle for a VB 6 code upgrade to VB .NET. Danijel is the author of "Professional Refactoring in Visual Basic" published by Wrox, and is a contributing author for several major publications. He holds a Microsoft Certified Solution Developer (MCSD) certification and was named Visual Basic MVP in 2005. He blogs at http://blog.vbrefactoring.com.

Comments (0)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.