Boost Your Coding Productivity

Tools for improving coding productivity using templates

Michael K. Campbell

April 6, 2011

9 Min Read
ITPro Today logo in a gray background | ITPro Today

A key technique that developers use to increase productivity is to adopt coding paradigms, techniques, and tooling that let you generate code instead of writing it. Or, as the saying goes within the developer world: "Good developers write good code, but great developers write code that writes good code for them."

The Case for Code That Writes Code
In addition to boosting overall productivity while in the saddle, adopting tooling and techniques that generate code instead of making you write it out by hand usually results in better overall code within your application as well.

Typically this is true for one of two reasons. First off, generating code with parameters or via templates is a much better approach than the copy-paste-tweak approach that you were likely going to use anyhow in addressing repetitive coding needs. (Because we all know that copy-paste-tweak is a quick way to get things done—one that always results in a few days of debugging some crazy, indeterminate bug that you just can't track down because you know the code works just perfectly.)

The second reason that generating or using templates to create repetitive code is a better idea is because code that you take the time to template is code that has typically been tackled a metric bazillion times. The code is at the point where it's become an "idiom" in the sense that it's typically robust and coded enough that it is—you hope—better code than something you would just pound out spontaneously. Or, in other words, generated code typically tends to represent "better"' code by nature of the time and energy spent in parameterizing or templating it, making it provide additional benefits than the obvious DRY (don't repeat yourself) benefits that you'd think you'd be getting.

As such, I wanted to take a look at several techniques for boosting productivity that I've been using over the last year or so with great success.

ReSharper and CodeRush
Put simply, you're either a huge fan of JetBrains' ReSharper or of DevExpress's CodeRush. Personally, I don't care which one you use. But if you're a professional developer and don't use one of these solutions, then you're cutting yourself insanely short—and spending way too much time doing stuff you don't have to do.

Personally, I'm a big fan of ReSharper. And while I originally grabbed it for its refactoring support, unit testing augmentations, and its huge amount of other additional UI helps, tweaks, and productivity boosts, one of the things I love the most about ReSharper is its support for templates that make it so much easier for me to quickly code repetitive tasks. In fact, I use ReSharper's templating functionality all the time when creating new repositories and when creating individual methods within those repositories—because ReSharper's support for "surround-with" templates gives me the ability to easily tackle some custom considerations around caching that I leverage pretty heavily in many repository methods.

Of course, it always takes a bit of time and effort to learn how to use something like more advanced templating features (to say nothing of the additional time it takes to acquire the muscle memory needed to commit these approaches to shortcut keys). But I've always found that whenever I take the time to learn a new approach such as extended templating, the tiny bit of pain I incur up front is richly rewarded later on with drastic increases in productivity that pay off over and over again.

CodeSmith Generator
And nothing I've worked with over the past few years illustrates that better than CodeSmith Tools' CodeSmith Generator. Simply put, this solution is powerful. Yet it's pretty simple to wrap your mind around—and if you spend just a bit of time learning how to use it, it can save weeks or even months of time.

As I covered previously, I'm a big fan of PLINQO—which ships as a bound solution or set of templates for CodeSmith Generator as a way to extend and "fix" LINQ to SQL—as it has saved me more lines of code than I can even fathom. In fact, using PLINQO, I've gotten to the point where I can easily pull a few components "off of the shelf," as it were, point it at my databases, and have core data access plumbing in place in a few minutes. And, what I love best about that, is that the code I'm generating is all code that 100 percent meets my own very picky requirements and biases. Which I can then easily whip into shape with repositories that I quickly generate using ReSharper templates.

T-SQL Templates and Red Gate's SQL Prompt
Speaking of templates, it almost seems like a bit of a detour to call this out, but SQL Server provides its own powerful support for templates as well. Yet I'm always surprised how few developers know about these.

