Being Cellfish

Stuff I wished I've found in some blog (and sometimes did)

Object Calisthenics: Rule 3: Wrap all primitives and strings

Change of Address
This blog has moved to

Object Calisthenics: Rule 3: Wrap all primitives and strings

  • Comments 3

This rule states that no parameters should be a of a primitive type (e.g. int, double) nor a string. Instead new objects should be created to represent what the parameters are. Is the integer really representing money or a coordinate. This small classes also creates great places to put methods that manipulate whatever the primitive type represents.

When I first tried the object calisthenics this was one of the harder rules actually. Not by itself but because of the ninth rule; don't use getters or setters. Together it really makes you have to think about how to represent things in your code.

By itself I think this rule is one rule that easily translates to your real code. Making sure you're passing objects to your methods which actually are what they represent rather than a primitive type that represent something is generally a good thing if you some day have to change the representation. E.g. money should be a double rather than an integer.

  • I recently talk to a friend who was migrating a large/complex system he wrote in J++ (ten years ago)

  • Money a double???!!  Never in a million years in the financial or banking world.

  • @Wes: the important thing is when you change the representation, not what it is.

Page 1 of 1 (3 items)