Jump to content

Why is doing this bad?


RuleBritannia
Go to solution Solved by requinix,

Recommended Posts

So, Throughout my script it seemed logically correct to call a function via a variable value.

 

Example I have

function computer()
{
return  'pcs are good';
}

function laptop()
{
return  'laptops are ok';
}

$value = 'computer';

echo $value();

$value = 'laptop';

echo $value();

However, After just googling this, I saw alot of criticism towards this technique, Whats the problem with this?

 

Thanks in advance.

Link to comment
Share on other sites

  • Solution

No support from the IDE about what is executing. No help from your IDE about possible errors. No idea where all the computer/laptop functions are being called. Slow function calls. Prone to bugs that can be difficult to track down. Lends itself to magical behaviors that are crazy to maintain and support. Adds another attack vector to your code. Using a feature just for the sake of using a feature. Unreadable.

Link to comment
Share on other sites

No support from the IDE about what is executing. No help from your IDE about possible errors. No idea where all the computer/laptop functions are being called. Slow function calls. Prone to bugs that can be difficult to track down. Lends itself to magical behaviors that are crazy to maintain and support. Adds another attack vector to your code. Using a feature just for the sake of using a feature. Unreadable.

Thanks for your in depth response, But I wanted to ask 1-2 questions.

 

"No support from the IDE about what is executing."

 

I dont use a  IDE, Just notepad.

 

"No help from your IDE about possible errors"

 

Again.

 

"Slow function calls."

 

So this actually causes more resources? A big amount or?

 

"Prone to bugs that can be difficult to track down."

 

If you captured error messages at all, Wouldnt the same apply however you called the function?

 

"Adds another attack vector to your code."

 

Yeh I can see this is a good point. Overlooked this.

 

"Using a feature just for the sake of using a feature."

 

It was logical in this case from my view at the time, so wasnt just out of the sake.

 

"Lends itself to magical behaviors that are crazy to maintain and support."

 

What other magical behaviors could those be?

 

"Unreadable."

 

Yes I guess, but only planned to use it once.

Link to comment
Share on other sites

"No support from the IDE about what is executing."

 

I dont use a  IDE, Just notepad.

 

"No help from your IDE about possible errors"

 

Again.

When you're ready to become a serious programmer, start looking into the tons of PHP IDEs available.

 

"Slow function calls."

 

So this actually causes more resources? A big amount or?

A small but unnecessary amount.

 

"Prone to bugs that can be difficult to track down."

 

If you captured error messages at all, Wouldnt the same apply however you called the function?

But where did $value get set to the wrong function? Is it happening that way in more than one place? It's a rabbit hole.

 

"Lends itself to magical behaviors that are crazy to maintain and support."

 

What other magical behaviors could those be?

"Magical", as in "we don't know quite how or why the variables are set to what they are so as far as you're concerned it's magical". Put like that there are tons of things that could qualify as magical. PHP even has things called "magic methods" (__get and __set and such) which are only demystified once you learn how the trick works, but until you learn about them being able to set values on member variables even though they don't exist is "magical".

 

Magical behavior is bad because you don't know why things work the way they do. Magical behavior is difficult to track down and often harder to change. It is poor practice because it impedes your work and the work of anybody who has to maintain the code in the future. Some PHP programmers avoid __get/set and such for that same reason, though not as much because they're firmly entrenched in its implementation of object-oriented programming. Variable variables are not; should they ever be then doing things like echo $value(); may gain wider acceptance... provided all the other problems with it are dealt with.

 

 

With all that said, sometimes there are legitimate uses for it. What I'm saying is that your example, and in fact most times I see it, is not one of them. Maybe you'd like to elaborate on what exactly you were planning?

Link to comment
Share on other sites

When you're ready to become a serious programmer, start looking into the tons of PHP IDEs available.

 

A small but unnecessary amount.

 

But where did $value get set to the wrong function? Is it happening that way in more than one place? It's a rabbit hole.

 

"Magical", as in "we don't know quite how or why the variables are set to what they are so as far as you're concerned it's magical". Put like that there are tons of things that could qualify as magical. PHP even has things called "magic methods" (__get and __set and such) which are only demystified once you learn how the trick works, but until you learn about them being able to set values on member variables even though they don't exist is "magical".

 

