

Ask HN: Is it possible to semantically analyze code for design patterns? - ashastry

I have been recently introduced to the importance of writing clean, maintainable code.<p>Even though I have studied Design Patterns and have read books on how to go about it, I find it difficult to recognize patterns in code.<p>I have been told that this skill improves over time of reading and writing more code. The question is can code be semantically analyzed on the fly to<p>1) Detect duplicate code<p>2) Detect design patterns<p>3) or taking it one step further: Suggest how to go about refactoring<p>Are there any tools which already do this?
======
dazbradbury
You may want to look at some static code analysis tools for 1 and 2:

[http://en.wikipedia.org/wiki/List_of_tools_for_static_code_a...](http://en.wikipedia.org/wiki/List_of_tools_for_static_code_analysis)

From my very limited understanding, the powerful ones are seemingly only in
reach for large enterprises [1].

With regard to number 3, In C# and Java, resharper/IntelliJ has partially
automated refactoring tools. I'm sure similar tools exist for other languages.

However, I think going down this route is substantially more work unless you
are inheriting a code base. I would aim to learn some of the design patterns
basics[2], perhaps read "The Algorithm Design Manual" [3], and aim to have a
clear idea of how to design your latest algorithm from the offset.

[1] - <http://semmle.com/solutions/what-we-do/index.html>

[2] - <http://en.wikipedia.org/wiki/Design_Patterns>

[3] - [http://www.amazon.co.uk/Algorithm-Design-Manual-Steven-
Skien...](http://www.amazon.co.uk/Algorithm-Design-Manual-Steven-
Skiena/dp/1848000693)

------
swah
Check this: <https://github.com/facebook/pfff>

I don't think you can easily detect design patterns... you'll end up thinking
about a "high level language where you can design just by dragging and
dropping some blocks" which kinda doesn't work IRL.

