Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A C# Refactoring Question

I came accross the following code today and I didn't like it. It's fairly obvious what it's doing but I'll add a little explanation here anyway:

Basically it reads all the settings for an app from the DB and the iterates through all of them looking for the DB Version and the APP Version then sets some variables to the values in the DB (to be used later).

I looked at it and thought it was a bit ugly - I don't like switch statements and I hate things that carry on iterating through a list once they're finished. So I decided to refactor it.

My question to all of you is how would you refactor it? Or do you think it even needs refactoring at all?

Here's the code:

        using (var sqlConnection = new SqlConnection(Lfepa.Itrs.Framework.Configuration.ConnectionString))
        {
            sqlConnection.Open();

            var dataTable = new DataTable("Settings");

            var selectCommand = new SqlCommand(Lfepa.Itrs.Data.Database.Commands.dbo.SettingsSelAll, sqlConnection);
            var reader = selectCommand.ExecuteReader();
            while (reader.Read())
            {
                switch (reader[SettingKeyColumnName].ToString().ToUpper())
                {
                    case DatabaseVersionKey:
                        DatabaseVersion = new Version(reader[SettingValueColumneName].ToString());
                        break;
                    case ApplicationVersionKey: 
                        ApplicationVersion = new Version(reader[SettingValueColumneName].ToString());
                        break;
                    default:
                        break;
                }
            }

            if (DatabaseVersion == null)
                throw new ApplicationException("Colud not load Database Version Setting from the database.");
            if (ApplicationVersion == null)
                throw new ApplicationException("Colud not load Application Version Setting from the database.");
        }
like image 754
james lewis Avatar asked Jun 11 '10 12:06

james lewis


People also ask

What is AC for?

An air conditioner provides cold air inside your home or enclosed space by actually removing heat and humidity from the indoor air. It returns the cooled air to the indoor space, and transfers the unwanted heat and humidity outside.

Which AC is best for home use?

LG, Samsung, Blue Star, Voltas, Hitachi, Daikin, and Panasonic, are some of the best brands in the top 10 AC brands in India. A. Yes. Features like easy filters, an auto cleaning system, a 100 percent copper condenser, and more make Samsung AC one of the most reliable and durable AC brands.

What does AC stand for cooling?

This mechanical system is an integral part of heating and cooling any modern building. AC is short for air conditioning. The terms HVAC and AC are often used interchangeably. CALL 1-800-365-1920 to speak with an HVAC professional today.


1 Answers

My two cents...

  1. As Bobby comments, use using on every disposable object
  2. I would avoid opening a table and iterate through all the records, use a filter if possible to obtain the values
  3. If not possible at all, avoid using switch on strings, as there are only two options you could do an if else with a string.Compare with the case insensitive option, so you don't have to make an upper each time.
  4. Check for null values before reading them to avoid unhandled exceptions
  5. If you have to open that kind of connections many times in your code you may use a factory method to give you the connection.
  6. I would avoid using "var" keyword when you already know what kind of object you are creating. You usually refactor to enhance code legibility.
like image 102
jmservera Avatar answered Nov 11 '22 18:11

jmservera