Friday, February 27, 2009

KIS(S) - Keep It Simple (Stupid)


If there's one thing that I always keep in my mind when I program is KISS. No, I am not talking about kissing my beautiful wife. I am talking about

Keep It Simple Stupid.

If you find yourself writing code that looks really cool and sounds awesome when you tell your friends about it... and then, when you try to debug it a couple of weeks later, it takes you ~ an hour to figure out what the hack you were doing there.... Well, that means that you were doing something wrong. To my opinion, one of the important things that a good programmer must have is the ability to write simple code. It may not look very cool, or it might be 1-2 lines longer. But when a co-programmer will have to debug your code, they will not have a puzzled and confused look on their face :)

Sunday, February 22, 2009

RegExp on ASP.NET vs. The World

I was working on a Create New User page and had to add some Regular expressions to some of the fileds on the page. Everything gone well, everything but the regex for the password field.
My regex looked like that:
    (?=^\S[^<>])(?=\S*[0-9])(?=\S*[A-Z])\S{6,20}$

Which translates into: Accept every string of characters that doesn't have white space or '<' '>' and must have at least 1 numeric character and at least 1 upper-case letter and has to be between six to twenty characters long.
I usually test my regex with a nice free tool called: Regex-Coach.

However, when wiring it up on my aspx page, it translated into: Accept every string of characters that doesn't have white space or '<' '>' and must have at least 6 numeric characters and at least 6 upper-case letters.

After hitting my head on my desk for a while I decided to modify my regex to the following:
    (?=^\S[^<>]{5,19}$)(?=\S*[0-9])(?=\S*[A-Z])\S*$

Which translates into the same thing, however this time I moved the "at least 6-20 characters" restriction to the beginning of the expression. This regex worked on both the regex coach and on my aspx page. It seems a little odd to me that the same regex is being translated somehow differently after all regex is regex?!

Sunday, February 8, 2009

Just Struct It

When will be a good case to use a struct?
Well, let’s look at the following code:

 private IDictionary<IClassKey, string> _someDictionary;

Everything should be working fine here, but what if we need to change our dictionary so it will hold an additional value in addition to the string? It seems like we have a couple of options:
I. We can create a new internal class to hold the data that we need:

 internal class SomeClass
 {
   public string MyString{ get; set; }
   public decimal MyDecimal { get; set; }
 }


and then of course pass it to our dictionary:
 private IDictionary<IClassKey, SomeClass> _someDictionary;

II. Or we can use a struct:

 public struct SomeStruct
 {
   public string MyString{ get; set; }
   public decimal MyDecimal { get; set; }
 


and then of course pass it to our dictionary:

 private IDictionary<IClassKey, SomeStruct> _anotherDictionary;

Now the question that we need to ask is which one should we choose and why.
In this particular case, struct will be a better choice. Creating a new class to hold two variables seems to be too much over-head and too expensive.
Note, that structs are created on the stack and therefore you are dealing directly with the struct object itself and not with its reference.
 
Watch the latest videos on YouTube.com