A forgetful programmer and the rule-that-must-not-be-forgotten

If I had an hour to save the world, I’d spend 55 minutes defining the problem

Every time en route to my seat, I’ve walked by a wall with this saying. And every time without fail, the saying has walked out of my mind

By Karen Roe from Bury St Edmunds, Suffolk, UK [CC BY 2.0 (https://creativecommons.org/licenses/by/2.0)], via Wikimedia Commons

Before I forget, I’ll show you how I solved one fateful programming problem to illustrate how much better life could have been if I had just remembered to remember this one saying. Only if I were an elephant. Sigh!

P.S.: Not that I wanna eat a ton of food everyday. Although, that wont be bad, I’ll admit. Who doesn’t wanna bankrupt Barbeque Nation! I want the elephant’s memory

The Fateful Programming Problem

Write a program to take a number as input and return the number-in-words as output. For example, 123414 should output one lakh, twenty-three thousand, four hundred and fourteen

Seemed easy enough. Below is how I went about solving it

First Principles Thinking

First principles are the fundamental truths about anything. Meaning we can’t break it down any further. Just thinking using common sense gets us to this stage quickly

The fundamental truths as I saw them were, we read numbers left to right, crores and lakhs to tens and ones. Exceptions are 11–19 which are unlike other two digit numbers, 11 is called eleven and not ten-one

In real-life, I convert numbers to words as easy as breathing. Everyone does. I was satisfied that the logic cant be broken down further. I went ahead and wrote code


Don’t repeat yourself

There was a lot of repetitive code in my first attempt. So, I made it better


What else can I do?

Instead of hardcoding, I can parametrize the breakpoints. The user can provide breakpoints in millions and billions if it suits their need

I remember now. They always said to avoid hardcoding

Totally forgot the testing aspect. I always do. The parametrized function is going to need a zillion test cases to test if it outputs the millions and billions and crores and lakhs correctly!

They also said write test cases first. So that I wont forget, I guess. Hmph!

If I break down the function into smaller components, they wont require so many test cases

Ah! Single Responsibility Principle

If I have smaller and generic component functions, then I can use them in other places too

Modularity and reusability say hello

There must be a zillion other things they said. I cant even remember all these programming practices

My forgetful memory remembers to say Hi! too

Will my code be always missing some trick? Can I write the best code anyone can write? How can I even know?

If I had an hour to save the world, I’d spend 55 minutes defining the problem

Looking at how I solved the problem, ah, I see that I’m already talking about solving the problem! I spent about half a heartbeat thinking about the problem, it was as easy as breathing remember? I spent zero time defining the problem and most of my time solving it

The proverbial 55 minutes

Einstein said the 55 minute thing and they say he is a genius. So, let me actually spend some time defining the problem and see what happens

Problem definition : Take One : ACTION!

Numbers. Numbers are made up of crores, lakhs, thousands and so on. Or billions, millions, thousands and so on. Given a number, say how many crores and lakhs and so on are present in the given number

Okay. These millions and crores are called number system units. The “how many” of each unit? Those in turn are numbers, again. Inception! But these numbers will max be three digit numbers, right? Like 1,234,567. Thats 1 million, 234 thousands and 567 ones

Problem definition : Take Two : ACTION!

Given one number made up of number system units, split it into number system units. Then spell out the 3-digit quantity of each number system unit, spell out the name of the corresponding number system unit. Put this all together to make number-in-words and return it

Reads better. I can write it point-by-point now

Problem definition : Take Three: ACTION!

Function to spell out any-digit number
Split the number into number system units and their quantities
If quantity has more than 3-digits
Call this same function to spell it out
Call the function to spell out a 3-digit number
For each number system unit, spell out its name
Put all these words together in the right order
Function to spell out a 3-digit number
Given a 3-digit number
Split it into hundreds and remainder
Spell out the quantity of hundreds
Spell out the remainder
Put those two words together in the right order

Ha! Now it is like I’m writing code in words. I broke down the problem definition into simple and clear parts. This took me around 5 mins so far

It helped me see that the quantity can have more than 3-digits when the number is bigger than 999 crores or 999 billions. I handled that. I can also see that I’ve not yet thought about how to handle zero anywhere

With a few more iterations I can define the problem very very clearly, handle special scenarios and it will be very close to the simplest way to represent this problem

Remember programming best practices? It feels to me like the characteristics of this problem definition will make a pretty good list of programming best practices

I don’t know if this’ll be the best anyone can do. But hey, thats why we have peer reviews

Okay Einstein! You are a genius alright. Only if you had taken 1 out of those 55 minutes to say something about how to remember stuff. Sigh!

A forgetful programmer and the rule-that-must-not-be-forgotten was originally published in Hacker Noon on Medium, where people are continuing the conversation by highlighting and responding to this story.