Technical Practices: No Public Statics

I've had this sitting in my queue for over a year. Waiting for some day when I have some thoughts on it.
Is today that day... Ehh... Probably not. I'm going to try anyway.
I'm writing this the same day I wrote the "MicroObjects Guiding Principle" post.
It had some hints of why this practice is powerful. I want to give this practice a little space of it's own.

Most of this practice just comes from following Fred George and Elegant Objects. Not a whole lot of variation and exposure myself.
I hadn't needed public statics. We found what I think are better ways, but... why?!

I never really had a good answer to that until the realization that spawned the earlier blog post. Represent the concepts in the code.

There's lots of reasons to have public static values in code. In all of these cases, it is a representation of something.

What is it?
Why does it exist?
What does it represent?

This is starting to sound a lot like Fred's "What's it's job?"

There are answers. I'm probably going to start giving people nightmares by asking, "What's it represent?".

While this applies to A LOT - It's very applicable to public statics. What does that value represent? Is it a flag of some kind? Is it a value we compare against?

It's like a Getter. Let's look at where it's used. How can we encapsulate the behavior associated with this value into an object? How can we define the concept that object now represents?

All of these public static values we have scattered through our code are concepts we don't have represented in the code.

Change that.

Find the behavior associated with the value and turn it into an object, then use that object. Hide the piece of data inside an object and expose behavior.

This is a pretty short post. It's not a very unique practice. It uses a lot of the insights of other practices. Especially No Getters. We don't want to allow raw data out - and here we are with a public static of raw data being used all over... no.
We don't use primitives. Create an object for the concept it represents.

Other of these technical practices also apply to public statics, but it needs to be called out separately because it doesn't fit cleanly into any of them, and uses many of them.

Don't use public statics; Create an object to represent the concept it is.

Show Comments