Refactor “if” statements – functional programming style

Have you ever seen code that look like this: public string GetStatusDescription(Model model){ if(model.HasProblemReports) { return "Errors"; } if(model.SystemState.WorkingMode == WorkingMode.NotManaged) { return "Manual"; } if(model.SystemState.IsInitializing) { return "Initialize"; } if(!model.SystemState.InService) { return "Not in service"; } if(model.SystemState.WorkingMode == WorkingMode.Paused) { return "Paused"; } if(model.Storage.Objects.Any(obj => obj.IsMoving)) { return "Movement in storage"; } return string.Empty;}I … Continue reading Refactor “if” statements – functional programming style

Enabling parameterized tests in MSTest using PostSharp

I have blogged about the shortcoming of Microsoft’s unit testing framework in the past. It has very good Visual Studio (and TYFS) integration out of the box but it seems that in order to use it I have to suffer lack of functionality I’m used to taking for granted when using any other .NET unit … Continue reading Enabling parameterized tests in MSTest using PostSharp

How to return default(Type) in runtime – a TDD example in four unit tests

I’ve found this question while going over my old StackOverflow answers:I'm using reflection to loop through a Type's properties and set certain types to their default. Now, I could do a switch on the type and set the default(Type) explicitly, but I'd rather do it in one line. Is there a programmatic equivalent of default?I … Continue reading How to return default(Type) in runtime – a TDD example in four unit tests

Using null-coalescing to find item in multiple collections

A co-worker showed me this:var result = collection1.GetObjectByAtSpecialLocation(location) ?? collection1.GetObjectByAtLocation(parent, location) ?? collection2.GetObjectByAtSpecialLocation(location) ?? collection2.GetObjectByAtLocation(parent, location);I’m still trying to decide whether it’s a good practice and if it’s readable or not…In case you’re not familiar with ?? operator a.k.a null-coalescing operator it’s basically returns a default value if the left side is null so the … Continue reading Using null-coalescing to find item in multiple collections