Magic Number Moan

Magic Number Moan

by Howard Miller -
Number of replies: 10
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
This is just a plea to developers, not to use so called 'magic numbers' in the code. Here's an example, in v1.3.3 when you save a quiz you get an error about an undefined offset (with debugging on). The offending line looks like....

$feedback[] = $subresult->feedback[0];

so, presumably, offset 0 has special meaning but there is no clue what. A constant here would have helped a lot.

Do dig intended - just something to think about smile
Average of ratings: -
In reply to Howard Miller

Re: Magic Number Moan

by Howard Miller -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
whoops - should have been "No dig intended". Who am I to talk - I can't even type a two letter word smile
In reply to Howard Miller

Re: Magic Number Moan

by Martin Dougiamas -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers
Well, in this case the feedback is either an array of stuff or a single item.  feedback[0] is just the first item.  Would you prefer feedback[FIRSTITEM] ?
In reply to Martin Dougiamas

Re: Magic Number Moan

by Howard Miller -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Am I being a pain (again)? blush

However, to keep digging my own grave - I take it you mean that in the case of some questions you know (dare I say assume) that there is only one feedback item and it must be in the first position (offset 0) of the array? If so then I think that for clarity that should be made specific. I would either refactor/generalise the code so that it doesn't work this way, or do something like..

$singlefeedback = feeback[0]
..stuff to process feedback...

which is a bit longer but makes it more readable. feedback[FIRSTITEM] wouldn't really help in that case.

OK... I'll shut up now!!
In reply to Howard Miller

Re: Magic Number Moan

by W Page -
Hi Howard,

You nean
$singlefeedback = feedback[0]

not

$singlefeedback = feeback[0]

right??

WP1

In reply to W Page

Re: Magic Number Moan

by Howard Miller -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
ok ok, you win big grin

I'm just a dull, boring, computer scientist. This is what we get using a language where we don't have to declare anything (and why object oriented code in PHP is more readable, and easier to debug, because you do) IMHO.

I really will shut up now!!!
In reply to Howard Miller

Re: Magic Number Moan

by Martin Dougiamas -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers
Howard, I completely agree: $singlefeedback = feedback[0]; is better for self-documentation.
In reply to Martin Dougiamas

Re: Magic Number Moan

by John Papaioannou -
Maybe

$singlefeedback = reset($feedback);

is even better in this context?
In reply to John Papaioannou

Re: Magic Number Moan

by Howard Miller -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Doesn't that break code readability rule number 1 - 'never use side effects if you can avoid it'??

big grin
In reply to Howard Miller

Re: Magic Number Moan

by John Papaioannou -
It does, to be sure! I 'm guilty as charged, and I wouldn't dream of suggesting such blasphemous practice but for these facts:

  1. I can hardly remember seeing iterator-based array access anywhere in the Moodle code (well, I can remember writing somesuch myself  in get_user_timezone() and maybe elsewhere wink)
  2. In this specific context it's safe since we know that there's only one element in there and blah blah.

Please don't take my keyboard license away!!! big grin