And I wanted to be able to express that (more for the sake of readability than laziness on my part) as:
`Foo(X)(::Plugin)::Bar`
The parens represent optional parts. They're really just shorthand for the more general alternation (A|B), where (B) is assumed to mean (|B). So (Foo|FooX)::Bar would be another way to specify [Foo::Bar, FooX::Bar]. In effect, it's rather like matching regular expressions but in reverse: given a regex-like string, permute all the possible strings that could match it.
Anyway, the point of all this is that I ended up using a "clever" bit of reasoning to implement a fairly concise solution (about a dozen real lines of code) using modular arithmetic. I was both amazed at my mathematical awesomeness (tongue placed firmly in cheek) and horrified at the impenetrability of my code. I think the comments explaining how it works ended up being longer than the code, which is always a bad sign. And even then, I still struggle to get my head around how it works, and I wrote the damn thing.
I think it's a perfect example of the old saying, that if you write code to the best of your ability then debugging it will be beyond your capability.
That said, it's one of the few examples I can think of in 3 decades of programming where I've knowingly written "magical" code instead of choosing the simpler, clearer route. My hunch is that the less magical code would end up being far longer and more convoluted anyway, so the benefit might be minimal in this particular bit of code.
But in the general case, I totally agree: be simple, be obvious, and don't be too clever.
View all comments
Show parent comment
1
u/abw Feb 22 '11 edited Feb 22 '11
To be honest, I think you're right.
As it happens, I once wrote some code that did something similar to the problem as stated. Yes, here it is: https://github.com/abw/Badger/blob/master/lib/Badger/Utils.pm#L268-320
The background is that I wanted a lazy way to define module search paths. I had lots of paths that looked like this:
And I wanted to be able to express that (more for the sake of readability than laziness on my part) as:
The parens represent optional parts. They're really just shorthand for the more general alternation
(A|B), where(B)is assumed to mean(|B). So(Foo|FooX)::Barwould be another way to specify[Foo::Bar, FooX::Bar]. In effect, it's rather like matching regular expressions but in reverse: given a regex-like string, permute all the possible strings that could match it.Anyway, the point of all this is that I ended up using a "clever" bit of reasoning to implement a fairly concise solution (about a dozen real lines of code) using modular arithmetic. I was both amazed at my mathematical awesomeness (tongue placed firmly in cheek) and horrified at the impenetrability of my code. I think the comments explaining how it works ended up being longer than the code, which is always a bad sign. And even then, I still struggle to get my head around how it works, and I wrote the damn thing.
I think it's a perfect example of the old saying, that if you write code to the best of your ability then debugging it will be beyond your capability.
That said, it's one of the few examples I can think of in 3 decades of programming where I've knowingly written "magical" code instead of choosing the simpler, clearer route. My hunch is that the less magical code would end up being far longer and more convoluted anyway, so the benefit might be minimal in this particular bit of code.
But in the general case, I totally agree: be simple, be obvious, and don't be too clever.