Monday, April 12, 2010

Regular Expressions in C#

Recently, while spending time in the .NET forums, I have come across an inordinate number of questions regarding regular expressions, which often sparked the usual religious argument between the proponents and opponents of regular expressions. One of the more widespread stances regarding regular expressions is that they are bad, and lead to unnecessarily unreadable and, in inexperienced hands, inefficient code. While there is some truth to this, regular expressions are no worse than the goto command, the if command, or any command for that matter; i.e. if used correctly, it can save time and enhance readability.

The thing to remember is that regular expressions are a field unto their own, and that it requires some expertise to use them right, as well as to properly judge when to use them at all. Two of the main misuses for regular expressions that I've encountered are:

  • Discovering their usefulness for a particular task, and then using them everywhere, just because you can.
  • Attempting to use regular expressions to parse structured text, such as a programming language, or XML.

To use regular expressions correctly, I follow these rules of thumb:

  • Keep the regular expressions short, don't let them span more than one line. But also remember not to compress them too much just so that they fit on one line. Keep them readable.
  • Use them only when necessary, if a concise, readable and efficient alternative exists to regular expressions, use that instead.
  • Use named capture groups where possible, to give semantics to the expressions.
  • Write comments regarding the expression, and include some examples of what it is expected to match and to reject.

Thursday, April 8, 2010

Introduction

For years I've been meaning to get me a blog, as sometimes I have ideas which are not good enough (or comprehesive enough) to warrant a CodeProject article, but which are also not small enough to deserve to disappear in page number >=2 in the forums which I visit.

This blog is for programmers, and mainly for .NET in general and the C# language. I personally have been writing code since the age of 9, when I got my first computer (Commodore VIC-20). That computer didn't come with any storage device, so at the end of the day I copied what I wrote using pencil and paper, and the next day I would type it all back in again, and continue coding... Things have changed slightly since then :-)

Anyway, I hope you find this blog interesting!

Aviad P.