I've actually detailed a full walkthrough of how these templates work and ways you can easily leverage them in one of my videos—and I still make very heavy use of them today. In fact, I commonly hand off T-SQL template solutions to my SQL Server Consulting clients as I find that templates provide the perfect amount of flexibility, yet with structure, when it comes to tackling new tasks that users may not be terribly familiar with addressing.

Likewise, another tool I use extensively when working on SQL Server is Red Gate Software's SQL Prompt. Not only does it provide better IntelliSense within SQL Server Management Studio than what you get out of the box, but it also provides a whole slew of snippets that you can use—along with the option to create and define your own snippets—which I use all the time.

Code Snippets
Speaking of snippets, I'm also a huge fan of Visual Studio's native support for snippets, and have been for a few years now. Only, I've never been a big fan of having to go in and manually manage snippet definitions by mucking around with XML files.

Instead, I've long been a fan of Snippet Designer, which is a free solution or plug-in that you can drop right into Visual Studio. With it you can easily define, customize, and design snippets to your heart's desire. Best of all, it's super easy to use Snippet Designer (which works with both Visual Studio 2010 and 2008) to parameterize snippets as needed in order to keep them robust.

Then, once you've created snippets as desired, you just copy them into the appropriate directory (Snippet Designer will help you locate this directory), and the snippets instantly begin working for you in Visual Studio.

For example, one thing I do a lot of when working with ASP.NET MVC applications is to create custom routes. Only, I hate the process of having to add in all the line breaks to keep everything semantically pristine and "conventional" with normal routing layouts as I like them. So, I long ago created a snippet (called add_route) that looks roughly like this:

routes.MapRoute(                "$RouteName$",                  "$RoutePath$",                  new { controller = "$Controller$", action = "$Action$" }            );

With this snippet, creating a new route is almost always just a couple of keystrokes and a few tabs away. And in the rare cases where I need to customize a route above and beyond this set of simple defaults, I'm still way ahead of manually working through the creation of a new route. Likewise, if I were to find that I'm doing this routinely, I'd take a few seconds and pound out a new snippet to address future needs. And, to that end, I've also created similar snippets for roughing-in unit tests, and a host of other considerations.

MVC Templates
Finally, speaking of MVC and templating, a few weeks ago I was complaining about how tedious it is to modify the actual project templates that ship with Visual Studio. Specifically, I was complaining that since these projects are .zip files buried deep within the bowels of the file system, the process of finding, tweaking, and then rezipping those contents (in such a manner that would be pleasing to Visual Studio) is just a bit too much of a pain—to the point where I was lamenting that there isn't something along the lines of Snippet Designer for modifying Visual Studio templates.

Happily, it appears that this is something that the MVC team has also recognized as being a bit too cumbersome as well—because Eilon Lipton responded to my lament with a detailed overview of how you can very easily modify the templates for new content in ASP.NET MVC 3 applications due to the fact that MVC 3 project templates actually use t4 templates.

Even better, Scott Hanselman went ahead and blogged about this issue—even going as far as to include a video presentation he'd done earlier where he touches on this exact issue.

And what I positively love about how MVC 3 projects leverage t4 templates is that you gain not only the ability to change template for all new projects by modifying the "root" t4 templates, but you can also customize this content on a project by project basis by merely copying the necessary t4 template files/folders into your project, then modifying them as needed.

So, in this regard, if I don't like the fact that controllers in MVC projects always default to containing a "using System.Web;" reference when I rarely if ever use that namespace in my controllers, then I can go in and nuke that at "machine" level so that I never have to deal with that again. Likewise, if I know that all my controllers for a given project need a specialized namespace reference or if I always want them to inherit from, say, "SuperController", I can make those changes at the project level. In this way, not only will those changes save me time, but I can include them in source control so that they'll save time for the other members of my team, and so on.

It's All About Productivity
Any way you slice it, using templates can be a great way to save time and boost productivity. I've shared a few ways that I've found to increase my coding productivity. What techniques and tools are you using? Or, what did I miss? Feel free to share your own tips and tricks in the comments.

Read more about:

Microsoft
Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like