PDA

View Full Version : Ternary or if/else, or something better?


Franki
25-05-2011, 06:37 PM
PHP gurus, which would be better to use in this case, and assuming that there are going to be 1 or 2 dozen variables to define:



if ($a == "yes"){
define('x', 'x');
define('y', 'y');
define('z', 'z');
}
else {
define('x', '1');
define('y', '2');
define('z', '3');
}

// or

$x = ($a == "yes") ? 'x' : '1';
$y = ($a == "yes") ? 'y' : '2';
$z = ($a == "yes") ? 'z' : '3';



Or is there a more efficient way?

Thanks.

indhyper
25-05-2011, 08:16 PM
What's the bigger thing you are trying to do? Perhaps there's a better way to do it higher up? (Checkbox sets etc)

Franki
26-05-2011, 09:16 AM
This is actually the "top" so to speak, everything else starts from here.

madpilot
26-05-2011, 10:19 AM
PHP gurus, which would be better to use in this case, and assuming that there are going to be 1 or 2 dozen variables to define:



if ($a == "yes"){
define('x', 'x');
define('y', 'y');
define('z', 'z');
}
else {
define('x', '1');
define('y', '2');
define('z', '3');
}

// or

$x = ($a == "yes") ? 'x' : '1';
$y = ($a == "yes") ? 'y' : '2';
$z = ($a == "yes") ? 'z' : '3';



Or is there a more efficient way?

Thanks.

Those aren't quite equivalent - in the first one you are defining constants, the second your are creating variables (Which in this case is more correct).

On ternary vs if, it's a matter of style - although I'd go with an if in this case, as it's less typing.

Shane SF
26-05-2011, 10:23 AM
Without knowing the bigger picture, I can only comment on the specific example above. The first version is more efficient. The second version does a comparison for every single assignment.

Franki
26-05-2011, 10:48 AM
Thanks gents.

In terms of efficiency, do you think there's much difference in performance if there are only a couple of dozen assignments?

I like the ternary on the thought that everything is in one line, so adding new variables may be less prone to errors and easier, ie less scrolling, and you don't have to add them in both if and else areas. ?

Shane SF
26-05-2011, 12:19 PM
I would favour correctness first. If you don't want the value of "x" to be changed once you've set it, then use a constant. It guarantees immutability and gives you the safety and peace of mind that your assumption at this point will be enforced later on.

I would favour clarity second. A define spells out your intention clearly - "This is a constant". You are telling the reader (yourself three months later, or another coder) in no uncertain terms what you mean for 'x'.

I would favour readability third. The first example also allows the reader to form an idea around the fact that you want a bunch of things to happen if $a is equal to yes. It's easier to understand versus having to re-evaluate each statement in isolation.

I would favour efficiency fourth.

Ease of editing the text shouldn't really come into it. That's what Vim is for :P

Franki
26-05-2011, 01:50 PM
ah gotcha. cheers :)

Mr C
13-06-2011, 05:46 PM
PHP gurus, which would be better to use in this case, and assuming that there are going to be 1 or 2 dozen variables to define:



if ($a == "yes"){
define('x', 'x');
define('y', 'y');
define('z', 'z');
}
else {
define('x', '1');
define('y', '2');
define('z', '3');
}

// or

$x = ($a == "yes") ? 'x' : '1';
$y = ($a == "yes") ? 'y' : '2';
$z = ($a == "yes") ? 'z' : '3';



Or is there a more efficient way?

Thanks.


Or even (assuming that in the future $a might sometimes have another option)




switch ($a){

case "yes":
define('x', 'x');
define('y', 'y');
define('z', 'z');
break;

case "sometimes":
define('x', 'x');
define('y', '11');
define('z', '42');
break;


case "no":
default :
define('x', '1');
define('y', '2');
define('z', '3');

}

nebbian
13-06-2011, 06:44 PM
I'd even be tempted to use an array for config variables:



$configArray = array(
"yes" => array(
"x" => "x",
"y" => "y",
"z" => "z"
),
"no" => array(
"x" => "1",
"y" => "2",
"z" => "3"
),
"file_not_found" => array(
"x" => "x-ray",
"y" => "yankee",
"z" => "zulu"
)
);

$myConfig = $configArray[$a];



Then when you want to use your variables, use something like


$theValueIWant = $myConfig['x'];


This has the benefit of separating your code from your data, which is almost always a good thing. It allows you to quickly scan your data to see what belongs to what, without having to mentally parse all those extra supporting statements. It also allows you to put that data somewhere else, in a config file for example, or in a database.

If you really wanted to use defines then you could then do this:

foreach($myConfig as $configKey => $configValue) {
define($configKey, $configValue);
}


but remember that global variables are bad, mmkay!

Franki
14-06-2011, 10:25 AM
Thanks! I do like switches :)

Why is global vars a bad thing? (I'm learning)

Mr C
14-06-2011, 01:05 PM
Some reasons why.
http://c2.com/cgi/wiki?GlobalVariablesAreBad

Franki
15-06-2011, 01:26 PM
thanks for that.