Justin Giboney wrote:
I went to a job interview last Thursday in which I felt I did well except
for one question that he asked me. He asked me to create a PHP function that
takes an input and outputs even if a number is even and odd if a number is
odd.* Below are the function that I wrote in the interview, and the function
that I would have liked to have written in the interview. Now here are some
questions for you:
1) is there a big difference in the two functions?
2) if you could grade these on completeness, how would you score them?
3) what would be your perfect answer?

1) Yes. Differences:

a) The second function fails when a value is passed in as a string, i.e. '2'.

b) The second function has additional output that was not in the problem description. It asked for even or odd. You're making an assumption about what would be "better" but assumptions are often wrong. What if they just want to put the output of the function in a tiny column, or switch based on the results?

c) The second function does extra work by doing up to two extra mods when the number is not even. If it's an integer and it's not even, it's odd. No need to do two more operations, allow more pathways for bugs (what if you'd forgotten about the -1 case?), make the code less readable, etc.

2) The second one is over-complete to the point of missing the specs, as well as limiting input to integer types only. It also does extra work and is less readable. Sometimes less is more. Comments are always good but the most important one would be "you must pass the value with an integer type for this to work."

3) I liked the first function better, maybe adding a brief comment.

_______________________________________________

UPHPU mailing list
[email protected]
http://uphpu.org/mailman/listinfo/uphpu
IRC: #uphpu on irc.freenode.net

Reply via email to