DEV Community

Pavel Morava
Pavel Morava

Posted on • Updated on

Do not lose information until the last minute

I am going to teach you one of the most important design principle in less than 20 lines.

Consider following:

round(10.4) + round(10.3) + round(10.4) = 30
Enter fullscreen mode Exit fullscreen mode

and now what about?

round(10.4 + 10.3 + 10.4) = 31
Enter fullscreen mode Exit fullscreen mode

Can you see the difference?

Well, you should because 30 doesn't equal to 31.

So thank you for the attention. This is the end of today's lesson. To put it bluntly, you should postpone any operation, in which any information loss can occur to the latest possible moment.

THE END

Explanation

To round a rational number may reduce data in a significant manner. Or it may not:

round(10.3) + round(10.7) = 21
round(10.3 + 10.7) = 21
Enter fullscreen mode Exit fullscreen mode

What is important here? The operation of rounding is irreversible, meaning we cannot deduce the input from the output.

See here

10 =  round(10.0 or 10.1 or 10.2 or 10.3 or 10.4)?
Enter fullscreen mode Exit fullscreen mode

So far, so good? It is an elementary school, right?

Now what about:

function read_file_name(file):
    return file.name if file.exists else None
Enter fullscreen mode Exit fullscreen mode

How would you decipher and reverse any useful information from None?

What about:

function read_file_name(file):
    return file.name if file.exists else
        FileNotFound("This file doesn't exist!")
Enter fullscreen mode Exit fullscreen mode

This a tad better, isn't it? At least we get the info what went wrong. Some file was not found. Unfortunately, we still have no idea what file was in the question. To make the information complete we have to add the file to the error message.

function read_file_name(file):
    return file.name if file.exists else
        FileNotFound("This {file} doesn't exist!")
Enter fullscreen mode Exit fullscreen mode

Is it perfect? For a human being, this brings enough data to understand the problem. Not so much from the computer's point of view. The offered text lacks something. Can you guess what?

Yes, it is the structure, apparently. Perhaps you can parse the file from the message, but typically, a computer demands us to be more explicit.

Let's rewrite our function once again:

function read_file_name(file):
    return file.name if file.exists else
        FileNotFound("This file doesn't exist!",
        file=file)
Enter fullscreen mode Exit fullscreen mode

Now do we have the complete information necessary to reverse the flow of data without any loss? In a sense yes, but we have shifted from the information loss to the information redundancy. We need to know what happened and what caused the problem, not a love letter explaining what exactly means that the concrete file was not found.

Suffice it to write:

function read_file_name(file):
    return file.name if file.exists else
        FileNotFound(
        file=file)
Enter fullscreen mode Exit fullscreen mode

I plan to dive into problems with redundancy later, so wait until the next article.

Conclusion

The later you reduce any information, the better. Let the decision how to proceed with the result be on the one who asks, not the one who answers.

Just imagine a clerk at a post office, throwing your letter into the bin because you have provided the incorrect address.

Would not you prefer if she returned the letter back to you with proper description of the issue, leaving onto you whether to scrap or rewrite it?

Discussion (0)