Magical behavior is bad because you don't know why things work the way they do. Magical behavior is difficult to track down and often harder to change. It is poor practice because it impedes your work and the work of anybody who has to maintain the code in the future. Some PHP programmers avoid __get/set and such for that same reason, though not as much because they're firmly entrenched in its implementation of object-oriented programming. Variable variables are not; should they ever be then doing things like echo $value(); may gain wider acceptance... provided all the other problems with it are dealt with.

 

 

With all that said, sometimes there are legitimate uses for it. What I'm saying is that your example, and in fact most times I see it, is not one of them. Maybe you'd like to elaborate on what exactly you were planning?

 

Thanks.

 

In regards to what im planning, more like already planned and executed.

 

I have some scripts which pull data from a table, this table data like

 

name > core i7

brand > intel

 

for each brand I have made functions, which have the settings for the brand pages(diff bg , header sizes, etc)

 

When I query the table, I can call the function like

 

$query['brand']();

So whatever is that value, the corresponding function will be called.

Link to comment
Share on other sites

As for how the data got in the table, I set it myself, So there wont be any brands without a function etc, Either way, This example probably falls into your "illegitimate" uses for this methodology.

 

If you care to provide a example, How would you personally deal with this?

 

Thanks in advance.

 

What do you mean "there won't be any brands without a function?"

Link to comment
Share on other sites

:(    you create a function that accepts the brand.

 

 

function isBrandOkay($brand){

     switch(strtolower($brand)){

           case 'laptop': return true;

           case 'computer': return true;

       }

       return false;

}

$thisBrand = 'computer';

echo  $thisBrand  . ((isBrandOkay($thisBrand)) ? ' is okay!' : ' is <b>not</b> okay');

Link to comment
Share on other sites

objnoob is on to the right idea.

 

function setSettingsForBrand($brand) {
  switch ($brand) {
    case 'intel':
      setIntelSettings();
      break;
    default:
      setDefaultSettings();
      break;
  }
}
So in the case of where you have a brand with no corresponding function, or misspelled the brand name, your application wouldn't error out but instead simply apply the default.

 

Magic is cool, but not in your code. Because in the end it will pull it's greatest trick on you.

Edited by ignace
Link to comment
Share on other sites

here is a 'test' that shows why you wouldn't write code with functions (or variables) named after specific categories/manufactures/brands.... -

 

to add, remove, or change a category/manufacturer/brand..., you should not find yourself writing or editing program logic. your program logic should be general purpose so that it can operate on any number of categories/manufactures/brands simply by defining the necessary 'data' entries somewhere (database tables, arrays, xml, serialized/json data.)

 

i'm betting the only thing your named hard-coded functions are doing differently between them is in the data values that are being assigned? if so, you have taken a data driven design and hard-coded it into your program so that it now requires a "programmer" to perform a simple operation like adding a new brand or changing one of the values. this will get you escorted out the door in a real business. anyone (authorized to do so) should be able to add/change categories/manufactures/brands through a user interface that modifies stored data values, not by adding/modifying functions in the code.

Link to comment
Share on other sites

What do you mean "there won't be any brands without a function?"

 

If there was no function to match the brand, When calling $brand(); it wouldnt work.

 

objnoob is on to the right idea.

 

function setSettingsForBrand($brand) {
  switch ($brand) {
    case 'intel':
      setIntelSettings();
      break;
    default:
      setDefaultSettings();
      break;
  }
}
So in the case of where you have a brand with no corresponding function, or misspelled the brand name, your application wouldn't error out but instead simply apply the default.

 

Magic is cool, but not in your code. Because in the end it will pull it's greatest trick on you.

 

 

This approach looks better, thanks

 

here is a 'test' that shows why you wouldn't write code with functions (or variables) named after specific categories/manufactures/brands.... -

 

to add, remove, or change a category/manufacturer/brand..., you should not find yourself writing or editing program logic. your program logic should be general purpose so that it can operate on any number of categories/manufactures/brands simply by defining the necessary 'data' entries somewhere (database tables, arrays, xml, serialized/json data.)

 

i'm betting the only thing your named hard-coded functions are doing differently between them is in the data values that are being assigned? if so, you have taken a data driven design and hard-coded it into your program so that it now requires a "programmer" to perform a simple operation like adding a new brand or changing one of the values. this will get you escorted out the door in a real business. anyone (authorized to do so) should be able to add/change categories/manufactures/brands through a user interface that modifies stored data values, not by adding/modifying functions in the code.

 

I would not do this in a program which is created for people other than myself, This application is just for me.

Edited by RuleBritannia
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.