Code Review: Making a drop down list out of an enum

I’ve come across code like this a couple of times and it is rather odd: IEnumerable<CalendarViewEnum> _calendarViewEnums = Enum.GetValues(typeof(CalendarViewEnum)).Cast<CalendarViewEnum>(); List selectList = new List<SelectListItem>(); foreach (CalendarViewEnum calendarViewEnum in _calendarViewEnums) { switch (calendarViewEnum) { case CalendarViewEnum.FittingRoom: selectList.Add(new SelectListItem { Text = AdminPreferencesRes.Label_CalendarViewFittingRoom, Value = ((int)calendarViewEnum).ToString(CultureInfo.InvariantCulture), Selected = (calendarViewEnum == CalendarViewEnum.FittingRoom) }); break; case CalendarViewEnum.Staff: selectList.Add(new SelectListItem …

Tip of the day: Using the null-coalescing operator over the conditional operator

I’ve recently been refactoring a lot of code that used the conditional operator and looked something like this: int someValue = myEntity.SomeNullableValue.HasValue ? myEntity.SomeNullableValue.Value : 0; That might seem less verbose than the traditional alternative, which looks like this: int someValue = 0; if (myEntity.SomeNullableValue.HasValue) someValue = myEntity.SomeNullableValue.Value; …or other variations on that theme. However, …

Why should you be returning an IEnumerable

I’ve seen in many places where a method returns a List<T> (or IList<T>) when it appears that it may not actually really be required, or even desirable when all things are considered. A List is mutable, you can change the state of the List. You can add things to the List, you can remove things …

Interpreting promotional codes

The application I’m working on these days has a thing in it called a booking code. Now a booking code can be many things. It can tell you which discount code to use, which third party was being used to make the order, which business customer is making the order and so on. It is …

Tip of the Day #15: Loop Performance

When you look at the code it will probably seem somewhat obvious, but it is interesting how the same thought process isn?t necessarily there when actually developing the code, especially when under the pressure of a looming deadline. Take for example this snippet of code (from a fictitious hotel management system) that may have been …

The value of smaller methods

A while ago on a forum I advised someone that their method was too long and difficult to read and maintain. I suggested that they break the code into smaller methods as it would improve the maintainability and readability of the methods. I got a response back from another person on the forum suggesting that …