如何删除这些 if 语句之一并缩短代码?
我有以下代码。唯一的问题是,我们通过 checkstyle 程序运行它,并出现错误“循环复杂度为 11(允许的最大值为 10)”。我想知道如何删除其中一个 if 语句以使其执行相同的操作并让程序通过测试。
/**
* Check if there is a winner on the board
* @return the winner if BLANK there is no winner
**/
public char checkWinner(){
this.winner = BLANK;
int totalTiles = GRIDSIZE*GRIDSIZE;
//Check if the game has a win
for (int i=0; i < GRIDSIZE; i++) {
if((grid[i][0] == grid[i][1]) && (grid[i][1] == grid[i][2])){
winner = grid[i][0];
return winner;
}
if((grid[0][i] == grid[1][i]) && (grid[1][i] == grid[2][i])){
winner = grid[0][i];
return winner;
}
}
if((grid[0][0] == grid[1][1]) && (grid[1][1] == grid[2][2])){
winner = grid[0][0];
return winner;
}
if((grid[0][2] == grid[1][1]) && (grid[1][1] == grid[2][0])){
winner = grid[0][2];
return winner;
}
//Check if the game is a tie
if (movesMade == totalTiles){
winner = TIE;
}
return winner;
}
I have the following code. The only problem is that we run it through a checkstyle program and it comes up with the error Cyclomatic Complexity is 11 (max allowed is 10). I would like to know how can remove one of the if statement to make it do the same thing and let the program pass the test.
/**
* Check if there is a winner on the board
* @return the winner if BLANK there is no winner
**/
public char checkWinner(){
this.winner = BLANK;
int totalTiles = GRIDSIZE*GRIDSIZE;
//Check if the game has a win
for (int i=0; i < GRIDSIZE; i++) {
if((grid[i][0] == grid[i][1]) && (grid[i][1] == grid[i][2])){
winner = grid[i][0];
return winner;
}
if((grid[0][i] == grid[1][i]) && (grid[1][i] == grid[2][i])){
winner = grid[0][i];
return winner;
}
}
if((grid[0][0] == grid[1][1]) && (grid[1][1] == grid[2][2])){
winner = grid[0][0];
return winner;
}
if((grid[0][2] == grid[1][1]) && (grid[1][1] == grid[2][0])){
winner = grid[0][2];
return winner;
}
//Check if the game is a tie
if (movesMade == totalTiles){
winner = TIE;
}
return winner;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我不知道检查器是如何工作的,但是这个怎么样:
如果这确实有效,那么讽刺的是,这似乎比你的代码可读性差一点。
I don't know how the checker works but how about this:
If this does work, the irony of course is that this seems a little less readable than your code.
您可以提取用于检查行和列的方法并重写代码,如下所示:
更易于阅读且复杂度更低。
旁注:显然,获胜者的东西可以使用重新设计,但这不是问题的一部分,如果读者(和评论者)愿意的话,可以作为练习。
You could extract methods for checking rows and column and rewrite your code something like this:
Easier to read and less complexity.
Side note: Obviously, the
winner
stuff could use a redesign, but that was not part of the question and is left as an exercise for the reader (and commenters) if they feel like it.解决方案已经在那里(结合 if 语句),但如果方法的代码适合单个页面,我不会让循环复杂度决定我的编码。在大型项目中,您想要达到的目标是可读性和易于理解性。请记住,代码可能只会编写一次,但会被读取多次。
The solution is already up there (combining the if statements), but I would not let Cyclomatic Complexity dictate my coding if the code of a method fits on a single page. The measure you want to aim for in a big project is readability and ease of understanding. Remember that code will be written potentially only once, but read quite a few times.
第一步可以是从等式表达式中删除一些冗余。 allEqual 使意图更加清晰。
将获胜者分配到一个领域是很奇怪的。我在重构中删除了它。如果您确实需要分配,您可以在调用 checkWinner 的单独方法中完成。返回和分配的问题在于调用者意外会产生这种副作用。
未解决的问题:
使用 GRIDSIZE 常量,您不必显式地寻址所有单元格:
The first step can be to remove some redundancy from the equal expression. The allEqual makes the intent a bit clearer.
Assinging the winner to a field is strange. I've removed that in the refactoring. If you really need the assignment you could do it in a separate method calling checkWinner. The problem with returning and assigning is that it's unexpected for a caller to have this side effect.
Open Problems:
Using the GRIDSIZE constant you do not have to address all cells explicitly:
循环复杂度是对代码中路径数量的度量。您的函数几乎完全由
if
语句组成。您可以使用
or
组合两个或多个if
语句:应替换为:
Cyclometric complexity is a measure of the number of paths through your code. Your function is composed almost exclusively of
if
statements.You can combine two or more
if
statements withor
:Should be replaced by: