I’m the first to admit—I love regular expressions.  It’s kind of a hammer and nail situation.  I see text, I immediately think:

using System.Text.RegularExpressions;

They’re just so useful.  How can you not like them?  Okay, they’re a bit obfuscated and horrid to debug (and that’s even true for the person who writes them).  I’m always thinking of my coworkers, so here’s a few regurgitated thoughts about how to improve readability and maintainability of Regular Expressions.

Since Regular Expressions can be so difficult to understand, it helps to properly document the individual tokens in the pattern.  There are a few reasons behind this:

  1. Check your work up front
  2. Clearly state what the expression intends to match
  3. Clearly state how the expression intends to match

I have this new friend—uh, buddy… his name is RegexBuddy.  Damn, I love this tool.  You can type in an expression, some input text, and see real time results.  But, there’s something invaluable about its presentation.  As you write an expression, it generates this great explanation.  The best part is that it is in plain English.  Looking at this got me thinking a little.

image
RegexBuddy even allows you to export the explanation to various places.  Seems like the clipboard could come in handy.  Wait, what if we could get this kind of information into comments?  Maybe it could be pasted and massaged into comments to look something like this:

//---------------------------------------------------------------------
/// 
///     This Regular Expression can be used to extract
///     a customer name from the salutation of a form letter.
/// 
#region Regex Explanation
// Expression:
// Dear (?<name>[A-Za-z ]*),
//
// Explanation:
// 	Match the characters “Dear ” literally «Dear »
// 	Match the regular expression below and capture its match into backreference with name “name” «(?<name>[A-Za-z ]*)»
//   	    Match a single character present in the list below «[A-Za-z ]*»
//      	Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
//      	A character in the range between “A” and “Z” «A-Z»
//      	A character in the range between “a” and “z” «a-z»
//      	The character “ ” « »
// 	Match the character “,” literally «,»
//
// Sample Input:
// "Dear Loyal Reader,
//
// Thanks for reading John Coder!  :-)"
//
// Matches:
// "Loyal Reader"
//
// Created with RegexBuddy
#endregion
public const string GetCustomer = @"Dear (?<name>[A-Za-z ]*),";

Is it overkill?  Maybe.  Although, there are a couple of things to note.  Jamming all of this “stuff” into the

Xml comment tag will inevitably break it.  Plus, you probably don’t want that much information to pop up in a tooltip anyway.  So, I wrapped it in a region to make it collapsible.  The summary is really a summary, and the drawn out explanation is deferred to a less-obtrusive location.

Is this easy enough to understand?  Leave a comment and let me know what you think.

Friday, July 17, 2009 12:34:24 AM (Eastern Daylight Time, UTC-04:00)  #    Comments [0] -
Coding Horror | Commenting | Jeff Atwood | Maintainability | RegexBuddy | Regular Expressions

Reading blogs tends to put you at the mercy of other programmers, and with every post you read it's easier to see that we're all alike in a lot of ways.  I came across some posts by Jeff Atwood at Coding Horror.  Now--I know this is a slew of links, but stay with me.  The links found in these articles can easily double the number of tabs in your browser (writing this blog alone, I found 20ish tabs in my browser).

  1. The Programmer's Bill of Rights - this was an interesting read because it revitalizes the importance of the most basic part of our profession: comfort.  One point Jeff Atwood makes is about your mouse and keyboard, relating it  to a painter's paintbrush.  To paraphrase his point, programmers should learn to understand and embrace the added value of comfort and convenience in "the essential, workaday tools we use to practice our craft and should be treated as such." 
  2. Five Things About Jeff Atwood - Jeff shares a few things with everyone, starting with a tour of his office and some insight about the little nuances that get you through the day.
  3. Thinking Putty Fetish (I think Steve and Todd can appreciate this) - A case study shows that exercise is good for your mental health, and thinking putty is legitimate exercise for your hands.  I'm even considering a tin for myself...  For a while, Jeff was buying a new tin for every new employee who started.
    Thinking Putty
  4. The Developer's Second Most Important ASSet - I think this quote describes this pretty well... "I've run the gamut from $99 OfficeMax specials all the way up to the exotics, and there's no question that a quality chair is a significant factor in productivity over an 8 hour day."  The infamous Herman Miller Aeron chair is mentioned.  I used to sit in one of these chairs at my previous job, and I have to say--that's easily the most comfortable office chair I've ever sat in.
    Herman Miller Aeron Chair

It's easy to get lost in a sea of day-to-day comfort philosophy, but it all has to start somewhere.  Productivity and ownership over work stems from the ability to relax and be comfortable in your immediate atmosphere.  Be unique.  Be you.  A programmer's cubicle might look like a playpen, but as Jeff Atwood says, "It's as much a reflection of...personality as anything else in...life."

Monday, May 25, 2009 9:21:00 PM (Eastern Daylight Time, UTC-04:00)  #    Comments [0] -
Coding Horror | Herman Miller Aeron Chair | Jeff Atwood | Thinking Putty

Welcome to my blog!  You maybe wondering "Why a blog?"  The answer is simple.  Engagement.  No, not marital engagement--more like active engagement.  On my own time, I try to stay on top of what is "up and coming" in the developer community.  When I find something interesting, it might find its way to this blog.  It could be related to a specific technology like C#, ASP.NET, JavaScript, Visual Studio 2008/2010, or SQL Server.  It might be a philosophical post involving design patterns and industry-wide best practices.

How is that active engagement?  Well, #1 indicator of success is that you've gotten even this far in the blog post.  If you (the reader) has found even a single blog post worth reading and you (hopefully) even learned something, then it was all worth it.

Blogging is a way for me to write, and thus become a better developer.  During an interview about Stack Overflow, Jeff Atwood advised that developers should blog about everything (blog, blog, blog).  Stack Overflow is a place for less known, but fantastic developers to gain presence in the community.  I found this to be great advice, especially if you're able to blog about something worthwhile time and again.  That lead me to Jeff's blog Coding Horror, and furthermore to an article titled "Is writing More Important Than Programming?"  In the article, Jeff refers to some advice from Joel Spolsky to college students:

"The difference between a tolerable programmer and a great programmer is not how many programming languages they know, and it's not whether they prefer Python or Java. It's whether they can communicate their ideas. By persuading other people, they get leverage."

As I post things that I've learned, it is important to consider that the ways I am showing are NOT always "the perfect way" to do something.

I hope that future posts may spread inspiration and insight.  Please feel free to comment (in fact, I encourage it).  Let me know if there is something you would like me to post about, or if you would like to see some more in-depth coverage of a certain topic.  Thanks for reading my post, and I hope you return with each new entry.

Friday, May 08, 2009 4:30:00 PM (Eastern Daylight Time, UTC-04:00)  #    Comments [0] -
Blogging | Coding Horror | Jeff Atwood | Writing

John Nelson

mugshot I am a passionate C# Developer working in ASP.NET on an e-commerce solution for ticketing software. I work across all of the application layers, including server side functionality, and client side programming with jQuery and MS Ajax. Although my full time job is in WebForms, I spend many of my off hours working with MVC. I am especially interested in productivity and good programming practices.

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2010
johncoder.com
Statistics
Total Posts: 41
This Year: 17
This Month: 0
This Week: 0
Comments: 4