Devlico.Us
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @devlicious

Derik Whittaker

Thoughts on Software Development, .Net, OOP, Design Patterns and all things cool



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.


Published Jul 02 2007, 07:25 PM by Derik Whittaker
Filed under: ,

Comments

Fl0yd said:

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.

# July 3, 2007 10:48 AM

Derik Whittaker said:

@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.

# July 3, 2007 11:11 AM

Fl0yd said:

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.

# July 3, 2007 11:30 AM

Derik Whittaker said:

@Fl0yd,

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

# July 3, 2007 11:38 AM

Omar said:

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.

# July 5, 2007 6:45 PM

expresso said:

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  +

# July 15, 2007 11:07 PM

expresso said:

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.

# July 15, 2007 11:08 PM

ericboyd said:

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);

}

# July 16, 2007 10:12 PM

gazza said:

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).

# August 23, 2007 9:30 AM

anima said:

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

# January 9, 2008 8:55 AM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add

About Derik Whittaker

Derik is a .Net Developer/Architect specializing in WinForms working out the northern suburbs of Chicago. He is also believer and advocate for Agile development including SCRUM, TDD, CI, etc.

When Derik is not writing code he can be found spending time with his wife and young son, climbing on his bouldering wall, watching sports (mostly baseball), and generally vegging out. Check out Devlicio.us!

Our Sponsors

Proudly Partnered With


This Blog

Syndication

News