Nerpa: Demeter Law analysis

Hi,

Does anybody here need analysis for Law of Demeter (LoD) violations for Nerpa?
Personally I think it is almost useless in its direct form. Here is why.

http://en.wikipedia.org/wiki/Law_of_Demeter
***********
Formally, the Law of Demeter for functions requires that any method M of
an object O may only invoke the methods of the following kinds of objects:
itself
its parameters
any objects it creates/instantiates
its direct component objects
***********

1. It is actually pretty complex. It requires tracking live instances of
variables through the flow graph, including aliasing and other compiler-level
techniques.
Not that it is impossible, but it should have good argumentation to start
implementing it and tackle complexity.

2. It requires external knowledge about code to avoid false positives. For
example, factory methods should be treated as "any objects it creates/instantiates"
rule for LoD, but formally it is "object returned by another method". Singleton.Instance
is a simple example of this. But there can be much more complex cases.

What should analysis algorithm think about the following case?

void foo(ISolution solution)
{
MySolutionComponent c = (MySolutionComponent)solution.GetComponent(typeof(MySolutionComponent));
//
c.DoWork(); //
}

If is treated as factory method, then it falls into "objects it creates/instantiates"
and is valid from the LoD point of view. If is an ordinary method
call, than is violation of rules.

3. Its definition is *intentionally left vague so that it can be adapted
to particular circumstances". Here are some definitions:

http://www.ccs.neu.edu/research/demeter/demeter-method/LawOfDemeter/object-formulation.html
I. Each unit should only talk to its friends; Don't talk to strangers. There
are several definitions about who is considered "a friend". I've found the
following to be the most specific:

1. A parameter of the method, including the enclosing object (this); For
pragmatic reasons: a global object;
2. An immediate part object (computed or stored)

  • An object that a method called on the enclosing object returns, including

attributes of the enclosing object;

  • An element of a collection which is an attribute of the enclosing object;

3. An object created within the method.

Note, the difference from wikipedia definition - it includes collections
and global objects. #3 should be extended to allow factory methods.

http://www.ccs.neu.edu/research/demeter/demeter-method/LawOfDemeter/general-formulation.html
II. Each unit should have only limited knowledge about other units: only
units "closely" related to the current unit.
unit = METHOD
closely related =
1. methods of class of this of METHOD
2. methods of immediate part classes

  • classes that are return types of methods of class of this (= computed)

  • the classes of data members (= stored))

3. methods of classes of objects that are created in METHOD.

These two definitions (I & II) seems similar, but they are different. The
first one specifies objects, while the second specifies types. For example,

void foo()
{
Bar bar = _field.GetExternalBar();
bar.Execute(); //
}

Bar GetBar()
{
}

By the first set of rules is invalid, while for the second set of rules
is valid. And both these formulations are from the same source! If one
looks at web, he'll find more different formulations, which treat various
cases differently.

However, all of these definitions of LoD are more specific cases of the Low
Coupling Principle. May be we just select some more simple and efficient
coupling analysis system and leave LoD to researchers and theory builders? :)

Sincerely,
Ilya Ryzhenkov


14 comments

Hello Ilya,

I believe that it is not only impossible, but even undecidable in C++ in
presence of pointer arithmetic. Not sure
about C# though.

Regards,
Dmitry Shaporenkov
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"

***********
1. It is actually pretty complex. It requires tracking live instances
of
variables through the flow graph, including aliasing and other
compiler-level
techniques.
Not that it is impossible, but it should have good argumentation to
start
implementing it and tackle complexity.



0

Hello Dmitry,

DS> I believe that it is not only impossible, but even undecidable in
DS> C++ in
DS> presence of pointer arithmetic. Not sure
DS> about C# though.

Well, here I'm talking about C#, as NERPA currently works with C# only :)

Sincerely,
Ilya Ryzhenkov


0

Totally useless, better divert your energies to something more useful than
Demeter, like &anything;



"Ilya Ryzhenkov" <orangy@netix.ru> wrote in message
news:5992f43b11fc988c7fecde49ca40e@news.jetbrains.com...

Hello Dmitry,

>

DS> I believe that it is not only impossible, but even undecidable in
DS> C++ in
DS> presence of pointer arithmetic. Not sure
DS> about C# though.

>

Well, here I'm talking about C#, as NERPA currently works with C# only :)

>

Sincerely,
Ilya Ryzhenkov

>



0

Hello Amir,

What do you mean by &anything; ?

Sincerely,
Ilya Ryzhenkov


