Home computickets Clean Code: Immounty Demetra Law

Clean Code: Immounty Demetra Law

Author

Date

Category

Now I read the book Pure code on a small chapter about the Demetra law. Before that, he came across the patterns from O’Reilly (Headfirst) and there was a little and understandable. After clean code – in the head Kavardak. There are a couple of questions:

1) Martin divides objects to objects and data structures. What is meant here under the structure? Struct (in C # for example) or as a whole class object in which except open fields (or properties) there is no more anything (methods, for example)?

2) from the first question follows the misunderstanding of what is meant by a hybrid (half an object, the head structure), which is better not to write. Is it considered a hybrid, for example, the class “car” class, where it is possible to see it, and even change the wheels, and does it have behavior too?

3) This question raised to an oral discussion in other developments (we are not juna, but also not “coarse”) and also misunderstanding the very essence of this law, why comply with it, which is the main reason: or the base for further challenges , or hiding the internal structure of the object, or even easily preventing and processing NULLREFERENCEEXCEPTION? Everyone understands different ways.

4) Is the law disrupts when dividing “bad” methods on a lot of small?

It was

public class class1
{
  Public String Dosmth ()
  {
    // It seems like the law is broken, because we call the method of returning values
    RETURN METHODCLASS1 (). DOSMTH ();
  }
  Public Class2 Methodclass1 ()
  {
    RETURN NEW CLASS2 ();
  }
}
Public Class Class2.
{
  Public String Dosmth ()
  {
    String Res2 = this.methodclass2 ();
    RETURN RES2;
  }
  Public String Methodclass2 ()
  {
    Return "Test";
  }
}

became

Public Class Class1
{
  Public String Dosmth ()
  {
    // Now only methods of the same class are called here.
    Class2 Res1 = Methodclass1 ();
    Return this.methodclass1_2 (RES1);
  }
  Public Class2 Methodclass1 ()
  {
    RETURN NEW CLASS2 ();
  }
  Public String Methodclass1_2 (Class2 Val)
  {
    Return val.dosmth ();
  }
}

Another small update: I personally had such an attitude: it is some kind of rule, the need for an explicit violation of which is an indicator that something is not so designed and you can find a solution that will be better and on business logic And for further accompaniment.


Answer 1, Authority 100%

In the book “Programmer Pragmatik” (Hunt, Thomas), the law of Demeter (such a curve Translation) is reduced to a short statement:

Minimize binding between modules

Example from the book on C++

class demeter
{
  Private:
    A * A;
    INT FUNC ();
  Public:
    Void Example (B & AMP; B);
}
Void Demeter :: Example (B & AMP; B)
{
  int f = func (); // 1
  b.invert (); // 2.
  a = new a ();
  A- & gt; setActive (); // 3.
  C C;
  C.Print (); // 4
}

Demmers law for functions states that any method of some object can only apply for methods belonging:

  1. yourself
  2. any parameters transmitted to the
  3. method

  4. any objects created by them
  5. anyone directly containing component objects

Based on this, you can give an answer to your question number 3: Reducing linations facilitates modification and support of code, facilitates spelling tests.

In practice, this means that you have to create a large number of shell methods that simply send a request further to the delegate. These shell methods entail costs, worsening performance and occupying memory.

Reversing the Demmers Act and tightly linking several modules, you can get a gain in performance.


Answer 2, Authority 50%

1) He clearly writes about the structure:

On the other hand, if CTXT, Options and Scratchdir are
Conventional data structures, not possessing the behavior , then they
naturally reveal their internal structure, and the law
Demmers do not apply to them.

Behavior is not hidden if the caller refers directly to the data, and not through get / set (this is a simple structure, as in Si, for example). At the same time, if the appeal goes through the get / set it still does not mean that there is some behavior there (it may be hidden there, but not a fact) and therefore:

Application of access functions makes it difficult to the situation.

2) and about hybrids, it seems available:

hybrids contain both functions for performing important operations and
open variables
or open read / write methods that
All relations make private variables open.

In my opinion, concealing behavior and is a key factor. If there is some kind of behavior and it is hidden, then this is an object if there is no behavior, but simply open data, then this is a structure, and if there is a hidden behavior and open data – here you are a hybrid.


Answer 3, Authority 25%

I will answer the 4th question. If “was” and “became” to leave such, in what form you have, then the law is violated in both cases. What are there any “violations”:

  • Class1 is essentially a wrapper for Class2 (at least from that code that you have provided) – such behavior should be achieved inheritance;
  • Class1 is very much dependent on Class2 (why Class1, which is not a factory, is engaged in creating an instance of Class2?).

must “become” so:

public class class2
{
  Public Return_Type Dosmth ()
  {
    // something is done there
  }
}
Public Class Class1
{
  Public Return_Type Dosmth (Class2 Val)
  {
    / *
    There must be some kind of logic, but not a stupid return, it seems:
    Return val.dosmth ();
    * /
  }
}

And even better, if the Class2 class instance is sent to the Class1 class constructor and contact the Class2 class object using the Class1 class property – by the way, this requires this context “Demers”. At the same time as Dependency It is not necessary to transmit the class itself, but an interface that will implement Class2.

On the 3rd question you yourself are partly and answered – the code becomes easier accompanied by, support, writing tests, the code becomes less complex to understand, more opportunities when you reuse code (otherwise the code will be duplicated), the dependence of the classes is reduced From each other.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions