Why is this PHP code better than the other?

I was talking to a friend of mine who is a back-end webdev for a while now about what code is better.
He said his code was better because it was "art" and i would understand in 5 years. But i don't get why his 30 lines of code is better than my 4.

What we did was that a user could input a number and the output would be something like this if the input was 7:
1 x 7 = 7
2 x 7 = 14
3 x 7 = 21
etc...

His code:

function createTablesFromNumber($x, $amount) {
$output = [];

for ($i = 1; $i <= $amount; $i++) {
	$output[] = $i * $x;
}

return $output;

}

/**
* Outputs tables to HTML
*/
function outputTablesInHTML($x, $tables) {
foreach ($tables as $i => $num) {
$i2 = ++$i;
echo "{$i2} x {$x} = {num}
";
}
}

$x = $_POST['getal'];

$tables = createTablesFromNumber($x);
echo outputTablesInHTML($x, $tables);

My code:

$getal = $_POST['getal'];

for($i = 1; $i <= 10; $i++){
echo $i . "x" . $getal . "=" . $i*$getal . "
";
}

It should in theorie do the same and i have not tested his code yet but i want to know why he would claim that his code is better.

Thanks :)

For this particular case, what he did was a bit of an overkill. I get what he is trying to say though. For future use his code is better because his function outputTablesInHTML can be used by other functions. In this particular case that's not going to happen but in real world development that happens a lot.

Its kind of hard to explain but in the long run his code will be easier to modify/build on top of by another developer.

2 Likes

Php is the language that I currently use more, but I'm not exactly a an expert, so what I about to say is a guess.

The code that he wrote is a result of an habit of dividing the code in blocks (functions) for the sake of reusing them in other parts of the program. I believe this part of the concept in programming called dry (don't repeat yourself) and helps reduce the number of lines of code and improves readability. I do this a lot in where usually a piece of code with more than 5 lines used at least 2 times i create function for that block.

Another thing is where he uses
echo "{$i2} x {$x} = {num}"; instead of echo $i . "x" . $getal . "=" . $i*$getal; which I believe is called interpolation, is used to improve readability.

I agree with @TheCount, in this particular case is the code that he made is completely overkill. I believe you should use these concepts but not every time, in another words, find a balance.

1 Like

His code is better.
I will tell you why in 5 years.

2 Likes

Others are right, just wanted to add my opinion:

When you read each line/function, you will, due to names, instinctively understand what it does and what its output will be.

Whereas yours is short, not commented, writing directly to the stream which will be very bad, once you want to:

  1. Change the number of lines you wan to print
  2. Change the format you want it to be printed in
  3. Add verification or Error Correction of some kind
  4. Understand what it is really doing in the future
    or why there is an error of some kind with unexpected input

Yes it should do the same, once you change createTablesFromNumber($x); to createTablesFromNumber($x,10);

That said, his code can still be changed slightly to improve readability even more.
(Like giving smaller variables slightly more meaningful names.)

1 Like