I don’t think this passes all tests (not sure about the use case, but): for strictly whole-number comparisons this would work, but I’m not sure if a decimal number would.
In the OP, a value of 4.5 would return 0, as it’s not a whole number, so it reaches the default case. In your code, it would return 4.5…
Trying not to be condescending here, but optimizing code generally means not changing the base function of the code, so trying to infer intended function is not a great idea.
Changing code to make more sense is great, but if there’s an edge case that snowballs out of control, it ends up being more of a headache.
Absolutely agree, if it were to be changed in this way it would at the very least need a question to the author of the code, or something similar to that
True! My mistake. I kind of assumed the default as a placeholder for the 0 when reading so it doesn’t match up very well. When I first started, my IDE hassled me to write a default case for my switch statements, so I did similar code to this, leaving out the last case and just writing it in the default. I read it through that thought process.
If decimal numbers getting converted to zeros was part of the thought process however, yeah for sure it doesn’t hold up. Thanks for pointing that out.
Exactly. This looks like php to me. Which means, $totalByDistance could be anything. The code makes sure, it is a valid integer between (including) 0 and 10
/**
| In this line,
| we sum a and b.
| This is being done
| so that the variable
| `C` contains the
| following value:
| -> a+ b
|
| If
| a = 1
| And
| b = 2
|
| The value of `C`
| would be:
| => 3
*/
// New line for better formating.
c =
a + b
You have no excuse to not use type hints! I'd go as far to say that you should always also declare(strict_types=1);
<?php
declare(strict_types=1);
class Transport
{
public function note(int $totalByDistance): int
{
if ($totalByDistance >= 10) {
return 10;
}
if ($totalByDistance > 0) {
return $totalByDistance;
}
return 0;
}
}
Also, break is never executed in a switch after a return. It's essentially boilerplate, return breaks the switch as well.
377
u/[deleted] Feb 04 '22
"You, 14 hours ago"
24
u/sn0r Feb 04 '22
This is why you don't code while stoned.
121
u/WILLJDM Feb 04 '22
return $totalByDistance >= 10 ? 10 : $totalByDistance;
50
u/rm_rf_slash Feb 04 '22
Involuntary eye twitch seeing break after every return
8
u/Xocolade Feb 04 '22
return max(min($totalByDistance, 10), 0);
7
u/SoulslikePursuer Feb 04 '22
There are so much wrong with this...
7
u/KnockKnockP wicked Feb 04 '22
did your iq drop 100 points 14 hours ago
5
u/[deleted] Feb 04 '22
I'm surprised they didn't use 10 if elses
9
u/[deleted] Feb 04 '22 edited Feb 04 '22
You have no excuse to not use type hints! I'd go as far to say that you should always also declare(strict_types=1);
Also, break is never executed in a switch after a return. It's essentially boilerplate, return breaks the switch as well.
8
u/_grey_wall Feb 04 '22
Paid per line
3
u/WooooshVictim Feb 04 '22
I can't believe this bullshit. Why did you not comment what every case does? Can't fkin read shit in a few months..
2
u/rustyredditortux Feb 04 '22
operators who?
1
u/gaussianCopulator Feb 04 '22
Wow, a return and a break!
1
u/x-talk Feb 04 '22
Now write a unit test 100% coverage.
1
u/el_cool Feb 04 '22
The unnecessary breaks are like adding insult to injury.
1
u/Robin_1791 Feb 04 '22
DETAILED CODE
1
u/Ok-Mulberry-4600 Feb 04 '22
You don't know what to say?
Sorry would be a start
1
u/using_mirror Feb 04 '22
Ctrl+A
DELETE
Ternary
1
u/frederik88917 Feb 04 '22
When you are paid by the amount of lines you write
1
u/editor_of_the_beast Feb 05 '22
Explicit is better than implicit
1
u/harrymurkin Feb 05 '22
Ahh yes the noob coder's aversion to testing.
1
u/Novinity1 shameless Feb 05 '22
this is scarring
1
u/187mphlazers Feb 04 '22 edited Feb 04 '22
return $totalByDistance < 0 ? 0 : min($totalByDistance,10);1
u/Watanuki_Taiga Feb 04 '22
does your company pay by the number of lines?