"A little while back, someone introduced the concept of 'self-documenting' code to our team," writes Ryan L. "It was certainly a step forward, but it's somehow taken us two steps backwards. Consider, for example, the following code from an MVC controller."
if (TheFormIsInvalid()) return View("Index", form); ... snip ... private bool TheFormIsInvalid() { return ModelState.IsValid == false; }
"Is it really that much easier to follow than if (!ModelState.IsValid)
? I'll set that question aside for the moment to instead present some other code from the same class that handles online registrations for events:
public bool IsEligibleToSeeThisEvent(EventRegistrationInformation eventRegistrationInformation) { if (ThisEventDoesNotRestrictRegistrantsBasedUponActivityType(eventRegistrationInformation)) return TrueBecauseThisEventDoesNotRegistrictBasedUponActivityType(); var activityTypes = GetTheActivityTypesAttachedToThisEvent(eventRegistrationInformation); if (NoActivityTypesAreAttachedToThisEvent(activityTypes)) return TrueBecauseThereAreNoActivityTypesToFilterOnThisEvent(); if (TheUserIsNotLoggedIn(eventRegistrationInformation)) return FalseBecauseNoActivityTypesAreAvailableForUsersWhoAreNotLoggedIn(); return ThereIsAtLeastOneSharedActivityTypeBetweenTheEventAndCurrentUser( eventRegistrationInformation, activityTypes); } private static bool TrueBecauseThisEventDoesNotRegistrictBasedUponActivityType() { return true; } private bool ThisEventDoesNotRestrictRegistrantsBasedUponActivityType( EventRegistrationInformation eventRegistrationInformation) { return eventActivityTypeFilterRetriever .DoesThisEventHaveARestrictionBasedOnActivityTypes( eventRegistrationInformation.EventId) == false; } private bool ThereIsAtLeastOneSharedActivityTypeBetweenTheEventAndCurrentUser( EventRegistrationInformation eventRegistrationInformation, IEnumerable<ActivityType> activityTypes) { var currentActivities = registrantActivityRetriever .GetRegistrantActivityProductCode(eventRegistrationInformation.AccountId); return activityTypes.Any(x => currentActivities.Any(y => y.Id == x.Id)); } private static bool FalseBecauseNoActivityTypesAreAvailableForUsersWhoAreNotLoggedIn() { return false; } private static bool TheUserIsNotLoggedIn(EventRegistrationInformation eventRegistrationInformation) { return eventRegistrationInformation.IsLoggedIn == false; } private static bool TrueBecauseThereAreNoActivityTypesToFilterOnThisEvent() { return true; } private static bool NoActivityTypesAreAttachedToThisEvent(IEnumerable<ActivityType> activityTypes) { return activityTypes.Any() == false; } private IEnumerable<ActivityType> GetTheActivityTypesAttachedToThisEvent( EventRegistrationInformation eventRegistrationInformation) { return eventActivityTypeFilterRetriever.GetAll(eventRegistrationInformation.EventId); }
[Advertisement]
BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!