Since it was shortly before Christmas, I put a wish on twitter last week:
Anyone who wants to give me a gift for x-mas, consider writing a programming language where “if (* == true)” results in a compiler error.
This inspired some ideas on the twittersphere, and I decided to bring this topic to the Hamburg Software Craftsmanship user group on last Tuesday. Here’s what we brainstormed together: The best programming language, ever.
I need to elaborate a bit on some of them.
Disclaimer: Don’t hate me, I’m just a messenger.
if (foo == true)
I hate looking at code like this:
if (foo == true)
This really should be simpler than that:
if (foo)
There are also more convoluted examples of this. I dare not to mention them. As far as I would say, this should be rejected from the compiler.
else
else
is really ugly. I don’t know why to use this to start with. If you can’t hold, use a guard clause, and deal with the else path directly. It will make you happier. Oh, and don’t care about the “single exit from function” paradigm. Only people led astray talk about such non-sense. else is worse.
foo ? whenTrue() : whenNot()
Really, like else before, stop using this crap. You no longer work on 80 x 25 screens, do you? Well, sorry, if you do. But The tenary operator is merely a bad replacement for a conditional with an else. Stop using it. Period.
if – else – if – else
Vertical code is even worse. I think there should be a check on the cyclomatic complexity during compilation time, rejecting code that has a deeper level than 5. Right, maybe such a language won’t become mainstream, but then again, maybe we shouldn’t hire mainstream programmers to start with.
throw null
Yeah, really. That works – or maybe worked a while ago. I didn’t check with later versions of Java. I think Nat Pryce made me aware of this. It ends up in a NullPointerException when the Java exception handler tries to print the stacktrace.
i++ + ++i
For all those friends of C/C++-style languages, this might give you a headache, since the results may vary from language to language, and sometimes even from compiler to compiler (depending on the optimization level you enable).
switch (true)
Yeah, I heard this the first time. NEVER USE THIS!
switch (true) { case a: // code when a is true case b: // code when b is true case c: // code when c is true default: // "else" }
Assignments in conditions
while (b = readLine() {
Really? Wtf! No one should write such low-level code nowadays anyways. Stop it, please.
Stuff we couldn’t agree on
There were other stuff that we couldn’t agree on like the never ending vi vs. emacs debate (emacs ftw!). This included checked exceptions, curly braces, and other stuff which seemed to matter for one language only (mostly PHP or Java). Take a closer look on the picture above for these. They are marked with x’s.
So, did I get my gift? Unfortunately not. But maybe someone will dare to build such a language for me – one day. What’s you favorite hated language feature?
These rules shouldn’t be enforced by the compiler, they should be enforced by your recruitment process!
Funny thing, Steve. As a consultant I wish it was enforced by the compiler, so that get fewer called in to companies that lack the maturity (pun intended) in their hiring processes. ;)
Interesting post, Markus. I’m gonna disagree with two of these.
The first is the ternary operator. Yeah some people misuse it, but in some languages where a command has to be in one line, the absence of the ternary would complicate things.
Assignments in conditionals, I don’t necessarily like, but then when you are trying to code cleaner in C# and dispose of resources then you end up with code like this
using (dr = SQLDataReader)
As ugly as that looks as a construct, I think it makes sense for C# and is cleaner than trying to remember to dispose in every try/catch/finally.
It’s ok to disagree. Then again, we’re talking about the best programming language. We can invent it from scratch and leave out everything that makes life more complicated. Maybe we should do that.
Hi Markus,
how would you replace the Else? Would you rather have
if a
if !a
in you code?
I’m not sure about the ternery operator. I like Scalas if very much which is more like the java ternery operator than the if statement, except for the syntax used.
Hi Jens,
I go for short functions (less than 10 lines), and end up with something like this:
if (a) return a_is_true;
return a_is_false;
I don’t need the else in here. a_is_true might be a function in this snipplet.
Whenever I see the ternery operator, I have always found that I can replace the particular line with a more meaningful function call. If I name that function well enough, I even communicate my intent. That’s the way I do it nowadays.