The Dilemma's of a Developer

by jmorris 12. January 2011 01:43

I ran across this today while refactoring/reviewing some code:

image

What in the heck I am I supposed to do with this? Delete it? Refactor it? Quickly close the file and pretend I never saw it? Mind you that it’s in code that’s in production and get’s run several times a day…and the code works as expected.

I chose to refactor it:

image

Unit tests pass:

image

Hold your breath!

Tags: , , , ,

Refactoring

Comments (3) -

Phil
Phil
1/20/2011 12:55:40 PM #

In the code you did refactor the variable i is never chagned from zero.

In the refactored code you return the number of articles, which mos def wont be equal to zero.

Or?

jmorris
jmorris
1/21/2011 1:29:03 AM #

The article count is zero if no articles exist within that category, which is acceptable. However, the next available article name is "article" + the count which increases by one every-time a new article is added, so:

article0
article1
...
article + n-1

An Phu
An Phu
3/12/2011 1:29:08 PM #

It looks like the foreach is missing an increment statement.  Maybe that is why it wasn't obvious to the OP.

Jeff Morris

Widget Tag cloud not found.

Unable to cast object of type 'ASP.widgets_tag_cloud_widget_ascx' to type 'App_Code.Controls.WidgetBase'.X

Widget Month List not found.

Unable to cast object of type 'ASP.widgets_month_list_widget_ascx' to type 'App_Code.Controls.WidgetBase'.X

Widget Page List not found.

Unable to cast object of type 'ASP.widgets_page_list_widget_ascx' to type 'App_Code.Controls.WidgetBase'.X

Widget Blogroll not found.

Unable to cast object of type 'ASP.widgets_blogroll_widget_ascx' to type 'App_Code.Controls.WidgetBase'.X

Widget Visitor info not found.

Unable to cast object of type 'ASP.widgets_visitor_info_widget_ascx' to type 'App_Code.Controls.WidgetBase'.X

Widget Visitor info not found.

Unable to cast object of type 'ASP.widgets_visitor_info_widget_ascx' to type 'App_Code.Controls.WidgetBase'.X