Horror code

Discussion of chess software programming and technical issues.

Moderators: hgm, Dann Corbit, Harvey Williamson

Sven
Posts: 4052
Joined: Thu May 15, 2008 9:57 pm
Location: Berlin, Germany
Full name: Sven Schüle

Re: Horror code

Post by Sven »

Henk wrote:
Henk wrote:Smallest change first. Looks like bug has gone. But my engine has more of these non thread safe static variables. Ok position factory should be singleton too.

Code: Select all

 
public static Field GetField(IBoardSquares chessBoard, ulong bitCoord)
{
            int i = BitBoardIndex.Instance.Index(bitCoord);
            var field = chessBoard[i];
            return field;
}
Better:

Code: Select all


    public class ChessBoard: IBoardSquares
    {
..
        public Field GetField( ulong bitCoord)
        {
            int i = BitBoardIndex.Instance.Index(bitCoord);
            var field = this[i];

            Debug.Assert(field.RowNr >= FIRSTROW);
            Debug.Assert(field.ColNr >= FIRSTCOLUMN);
            Debug.Assert&#40;field.RowNr <= LASTROW&#41;;
            Debug.Assert&#40;field.ColNr <= LASTCOLUMN&#41;;

            return field;
        &#125;
..
Very good. Looks much better now!

Making the "position factory" a singleton would give it a state, which you probably don't want to do. I think it is sufficient for a factory to have static methods.
mar
Posts: 2552
Joined: Fri Nov 26, 2010 2:00 pm
Location: Czech Republic
Full name: Martin Sedlak

Re: Horror code

Post by mar »

Forget about "design patterns", use common sense :)

you really don't need 20 "factories", 50 "managers" and 200 "singletons" to accomplish something that's actually very simple

"design patterns" are nothing but weird names (seemingly cool) for common (or less common) ideas
invented by people who want to sell you their books
Henk
Posts: 7210
Joined: Mon May 27, 2013 10:31 am

Re: Horror code

Post by Henk »

Pool of moveList should be thread safe too. If Skipper would only use new I can expect an out of memory exception.
Sven
Posts: 4052
Joined: Thu May 15, 2008 9:57 pm
Location: Berlin, Germany
Full name: Sven Schüle

Re: Horror code

Post by Sven »

mar wrote:Forget about "design patterns", use common sense :)

you really don't need 20 "factories", 50 "managers" and 200 "singletons" to accomplish something that's actually very simple

"design patterns" are nothing but weird names (seemingly cool) for common (or less common) ideas
invented by people who want to sell you their books
We are talking about one factory and one singleton, actually. I have no doubt that it can be discussed whether they are useful. But there should also be no doubt that once you use them, you should do it correctly. Also nobody forces you to use design patterns but that does't mean yet that using them is bad in general.