Fabiola Martinez

Fabiola Martinez

  • 2.1k
  • 9
  • 248

How to improve the functionality of this code in the face of new requi

Apr 9 2021 4:26 AM
Hi, I'm working with asp.net mvc c#. I have a form the allows users to register absences. 
 
This form has three fields, a DropDownList with the list of reasons for absence (Illness, Study day, Other reason), date (the system takes the date) and description. But depending on the selected item it is validated for example. If the user selects the Illness item, it shows another DropDownList with the items (Abdominal Pain, Fever) and if it is by Study Day, they are allowed to select a date but with the restriction of notifying the absence of it 5 days in advance. If the user selects Other Reasons, another DropDownList is shown with the following items (Free Day, Moving) with the restrictions, for Free Day it should only be allowed between the 1st to the 5th of each month, after those days it should no longer allow record an absence. For Moving three days in advance, the absence must be registered. 
 
This is my code
  1. public bool CreateAbsence (Absence absence)    
  2. {    
  3.     /*Id Reason Study Day = 2*/     
  4.     if (absence.ReasonId == 2)    
  5.     {    
  6.          int _dayAb = dayAbsence.Day;    
  7.          int _dayCu = dateCurrent.Day;    
  8.          if ((_dayAb - _dayCu) <= 5) //You must register 5 days in advance.    
  9.          {    
  10.              //It is not allowed to create absence.     
  11.             return;    
  12.          }    
  13.     }    
  14.         
  15.     /*Id Reason Free Day = 5*/     
  16.     else if (absence.SubReason == 5)    
  17.     {    
  18.         int dayAb = dayAbsence.Day;    
  19.         if (dayAb >= 5) //It is only allowed until the 5th of each month.     
  20.         {    
  21.              //It is not allowed to create absence.     
  22.             return;    
  23.         }    
  24.     }    
  25.     
  26.     /*Id Moving = 8*/     
  27.     else if (absence.SuBReason == 8)    
  28.     {    
  29.         int _dayAb = dayAbsence.Day;    
  30.          int _dayCu = dateCurrent.Day;    
  31.         if ((_dayAb - _dayCu) <= 2) //You must register 3 days in advance.    
  32.         {    
  33.              //It is not allowed to create absence.     
  34.             return;    
  35.         }    
  36.     }    
  37.         
  38.     //it is allowed to create the absence    
  39. }
I want to know how I can improve this code when new requirements appear. What do you recommend me? I'm looking at a bit of SOLID principles but still don't figure out how to fix this. Sorry if I extended myself too long. Thanks, 

Answers (3)