Updated to fix conditional coercion example: “0” == “false” is false, but “0” == false is true.
"false" == false evaluates to false, but
The paper addresses the following three questions:
Key results are as follows:
- Type coercions are widely used: 80.42% of all function executions perform at least one coercion and 17.74% of all operations that may apply a coercion do apply a coercion, on average over all programs. (RQ 1)
The most common point of coercion is in conditionals:
- In contrast to coercions, explicit type conversions are significantly less prevalent. For each explicit type conversion that occurs at runtime, there are 269 coercions. (RQ 1)
- 98.85% of all coercions are harmless and likely to not introduce any misbehavior. (RQ 1)
The most prevalent potentially harmful coercion (by number of static occurrences) are non-strict (in)equality checks that compare two objects of different types, which is a common source of confusion. Several prevalent kinds of potentially harmful coercions involve undefined, such as concatenating undefined with a string, which yields a string that contains “undefined”, and relative operators applied to undefined and a number, which always yields false. We speculate that most of these coercions are caused by an undefined value that accidentally propagates through the program.
The authors looked more closely at the reputedly error-prone binary + operator:
- Out of 30 manually inspected potentially harmful code locations, 22 are, to the best of our knowledge, correct, and only one is a clear bug. These results suggest that the overall percentage of erroneous coercions is very small. (RQ 2)
- Most code locations with coercions are monomorphic (86.13%), i.e., they always convert the same type into the same other type, suggesting that these locations could be refactored into explicit type conversions for improved code understandability. (RQ 3)
- Most polymorphic code locations (93.79%), i.e., locations that convert multiple different types, are conditionals where either some defined value or the undefined value is coerced to a boolean. (RQ 3)
These results suggest that most coercions do not significantly harm code understandability, at least not because of polymorphic code locations. A more detailed study on how coercions influence human understanding of code, e.g., through a human study in the style of  remains for future work.