3 ms·
> A better example might go a long way here. I am a Bad Programmer (it's not my day job, though I have managed programmers for many years), so maybe I am just
by trog 5y ago
> A better example might go a long way here.
I am a Bad Programmer (it's not my day job, though I have managed programmers for many years), so maybe I am just missing the point in examples like this - but I often find this is the case in many code samples for language features. They usually have a trite example that shows how it can work in a really basic case.
This is helpful from a purely technical level - as here, I can see what is going on neatly.
But they rarely seem to show it being used how the designer expects it to be used in the wild to save time, or increase productivity, or whatever. Like, why would anyone want to do this?
I would personally NEVER want to see a function like that in a codebase when it could be written so much more clearly as in your examples. Although I personally would prefer a third style:
function slug(string $s) {
$s = strtolower($s);
$s = str_replace(" ", "-", $s);
return $s;
}
... more verbose, but I never see the point in trying to save a line if it sacrifices even a small bit of readability. Again, as a Bad Programmer, I like things to be spelled out super clearly whenever possible, so maybe that's just me.
- antifa 5y agoAlso your preference is faster.
- 2000UltraDeluxe 5y agoThis. I too am a bad programmer and readability is so much more important to me than the LOC count. Sure, the pipes example might look nifty, but from a practical viewpoint, it's just not that useful. Next thing you know, someone throws an eval into the mix by mistake, and we end up with unintended consequences.