This function checks if the cell of a scrabble board is a double letter bonus. It has a 23 cyclomatic complexity which is higher than the threshold of 20. I don't know how to do it another way, I think that this is the only way to do it. Here is my function :
checkDoubleLetterCell(row: number, column: number): boolean
{
if((row === middle- 1 || row === middle + 1)
&& (column === middle - 1 || column === middle + 1)
|| (row === 0 || row == SCRABBLE_SIZE - 1 || row === middle)
&& (column === middle + Math.round(middle/2) || column === middle - Math.round(middle/2))
|| (column === 0 || column === SCRABBLE_SIZE - 1 || column === middle)
&& (row === middle + Math.round(middle/2) || row === middle - Math.round(middle/2))
|| (column === middle + 1 || column === middle - 1)
&& (row === middle + Math.round(middle/2) + 1 || row === middle - Math.round(middle/2) - 1)
|| (row === middle + 1 || row === middle - 1)
&& (column === middle + Math.round(middle/2) + 1 || column === middle - Math.round(middle/2) - 1))
{
return true;
}
return false;
}
The solution is to represent static knowledge in data instead of in procedural code. If the knowledge about the location of the double letter squares is represented in a statically initialized data structure, say a boolean matrix dblScore of the right dimensions, then your code reduces to:
checkDoubleLetterCell(row: number, column: number): boolean
{
return dblScore[row,column];
}
Excess complexity is often the side-effect of choosing an ill-fitting representation. A general rule: static knowledge should be represented in data, not in procedural logic.