|
Posted by Rami Elomaa on 04/12/07 16:01
Jerim79 kirjoitti:
> As for the loop that was given, it may surprise you that I don't label
> my variables as $VariableX. Variables should have unique names that
> signify what they are. Seeing as how I have unique names for my
> variables, a loop there becomes impractical. Unless of course I
> utilize pointers, which is open to a matter of discussion of whether
> it is worth it or not.
Okay, so variables have unique labels, that doesn't mean they still
couldn't be handled in a loop. I'll just tell you what I personally
would do, and I'm not trying to say this is how you should do it.
Php's arrays can be both indexed and associative, I'd use the lable
strings as keys instead of numbers.
Okay, so the variables aren't called $variable1, $variable2, but let's
say Peter, Paul and Mary, etc... So just make a array of them:
$my_labels = array('Peter', 'Paul', 'Mary',);
Collect the values from form:
foreach($my_labels as $key)
$my_values[$key] = $_POST[$key];
Then check for errors:
foreach($my_values as $key => $val)
if (empty($val)){
$msg[$key] = " * You have not answered this question.";
$error++;
}
You still have them as uniquely named array indexes... (indices? whatever.)
I believe that for the new guy this code would be readable, and
identifying problems should really not be any more difficult with this,
plus I think that it actually might save some time to write the actual
code from the beginnig, even though it's not at it's final stage,
instead of first writing everything spread out, and then rewriting the
same code again cleaned. If anything, writing the code twice will only
introduce new bugs which still would need to be detected.
However, my original intent was not to concentrate on the bad code, err,
your personal style of writing test code, but to draw your attention to
the fact that there was so much of it. When you write to a forum such as
this, requesting for help, do concider trimming the code to a very very
short piece where the problem is shown. If you post several hundreds of
lines of code, who's gonna start reading it all? Most just take a
glimpse at it and think "spaghetti!" and ignore it. Had you isolated the
problem to less than 20 lines, people might've actually paid attention
to it and been able to help you with it. You see, people in c.l.php have
the patience of an adhd child high on sugar. If the problem isn't solved
within 10 seconds, it's ignored. If you expect a person to spend an hour
reading your entire code and parsing it in their head, you wont get any
help and have to solve the problem by yourself.
I'm sure you have your own style of coding and after reading your
explanation you've convinced me that you may not actually be entirely
rookie in coding. The difficlty seems to be that you've not yet grasped
the fine skill of posting a question to usenet. I wouldn't want to think
that this was all waste of both of our time, so I'll try to learn the
lesson here and not judge people and their code so harshly, if you'll
consider sending shorter piece of sample code the next time you post,
agreed? :)
A final word on refusing to use loops is an educating story on Worse
Than Failure (formerly known as The Daily WTF) that you might want to
read: http://worsethanfailure.com/Articles/Checking_Your_Digits.aspx
--
Rami.Elomaa@gmail.com
"Wikipedia on vähän niinq internetin raamattu, kukaan ei pohjimmiltaan
usko siihen ja kukaan ei tiedä mikä pitää paikkansa." -- z00ze
Navigation:
[Reply to this message]
|