Created
December 19, 2012 00:49
-
-
Save rofr/4333472 to your computer and use it in GitHub Desktop.
Having a hard time writing clean code with the modern LINQ, functional and fluent styles. How would you improve this code?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class FrontierEvaluator : Evaluator | |
{ | |
public override float Evaluate(Disc playerToMove, Board board) | |
{ | |
int frontierDiscComparison = Square.All | |
.Where(square => board[(Square) square] != Disc.None) | |
.Sum(square => square.Neighbors | |
.Count(neighbor => neighbor != null && board[neighbor] == Disc.None) * | |
(board[square] == playerToMove ? -1 : 1)); | |
return frontierDiscComparison; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'd certainly encapsulate the board[x] != Disc.None into a method so that I would know if it's checking for the entry being on the edge, or non-board entry, or similar. Potentaially board.IsEmpty(square)
also probably would move the board number of neighbors (Count(neighbor => neighbor != null && board[neighbor] == Disc.None) to its own method.
These are just my initial gut reactions - essentially just as I would move logic from if statements into methods, I would move these LINQ parts into methods too. Definitely if it is used in more than one place, maybe if used only here (in the latter case, I might just move it to instance methods on FrontierEvaluator)