Delen via


Object Calisthenics: Rule 3: Wrap all primitives and strings

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.

Comments

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

  • Anonymous
    December 26, 2011
    Money a double???!!  Never in a million years in the financial or banking world.

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