Succinct Code
I like simple things. It should be self-evident - I recently ditched WordPress for a hundred lines of PHP and flat-files. I'm much happier for it.
I used to write a lot of C++ code, but for the last several years I've completely eschewed it in favor of C. I can't ever see myself ever going back to C++, as I feel like C is a better language, and it's much easier to keep a large project nice and neat using it. C++ code has a tendency to grew crufty almost from the start. C++ has more abstractions, and although all abstracts are leaky, I think C++ lends itself to creating especially leaky abstractions. C++ makes it much easier to build yourself into a corner compared to C.
Anyway, a few days ago I witnessed an exchange on Stack Overflow that I thought was interesting. It pretty well illustrated some of the differences I find between C and C++ code. And also showcased some of the problems I have with Stack Overflow in general.
While keeping an eye on the new tab, a question about random number generators in C++ came up. A kind answerer posted the following code as an example of a linear congruential generator.
unsigned int seed = 12345;
unsigned int random() {
seed = (1664525 * seed + 1013904223);
return seed;
}
In my opinion, this is excellent code for it's purpose. It's succinct, and it demonstrates exactly the LCG algorithm and nothing more. It's excellent teaching code.
Of course, it seems that my opinion is the minorities'. Almost immediately I watched this answer get down-votes and negative comments. Comments like "Global variables are evil!" "This belongs in a class!" etc.
The question wasn't about global variables. Anyone with a modicum of
proficiency in C knows ten different ways to refactor out the global seed
variable. And the code has much larger impediments to real world usage than its
global variable (such as the inherent limitations of the LCG algorithm). But
the code is great for illustration of the algorithm, and I thought that's what
Stack Overflow was about.
Another user noticed the trend and decided to capitalize on the opportunity to gain rep. Not long after the original answer, he posted the following code:
class Random
{
private:
unsigned int seed;
public:
Random(unsigned int s);
unsigned int next();
};
Random::Random(unsigned int s)
{
seed = s;
}
unsigned int Random::next()
{
seed = (1664525 * seed + 1013904223);
return seed;
}
This longer, more verbose, needlessly abstracted, C++ code quickly gained up-votes, positive comments, and was later accepted as the official answer. It reminded me of why I don't generally like C++. It's all the boilerplate. Making a class is like filling out tax forms. It's tedious and boring. It provides friction to creating anything and especially to refactoring anything. And for what advantage?
Eventually the moderators closed the question as a duplicate, the first answerer deleted his answer, and everybody lost. The end.
Like this post? Consider following me on Twitter or following me on Github. Don't forget to subscribe to my feed.