Archive for February, 2007

Framework Design Guidelines book

This is a book that should be required for every .NET developer to read:
 
Even though some might think this book is only meant for those who write frameworks, I don’t believe this is the only case at all. To me, this is a book to be read by any developer writing ANY .NET code. Every code written should follow standards and conventions. That makes so much easier for people to maintain and use it. Whenever a developer starts creating a class, or a method, etc., it should be done in the most professional way possible (a method you write today to solve a specific problem may be used tomorrow by another developer who happens to be facing the same problem, so you should always be striving to write the code as best as you can).
 
Reading through the book we learn from the MS developers why some things were done in such a way, and how they’ve managed to improve things based on developers’ feedback. For instance, they mention a usability test they’ve applied to a group of developers, where the task was "simple": to write some code that takes a string and saves it to a file on disk. They had 30 minutes to do so. Something like 9 out 10 developers couldn’t get the job done. They figured it was really hard to accomplish something that simple (and that’s needed quite often), and therefore they’ve come up with easier ways to get to the functionality. Things like that get you thinking about how you’re designing your classes (is it going to be simple enough for other developers – or even myself – to use it?).
 
That means that besides helping the reader improving how to write better code, the book also helps with understanding better the .NET framework.
 
Again, this should be a required reading for every .NET developer.  🙂
Advertisements

1 Comment

Good free book on Code Review

 
I just finished reading it a few weeks ago. It’s a good read. It provides some good insigths about the different kinds of Code Review a company could use, but besides that, it also provides some good info that can make developers think a little more about how they write code so to make it easier for whoever is reading it to understand (which could be either another person acting as a reviewer, or even the author of the code him or herself having to maintain the code).

Leave a comment

Studying for the 70-536 exam: the shortage of good books

As usual, the training kit books put out by Microsoft Press sucks. I’ve tried many of them over the last couple of years, and they always suck. And everytime I read reviews on Amazon about those books, I see that other people feel the same way. Well, for the 70-536, that’s still true.
 
The best books I’ve used in the past were the ones written by Amit Kalani (published by Que). Those were really great, as far as I’m concerned. This time around, however, I can’t use that one because it hasn’t been published yet. It’s been anounced for many months now, but it seems like nobody knows when it’s coming out: http://www.amazon.com/MCTS-70-536-Exam-Prep-Foundation/dp/078973558X/
 
Because of that, we (me and the guys here in the office that are studying for the test) decided to go with the book by Microsoft Press, since that’s the only one available out there: http://www.amazon.com/MCTS-70-536-Exam-Prep-Foundation/dp/078973558X/
 
As usual, the editing of the book is bad, several examples are almost meaningles, the number of errors is just ouchy… check out the errata for it: http://support.microsoft.com/kb/923018/en-us. That’s huge.  😦
 
Well, I think I’ll just have to live with it and use some other material to complement my studyings. Anyways, I wanted to express my thoughts on it.  🙂

1 Comment

Those Regular Expressions again…

I’ve been growing everyday day more and more fondly of the following principle: Code Talks! The main idea is that well-written code is self-documented and should not require a lot of comments in-line to be understood. 
 
That said, if code talks, when I see code like the one below, I think to myself: “Well, if code talks, this one swears…”:  J  

public bool IsValidPhoneNumber(string number)
{ 
   return Regex.IsMatch(number, @"^\(?(\d{3})\)?[\s\-]?(\d{3})\-?(\d{4})$");
} 
 
Once upon a time I’ve posted something regarding putting comments in regular expressions:
http://claudiolassala.spaces.live.com/Blog/cns!E2A4B22308B39CD2!117.entry

When I look back, that still seems a bit cryptic, though. Last week I was thinking: “how the heck can a developer do code review where there’s a regular expression involved?”. Given the sample code above, I’ve been thinking about splitting the RegEx into something that’s easier to read, and therefore, easier to review. The code would look something like this:  

/// <summary>
/// Checks whether a given number is a valid phone number (according to the common format).
/// </summary>
/// <param name="number">The phone number.</param>
/// <returns>True if the number is valid, or false if it is invalid.</returns>
/// <remarks>
/// Examples of valid phone numbers:
///    (123)456-7890
///    (123) 456-7890
///    123-456-7890
///    1234567890
/// </remarks>
public bool IsValidPhoneNumber(string number)
{ 
    return Regex.IsMatch(number, REGEX_VALID_PHONE_NUMBER);
}

Notice that I’ve replace the RegEx by a constant that is just easier to read. That constant is defined as follows:  

private const string REGEX_VALID_PHONE_NUMBER = 
  MATCHES_BEGINNING + 
  MATCHES_OPTIONAL_OPENING_PARENTHESIS + 
  MATCHES_EXACTLY_THREE_NUMERIC_DIGITS + 
  MATCHES_OPTIONAL_CLOSING_PARENTHESIS + 
  MATCHES_EITHER_SPACE_OR_HYPHEN + 
  MATCHES_EXACTLY_THREE_NUMERIC_DIGITS + 
  MATCHES_OPTIONAL_HYPHEN +              MATCHES_STRING_ENDS_WITH_FOUR_NUMERIC_DIGITS_REQUIREMENT;

 That’s a lot more verbose, but in this case, something more verbose than the cryptic RegEx. The other constants are defined like so: 
 
private const string MATCHES_BEGINNING = "^";
private const string MATCHES_OPTIONAL_OPENING_PARENTHESIS = @"\(?";
private const string MATCHES_EXACTLY_THREE_NUMERIC_DIGITS = @"\d{3}";
private const string MATCHES_OPTIONAL_CLOSING_PARENTHESIS = @"\)?";
private const string MATCHES_EITHER_SPACE_OR_HYPHEN = @"[\s\-]";
private const string MATCHES_OPTIONAL_HYPHEN = @"\-?";
private const string MATCHES_STRING_ENDS_WITH_FOUR_NUMERIC_DIGITS_REQUIREMENT = @"\d{4}$";  

This does seems a lot easier to review, but there’s one part that I’m not sure it would work: when we’re building and testing a RegEx, we normally use a tool such as Regulator or RegEx Buddy. I’m thinking I need some little tool where I can select the pieces of a RegEx and then create the declarations for the constants out of it, otherwise it’d be painful to do it for a long and complex expression.

I’m wondering what other developers are doing out there. Any thoughts?  

Even though some RegEx developers out there may think this is silly, most of the developers I’ve encountered aren’t that familiar even with the most simple expressions, so I don’t think I’m lone on the frustration of trying to understand those cartoon swear expressions.  🙂

3 Comments