3 ms·
Hash is so powerful in Ruby that people often overuse them. One of the most common issues I found on Ruby code-bases is to not create classes to represent thei
by Draiken 3y ago
Hash is so powerful in Ruby that people often overuse them.
One of the most common issues I found on Ruby code-bases is to not create classes to represent their domain and simply use hashes everywhere.
The downside is that a hash has no shape. It can (and will) be anything you want it to be, often causing havoc once the system grows.
Checks for keys everywhere. Almost all statements use the safe navigation because you never know what shape you're dealing with. Multiple places performing the same map/reduce/filter/etc. All because people stick to hashes a bit too long.
- cies 3y agoRuby newly added type system can also help here. For starters, it'd be nice to know what type(s) the keys and values can be.
- Draiken 3y agoThat's what POROs are there for. I truly don't want a faux type system only to then keep using hashes for everything...
- arooaroo 3y agoAmen. This is an issue at the company I work at. Common typos when looking up has keys will return nil - this has a tendency to silently keep working and blow up with a runtime error further down the chain. I am trying to insist on using .fetch to force an exception. When the company switches to 3.2 I will insist on everyone using the new Data class for value objects rather than hashes.
- mcv 3y agoThis is perhaps the biggest killer feature of typescript: that your object literals (which are basically hashes) can have types. Or possibly interfaces.