morfizm (morfizm) wrote,
morfizm
morfizm

Antipatterns

Originally posted by rss_thedailywtf at CodeSOD: Self Documenting

"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);
}





Tags: software development
Subscribe

  • Израиль и Палестина

    В связи с участившимися случаями поддержки терроризма во всём мире, а также с тем, что ООН стал антисемитстким и про-террористическим, я продублирую…

  • Sony a7S-III

    Напишу апдейт про фото-хобби и про камеру-мечту, которой у меня пока нет. Что я снимаю нынче Фотография эволюционировала в сторону портретов.…

  • Диета

    Социальный аспект ⌘ Приготовление еды - раздельное, тут без шансов. Вместе можно есть (раздельно приготовленное) и пить чай-кофе. Надо говорить…

  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 0 comments