4
\$\begingroup\$

I have a long set of IF statements that basically set a null object Value to 0, I feel that because i am doing the same action each time their has to be a simple way to make this allot shorter. It just looks like something I wouldn't normally see in code.

My code looks like this:

if ($data->sl1 === NULL){ $data->sl1 = "0";}
if ($data->sl2 === NULL){ $data->sl2 = "0";}
if ($data->sl3 === NULL){ $data->sl3 = "0";}
if ($data->sl4 === NULL){ $data->sl4 = "0";}
if ($data->sl5 === NULL){ $data->sl5 = "0";}
if ($data->sl6 === NULL){ $data->sl6 = "0";}
if ($data->sl7 === NULL){ $data->sl7 = "0";}
if ($data->sl8 === NULL){ $data->sl8 = "0";}
if ($data->sl9 === NULL){ $data->sl9 = "0";}
if ($data->sl10 === NULL){ $data->sl10 = "0";}
if ($data->sl11 === NULL){ $data->sl11 = "0";}
if ($data->sl12 === NULL){ $data->sl12 = "0";}

if ($data->sn1 === NULL){ $data->sn1 = "0";}
if ($data->sn2 === NULL){ $data->sn2 = "0";}
if ($data->sn3 === NULL){ $data->sn3 = "0";}
if ($data->sn4 === NULL){ $data->sn4 = "0";}
if ($data->sn5 === NULL){ $data->sn5 = "0";}
if ($data->sn6 === NULL){ $data->sn6 = "0";}
if ($data->sn7 === NULL){ $data->sn7 = "0";}
if ($data->sn8 === NULL){ $data->sn8 = "0";}
if ($data->sn9 === NULL){ $data->sn9 = "0";}
if ($data->sn10 === NULL){ $data->sn10 = "0";}
if ($data->sn11 === NULL){ $data->sn11 = "0";}
if ($data->sn12 === NULL){ $data->sn12 = "0";}

if ($data->sr1 === NULL){ $data->sr1 = "0";}
if ($data->sr2 === NULL){ $data->sr2 = "0";}
if ($data->sr3 === NULL){ $data->sr3 = "0";}
if ($data->sr4 === NULL){ $data->sr4 = "0";}
if ($data->sr5 === NULL){ $data->sr5 = "0";}
if ($data->sr6 === NULL){ $data->sr6 = "0";}
if ($data->sr7 === NULL){ $data->sr7 = "0";}
if ($data->sr8 === NULL){ $data->sr8 = "0";}
if ($data->sr9 === NULL){ $data->sr9 = "0";}
if ($data->sr10 === NULL){ $data->sr10 = "0";}
if ($data->sr11 === NULL){ $data->sr11 = "0";}
if ($data->sr12 === NULL){ $data->sr12 = "0";}

One way I know could work would simply be to iterate through the entire object with a simple foreach like this:

foreach ($data as $item) {
    if ($item === NULL){ $item = "0";}
}

But let's say I don't want to change every single null object to 0. Do I have any other option?

I am using PHP in my example, but I am curious to hear answers for other languages as well.

\$\endgroup\$
5
  • \$\begingroup\$ So there are some conditions? A subset of lists matching certain conditions? It's hard to make one up with no background. \$\endgroup\$
    – CppLearner
    Feb 12 '14 at 23:03
  • \$\begingroup\$ The variable names could be a little more descriptive, but take a look at get_object_vars. \$\endgroup\$
    – Comintern
    Feb 12 '14 at 23:05
  • \$\begingroup\$ I wish they could be, but these are names from a parsed CSV provided by our client, my current solution is fine for them, but i am curious for future projects if i can do this in a quicker way. \$\endgroup\$ Feb 12 '14 at 23:06
  • \$\begingroup\$ I might be wrong but it seems get_object_vars still iterates through the entire object doesn't it? \$\endgroup\$ Feb 12 '14 at 23:08
  • \$\begingroup\$ It does - it gives you all of the public members in an array. You can take the result and loop over it. C# gives similar functionality through reflection, but has the advantage of being strongly typed (so you can check what type of parameter it is). \$\endgroup\$
    – Comintern
    Feb 12 '14 at 23:11
5
\$\begingroup\$

In short, the CSV parser is using the wrong data types. Whenever you have contiguous variable names, it screams "use an array!" If you at all, possibly can, use an array. That allows you to operate on it much, much more naturally, it's more flexible, and it's way less prone to human error.


If you used an array, this would be much, much simpler:

foreach ($this->sl as $k => $sl) {
    $this->sl = ($sl === null) ? "0" : $sl;
}

Since you seemingly can't use an array, your best bet is probably to either process it into an array, or use some nasty variable-variable hackiness to pretend it's an array.

$sl = array();
for ($i = 1; isset($this->{'sl' . $i}; ++$i) {
    $sl[] = $this->{'sl' . $i};
}

Or if (for some reason) you don't want to use an array:

for ($i = 1; isset($this->{'sl' . $i}; ++$i) {
    $this->{'sl' . $i} = ($this->{'sl' . $i} === null) ? "0" : $this->{'sl' . $i};
}
\$\endgroup\$
4
  • \$\begingroup\$ The reason i can't use an array is because each item (ss1 ss2 ss3) is a unique column in the database, so each one is returned separately. I agree the names are very stupid, but the mutual fund we are working with prefers to use a shorthand to represent their data like that. How ever your last solution would indeed work, i hadn't thought to treat the object name as a string like that. \$\endgroup\$ Feb 12 '14 at 23:16
  • 2
    \$\begingroup\$ @AndrewFont You could always unpack the array back into columns. That is an unfortunate situation though. It sounds like it's likely badly normalized database. I'm sure that's completely unchangeable though :/. \$\endgroup\$
    – Corbin
    Feb 12 '14 at 23:18
  • \$\begingroup\$ Also i made that terrible parser =(, but i was forced to feed the data to the specific columns in the database, so i was not able to change the naming scheme for them, so i chopped it up and saved each piece of information where it needed to go \$\endgroup\$ Feb 12 '14 at 23:18
  • \$\begingroup\$ Yea this project was kind of a data nightmare because everything had strange shorthand names and the database schema for each and every CSV i had to parse was made by a different developer then the last. \$\endgroup\$ Feb 12 '14 at 23:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.