Donot mix up layers in one function

Recently i did some refactoring on the old code. The code works fine as expected. Except that it was hard to test since there were too many dependencies on the implementation.

Here what it look like:

      public static void Accept(Task task)
      {
         task.ActualStartDate = CGDateTime.Now;
         task.Completed = false;

         if (task.ResponsibleResource.ResourceType == ResourceType.Group && task.GetSetting(RuleResource.Rules.Personalize_On_Acceptance))
            task.ResponsibleResource = AccessFactory.CurrentUser;

         task.Save();

         Comment acceptance = new Comment();
         acceptance.CreatedBy = acceptance.ModifiedBy = AccessFactory.CurrentUser;
         acceptance.CreatedDate = acceptance.ModifiedDate = CGDateTime.Now;
         acceptance.Type = CommentType.Acceptance;
         acceptance.Resource = task;

         acceptance.Save();

         UpdateAcceptedChildren(task, acceptance);

         TaskInformer.RegisterForCatTask(task, ApplicationVirtualPath, TaskInformer.Action.Accept);
      }

I am not going to explain more about that implementation and why and how i did the refactoring since it belongs to another topic.

In general, after creating some interface i had this one:

public static void Accept(Task task)
{
// #1 – The implementation
        task.ActualStartDate = CGDateTime.Now;
         task.Completed = false;

         if (task.ResponsibleResource.ResourceType == ResourceType.Group &&
             task.GetSetting(RuleResource.Rules.Personalize_On_Acceptance))
            task.ResponsibleResource = AccessFactory.CurrentUser;

         task.Save();
// #2 – The abstraction
         Comment comment= _commentcreator.CreateAcceptedComment(task);
         _childenupdator.UpdateAcceptedChildren(task, comment);
         _informer.RegisterForCatTask(task,ApplicationVirtualPath, TaskInformer.Action.Accept);

}

 _commentcreator is interface responsible for creating comment based on task

_chidrenupdator is interface responsible for updating its children information

_informer is interface responsible for informing others in the system.

It seems that we’ve alread done or at least at that time i can easily test the function without caring too much on the other parts. However, if you look at the code again, which i have marked with #1 and #2.
WE ARE MIXING THE IMPLEMENTATION AND ABSTRACTION IN ONE FUNCTION.

It should be in the same level.

How to solve the problem? Simply just abstract the implementation part. Here comes with the final code:

public static void Accept(Task task)

{

       _taskinfor.UpdateTaskInfoOnAcceptance(task);

         Comment comment= _commentcreator.CreateAcceptedComment(task);
         _childenupdator.UpdateAcceptedChildren(task, comment);
         _informer.RegisterForCatTask(task,ApplicationVirtualPath, TaskInformer.Action.Accept);

}

The benefit of doing this is obvious.

Value of 30 morning minutes

Have you ever thought of getting up 30 minutes earlier in the morning? Let’s see the real situation of a working day:

I am supposed to be at work at 7h45, and since i can delay till 8h30 or even later. I just need to fulfill my hour-working day.

Ok, Here was how can i start a day before:

  • 7h: Wake up and try to get up at 7h05.
  • Do personal hygiene and take a wash -> yeah 7h30 already.
  • Dress on and have breakfast

Can you guess what time will be when you done with breakfast? And time to get to work -> You are late. However, that is not a big problem in nowadays. We accept that as the sunshine.

Assuming that you are 30 minutes late at work, then you need to work extra for that part. As a result, you end up with going out the office at about 6h-6h30. Adding time for relaxing, washing, dinner,… how much time do you have left for doing something at night: reading book, article, body exercise, friends…?

So, what happen if you get up at 6h30, GET UP NOT WAKE UP? Here are what i get while applying:

  1. Have more time in the morning, therefore, i do not put myself in leg-on-head situation.
  2. At work on time.
  3. More time in the evening: Out of the office at about 6h, washing, dinner. At 7h, i can take a class or play badminton, or go out with friends.

If anyone has tried, then please tell me your idea or suggestion.