AK> Totally useless, better divert your energies to something more
AK> useful than Demeter, like &anything;
AK>
AK> "Ilya Ryzhenkov" <orangy@netix.ru> wrote in message
AK> news:5992f43b11fc988c7fecde49ca40e@news.jetbrains.com...
AK>
>> Hello Dmitry,
>>
>> DS> I believe that it is not only impossible, but even undecidable in
>> DS> C++ in
>> DS> presence of pointer arithmetic. Not sure
>> DS> about C# though.
>> Well, here I'm talking about C#, as NERPA currently works with C#
>> only :)
>>
>> Sincerely,
>> Ilya Ryzhenkov


0

Anything

"Ilya Ryzhenkov" <orangy@netix.ru> wrote in message
news:5992f43b11fca38c7fefca634114d@news.jetbrains.com...

Hello Amir,

>

What do you mean by &anything; ?

>

Sincerely,
Ilya Ryzhenkov

>
>

AK> Totally useless, better divert your energies to something more
AK> useful than Demeter, like &anything;
AK> AK> "Ilya Ryzhenkov" <orangy@netix.ru> wrote in message
AK> news:5992f43b11fc988c7fecde49ca40e@news.jetbrains.com...
AK>

>>> Hello Dmitry,
>>>
>>> DS> I believe that it is not only impossible, but even undecidable in
>>> DS> C++ in
>>> DS> presence of pointer arithmetic. Not sure
>>> DS> about C# though.
>>> Well, here I'm talking about C#, as NERPA currently works with C#
>>> only :)
>>>
>>> Sincerely,
>>> Ilya Ryzhenkov
>



0

Okay, I will add "Anything analysis" to Nerpa. It will suggest to "Change anything to conform to anything" for any declaration.

0

More specifically, "Anything analysis" should provide uniform distribution between member declaration to pay attention for. So it should get some static property of member, like file name and type name and member name, and calculate some integer value, like hash code for the concatenated string. This integer value should then be used with something like day of the year, to calculate true/false value, which means suggest the member for analysis or not. We can also mix with current user name, so different developers get different member for random analysis and hopefully code will randomly improve over time.

Does this make sense?

0

Wow, did you completely misunderstand his response.


"Ilya Ryzhenkov" <no_mail@jetbrains.com> wrote in message
news:12318504.1139944272362.JavaMail.javamailuser@localhost...

Okay, I will add "Anything analysis" to Nerpa. It will suggest to "Change
anything to conform to anything" for any declaration.



0

"Ilya Ryzhenkov" <no_mail@jetbrains.com> wrote in message
news:5699763.1139945212260.JavaMail.javamailuser@localhost...

More specifically, "Anything analysis" should provide uniform distribution
between member declaration to pay attention for. So it should get some
static property of member, like file name and type name and member name,
and calculate some integer value, like hash code for the concatenated
string. This integer value should then be used with something like day of
the year, to calculate true/false value, which means suggest the member
for analysis or not. We can also mix with current user name, so different
developers get different member for random analysis and hopefully code
will randomly improve over time.

>

Does this make sense?


Ilya, please, don't smoke that hemp again... :)

--
Eugene Pasynkov
Developer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"


0

Yes I did. Could you please explain?

What I know, is that "&anything;" is either valid XML entity, if declared in DOCTYPE, or valid C++ construct, which is address of anything.

;)

0

ROTFLMAO!!!

"Ilya Ryzhenkov" <no_mail@jetbrains.com> wrote in message
news:32685332.1139953818113.JavaMail.javamailuser@localhost...

Yes I did. Could you please explain?

>

What I know, is that "&anything;" is either valid XML entity, if declared
in DOCTYPE, or valid C++ construct, which is address of anything.

>

;)



0

Personally, I think it was as good as anything else...

"Ilya Ryzhenkov" <no_mail@jetbrains.com> wrote in message
news:18891612.1139953529614.JavaMail.javamailuser@localhost...

Hehe, did you like that? :)



0

In the XML spirit, you can replace the &anything; entity with, well,
anything. For example, 'taking the dog out for a walk', 'counting the
different types of whitespace in the code and extrapolating the weather',
and so forth.

"Ilya Ryzhenkov" <no_mail@jetbrains.com> wrote in message
news:32685332.1139953818113.JavaMail.javamailuser@localhost...

Yes I did. Could you please explain?

>

What I know, is that "&anything;" is either valid XML entity, if declared
in DOCTYPE, or valid C++ construct, which is address of anything.

>

;)



0

Please sign in to leave a comment.