Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do preconditions ALWAYS have to be checked? [closed]

These days I'm used to checking every single precondition for every function since I got the habit from an OS programming course back at uni.

On the other hand, at the software engineering course we were taught that a common precondition should only be checked once, so for example, if a function is delegating to another function, the first function should check them but checking them again in the second one is redundant.

I do see the redundancy point, but I certainly feel it's safer to always check them, plus you don't have to keep track of where they were checked previously.

What's the best practice here?

like image 879
Pin Avatar asked May 28 '10 11:05

Pin


People also ask

What happens when a precondition is not met?

If a precondition is violated, the effect of the section of code becomes undefined and thus may or may not carry out its intended work. Security problems can arise due to incorrect preconditions. Often, preconditions are simply included in the documentation of the affected section of code.

What's a precondition check?

Preconditions are the combination of all necessary preparatory steps (program settings, testing environment), needed for executing this test case. Preconditions contain important data (steps) needed for the initial preparation of OS, test program, mobile device, browser, etc.

What is the difference between precondition and postcondition?

A precondition is something that must be true at the start of a function in order for it to work correctly. A postcondition is something that the function guarantees is true when it finishes. An invariant is something that is always true at a particular point inside a piece of code.

What is the purpose of a precondition?

A precondition is defined on operations as a constraint that must hold true before the execution of an operation.


1 Answers

I have seen no "hard and fast" rule on how to check preconditions, but I generally treat it like method documentation. If it is publicly scoped, I assert that the preconditions are met. The logic behind this would be that the scope dictates that you are expecting consumption on a broader scale and with less influence.

Personally, the effort to put assertions around private methods is something I reserve for "mission critical" methods, which would basically be ones that either perform a critical task, are subject to external compliance requirements, or are non-recoverable in the event of an exception. These are largely "judgment calls".

The time saved can be reinvested in thorough unit and integration test enhancement to try and flush out these issues and puts the tooling in place to help enforce the quality of the input assertions as it would be consumed by a client, whether it is a class in your control or not.

like image 138
Joseph Ferris Avatar answered Oct 22 '22 07:10

Joseph Ferris