I'm reading a book about testable JS and there is a chapter about Cyclomatic complexity, but it doesn't really tell how to calculate it. It just says that
Cyclomatic complexity is a measure of the number of independent paths through your code.
And it gives this example stating that it has cyclomatic complexity of 2:
function sum(a, b) {
if (typeof(a) !== typeof(b)) {
throw new Error("Cannot sum different types!");
} else {
return a + b;
}
}
Hence I'm wondering whether this made up example has a cyclomatic complexity of 3:
function madeup(a) {
if (typeof(a) === "string") {
if (a === "some") {
console.log("is a some");
} else {
console.log("not a some");
}
} else {
console.log("not a string");
}
}
And this of 4:
function madeup(a) {
if (typeof(a) === "string") {
if (a === "some") {
console.log("is a some");
} else {
console.log("not a some");
}
} else {
if (a === 5) {
console.log("is a 5");
} else {
console.log("not a 5");
}
}
}
?