What is the purpose of the "assert()" function and when should we use it?

Asked

Viewed 2,240 times

11

According to the PHP manual

assert - Check whether a statement is FALSE

Or

assert() checks the informed assertion and takes appropriate action if its result is FALSE.

And there’s this example of using function assert() which is used to check whether a statement is false:

<?php
assert(true == false);
echo 'Hi!';

Source.


Question

Even reading about the function assert() I still can’t understand what its purpose is, and when I should use it. If it serves only to check whether a statement/statement is fake because we don’t use a if (condição falsa/verdadeira) just at once instead of her?

Therefore, I would like to know what is the purpose of the function assert() and when we should use it?

1 answer

9


Definition

It is a function of assertion. It exists in almost all languages. Some have more sophisticated and alternative mechanisms.

Assertion - Proposition judged true

Priberam Dictionary.

So it’s a resource to prove if something is true. It’s not a decision mechanism. It’s just a statement.

When to use

We use it when we hope it will always be true. In a if we expect both things to happen. In assert a false result indicates problems. It is a documentation mechanism and not the algorithm itself. Her only goal is to say whether or not something checks with the expectation that the programmer wrote there.

In PHP I don’t see people using. The type of application (doesn’t need the maximum robustness, after all are scripts, right? ) and the kind of typical PHP programmer doesn’t care much about that.

We don’t use the if because this code is not part of the application, it is part of the "test" of the application. The goal is another. In production it "does not exist". Developing helps identify situations where the code is not behaving as expected.

This function should only be used for formal testing or eventual debugging testing. So it can be turned off. So in production it should not run, and it will not cost (in PHP it technically has because it will need to be interpreted, but not executed, but this cost is/should be negligible). Obviously some tools can be used to automate the verification process.

Mechanism is different from result

That’s what I said in a response on the if. Programmers need to dissociate the idea from a boolean expression and if to walk together. In this case the Boolean expression serves to say for the programmer if the code is ok, but it’s the mechanism used that does it. The boolean expression only generates a result, what will be done with it is the subject of the programmer. It can be used in:

  • a calculation,
  • in a flow control instruction making a decision,
  • only print,
  • or if you just want to know if it’s right,
  • etc..

Validity and quality of use

The quality of the expression helps to test is as good as the programmer’s ability to identify what might go wrong. Naive programmers tend to find themselves filled with assert() or other similar things the code will be bulletproof (as occurs with exception). Relapsed programmers don’t care about testing, of any kind. Pragmatic programmers know when to use everything.

This is achieved by studying each resource, seeing the use in good third-party codes and using your own experience. It is normal to have difficulty using in the beginning not only the assert(), almost anything in programming. So it’s always interesting to have quality mentors to go showing in each case whether you’re doing it right or not.

Testing and contracts

Note that this has nothing to do with unit tests and other tests existing outside the code. But asserts used to be used inside them.

He can be seen as a design by Contract poor. Therefore it is very common to be used to evaluate function parameters, results that they return, or if the state of an object has come out of a valid situation at some point. In some languages it is part of the language and helps the compiler produce better code. In a dynamically typed language is even more important and type checking is one of the greatest uses.

An example would be to check if the received parameter is within a range of values expected. Note that this differs from if because in this case if a number comes out of the track means there was a programming error, it was not an expected situation. If formal tests are done properly in every application, you may encounter problems using that function before sending to production.

Abuse

Some people use it as code control in production. This can be considered an abuse, although it works. In general the execution is interrupted, as an exception, after all if a assert is false, there is a programming error that needs to be fixed. It is even possible to launch an exception optionally.

When you do this you cannot disable its use, then the assert has now been used in place of if, mixed mechanisms, used the test where the logic of the algorithm should be.

Reading supplement

  • 1

    +1 for the very complete answer. I also don’t see PHP programmers using assert, perhaps depending on the language type system.

  • @eightShirt edited, in fact the language should use even more. PHP 7 has mechanism that partly dispenses with the assert to check types.

  • @Maniero in case the assert is false what happens? The script aborts with a message? Or will some message for the logo?

  • 1

    @Jorgeb. Actually I don’t remember anymore, it’s been a while since I didn’t use PHP and I’ve never been a great user of this function, and I know it’s been changing in recent versions. What I remember you turn on and off or configure what to do, but don’t ask me for details :D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.