Derik Whittaker

Syndication

News


Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
Learning 'bad coding practices' while studying for a Certification :(

Ok, I will admit, maybe I am being a little nit picky here.  However, it is driving me crazy.  As I have posted in prior post (here and here), I am in the middle of studying for the MCTS exam #70-536.

As I am reading though the book, looking over the code snippets I notice some of the 'bad' practices that are going on. 

For example:

Using 'Magic Strings' in place of constants

Above they are using a 'magic string' in 2 different places as keys for serialization.  Why not use a constant?

Use the + for string concatenation:

Here they are using the '+' operator to concatenate strings.  However, a few chapters prior in the book they explicitly talk about NOT doing this.  They talk about using the StringBuilder object or String.Format.


You may be asking yourself, why does he care about this?  The answer is quite simple.  Many people are reading this book that are trying to learn .Net (this is actually the case at my shop).  If the see the authors of books like these (this book is not unique) doing 'poor practices' they will start to think that doing this stuff is acceptable.

Again, maybe I am being picky, but I would think that an educational book would attempt to follow best practices when ever possible.


Posted 07-02-2007 7:25 PM by Derik Whittaker
Filed under: ,

[Advertisement]

Comments

Fl0yd wrote re: Learning 'bad coding practices' while studying for a Certification :(
on 07-03-2007 10:48 AM

Whilst I completely agree that the use of magic strings in that example is very poor coding practice, I think the use of string concatenations in the ToString method is justified.  It is much more readable than a StringBuilder implementation would be and shouldn't present a performance hit (remember not to prematurely optimize!)

I don't know about .Net but in Java, the set of string contatenations would be converted to a StringBuffer/Builder by the (optimising) compiler, hence performance would be maintained even if the ToString was called repeatedly.

Derik Whittaker wrote re: Learning 'bad coding practices' while studying for a Certification :(
on 07-03-2007 11:11 AM

@Fl0yd,

While i agree about the premature optimization, my point is that a few chapters earlier the book talks about NOT doing what they are doing.

Not sure about the optimizing compiler.  Something tells me that it does NOT replace it with the StringBuilder.

Fl0yd wrote re: Learning 'bad coding practices' while studying for a Certification :(
on 07-03-2007 11:30 AM

Perhaps you're right, looking at 'How to: Join Multiple Strings'

http://msdn2.microsoft.com/en-us/library/ms228504(VS.80).aspx

seems to suggest that no optimizations are performed.

My only suggestion therefore would be to use String.Format() instead of a StringBuilder, for the sake of readability.

Derik Whittaker wrote re: Learning 'bad coding practices' while studying for a Certification :(
on 07-03-2007 11:38 AM

@Fl0yd,

I would agree with the string.format.  But anything is better then using the +

Omar wrote re: Learning 'bad coding practices' while studying for a Certification :(
on 07-05-2007 6:45 PM

While I agree with "Magic Strings" in general being bad, I'm going to have to disagree with using a string builder.

The reason you should use a String builder over string concatonation or String.Format() is when you are adding lots of strings together in a loop.

this is because if you do the following

String x = "wahoo";

x += " yeah yeah yeah";

a new string is created, whereas with the string builder you are just adding to a variable.

string builders are overkill for building up a literal.

if these are the only two bad habits you are learning from the book you are lucky. I'm reading through the book myself and to be honest it's not very well put together.

I think in this case however the readability of the code makes it okay, he's already told you not to this this elsewhere and I seem to remember there being a disclaimer somewhere that the code is simplified.

code in text books rarely handles exceptions, or checks for null, or ensures that the text is appropriate for the current locale, thats because thats not what the code snippet is trying to illustrate. The same applies to Magic strings technically but thats a different kettle of fish.

expresso wrote re: Learning 'bad coding practices' while studying for a Certification :(
on 07-15-2007 11:07 PM

I think that StringBuilder is great for longer string concatenation.  But for just a few concatenations (like up to maybe 4) it's fine to use  +

expresso wrote re: Learning 'bad coding practices' while studying for a Certification :(
on 07-15-2007 11:08 PM

basically per your point t though yes...the author was just lazy.  At the least, he should have explained why he was using +.  I know that in other good books, the authors know what they are doing, and take the time out to explain in a quick sentence why they deviated....definitely poor writing there.

ericboyd wrote re: Learning 'bad coding practices' while studying for a Certification :(
on 07-16-2007 10:12 PM

I am not a fan of "+" string concatenation. I think String.Format is the best option for this situation and is by far the cleanest out of the three options. As a rule of thumb, you typically will get a significant performance increase using the StringBuilder class when looping and concatenanting large strings.

public override string ToString()    

{      

    return name + " was born on " + dateOfBirth.ToShortDateString() + " and is " + age.ToString() + " years old."    

}  

public override string ToString()

{

    StringBuilder sb = new StringBuilder();

    sb.Append(name);

    sb.Append(" was born on ");

    sb.Append(dateOfBirth.ToShortDateString());

    sb.Append(" and is ");

    sb.Append(age);

    sb.Append(" years old.");

    return sb.ToString();

}

public override string ToString()

{

    return string.Format("{0} was born on {1} and is {2} years old.", name, dateOfBirth.ToShortDateString(), age);

}

gazza wrote re: Learning 'bad coding practices' while studying for a Certification :(
on 08-23-2007 9:30 AM

If you look at the msil produced, you'll see for the + operator for 2 or 3 values that it is replaced under the hood by String.Concat(), and is much more readable.

String.Format() creates a StringBuilder under the hood, and is much too heavyweight for this simple case.

Definitely use a StringBuilder if you're creating loads of strings, in a loop etc (I think I read on an msdn article that if you're adding over 16 strings the overhead of StringBuilder becomes less than the overhead of too many string allocations).

anima wrote re: Learning 'bad coding practices' while studying for a Certification :(
on 01-09-2008 8:55 AM

because all comments here have been with a degree of intellgence and there has to be at least one of these everywhere on the net:

lol stfu noob u dnt know wht u on about book is good not bad haha XD

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)