a companion discussion area for blog.codinghorror.com

The Best Code is No Code At All


(I’m sure the C# version is similarly wordy.)


var result = from x in source select x*x;

Of if you perfer VB:

Dim result = From x In source Select x^2

And since it result is a query, you don’t waste time calculating all the values if you happen to only need the first 5.


I think that a lot of discussions about minimal semantic differences in short code results from bad compilers as well. As I read in some link, comparing to “” pulls an object from the internal string pool, while String.Empty just checks for emptyness. However, why is a compiler not able to recognize a foo == “” and replace it with foo == String.Empty? That should not be that hard imo. And it whould allow Programmers to just write their style and get the most efficient code out of it.

Furthermore, I cannot agree more on the second part Fred mentioned. Atm, we have some coding course and we are supposed to solve acm-problems. In some very hard problems its just more efficient to just throw the whole solution into the bin and start again if it has some serious bugs. Usually its just harder to track down those bugs and fix them (fixing them usually results in a 90% rewrite anyway) then just to think a minute about the right solution and coding that down. And, code that was written in 1 walk usually looks cleaner than code that went through massive bugfixing. (Ok, I have to use std-c, thus, the code usually looks akward, but oh well…)



You had me up until your String.Empty vs “” example.

Every time you use “” the compiler creates a new empty string to compare your string against. If you make that comparison 100 times then you create destory 100 objects with “” in them.

If you use String.Empty then you’re comparing against the one static “” created and maintained by the String class.


Every time you use “” the compiler creates a new empty string to compare your string against.

Not in .NET 2.0 or greater, it doesn’t. Are you smarter than the runtime?

If you’re into micro-optimizations, then go here:

Warning: the time spent reading is far, far greater than the .00000001 seconds you’ll save with said optimization.


As Shakespeare put it in Hamlet

“Brevity is the soul of wit”


Thanks for reminding me again why I love Perl so much. :slight_smile:


Well, I thought I knew what (s=="") meant, but now I sure don’t.

If s is uninitialized, will it return true or false?

Thanks Jeff.


Here is another of my BRILLIANT SUGGESTIONS, lol:

Jeff – could you write a blog entry about GOOGLING FOR ERROR MESSAGES? This turned out to be an extremely powerful debugging technique for me. I just came across it again and I realized … if I had a blog I’d write about it and other cool things, but you’re the man! Instead of spending time setting up my own blog about tech stuff and having 5 people read it, I readily admit that I am much too lazy for that. But I think it’s a fascinating thing no one has really talked about in depth yet.

For example, today I was working with the symfony framework, trying to use the propel library to translate a schema I had written in yml to an actual database. I wrote the yml file, invoked propel… and I got the following error:

Incorrect settings for column order.

Oops! What is a boy to do? Peer into the source? Au contraire* , I quickly made use of the web and other people’s experience with the problem, by googling for the exact phrase. Nothing. Hmm! So I googled for a substring, which happened to be “Incorrect settings for column”. And sure enough, I found quotes like “Incorrect settings for column $col_name”. Aha, that’s right… I probably had a column named “order” in my .yml file! I quickly glanced at it and sure enough, there was the error. Incidentally, after I fixed it, I got another error message, this time saying “Incorrect settings for column name.”

Thank you google and fellow programmers.

  • (that’s my salut to the french guys who wrote symfony)


Smart code is not about saving execution time, smart code is in not duplicating what you have done already, optimization must only be brought in at bottlenecks. Having inherited a huge code base with areas where I have already optimized the code into a third of the original amount by taking duplication away I can’t agree more with code smaller. A third of the size and not yet even great code. In the same application there is also speed issues in sections, but I expect the Issues to be lessened by removing bad code.


Well, I prefer

if (s.Length == 0) …

/because/ it crashes for a null string. I almost never want a null string treated as equivalent to an empty one – the latter is a real value, the former is probably a not yet set up variable.

Far better a reliable crash which I’ll find and fix at development time than a program which quietly goes about its way and does the wrong thing.


It seems that it would make sense to move toward higher-level languages that do more with less. I’m more a sysadmin than developer so I gravitate more to Ruby/Perl/Python sorts of languages than C#/Java/C++/C languages, but I treat the former as first-class languages rather than a more powerful shell. Despite whatever performance hit you might take with Perl (and compared to Java, it’s a wash), you get a more expressive language per line of code, even without resorting to baffling idioms, side-effects, and mind-bendingly terse code. Remember, C was designed for systems programming, not application development and that’s a disadvantage shared by all languages that inherit from it.

You’re wasting your time second-guessing a compiler. If performance matters then you need to profile your code and hit up Knuth or Sedgewick for a better algorithm or design.

I don’t do C# or .NET [1] or so I have nothing to add to the ‘which code fragment is better’ argument other than to ask what condition the developer really means to test for, and what the language reference manual says about the different tests suggested. I’d worry about a language that doesn’t optimize “” into a constant, though if “” is created and destroyed in each iteration of a loop, is it affecting performance enough to care about? How would you know?

Then again, I’m a sysadmin. My job is cleaning up after developers; rarely do they ask me for advice. :slight_smile:

[1] Frankly, if all my Windows servers disappeared tomorrow I’d take that as evidence of a benevolent, omnipotent deity.


blockquotepEvery new line of code you willingly bring into the world is code that has to be debugged, code that has to be read and understood, code that has to be supported./p/blockquote
pAnd once again, Dijkstra was right on the money:/p
blockquotep[…] If we wish to count lines of code, we should not regard them as emlines produced/em but as emlines spent/em: the current conventional wisdom is so foolish as to book that count on the wrong side of the ledger./p/blockquote


Every new line of code you willingly bring into the world is code that has to be debugged, code that has to be read and understood, code that has to be supported.

And once again, Dijkstra was right on the money:

[…] If we wish to count lines of code, we should not regard them as “lines produced” but as “lines spent”: the current conventional wisdom is so foolish as to book that count on the wrong side of the ledger.


To take the analogy one step further…
Programmers tend to see every problem as a program, DBA’s see every problem as a database and IT guys see every problem as a Network.
Sometimes we are guilty of concentrating on modeling the business process so intently that we forget to examine the business process itself and possibly eliminate the need to build the system in the first place ….
My first questions that I ask now are: why do you need it, what does it do, and is there a better way to do it.
You would be surprised how often we have solved the problem without cutting one line of code…
(I am embarrassed to admit that my boss, an MBA taught me this lesson)


Personally, I find String.Empty much easier to read, but that’s a bit of a religious issue.

I don’t think Jeff is talking about Brevity - and I think he even says that - but about Clarity. Clarity is simple, obvious, and to the heart of the matter. Real clarity is brief, but easy to read. That includes comments, too.

It could be made shorter in ‘text speak’, but how clear are those messages? There is a reason we don’t use 1 letter variable names!

It’s not tiny fragments of obscure code that does massive amounts (yes, I’m thinking of Perl in that, amongst others). And it sure as hell isn’t about lines of code - many languages (with notable exceptions) let you put an entire program on one line. Ever tried reading one? I did - you can’t.

It’s just about being simple, clear, and to the point. It’s kind of like Buddhist enlightnment - someday, with enough practice, I’d might just achieve that.

Unfortunately, I do think that we’re constrained - people often expect very complex things and lots of features from their software, and when people are demanding complex programs, you get complex code. Look at the complexity cost of, say, just having a simple UI for a Windows app, as opposed to a console app. Mickey is right - often, we’re trapped by bloated design. KISS is my watchword (and occasionally gets me a slap too).

Fred Ross is right, too, in his comment about code maintenance - but he does miss the point that often we’re under pressure from the boss to ‘just get it done’, and the idea of ‘get it done, but in a way that will save us time later’, well, usually, that dog won’t hunt.


The performance difference between “” and String.Empty, whatever it may be, is irrelevant when your code is waiting on a database, or file I/O, or any number of other things that are orders of magnitude slower.

I find the brevity argument a bit silly. If saving 10 characters is “brief”, why not delete all comments, indentation and blank lines, and rename all variables to a single character? That’s even briefer!

I mostly use String.IsNullOrEmpty() because it clarifies my intent to the next developer. Simple as that.


As to string.empty vs “”, there is a difference: every time you create a literal, NET has to allocate a bit of memory and assign it to “”, which then gets pointed to by your stack variable. If you happen to have lots of “” uses, well, that’s a bit 'o wasted memory; with String.Empty, the one constant is shared


if s


Interesting. I think the example given here has proved the point which this column wanted to prove. You see there are at least 10 entries discussing the pros and cons of those 2 (just two) lines of code. In real life, this is the time wasted and it did happen here also. Why? I don’t know!

The problem with software development and of course with software developers is that nobody wants to understand existing code. It is difficult and more difficult when you do not have any documentation, which is true in 90% cases.

I think it all starts with software requirements. The more you understand those requirements the more correct code you will write. If requirements are vague, you will end up writing crap. This is like some soup recipe. If recipe itself is wrong, so is your soup!!
My personal experience is wherever I spent some time understanding the exact requirements, I ended up with good code; with all the documentation and to my surprise the code was very simple and straigtforward.


A very important feature of code is its readability. Damian Conway (Perl 6 designer with Larry Wall) keeps saying that we read code many more times than we write it. So write it to be readable. Brevity and expressiveness are dimensions to get there.