You are here: Re: problem returning value using recursive function « All PHP « IT news, forums, messages
Re: problem returning value using recursive function

Posted by Steve on 04/12/07 14:53

| consider this:
|
| function level($n, $l = 0)
| {
| $ff = get_folders();
| for ($i = 0; $i < count($ff); $i++)
| {
| if ($n != $ff[$i][0]){ continue; }
| if ($ff[$i][4] != 0)
| {
| $l++;
| level($ff[$i][4], $l);
| } else {
| return $l;
| }
| }
| }

lemme practice what i preach (and usually do)...

function level($n, $l = 0)
{
$ff = get_folders();
for ($i = 0; $i < count($ff); $i++)
{
if ($n != $ff[$i][0]){ continue; }
if ($ff[$i][4] != 0)
{
level($ff[$i][4], ++$l);
}
return $l;
}
}

the difference? NO else statements (as none are needed, as they rarely are)
and $l is only incremented in the recursive calls to level()...which helps
more plainly show that it is only a change for parameter calls in level()
rather than massaged elsewhere in level().

either way, it's still shitty code...there's just less of it to cause a
stinch and is more readily identifiable as shit since it can be read and
understood at a glance.

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация