Scientists Find Sugar Deep In Our Galaxy
Read more of this story at Slashdot.
Tory MP Patrick Spencer groped breasts of two women at London's Groucho club after pestering them to have a drink with him, court hears
Drug dealer who threatened to stab teenage boy sent to prison
Venue issues update as summer concerts with worldwide artists cancelled
Venue issues update as summer concerts with worldwide artists cancelled
I've lived in 25 countries - here's why Turkey's Turquoise Coast beats Thailand and the Caribbean
QUENTIN LETTS: Her death elicited anguish, affection and humour. All I could feel was a hollowing sorrow
Revealed: Alarming reasons behind Britain's alcohol death crisis - according to the scientists that spotted the surge in victims (and it's not just Covid)
Declan Rice is FIT for England's World Cup semi-final showdown with Argentina after shaking off sickness bug
'If we die, we die together': Wife who held on to her husband's legs after he was sucked out of Ryanair jet window at 20,000ft describes battle to save him
CodeSOD: AAYFN
Jason M sends us some Ruby code.
def bv(prop, tv="Yes", fv="No", nv="Not Specified") v = self.send(prop) if v === true tv elsif v === false fv else nv end endThe obvious WTF here is the function name and parameter names. AAYFN(always abbreviate your function names) seems to be the convention here. But it also contains a more Ruby-specific WTF.
The function name bv is short for "boolean value", obviously. tv is the "true value", fv is the false value, and nv is the null value. So this is really about pretty-printing boolean values and converting them to strings. While the terrible names make it hard to understand, it's not that hard to figure out what's going wrong here. I hate it, don't get me wrong, but it just makes me sigh with disappointment, not groan.
No, the thing that makes me groan is v = self.send(prop). This is a very Ruby idiom that lets you access a member of the class by name; essentially it's like doing self.prop, but since prop is a variable containing a property name, we have to send it.
This is metaprogramming by strings, which is the main reason I end up hating it. But in this specific instance, it offers us a lot of potential issues. First, if prop is anything not boolean, we just return "Not Specified", which is incredibly misleading. I'd argue that if we attempt to use it on a non-boolean field we should throw an exception. Which opens the question: if prop doesn't exist, is that a non-boolean field, or is that a "enh, just call it null" situation? Because right now, that will throw an exception. It'll also throw an exception if the thing being accessed is a function that takes parameters. These behaviors may be surprising.
Now, I don't know the calling pattern. It's possible that whatever function calls this already has a good list of the allowed boolean values, and will never call this on a prop that doesn't exist. Certainly, that's what the Ruby docs recommend. But I'm going to hate it anyway, because this kind of runtime metaprogramming by passing strings around is eternally asking for trouble. And while I haven't done a huge amount of Ruby, I've done enough to know that any non-trivial codebase ends up like this once the metaprogramming band-aid is taken off.
Now, if you don't mind, I'll go back to doing my metaprogramming with C++ templates, which are simple, clear, and never result in wildly unmaintainable code because you ended up reimplementing LISP in template operations.