Categories
Uncategorized

Never expose boolean

Imagine a startup creating a management system for a company that operates in a single country. Sooner or later, there comes a feature to distinguish contractors between home country and foreign ones.

You may imagine a developer introducing a small private class with a boolean property IsForeign, so the class that represents a contractor looks like this.

class Contractor
{
    public string Name { get; private set; }
    public bool IsForeign { get; private set; }
}

For a while, everything works smoothly – the company gains new investors, introduces new contractors. In the meantime, the application grows – classes become modules, modules become microservices, interfaces grow to data contracts and APIs.

Now there are thousands of contractors stored in databases and one may query a microservice to receive an HTTP response to find out that a particular contractor origins from a foreign country.

And then the unexpected happens: business people decide that the company goes international.

What happens to a boolean then? It is dangerous now! Not only it is inconsistent. Not only it is wrong. The client of the API does not know what it means and new clients cannot rely on it anymore. It can lead to serious problems and the only solution is to remove it. But you cannot simply remove something that has been published to an API. You need to notify a lot of people, release a new version and make plenty of serious changes.

If only there had been an easy way to make this property obsolete without tremendous changes one day, such as in example below.

class Contractor
{
    public string Name { get; private set; }
    public Country Country { get; private set; }
}

enum Country
{
    Home,
    Foreign,
    NonApplicable
}

Leave a Reply

Your email address will not be published. Required fields are marked *