string.Empty versus ""(dotnetperls.com)

submitted by samdnpsamdnp(980) 3 years, 1 month ago

The author shows ways that "" is better than string.Empty and results in clearer and possibly faster code.

15 comments |category: |Views: 939

tags: another

new Add a live kick counter to your blog >> liveImage

You can even customize the image by choosing your own colors, and then clicking the button below to update the preview and the html code:

  • "Kick It" text
  • "Kick It" background
  • kick count text
  • kick count background
  • border

Simply copy and paste this HTML into your blog post.


Users who kicked this story:
Comments:

posted by davembushdavembush(15) 3 years, 1 month ago 0

I can't believe this article got 5 kicks.

Watch for my reply to this on Monday at http://blog.dmbcllc.com

Reply

posted by photozphotoz(80) 3 years, 1 month ago 0

is this guy full of crap or something?

Reply

posted by duckieduckie(150) 3 years, 1 month ago 0

Why should we wait?

http://www.google.dk/search?q=string.empty

Reply

posted by schwankieschwankie(0) 3 years, 1 month ago 0

Yet another reason to ask for a kick down option

Reply

posted by senfosenfo(881) 3 years, 1 month ago 0

What the hell? Author needs to learn C#.

Reply

posted by samdnpsamdnp(980) 3 years, 1 month ago 0

Guys, when you refute an argument, generally you need to offer a reasoned response. None of you even made an attempt. GOOD JOB!

Reply

posted by ericswannericswann(145) 3 years, 1 month ago 0

Hmmm....if this ever becomes relevant to a project's performance or design, I'll know that project is complete. ;)

Reply

posted by samdnpsamdnp(980) 3 years, 1 month ago 0

Eric, there are lots of questions on the Internet about this topic. Therefore it is absolutely relevant to C# programming, because programmers have questions about it. It is the goal of my website to answer these questions.

The author is 100% convinced of this :)

Reply

posted by offwhiteoffwhite(975) 3 years, 1 month ago 0

This is a disappointing headline on DNK. Developers need to write maintainable code that works. That should be the top priority. Performance is a secondary concern, but it is still important. Now if you have to loop over a million times to find a measurable difference between "" and String.Empty I am going to say this is not an optimization I will ever make.

The reason to use a constant for values like an empty string and 0 is that someone reading your code (or perhaps yourself several months or years later) knows that you intended that value.

string url = String.Empty;

That clearly sets the variable to an empty string.

string url = "";

Here the developer may have intended to put a value in here later, but never came back to fill it in. Remember, code is for people and machine code is for machines. Do not think that by reducing the syntax down by a few characters or lines of code that it compiles down to even more efficient code. It just makes it harder to read and maintain. Write verbose code that you can read and can verify is working as intended.

This headline justifies the "kick down" feature. Without it DNK will be encouraging bad coding practices.

Reply

posted by daboomdaboom(15) 3 years, 1 month ago 0

Huh? Did you ever need to compare an empty string value with null??

Btw, with:

if (string.Empty == val) {
...
}

if ("" == val) {
...
}

the times become something like 8.84s vs 8.44s and that's under 5% difference.

Reply

posted by samdnpsamdnp(980) 3 years, 1 month ago 0

People, the article is objective. It backs up its statements with facts, as in internal BCL code and proof. It does not state that you should use "". It is absolutely not telling you that you should use "". I don't want you to use "" and I never said that "" is better.

Also, the ""==null check is not ever executed, it is removed before runtime. In other words the benchmark tests an empty loop. I don't feel the article is deceptive in this regard.

Reply

posted by offwhiteoffwhite(975) 3 years, 1 month ago 0

This blog post completely ignores the fact that I/O is usually the performance issue and String.Empty versus "". And the time saved in using "" is so minimal that is not going to be significant in nearly every case. Now if you are a building real-time application which has code running in a loop many times, like in this example, then you may have a case to use "". But at that point, you should not be using C# to be writing a real-time application. No language/framework that uses Garbage Collection should likely be used for such an application, like a missile guidance system.

Are you writing a missile guidance system? I doubt it. And do you think that users on your ASP.NET web site will notice the difference of 0.00000002ms because you are using "" instead of String.Empty? Your efforts are better spent in optimizing the I/O bound parts of your system by tuning caching or SQL. Instead of saving 0.00000002ms you could save 100ms or more.

If you go over your code and replace String.Empty with "" you are wasting your time and making your code less readable.

Reply

posted by samdnpsamdnp(980) 3 years, 1 month ago 0

offwhite, you make a very valid and correct point. I agree with you completely.

As for the 0.00000002ms, I don't think that is correct. I was not able to measure any such difference. The "" loop contents are entirely removed and never executed. It is a benchmark of a loop with no body.

I find it disappointing that not only do people not understand the technical content, they do not understand the premise of the article. It may be partly my fault because I did not make it simpler.

Reply

posted by samdnpsamdnp(980) 3 years, 1 month ago 0

Also, regarding the missiles, I happen to know that my site is recommended as a reference on the NASA intranet :)

Reply

posted by software.developersoftware.developer(8) 3 years, 1 month ago 0

I think the performance gain is negligible, and it all comes down to personal preference and readability. With the latter having higher importance. To me, the only thing more readable than string.Empty is

// Overkill
public static const string EMPTY_STRING = string.Empty;

Also lol@missile guidance system.
</holywar>

Reply

information Login or create an account to comment on this story