Functions defined within conditional blocks

Asked

Viewed 29 times

0

I am studying PHP and reading the documentation I came across this example:

<?php

$makefoo = true;

/* Nos nao podemos chamar foo() daqui
   porque ela ainda não existe,
   mas nos podemos chamar bar() */

bar();

if ($makefoo) {
  function foo()
  {
    echo "Eu não existo até que o programa passe por aqui.\n";
  }
}

/* Agora nos podemos chamar foo()
   porque $makefoo foi avaliado como true */

if ($makefoo) foo();

function bar()
{
  echo "Eu existo imediatamente desde o programa começar.\n";
}

?>

I understood that the function will only come into existence if the condition is evaluated as true, but opened me some doubts, because what I believe to be normal (from what I see in scripts third party) is to define functions and decide which will invoke from conditional tests, but in this example of documentation the test will define whether the function will be declared or not, preventing it from being invoked later in negative case, which leads me to some doubts:

Is that considered good practice? Since the code I believe becomes more complex, because the rest of my code must be prepared to work perfectly even with the absence of this function.

Would you have any example of application? I’m the type who understands things more clearly with some example.

  • 1

    I personally don’t see any reason to call a function before it’s set. In my opinion it is counterintuitive and the code should be intuitive. In fact, what should be bar in his case?

1 answer

2


For those who follow me know that I am critical of good practices, and of course, the bad ones too. I like to understand motivations, have the foundation to do right. And I talk a lot about context. Everything can be valid in the right context. And it may be that the valid context is a meaningless one.

The question does not show the reference to give context because the code was made like this. It may have a reason. And whoever did it should normally justify it. In programming we must do only what we can justify. We cannot do because someone has done it before, it must always be because they need it. And it can’t be contrived, forced justification, it has to be a fair use even.

What problem does that attempt to solve? I cannot see a reason for it. So if the user cannot show the advantage is not good practice.

I suspect, and I only suspect that it may be an optimization because it is an interpreted language, and silly, because if language needs it and you benefit fundamentally from doing so it is the wrong tool. And there are cases that will be worse, especially if you are using some tool that caches compiled code.

It could also be defensive programming, but in the wrong way. The code is trying to solve a possible problem, but this is programming error, and we fix the code and do not try to circumvent the execution.

Finally it could be some "clever trick," what’s called Clever code to be able to do some very flexible things, but seems to me gambiarra. It may be a way to create a template, but that way I would never use.

If at least the function had local scope, but will have global scope equal to the others.

In fact the code becomes complex and can not see the advantage. I actually consider it an error of the language to allow this kind of thing.

Create the functions you need and decide at some point whether to call them, but not the definition. Declarations and definitions should never be conditional.

  • Reading the documentation and all of a sudden you get this way of defining functions, I thought "Nossa nunca tinha visto, mas onde usaria?

  • It’s like you said: "Dependendo do contexto, tudo pode ser válido".

Browser other questions tagged

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