Use variables to define an object or not?

Asked

Viewed 34 times

0

Recently watching some tutorials, I see programmers omitting the variables in code block. I don’t know what this practice is called, but I wonder if it is recommended, what are the advantages and disadvantages in its use, and if the way I put it is correct. The point is, it’s working...

Example:

$head = new MakeHead("Title");
    $head->_addmenu("180","./insert");
    $head->_addtoggle('190', 'toggle', 'bar');
$head->_printhead();

To:

(new MakeHead("Title"))
    ->_addmenu("180","./insert")
    ->_addtoggle('190', 'togglesearch', 'searchbar')
->_printhead();
  • 1

    I see no advantages, only the disadvantages in which you completely lose your object, which can make it difficult to maintain the code and debug operations, not to mention that, in my view, it impairs the readability of the code.

  • i agree with @Woss, maybe it makes sense to use an instance that will only do an operation maybe, but in this example makes 3, it would make more sense to have the variable and help in debug tbm

  • If you are not using variable it is almost certain that you also did not need a class.

  • I get it. Although it works, Phpstorm gives a Warning saying that my Function is not VOID, and even if I define them as method():void, Phpstorm removes Warning but fails to provide the class and method HINTS...

1 answer

1

The answer as always Depends.

Before deciding whether or not you need a variable you have to understand why you want the object. The goal of a variable is to store a reference to an object or value so that you can use it later, in this sense if there is no need to use it later you don’t need to store it in a variable (or constant).

In this example you gave we see a fluid interface, we give this name to classes whose methods return a reference of itself or a new instance of it, allowing to invoke a method upon the return of the method.

An example of this can be found in the PSR-7 specifications, where as you invoke the methods to build an http message (e.g. Request) you receive a new instance of that class and can chain calls.

This type of behavior is also common in the Builder project pattern, where a Builder returns to itself until a construction method (e.g. make, build or create) is called by returning what the Builder builds.

The main points where you don’t need to have a reference in a variable are:

  • You will create the object only to call a method, e.g..
(new Logger("Startup"))->log("mensagem");

In this example we assume that we will not need this instance of logger after using it, if we needed we could store its reference in a variable.

  • You will pass an object as a parameter once you create it.
$client->send(new User("João"));

In this example if the object is simple and the reading is not compromised you can instantiate the direct object in the method call.

An important point to remember is that you’re not programming for the computer to understand, you’re programming so that humans (you and whoever else is going to maintain this code) understand what’s been programmed, then you should try to make your code readable whenever possible, if it requires creating variables, create.

If you were making code for machines only you could name all your variables with varNumer, ex. var1, var2, var3, var4, etc., since the machine would understand, giving names that mean something is something we do so that we developers can understand what the program does with as little cognitive effort as possible.

  • Got it. So, in other words, in the example I posted it wouldn’t be interesting to use. Basically this routine creates a header on my page using a template of its own. Although there is no treatment, only execution of the requested methods with the past information

  • Hello @Evandroph, it actually depends, if it is readable for you and your team and you no longer need to call this instance of the header then you do not need to associate to a variable. It is a matter of intention, what is your intention with this object, just print the header and do nothing else? In this case maybe that way it works better. One advice I can give however is not to sacrifice reading to save lines, php has an optimizer that checks the code before running, so it will not be declare a variable that will end the performance of your program.

  • Just what I imagined. I did some more research and found something about chained methods, which is basically what I mentioned. In practice it will not change much for me not. In fact it would only be for the sake of indentation of the same code. The Phpstorm Insert correctly if I use the Chained Method, if I don’t use it removes the indentations.

Browser other questions tagged

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