In Defense of Verbosity

I may be in the minority, but I love ?: and use it constantly in Java. It’s perfect for null-checking, like so:

return (name==null?"%":name);
return (active==null?“0”:active);

Ruby also has an operator exclusively for “assign only if variable is nil”:

active ||= “0”

In my mind this is infinitely better than writing if/else’s everwhere, but as Eric pointed out above, whatever works for you and your team is best.

So, I came across this article because I am trying to come up with support for putting the smack down on one of my coders and getting him to stop using the ternary operator for everything.

I honestly had no idea that there was even a single coder who thought as I, let alone a group. (though I am a bit miffed that my “source is for humans, not machines” philosophy is not as original as I once thought)

Like pretty much anything in the universe, the issue comes down to approriate usage. The ternary operator is perfect for things like:

String myWords = "You have " + cntItems + cntItems 0 ? “item” : “items” + “in your shopping cart.”

(or the return statements above, THAT is what I call elegant code, as opposed to “how much disconnected crap can I jam on a single 399 column line??”)

It is HORRENDOUS when there are 13 different things, several constructors, a method call or two, a cast and a kitchen sink all sandwiched in a ?:

Again, source is for humnas. Too often, it is used by humans to attept to display how crazy mad cool they are and as a result comes out being utterly useless.

Thanks for the great conversation.

Let’s be honest- VB is for BASIC coders and C# is for C++/Java coders, it’s that simple. I coded for years in C/C++ and Java so C# is my choice when I’m in the MS env.

I think this a real strawman argument. I’m moving from VBA (visual basic for applications) to VB.Net, and I find the verbosity somewhat appauling. I’m fine with “End If” and “End Select”–these add some readability. From my old days in C, I’m also fine with “}”–concision cumulatively saves much time and (eye) space without losing much; more elegant.

The problem I DO have is more with the whole new generation of verbosity–all of .NET, and others–as a result of classing everything, and burying every last function in some deep tree, and recasting the most basic functions into object orientation. I understand that thinking in terms of objects brings a certain fluidity of use between libraries. But I find it really disturbing that the most basic needs (e.g. string handling) have been converted to OO; it seems unnaturally rigid; verbose; a typing tax and a reading tax; and most of all, mental convolution.

I wrote a brief blog post on this topic here: http://www.cooltechblog.com/?p=51

I suppose I’ll get over it, but it makes working in VB.Net seem a lot more work than something before all the classism (e.g. VB, VBA). There must be a better way…

and burying every last function in some deep tree,
and recasting the most basic functions into object orientation

Oh, I always want to scream in horror when I have to write those int-to-short casts (among others). I understand the technical side (and I even agree), but hey… tons of casts make the code really unreadable.

There are a couple things stopping me from switching to C#, the major obstacle being that I use KNF style intention, yet C# forces me to use Allman style. I could just disable the auto-complete and get around it, but then why should I even use the IDE at all and just edit my code in vim or something instead?

My other main argument is that C# does not allow for integer expansion. Preventing a cast from Button to XmlNode is one thing, but when I can’t do something like:

int16 plus_six(int16 n){
return n + 6;
}
int32 five_plus_six = plus_six(5);

When it works in Visual Basic?

God what a load of crap. C# vs VB.Net is an old, tired and ultimately pointless argument. It all boils down to developer familiarity/preference, although I would recommend a new starter use C# as the syntax is closer to other modern languages. They both compile down to virtually identical IL and can accomplish the same tasks (aside from VB being unable to make use of pointers). I use both interchangably without problems, although given the option I will use C# since I am more productive with it and find it easier to scan through when I come back to old code. Yes, I DO like the ternary operator - this is one case where I find VB a pain since IIf does NOT accomplish the same thing (as some people mistakenly think). Consider:

C#

public string GetDBText(Nullableint value)prefer
{
return value.HasValue ? value.Value.ToString(“0”) : “NULL”
}

VB.Net

C# vs VB.Net is an old, tired and ultimately pointless argument.

To be fair, this was posted in 2005!

Wait a minute, so how does this apply to the var keyword (http://www.codinghorror.com/blog/archives/001136.html)?

What a difference 2.5 years seems to make, eh? How long until verbosity is back in style?

Personally I find C style syntax a little easier to read because it makes it easier to skip the chaff.

Basically since we generally associate blocks with tabbing amount, the End/} endings are basically overkill. Not to say I think they should be removed, Python has a good idea with making whitespace matter, but there is just something about making an invisible entity syntactically important that doesn’t sit well with me.

So it comes down to which one is easier to filter out, I would say }, since End X always makes me read the line to figure out what is going on, while } I mentally block out (Although if I am thinking there is a mistake it is easy to figure everything out via Ctrl + ])

Either way it really comes down to taste. Do you like the verbosity of saying explicitly what is going on, or the simplicity of saying the minimum required to get the point across. Speed of coding has always been a BS argument anyways. Even a slow typist would only take a second to type End Select, without the IDE’s help, and that is cake compared to the amount of timing writing the contents of the clause, not to mention planning etc.

When I read code, I’m not reading English. There’s a reason code is normally in monospace and English usually has a less fixed width. I can’t stand operators that are spelt out, because that screws up my mental parsing of what’s an operator and what’s not. Math is also a language, and you’ll find English is clumsy when it comes to conveying the elegance of a concise mathematical statement

IMHO, saving keystrokes is important.
Verbosity takes valuable screen space, and may force more scrolling than necessary.
In fact, when cleaning up code, I sometimes make it less descriptive voluntarily. This is to avoid wasting space on routine operations and keep the focus on what is important

At any rate, they’re a prime example of the false economy of trading write-time conciseness compared to readability, so whatever they’re called, we can agree on that :wink:

Actually, I disagree. If statements and the ternary operator don’t do the same thing. The ternary operator actually returns a value.

So:
string value = item != null ? item : “[Empty]”;

Versus:
string value = “[Empty]”;
if(item != null)
{
value = item;
}

Great post–as always. Since I know how much you do care about the quality of your posts though, I believe there’s a broken link at “dropped the vowels and the stopwords.” Just wanted to let you know : )

I think the clarity of curly braces is a pretty minor problem. The big problems are things like abbreviated, cryptic variable names. I’ve been trying to read through some really old code that has plenty of comments about what it’s doing but still does a poor job of explaining what the variable names actually mean, and it’s a nightmare.