Posts Tagged ‘Coding

08
Nov
07

Coderev: Paint.NET pt.I

I’ve started with reviewing the Locale and Setting modules. The code is very clear to me, and I’d like to scribble points I’ve made to myself here.

Settings is a module which rides the registry for saving/fetching settings. This is useful, since you could drop any kind of object and get any kind of object (not just strings) from the registry.

So they did. You can get/set booleans:


public bool GetBoolean(string key, bool defaultValue)
{
return bool.Parse(GetString(key, defaultValue.ToString()));
}

While GetString relies on GetObject (this is the default value version, more on that later):

public object GetObject(string key, object defaultValue)
{
try
{
using (RegistryKey pdnKey = CreateSettingsKey(false))
{
return pdnKey.GetValue(key, defaultValue);
}
}

catch (Exception)
{
return defaultValue;
}
}
Which is the heart of the thing.

Elegance

The do their best to provide a clean and efficient interface:

Settings.CurrentUser.[SetString/GetString/SetObject/GetObject...]
Settings.SystemWide.[SetString/GetString/SetObject/GetObject...]

Which is done simply by instantiating two singletons to the respective HKCU/HKLM keys (CurrentUser, SystemWide are both Settings objects).

Another point to make is the use of “default value”. For some reason I find this a Good Thing. A get method takes a default value, an returns it instead of the null-value it should usually return when encountering error/absence of data.

All in all, this is a module that cries for reuse. And I will definitely put it in may favorite sources box, after I make sure it is generic enough.

05
Nov
07

Enumerations and Aesthetics

Lets take an example case: a game of cards. A Card has a rank and suit, and we would like to create a Card class, which instantiate with an enumeration of Rank and an enumeration of Suit. Suit is either Hearts, Spades, Clubs or Diamonds. Rank is one (Ace) up to 13 (King).

One way to do this would be:

Card sixofhearts = new Card(Suit.Heart, Rank.Six);

However, more aesthetically we can write it like so:

Card sixofheards = Card.Hearts(Rank.six);

The code is alot cleaner, and nicer. We have saved the programmer a knowledge of the Suit enumeration. The key to this, is to identify the closed-set (enumeration) nature of one parameter of your object ctor, and create a single method for each item in that set, with providing a factory. Of course, its much more economical to use the smaller set of items for the method calls (here, the card suits, only four methods).

Another example, this time using an enumeration and free running variables.

Car aCar = Car.Toyota(Color.FromRGB(22,33,44), "33-223-44");

Neat.

19
Oct
07

Evolving

As a part of striving to be a better developer, I hereby declare that I will:

1. Read one new source per day.

2. Familiarize with one new tool per day, and deeply familiarize with one per week.

3. If time allows implement/re-implement a ‘Thing’ or read a heavy article.

4. If time allows read a new book every 2 weeks(!).

5. Post here as I go along.