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
Testing Pre-Requisite - Care About Your Code

There's a lot of posts and chatter about testing, TDD, and a number of other topics on this blog, the larger devlicio.us site, and the .NET community in general.  Before all that, it should go without saying, but it must be said; there is a pre-requisite to testing, care about your code.

You have probably come across some code in your career that is less than ideal.  Maybe you've even submitted something to the Daily WTF. I'm currently combing some old code trying pull out some main concepts.  As I'm going through the code I'm seeing some things that really are quite alarming.  I'm not talking about differences in what I would do in terms in architecture, implementation, or even language choice.  I'm talking about blatant code rot, code that no one has cared for.

Some stats for the current file I'm in:

  • Lines in file: 1820.  This is the mildest offense, while I don't endorse a class/file that large I can live with it.
  • Classes defined in the file: 19.  I find this a "little" excessive, Even if you don't subscribe to the "one class per file" rule, 19 is a bit much don't you think?
  • Example Method: 697 lines.  That number isn't entirely accurate as 395 of the lines are commented out.  I understand the scenario that probably led to this, in fact I'll admit to doing it.  You comment out some code as a fail-safe in case the new implementation doesn't work.  The problem here is that the code does work (and I use that term loosely).  The developer never cleaned up after themselves and their trial code.  This makes absolutely no sense when you have source control in place.

Here's a method I came across today:

   1: Public Overrides Function ToString() As String
   2:     If IsNothing(_fields) Then
   3:         Return ""
   4:     End If
   5:  
   6:     Dim r2 As String
   7:     'make room for 1 char and 1 delimeter for each field
   8:     Dim retVal As StringBuilder = New StringBuilder("", _fields.Length * 2) 
   9:     For Each f As Field In _fields
  10:         r2 = r2 & f.Prefix &_ 
  11:             f.Value &_ 
  12:             f.Postfix &_ 
  13:             ConfigurationSettings.AppSettings("ControllerFieldDelimeter")
  14:         retVal.Append(f.Prefix)
  15:             .Append(f.Value)
  16:             .Append(f.Postfix)
  17:             .Append(ConfigurationSettings.AppSettings("ControllerFieldDelimeter"))
  18:     Next
  19:  
  20:     r2 = r2 & "<EOT>"
  21:     retVal.Append("<EOT>")
  22:  
  23:     'Return retVal
  24:     Return retVal.ToString
  25: End Function

The first thing I noticed was the string concatenation in lines 10-13.  The problem is the "_fields" (the collection over which this code is iterating) can be large.  So concatenation isn't the best choice.  While clearly not optimal, let's let that slide for a moment.  The very next line, lines 14-17, there is some appending of seemingly the same data.  In the end r2, the concatenated value which was so expensive to generate, is never returned or used elsewhere.  All of those extra, wasteful concatenation calls are spent on worthless code.  The developer(s) who worked with this over time simply never cleaned up after themselves or cared for the code.

I'm amazed that code like what I'm looking at is found in code bases.  I probably shouldn't be surprised but I am.  Today I was posting my reactions on Twitter and a friend Rhys Campbell commented:

"395 of them are commented out".. delete them! do it! You know you want/need to

I think Rhys is right, I will fix the code as I see it.  The codebase is being phased out but that doesn't mean it shouldn't be cared for.  If I am caring for code that is going to be obsolete and replaced soon, how much more important do you suppose is it to care for your current code?  If any of the above sounded like your code, get with it, clean up and care for your code.


Posted 10-02-2008 9:20 PM by Tim Barcz
Filed under:

[Advertisement]

Comments

Darrell Mozingo wrote re: Testing Pre-Requisite - Care About Your Code
on 10-02-2008 11:07 PM

I completely agree. The little things add up. I hate having to waste a few extra seconds here and there processing code that doesn't need to be there.

Reshaper and all of its hints makes cleanup like this super easy though.

