Home > database >  Assertive code in programming and the definition of it
Assertive code in programming and the definition of it

Time:11-20

Is there a simple definition?

What is the nature, so to speak, of "assertive code"?

All the definitions of this I have, by now, found are very vague.

Is there something I can read that is concise and to the point without using a lot of jargon?

I think that the jargon could be a problem in my case. I am quite dumb but I wanna learn it so any help and pointers are welcome.

CodePudding user response:

An assert statement is essentially an if statement that will print an error (and, sometimes, stop the program) if the condition is false. If the condition is true, it will do nothing.

Assertions are normally used in software testing. You use them to check that a program behaves in a way that you expect it to. In other words, they will sound an alarm when a program violates an assumption that the programmer wanted to check.

However, there's nothing preventing you from leaving assertions in your production code too. This can sometimes be beneficial, especially in cases where you cannot easily simulate the program with a test - for example because you don't have the real data to test it with.

In such cases you typically want failed assertions just to print a message to a log file. After having your program run for a while, you check the log file and if everything is OK, there should be no messages about failed assertions.

CodePudding user response:

When you write "imperative code", you tell the computer what to do.

When you write "declarative code", you tell the computer what to produce.

When you write "assertive code", you tell the computer what you expect to be true.

The phrase "assertive code" isn't nearly as common as the other two, and is used in different ways in practice. In an common OO language it usually just refers to using assert expressions to catch bugs. In functional programming (the example you provide), it usually refers to pattern matching and destructuring constructs that imply a particular shape for their inputs. In a language like Prolog, it can refer to a definition of goals that the program must resolve.

  • Related