Overuse of Global State
This is one in a series of articles on technical debt.
Act I
Imagine you're building a new house. It's a big house, five bedrooms, three and a half bathrooms, an outdoor kitchen, a wet bar, and even a steam room. This house is going to require some extensive plumbing.
To save yourself some money on this job you decide to act as your own general contractor. As such, you're responsible for hiring and overseeing each subcontractor. You get quotes from five plumbers. Four of them submit similar proposals, but one comes in way under the others. Without reviewing the details of the proposals, you pick the cheapest one. After all, you're trying to save money, right?
You hire the plumber and he immediately goes to work. First, he installs the water heater. Then he runs water lines: one line to the kitchen, one line to each bathroom, a line to the outdoor kitchen, another line to the wet bar, and one last line to the steam room.
He then installs faucets and showers. He hooks up appliances to the water lines: dishwasher, refrigerator, etc. He calls you up to tell you he's all done. He actually came in under budget and ahead of schedule!
You're waiting on the electrician to finish up. Then it's just drywall, paint, one final inspection, and you will be able to move in.
Act II
Move-in day arrives and you're beside yourself. You never could have imagined being able to afford such an amazing house. And, if not for the money you saved acting as general contractor, you probably could not have afforded a house like this. You smile with pride as you walk in the front door.
You're so excited you can't contain yourself...literally. You go to the bathroom for some much needed relief. The toilet flushes with a satisfying whoosh and you think to yourself, "I can't believe those other plumbers wanted to charge me so much more than the guy I hired. It's just plumbing, how hard could it be anyway?"
You go to wash your hands, but the water never warms up. It's ice cold. You go to turn it up, but there's not even a hot or cold indicator on the faucet. It seems odd, but you shrug it off. You head to the kitchen to wash your hands there. More ice cold water. And this faucet has no hot or cold indicator, either.
You call up the plumber, "Hey, I don't think the water heater is working right. Can you come check it out?"
"Sure, I'll be right over."
The plumber arrives and inspects the water heater. He comes back after a few minutes. "Everything looks good," he says.
"Great, let me test the sink quick," you reply. You turn the kitchen faucet on and let the water run. It's still ice cold. "Must need a minute to warm up," you say. "Hey, while we're waiting, can you show me how to adjust the temperature on this faucet? There's no hot or cold indicator."
"Oh, you don't adjust the temperature at the faucet," the plumber replies. "You do that at the water heater."
"I don't understand."
"Yeah, the water temperature is controlled at the water heater."
"Are you talking about an anti-scald feature that limits the maximum water temperature?"
"No, I mean the water temperature in the house is controlled at the water heater. If you want a glass of ice water, you go to the water heater and set the temperature to cold. When you want to take a hot shower, you set the temperature to hot. When you want to use the steam room, you turn the temperature as high as it will go. It couldn't be simpler."
"Are you telling me that the only way to set the water temperature for any sink in the whole house is to adjust it at the water heater? You can't be serious," you say incredulously. "Why would you do it that way?"
"It's easier. And cheaper. If you wanted to control the temperature at ... every ... single ... faucet ... I would have had to run two water lines to each room: a hot and a cold. Plus, every faucet would have needed to accept two water lines."
"But what if I want a glass of ice water while someone is in the shower?"
"I don't know what to tell you. You're going to have to be aware of the side effects of this design. Whenever you change the temperature it's going to affect the whole house. I guess you'll just have to be really careful. Any time you use the water in one room, you'll have to first make sure that it's not being used in any other room."
"But what if my wife is using the steam room and I have to use the bathroom?!"
"Well, in that scenario," the plumber smiles, "I would recommend you not use the bidet."
Act III
After firing the plumber, you call one of the others on your list to have him come in and fix the problem. You ask him for a proposal, and his quote to fix the problem is higher than his original quote to plumb the whole house!
"How can this be!" you demand.
"Well, there's a lot more work involved now. When running water lines in new construction, all the walls are open. It's easy to get access to the studs and drill holes to run the lines. Now I'll have to cut out sections of drywall to run all the additional lines. That means I'll also have to come back through and replace and repaint the drywall when I'm done running lines.
"Then there's the matter of timing. Since the house is occupied now, I'll have to work on one room at a time. I can't work on all the bathrooms simultaneously--which would be more efficient for me--because you always need to be able to use at least one."
"Is that all?" you ask wearily.
"There is one more thing," he says. "You won't be able to start controlling hot and cold water in the individual rooms until I get every room re-plumbed."
You stand there dumbstruck as you take it all in.
After several seconds, the plumber breaks the silence. "Can I ask you a question?"
"Sure," you wearily reply.
"Why in the world did you plumb your house like this in the first place?"
You sheepishly reply, "I wanted to save money."
Try as he might, your plumber can't quite stifle his belly laugh. "Well I guess that didn't work out so well for you, now did it?"
Image by Heiko Koppenhagen from Pixabay