Re: Code elegance and cleanliness
| From: | Al | Date: | Sun, 25 Jul 2004 20:35:29 +0000 |
| Subject: | Re: Code elegance and cleanliness | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-191836@lists.php.net to get a copy of this message | ||
Not really knowing why you need to provide indexes for the array, since they are generated automatically if none are provided, I'd use:
$imgBkgrnd = array(
'bkgrnd-default.gif',
'bkgrnd-positive.gif',
'bkgrnd-negative.gif'
);
$imgNeeded = table['field'];
$imgName = $imgBkgrnd[$imgNeeded];
The last two lines are a it doesn't matter.
Note the use of single quotes in the array.
The parser can simply treat the values as text rather than having to check for variables, etc.
Robb Kerr wrote:
Just a quick question. Does it really matter how long your code is or how many variables you use? For instance you can enter the following... $imgBkgrnd = array("1"=> "bkgrnd-default.gif", "2" => "bkgrnd-positive.gif", "3" => "bkgrnd-negative.gif"); $imgNeeded = table['field']; $imgName = $imgBkgrnd[$imgNeeded]; or, you can code the same thing as... $imgBkgrnd = array("1"=> "bkgrnd-default.gif", "2" => "bkgrnd-positive.gif", "3" => "bkgrnd-negative.gif"); $imgName = $imgBkgrnd[$table['field']]; The first example uses one more variable and a longer code block but is easier to read and debug. The second example uses one less variable and a shorter code block but is harder to debug later. Should I be striving for the shortest most compact code utilizing the fewest possible variables. Or, can I code in a style which is easier to read and debug?