## DEV Community is a community of 801,489 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

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
``````

``````round(10.4 + 10.3 + 10.4) = 31
``````

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
``````

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)?
``````

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

``````function read_file_name(file):
return file.name if file.exists else None
``````

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

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

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!")
``````

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)
``````

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)
``````

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?