3 ms·
> Do programmers generally enjoy some amount of complexity? Yes, to their own (and the team's) detriment. I really hate complex/clever/dense code. Just about
by joshstrange 2y ago
> Do programmers generally enjoy some amount of complexity?
Yes, to their own (and the team's) detriment.
I really hate complex/clever/dense code. Just about as much as I hate "Look I found a want to look through this and write it once instead of mapping every property 1-by-1"!
Every, single, time I've seen that "shortcut code" it's turned from:
foreach($item as $key => $value) {
$newItem->$key = $value
}
Into
foreach($item as $key => $value) {
if(/*special case 1*/) {
// Special Logic
} else if(/*special case 2*/) {
// Special Logic
} else if(/*special case 3*/) {
// Special Logic
} else if(/*special case 4*/) {
// Special Logic
} else {
$newItem->$key = $value
}
}
In record time.
Just doing:
$newItem->keyOne = $item->keyOne;
$newItem->keyTwo = $item->keyTwo;
$newItem->keyThree = $item->keythree;
Is much more maintainable and gives you unlimited "hooks" if you need special logic for a given key.
Note: this is a contrived example as the keys are the same, most cases I've seen this in dealing with mapping DTOs (maybe going from snake_case to camelCase) in/out of a system, a use-case where I 100% do not want "magic".
I've moved from code-golfing and being as clever as I can to loving boilerplate and verbose code.
- keepamovin 2y agoI don't think tyhat's a contribed example. I think tha'ts a really good example that makes super clear what pointless abstraction is in a perfect pithy little package.