Answers.Body: <p>If the system claims that a piece of code has violated standards, then to be useful to the programmer, it really needs to provide more information than just a 'yes/no' classifier: you need some form of explanation about <em>why</em> it is claimed to be wrong.</p>
  
  <p>Clearly ANNs aren't much use for that.</p>
  
  <p><em>If</em> I were tackling such a problem (and my suspicion is that a lot of effort could be spent trying and failing to reproduce coding standards which are already well-understood), then my inclination would be to use a more explicitly rule-based representation.</p>
  
  <p>Possibilities include:</p>
  
  <ul>
  <li><p>Genetic Programming</p></li>
  <li><p>Learning Classifier Systems </p></li>
  <li>Decision Trees</li>
  </ul>
  
  <p>The ever-useful <a href="https://cs.gmu.edu/~sean/book/metaheuristics" rel="nofollow noreferrer">"Essentials of Metaheuristics"</a> has a whole section on the evolution of rulesets. Obviously, nothing prevents you from initializing the evolutionary process with rules known to be useful.</p>
  
  <p>As I point out <a href="https://ai.stackexchange.com/questions/1420/how-close-are-we-to-creating-ex-machina">here</a>, with our current AI algorithms, the success of an  approach is very sensitive to human expertise/effort in feature selection/preprocessing, choice of training set etc, so creative experiment with this is vital.</p>
  
  <p>Training set: how about two sets of negative and positive examples, consisting of (features extracted from) bad code and from a refactored version (respectively)?</p>
  
  <p>One elementary choice of features would be to apply a bunch of code complexity metrics and have the learning algorithm combine those. The plus side of working in such a numeric domain is that the learning algorithm might readily find a gradient to exploit. The downside is that the rules (which are then likely of the form <code>if mcabe &gt; 2.8</code> etc) are still not as informative as might be desired.</p>
  
  <p>For more complex rules (e.g. requiring <code>if elseif else</code>) you may want to extract your features from the abstract syntax tree. You could in principle use the entire tree but to my knowledge <a href="http://jmlr.csail.mit.edu/papers/volume11/vishwanathan10a/vishwanathan10a.pdf" rel="nofollow noreferrer">ML on graph and tree structures</a> is still in relative infancy.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-06-02T16:08:00.897Z
Answers.Text: I don't think that providing more info other than an answer "yes" or "no" is necessarily useless. It can be useful because it might point out which parts of the code are potentially not following the standards.
Answers.UserDisplayName: nbro
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-08-11T18:38:13.963Z
Answers.LastActivityDate: 2016-08-11T18:52:07.540Z
Answers.LastEditDate: 2017-04-13T12:53:10.013Z
Answers.UserDisplayName: NietzscheanAI
Body: <p>Are there any machine learning techniques (such as deep learning or evolutionary algorithms) to detect coding standard violations? Which one would be more suitable?</p>
  <p>I don't have any specific programming language in mind, but something similar to <a href="http://pear.php.net/package/PHP_CodeSniffer" rel="nofollow noreferrer">PHP_CodeSniffer</a> (following <a href="https://www.drupal.org/coding-standards" rel="nofollow noreferrer">these standards</a>), but instead of using hardcoded rules, the algorithm should learn good techniques, but I'm not sure based on what training data. How would you approach the training session, any suggestions?</p>
  
Tags: <neural-networks><machine-learning><reference-request><evolutionary-algorithms><computer-programming>
Title: Are there any machine learning techniques to detect coding standard violations?
UserDisplayName: kenorb
ThreadText: # Are there any machine learning techniques to detect coding standard violations?
  
  (Asked by: username_0 on 2016-08-11)
  
  Are there any machine learning techniques (such as deep learning or evolutionary algorithms) to detect coding standard violations? Which one would be more suitable?
  
  I don't have any specific programming language in mind, but something similar to [PHP\_CodeSniffer](http://pear.php.net/package/PHP_CodeSniffer) (following [these standards](https://www.drupal.org/coding-standards)), but instead of using hardcoded rules, the algorithm should learn good techniques, but I'm not sure based on what training data. How would you approach the training session, any suggestions?
  
  **Tags:** neural-networks, machine-learning, reference-request, evolutionary-algorithms, computer-programming
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2016-08-11. Score: 4 | Accepted answer
  
  If the system claims that a piece of code has violated standards, then to be useful to the programmer, it really needs to provide more information than just a 'yes/no' classifier: you need some form of explanation about *why* it is claimed to be wrong.
  
  Clearly ANNs aren't much use for that.
  
  *If* I were tackling such a problem (and my suspicion is that a lot of effort could be spent trying and failing to reproduce coding standards which are already well-understood), then my inclination would be to use a more explicitly rule-based representation.
  
  Possibilities include:
  
  * Genetic Programming
  * Learning Classifier Systems
  * Decision Trees
  
  The ever-useful ["Essentials of Metaheuristics"](https://cs.gmu.edu/~sean/book/metaheuristics) has a whole section on the evolution of rulesets. Obviously, nothing prevents you from initializing the evolutionary process with rules known to be useful.
  
  As I point out [here](https://ai.stackexchange.com/questions/1420/how-close-are-we-to-creating-ex-machina), with our current AI algorithms, the success of an approach is very sensitive to human expertise/effort in feature selection/preprocessing, choice of training set etc, so creative experiment with this is vital.
  
  Training set: how about two sets of negative and positive examples, consisting of (features extracted from) bad code and from a refactored version (respectively)?
  
  One elementary choice of features would be to apply a bunch of code complexity metrics and have the learning algorithm combine those. The plus side of working in such a numeric domain is that the learning algorithm might readily find a gradient to exploit. The downside is that the rules (which are then likely of the form `if mcabe > 2.8` etc) are still not as informative as might be desired.
  
  For more complex rules (e.g. requiring `if elseif else`) you may want to extract your features from the abstract syntax tree. You could in principle use the entire tree but to my knowledge [ML on graph and tree structures](http://jmlr.csail.mit.edu/papers/volume11/vishwanathan10a/vishwanathan10a.pdf) is still in relative infancy.
  
  > **Comments:**
  > **username_2**: I don't think that providing more info other than an answer "yes" or "no" is necessarily useless. It can be useful because it might point out which parts of the code are potentially not following the standards.  
  
  ---
  
  