Tim Barcz wrote re: Testing Pre-Requisite - Care About Your Code
on 10-02-2008 11:18 PM

@Darrell

I am a fan of both Resharper and Refactor Pro, both are great assistance tools.

When you find code that's like the code I mention do you change/fix it or leave it alone?

2008 October 03 - Links for today « My (almost) Daily Links wrote 2008 October 03 - Links for today &laquo; My (almost) Daily Links
on 10-03-2008 4:19 AM

Pingback from  2008 October 03 - Links for today &laquo; My (almost) Daily Links

chrissie1 wrote re: Testing Pre-Requisite - Care About Your Code
on 10-03-2008 4:23 AM

Change/fix it of course else somebody would think that you too don't care about the code.

Dew Drop - October 3, 2008 | Alvin Ashcraft's Morning Dew wrote Dew Drop - October 3, 2008 | Alvin Ashcraft's Morning Dew
on 10-03-2008 9:35 AM

Pingback from  Dew Drop - October 3, 2008 | Alvin Ashcraft's Morning Dew

bookmarking service wrote re: Testing Pre-Requisite - Care About Your Code
on 01-18-2013 2:04 PM

NSiJoL Thank you for your post. Keep writing.

buy imitrex generic wrote re: Testing Pre-Requisite - Care About Your Code
on 02-15-2013 5:18 PM

MwaQt3 Appreciate you sharing, great blog.Really looking forward to read more. Fantastic.

buy viagra discount wrote re: Testing Pre-Requisite - Care About Your Code
on 02-23-2013 6:01 PM

U4M5uF Great blog article.Really looking forward to read more. Really Great.

buy clomid wrote re: Testing Pre-Requisite - Care About Your Code
on 02-28-2013 7:21 PM

2B2X41 Im obliged for the article post.Thanks Again.

buy discount viagra wrote re: Testing Pre-Requisite - Care About Your Code
on 03-03-2013 6:25 PM

w08vIG I really like and appreciate your blog.Really looking forward to read more.

bookmaring service wrote re: Testing Pre-Requisite - Care About Your Code
on 03-13-2013 12:18 PM

QjyS9A I truly appreciate this blog post.Really looking forward to read more.

buy social bookmarks wrote re: Testing Pre-Requisite - Care About Your Code
on 03-22-2013 5:22 PM

NJrYIN Thanks for the post.Really thank you! Fantastic.

comedy wrote re: Testing Pre-Requisite - Care About Your Code
on 04-06-2013 12:56 PM

A big thank you for your blog article.Really thank you! Awesome.

buy social bookmarks wrote re: Testing Pre-Requisite - Care About Your Code
on 04-12-2013 2:37 PM

M0gUjq I value the blog.Really looking forward to read more. Much obliged.

Social bookmarks wrote re: Testing Pre-Requisite - Care About Your Code
on 04-16-2013 3:58 AM

6mqozj This is one awesome blog post.Really looking forward to read more.

Social bookmarks wrote re: Testing Pre-Requisite - Care About Your Code
on 04-20-2013 12:00 AM

Xjsmm2 Thanks again for the post.Really looking forward to read more. Really Cool.

best news of them all wrote re: Testing Pre-Requisite - Care About Your Code
on 08-04-2013 11:31 PM

mYnKyY I really liked your blog.Much thanks again. Fantastic.

top seo guys wrote re: Testing Pre-Requisite - Care About Your Code
on 10-25-2013 8:55 AM

XjqUAU I think this is a real great article post.Really looking forward to read more. Want more.

crorkservice wrote re: Testing Pre-Requisite - Care About Your Code
on 07-18-2014 6:57 PM

n329xT Im obliged for the blog post.Thanks Again. Cool.

matt wrote re: Testing Pre-Requisite - Care About Your Code
on 07-20-2014 1:27 AM

Add a Comment

(required)  
(optional)
(required)  
Remember Me?

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)