Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there any static analysis tools that will report how closely the SOLID principles are followed?

I know blindly following any "best practice" can still lead to a stinking pile of crap that strictly adheres to the best practice. The SOLID principles are just that, principles. They don't apply to every situation but they are still very good heuristics for finding possible improvements in your code.

The downside to them is that they sometimes require a deep analysis of your source code to apply them. I, like most programmers, am constantly on the lookout for more efficient ways of doing things. So, I am curious if anyone has heard of an analysis tool that attempts to test for the application of SOLID principles (or lack thereof).

SRP The Single Responsibility Principle

A class should have only one reason to change.

OCP The Open-Closed Principle

Software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification.

LSP The Liskov Substitution Principle

Subtypes must be substitutable for their base types.

ISP The Interface Segregation Principle

Clients should not be forced to depend upon methods that they do not use. Interfaces belong to clients, not to hierarchies.

DIP The Dependency Inversion Principle

Abstractions should not depend upon details. Details should depend upon abstractions.

-From Agile Principles, Patterns and Practices by Robert C. Martin.

like image 299
Kenneth Cochran Avatar asked Oct 26 '09 20:10

Kenneth Cochran


People also ask

Which activity is performed using static analysis tools?

Static code analysis (or static analysis) is a software testing activity in software development, in which the source code is analyzed for constructs known to be associated with software errors or security vulnerabilities.

Why do we need static analysis tools?

By using static code analysis, you can help developers find errors before they compile or run the code and alert them about any other issues, such as a lack of inline documentation, bad coding standards, security issues, performance issues, and so on.


1 Answers

I don't think that automatic static analysis can determine if the principles are respected. To write such a tool you'd need to define formally what each concept means and have a way to check it against any code. How would you formalize the notion of a responsibility? I have personally no idea.

That said, you can have tools to help you detect the likelihood of violation. For example you could use code metrics such as number of methods per class, number of members per class to determine if a class is too big and therefore likely to violate SRP.

An exception might be the Liskov Substitution Principle. IF you define the contracts on all the methods (preconditions, postconditions, invariants) then you can check that a method redefining a method of a superclass don't strengthen the precondition, don't weaken the postcondition and respect the invariants of the superclass's method. I think that the tool ESC/Java performs those checks. Reading the wikipedia page about LSP more checks would have to be performed.

like image 172
Monkeyget Avatar answered Oct 16 '22 17:10

Monkeyget