Wednesday, September 26, 2007

Data and type safety.

What's the difference between :

String path = ...


and

File path = ...


?

For that matter what's the difference between:

int imageId = ...


and

ImageId imageId = ...


?

What is the point of creating an class for an object that contains one piece of data? Well...

In languages with a compiler enforced typing system, like java, it's good practice to leverage the type system wherever possible. Creating types for things like paths or ids can prove very useful. By using the type system the compiler will help you catch more errors earlier. This make the code easier to write and maintain.

When should we create a class for a type? If you look at InteleViewer's code base, we don't always substitute implicit types (of the form "int id") with explicit types (of the form "ImageId id"). The main reason is that sometimes it doesn't give us anything and the class can add a layer of bureaucracy that just serves to make the code more complex.

There's really a set of cases that determine how important/useful it is to turn an implicit type into an explicit type.

Cases:
- You have a 1 variable primitive type in which all values possible for the primitive value are allowed values for the implicit type but you don't do anything that actually needs to rely on the type.

You generally don't need to worry about this case unless your app is getting confusing enough that you want to use the compiler to check your code.

- You have a 1 variable primitive type in which all values possible for the primitive value are allowed values for the implicit type and you are often doing operations on it (formating, funky math etc..)

If you're doing operations on it (especially operations that take an object of the same type as a parameter. Like addition, for example.) then *always* make a class for that type. Dates are fairly good examples of this. They have a mapping to longs but we generally want to manipulate them in respect to each other. File objects are another good example (I think every value String more or less makes sense as a path).

- You have a 1 variable primitive type but there are some values that are valid for the primitive type but not valid for the implicit type.

I generally make a class for this if the value escapes from the object/code module it's in. The reason is that things can get dangerous across module boundaries.

You generally want to defend against badly formated implicit types across functional boundaries so that errors are caught early. Having an explicit type guarantees that the data you're getting as a param is well formed.

- You have a 2 (or more) variable implicit type.

Always make a class for this case.


Oh, one last thing.. Always make your data types immutable if at all possible. See ya.

No comments: