The Daily WTF

I took a break from coding for the RDU Code Camp and went over to the daily WTF. This is a great WTF that I decided to dig into.

I created a project and added in an enum with the following signature:

1 public enum ApplicationTypeEnum 2 { 3 Stradegy2, 4 AdDetector, 5 MagAdvisor, 6 AdDetectorAlerts, 7 MarketAdvisorAdTel, 8 NewspaperAdvisor, 9 MarketAdvisorMarketSpender, 10 StradegyOnline, 11 AdSpender, 12 Evaliant, 13 eBooks, 14 FrenchAdex 15 } 16

I then added an Application Class and copy/pasted in the method that was in the post:

1 public class Application 2 { 3 private int SetApplicationId() 4 { 5 switch (ApplicationUserInfo.Current.ApplicationType.ToString()) 6 { 7 case "Stradegy2": return Convert.ToInt32(ApplicationTypeEnum.Stradegy2); 8 case "AdDetector": return Convert.ToInt32(ApplicationTypeEnum.AdDetector); 9 case "MagAdvisor": return Convert.ToInt32(ApplicationTypeEnum.MagAdvisor); 10 case "AdDetectorAlerts": return Convert.ToInt32(ApplicationTypeEnum.AdDetectorAlerts); 11 case "MarketAdvisorAdTel": return Convert.ToInt32(ApplicationTypeEnum.MarketAdvisorAdTel); 12 case "NewspaperAdvisor": return Convert.ToInt32(ApplicationTypeEnum.NewspaperAdvisor); 13 case "MarketAdvisorMarketSpender": return Convert.ToInt32(ApplicationTypeEnum.MarketAdvisorMarketSpender); 14 case "StradegyOnline": return Convert.ToInt32(ApplicationTypeEnum.StradegyOnline); 15 case "AdSpender": return Convert.ToInt32(ApplicationTypeEnum.AdSpender); 16 case "Evaliant": return Convert.ToInt32(ApplicationTypeEnum.Evaliant); 17 case "eBooks": return Convert.ToInt32(ApplicationTypeEnum.eBooks); 18 case "FrenchAdex": return Convert.ToInt32(ApplicationTypeEnum.FrenchAdex); 19 20 } 21 return 0; 22 } 23 } 24

I then had to tackle this line of code to get the app to compile:

1 switch (ApplicationUserInfo.Current.ApplicationType.ToString())

I created a ApplicationUserInfo class like so:

 

1 public class ApplicationUserInfo 2 { 3 public Current Current { get; set; } 4 } 5

And then a Current class like so:

1 public class Current 2 { 3 public ApplicationTypeEnum ApplicationType { get; set; } 4 } 5

Since properties can’t be static in C#, I needed to create an instance of the ApplicationUserInfo class in the Application class via a property:

1 public ApplicationUserInfo ApplicationUserInfo { get; set; }

I changed the scope of the SetApplicationId method to public and was then ready to throw a couple of unit tests on this:

1 [TestMethod()] 2 public void SetApplicationId_SetStradgy2ToApplicationUserInfo_Returns0_Passes_Test() 3 { 4 Application application = new Application(); 5 application.ApplicationUserInfo = new ApplicationUserInfo(); 6 application.ApplicationUserInfo.Current = new Current(); 7 application.ApplicationUserInfo.Current.ApplicationType = ApplicationTypeEnum.Stradegy2; 8 int expected = 0; 9 int actual = application.SetApplicationId(); 10 Assert.AreEqual(expected, actual); 11 } 12 13 [TestMethod()] 14 public void SetApplicationId_DoNotSetApplicationUserInfo_Returns0_Passes_Test() 15 { 16 Application application = new Application(); 17 application.ApplicationUserInfo = new ApplicationUserInfo(); 18 application.ApplicationUserInfo.Current = new Current(); 19 int expected = 0; 20 int actual = application.SetApplicationId(); 21 Assert.AreEqual(expected, actual); 22 } 23

The tests point out 2 WTFS immediately. The classes depend on an external variable that may or may not be instantiated. There is no argument validation. Also, note how 0 is a return value for “Stradegy2” and “None provided”. If “Stradegy2” is supposed to be the default value, it would be put into the “defult” section of the switch…case. Oh wait, no default – WTF #3.

Finally, SetApplicationId can be cleaned up with this nugget:

1 public int SetApplicationId() 2 { 3 return Convert.ToInt32(ApplicationUserInfo.Current.ApplicationType); 4 } 5

If you read the comments of the question, this was suggested in various forms (some even with the right syntax). WTF #4.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: