On the "naturalness" of buggy code

Baishakhi Ray, Vincent Hellendoorn, Saheel Godhane, Zhaopeng Tu, Alberto Bacchelli, Premkumar Devanbu
2016 Proceedings of the 38th International Conference on Software Engineering - ICSE '16  
Real software, the kind working programmers produce by the kLOC to solve real-world problems, tends to be "natural", like speech or natural language; it tends to be highly repetitive and predictable. Researchers have captured this naturalness of software through statistical models and used them to good effect in suggestion engines, porting tools, coding standards checkers, and idiom miners. This suggests that code that appears improbable, or surprising, to a good statistical language model is
more » ... nnatural" in some sense, and thus possibly suspicious. In this paper, we investigate this hypothesis. We consider a large corpus of bug fix commits (ca. 7,139), from 10 different Java projects, and focus on its language statistics, evaluating the naturalness of buggy code and the corresponding fixes. We find that code with bugs tends to be more entropic (i.e. unnatural), becoming less so as bugs are fixed. Ordering files for inspection by their average entropy yields cost-effectiveness scores comparable to popular defect prediction methods. At a finer granularity, focusing on highly entropic lines is similar in cost-effectiveness to some well-known static bug finders (PMD, FindBugs) and ordering warnings from these bug finders using an entropy measure improves the cost-effectiveness of inspecting code implicated in warnings. This suggests that entropy may be a valid, simple way to complement the effectiveness of PMD or FindBugs, and that search-based bug-fixing methods may benefit from using entropy both for fault-localization and searching for fixes. 2. This unnaturalness drops significantly when buggy code is replaced by fix code. 3. Furthermore, we find that above effects are substantially stronger when: • the buggy code fragment is shorter (fewer lines), and • the bug is "short-lived", viz. more quickly fixed. 4. Using cost-sensitive measures, inspecting "unnatural" code indicated by language models works quite well: Performance is comparable to that of static bug finders FindBugs and PMD.
doi:10.1145/2884781.2884848 dblp:conf/icse/RayHGTBD16 fatcat:dov4rms53jaznhcto3yunlpe2i