Does anybody know of a tool that I could use with a C# application to find possible Law of Demeter violations? I know that it would give a lot of false positives, but I think it could still be useful. Especially during the early design process.
-
wiki definition of Law of Demeter: http://en.wikipedia.org/wiki/Law_of_Demeter – Fosco Aug 11 '10 at 16:10
-
1*The law of Demeter* should be *The rule of thumb of Demeter* imho . . . – Binary Worrier Aug 11 '10 at 16:16
-
I don't know of any such tools, but a good approach to one in .NET would be to base it on reflection and IL so that it could work for any .NET language. – TechNeilogy Aug 12 '10 at 02:42
1 Answers
If you're just looking for something.somethingelse.violation
, then you can use Visual Studio.
In the find dialog, check the box at the bottom to "Use" and select "Regular Expressions."
Not very robust, but you can use <[:a_]+\.([:a_]+\.)+[:a_]+
to find the pattern above.
A better tool would be grep
or similar on the solution directory, so you can use more powerful regex options like negative lookbehind, which would allow you to exclude things like using
directives and namespace
declarations.
You could probably very quickly write a .NET app that would recurse a given directory and search .cs and/or .vb files for same, using .NET Regex, which has lookarounds, but of course the advantage of using VS is that you remain right in the source code editor.

- 56,361
- 10
- 99
- 123
-
Hmm, I just found 5 'dots' in my program... according to [six degrees of separation](https://en.wikipedia.org/wiki/Six_degrees_of_separation), I think that means it can access anything in my program right? :P – Jeff B Jun 13 '14 at 21:52