I'm trying to write a procedure similar to the one below that would not use any of these expressions:
if | while | for | case | default | continue | goto | && | || | catch
| ternary operator ?: | ??
The reason is that when a plugin like metrics (eclipse) calculates the cyclomatic complexity, each of those expression increases the cyclomatic complexity by one based on the number of paths.
String getWeekday (int order) {
switch (order) {
case 0: return "Monday";
case 1: return "Tuesday";
case 2: return "Wednesday";
case 3: return "Thursday";
case 4: return "Friday";
case 5: return "Saturday";
case 6: return "Sunday"
default: throw new IllegalArgumentException();
}
}
For this simple procedure the cyclomatic complexity would actually be quite high because of the amount of case statements, even though for the human brain it is not complex.
For practice, I'm curious if it is possible to create a procedure with as many decisions using mostly these or other expressions (while not excessively using the ones above -- one or two would be fine)?
else | do | switch | try | using | throw | finally | return
| object creation | method call | field access
This would "fool" the plugin into thinking the CC is lower than it should be because there more paths but it ignores these expressions.