Skip to content

Instantly share code, notes, and snippets.

@rofr
Created December 19, 2012 00:49
Show Gist options
  • Save rofr/4333472 to your computer and use it in GitHub Desktop.
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?
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;
}
}
@zsoldosp
Copy link

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment