Thursday, July 9, 2009

State vs Status API guide

Today I had to make the choice whether I would use the word state or status in my program API, and I got confused. What is actually the difference?

This is not about states in the sense of the United States, but about the condition of something.

As an example, I will use the condition of the "something" called H2O, also known as water.

Luckily we as programmers are already familiar with the concept of a definition (class) and an instance (object). With state and status it's the same. State is a definition, and status is an instance of that definition.

So, H2O has several states, namely; Solid, Liquid and Gas. Solid is one state of the 3 states in which H2O can be. If we would query for the state of a H2O substance, then we would ask for its status. Like, Q: "What is the status of this 'water'?", A: "It's in a solid state, it's ice.".

In an API, you would have to stick to the right naming, because it can confuse the use of the API. Let me show you with an example in C#:

   enum H2OStates
{
Solid,
Liquid,
Gas
}

class H2O
{
public const H2OStates IceState = H2OStates.Solid;
public const H2OStates WaterState = H2OStates.Liquid;
public const H2OStates VaporState = H2OStates.Gas;

public H2OStates Status;
}


The confusion will probably occur with the definition of the H2O class. When you have a H2O object, something that comes to mind is to ask the question "What is the state of this H2O?". The property to return the state, should not be called State, but Status. Why? Because it could return any of the possible states, and not a single hard-coded state value of the H2O, like it is with the constants in the H2O class. Those constants should be named with State in stead of Status, because they always return the same static value, and not an item within a range of possible values.

But now I hear you ask, what about statuses? That's nothing more than a bunch of H2O instances with each having their Status property referring to a specific state.

1 comment:

Anonymous said...

- H20States should not be plural. The type holds one state at a time. Integer or int isn't called Integers or ints.
- It should probably be PhysicalState not H20State. Many things besides water can be solid/liquid/gas (/plasma/...). Maybe that doesn't seem important but:
- the state of the water also includes its temperature, purity, pH level, etc. The state of a thing is all its properties (which is typically encompassed by the complete class, ie class H20 contains the complete state of that instance of water). That is why you need to be more specific about which property you mean - and solid/liquid/gas in physics is typically called the 'physical state' (or "states of matter") so a more specific name such as PhysicalState would be more appropriate

- "status" _usually_ is more of a judgement whereas "state" is factual. ie the state of a project may be "one man sick, part A 60% complete, part B messy but working,..." whereas the status may be "green/yellow/red". To projects in the same state might differ in status due to different judgement on the part of the managers.
- also "status" is usually more for states with fixed progression (think social status). Physical states have somewhat of a progression, but hey, sublimation.

- I say "usually" because really, state and status are usually just mixed up. Thanks for trying to find a separation.

For class H20, I think the right answer is:

class H20 {
...
PhysicalState physicalState;
...
}

Tony