Spectrum-Based Fault Localization (SBFL) is a popular technique to assist developers
in pinpointing faulty elements within their code based on test outcomes and code coverage.
In this paper, we examine the impact of context switching, i.e., when developers must
frequently shift their attention between different code parts (such as methods and
classes) while going down the SBFL ranked list to find the faulty statement. The basis
of our study is the observation that it requires less effort to investigate statements
that are next to each other rather than those in different methods and classes. In
particular, we analyse the number of visited methods and classes, as well as the frequency
of switches between them during the fault localization process. We found that, in
programs from the Defects4J benchmark, developers need to explore 40 methods and 12
classes on average, before finding the faulty statement, leading to 53 method- and
40 class switches, respectively.
We introduce a novel context-aware metric that better approximates the total cost
of finding a bug than traditional metrics that solely count the number of statements.
Our metric considers both the statement number and the added cost of context switches.
We also propose a new strategy for reducing the cost by optimizing the traversal of
the elements in the ranked list based on the new context-aware metric. The algorithm
not only lowers the number of statements that need to be investigated by 12% but also
significantly reduces the number of class and method switches by 52%.