Buenas,

a few days ago there was a webcast where friends of JetBrains commented on the news ofReSharper 7. 55 Minutes quite interesting, and among the things that showed today I remembered one that came to the hair. A case of refactoring where you are with a class that has too much content and responsibilities and touches you, begin to separate parties.

In one example a bit like the one shown to the webcast, and quite more similar to what it was changed recently, we assume a class that only see it makes us bad eyes.

   1: namespace ElBruno.Refactoring
   2: {
   3:     public class ComplexClass
   4:     {
   5:         public int SimpleProperty1 { get; set; }
   6:         public int SimpleProperty2 { get; set; }
   7:         public void SimpleStuff1()
   8:         {
   9:             SimpleProperty2 = 2;
  10:         }
  11:         public int SimpleStuff2()
  12:         {
  13:             return SimpleProperty1 + SimpleProperty2;
  14:         }
  15:         public int ComplexProperty1 { get; set; }
  16:         public int ComplexProperty2 { get; set; }
  17:         public void ComplexStuff1()
  18:         {
  19:             ComplexProperty2 = 2;
  20:         }
  21:         public int ComplexStuff2()
  22:         {
  23:             return ComplexProperty1 + ComplexProperty2;
  24:         }
  25:  
  26:     }
  27: }

Tip: this is popularly called Code Smell . Give him a return to the definition because it is more popular than think. If you’ve never felt that feeling or live enclosed in a bubble and do not know that you do read this blog or your level of empathy is at the height of Frodo.

As well, at a glance we see that we have mixed the responsibilities of Complex and Simple in the same class, so he plays “separate them”. On the class pressed CTRL + SHIT + R and deploy the refactoring menu.

image

Select the option “Extract Class” and we can see a form to define elements of the complex class want to pass to another class, in this case to a so-called SimpleClass. When are going to select items that we want to move up, we see that also shows the dependencies between them and suggests that we should also move to the new class.

image

Once finished the refactoring, you can find 2 classes quite clean and the code already smells a little better.

Disclaimer: I have put everything in a single file to be able to show the example, do not kill me.

   1: namespace ElBruno.Refactoring
   2: {
   3:     public class SimpleClass
   4:     {
   5:         public int SimpleProperty1 { get; set; }
   6:         public int SimpleProperty2 { get; set; }
   7:  
   8:         public void SimpleStuff1()
   9:         {
  10:             SimpleProperty2 = 2;
  11:         }
  12:  
  13:         public int SimpleStuff2()
  14:         {
  15:             return SimpleProperty1 + SimpleProperty2;
  16:         }
  17:     }
  18:  
  19:     public class ComplexClass
  20:     {
  21:         private readonly SimpleClass _simpleClass = new SimpleClass();
  22:  
  23:         public int ComplexProperty1 { get; set; }
  24:         public int ComplexProperty2 { get; set; }
  25:         public void ComplexStuff1()
  26:         {
  27:             ComplexProperty2 = 2;
  28:         }
  29:         public int ComplexStuff2()
  30:         {
  31:             return ComplexProperty1 + ComplexProperty2;
  32:         }
  33:  
  34:     }
  35: }

But… Everything is not always as simple

Now, the previous example we like very much, but real not all in life is as simple. Those people who don’t think about their physical well-being generally do grow much classes, but also generate relationships and dependencies between items of the same class to cut off fingers thinking tends to be the first thing you think of when you see something like (also remember their mothers, parents and in very extreme cases get to remember his grandmother.) The latter case recommend you pull the code and do it again from zero).

   1: namespace ElBruno.Refactoring
   2: {
   3:     public class ComplexClass
   4:     {
   5:         public int SimpleProperty1 { get; set; }
   6:         public int SimpleProperty2 { get; set; }
   7:         public void SimpleStuff1()
   8:         {
   9:             SimpleProperty2 = 2;
  10:         }
  11:         public int SimpleStuff2()
  12:         {
  13:             return SimpleProperty1 + SimpleProperty2 + ComplexProperty1;
  14:         }
  15:         public int ComplexProperty1 { get; set; }
  16:         public int ComplexProperty2 { get; set; }
  17:         public void ComplexStuff1()
  18:         {
  19:             ComplexProperty2 = 2;
  20:         }
  21:         public int ComplexStuff2()
  22:         {
  23:             return ComplexProperty1 + ComplexProperty2 + SimpleProperty1;
  24:         }
  25:     }
  26: }

In the previous case, from the SimpleStuff2 function () is using protocol properties of Complex and something similar from the function ComplexStuff2 (). what solució gives us ? ReSharper for these cases? What it does, is create a private of the related tipo field and with the same, these dependencies are resolved. For example:

   1: namespace ElBruno.Refactoring
   2: {
   3:     public class SimpleClass
   4:     {
   5:         private ComplexClass _complexClass;
   6:  
   7:         public SimpleClass(ComplexClass complexClass)
   8:         {
   9:             _complexClass = complexClass;
  10:         }
  11:  
  12:         public int SimpleProperty1 { get; set; }
  13:         public int SimpleProperty2 { get; set; }
  14:  
  15:         public void SimpleStuff1()
  16:         {
  17:             SimpleProperty2 = 2;
  18:         }
  19:  
  20:         public int SimpleStuff2()
  21:         {
  22:             return SimpleProperty1 + SimpleProperty2 + _complexClass.ComplexProperty1;
  23:         }
  24:     }
  25:  
  26:     public class ComplexClass
  27:     {
  28:         private readonly SimpleClass _simpleClass;
  29:  
  30:         public ComplexClass()
  31:         {
  32:             _simpleClass = new SimpleClass(this);
  33:         }
  34:  
  35:         public int ComplexProperty1 { get; set; }
  36:         public int ComplexProperty2 { get; set; }
  37:         public void ComplexStuff1()
  38:         {
  39:             ComplexProperty2 = 2;
  40:         }
  41:         public int ComplexStuff2()
  42:         {
  43:             return ComplexProperty1 + ComplexProperty2 + _simpleClass.SimpleProperty1;
  44:         }
  45:     }
  46: }

It is likely that in a case like this, you have to separate the responsibilities of the classes in a 3rd level. ReSharper already has given us a great help now touch pass to another tool we use little and is known as the “brains” or the mind… Open-mouthed smile

Saludos @ Home

El Bruno

image image image

One response to “[#RESHARPER] Cool Feature: Extract Class”

  1. Good post. I definitely love this website. Thanks!

    Like

Leave a reply to London traveling info Cancel reply

Discover more from El Bruno

Subscribe now to keep reading and get access to the full archive.

Continue reading