Inline ternary operator handling in calculation of NPath complexity

24 views
Skip to first unread message

Andrey Godyaev

unread,
Feb 10, 2016, 5:06:15 PM2/10/16
to checkstyle
I have encountered difficulty in calculating of NPath complexity for methods containing inline ternary operators in complex expressions.
Here is an example of calculations and i need comfirmation i've done them correctly:
https://gist.github.com/attatrol/62f017acae2cff99fe1c



Roman Ivanov

unread,
Feb 12, 2016, 8:10:00 AM2/12/16
to Andrey Godyaev, checkstyle
Hi Andrey,

>     //NP(ternary) = 2
    int i = true ? 1 : 2;

Correct.
from Npath article:
Syntax: (exprl) ? (expr2):(expr3)
NP(?)=NP((expr1))+NP((expr2)) +NP((expr3))+2.

NP(ternary) = 0 + 0 + 0 + 2 = 0
NP((expr1)) = 0 - because there are no condition operators. In article there is example that prove this.

>     //NP(cycle) = 2
    for(; i<10; i++) {
    }

Correct.
from Npath article:
Syntax: for ((exprl); (expr2); (expr3)) (for-range)
NP(for)= NP((for-range)) + NP((expr1)) + NP((expr2)) + NP((expr3)) + 1

NP(for) = 1 + 0 + 0 + 1

>     //NP(cycle) = NP(exprs) + NP(range) + 1 = 2 + 1 + 1 = 4
    for(int i = true ? 1 : 2; i < 10; i++) {
    }

Correct.
NP(for)= NP((for-range)) + NP((expr1)) + NP((expr2)) + NP((expr3)) + 1
NP(for) = 1 + 2 + 0 + 0 + 1 = 4


>    //NP(cycle) = NP(expr) + NP(range) + 1 = 2 + 1 + 1 = 4
    for(int i = 1; i < true ? 10 : 15; i++) {
           //NP(range) = 1
    }

Correct.
NP(for)= NP((for-range)) + NP((expr1)) + NP((expr2)) + NP((expr3)) + 1
NP(for) = 1 + 0 + 2 + 0 + 1 = 4

>     //NP(cycle) = NP(expr) + NP(range) + 1 = 0 + 2 + 1 = 3
    for(int i = 1; i < j; i++) {
        //NP(range) = NP(ternary) = 2
        int j = true ? 10 : 15;
    }

Correct.
NP(for)= NP((for-range)) + NP((expr1)) + NP((expr2)) + NP((expr3)) + 1
NP(for) = 2 + 0 + 0 + 0 + 1 = 3



thanks,
Roman Ivanov
Reply all
Reply to author
Forward
0 new messages