Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: eval() in or out of board structure?

Author: Russell Reagan

Date: 11:55:17 02/13/04

Go up one level in this thread


On February 13, 2004 at 13:50:32, Andrew Wagner wrote:

>In general, would you guys recommend having functions like eval(), search(),
>gen_moves(), etc. as members of the board structure, or is it better to pass the
>board structure to those functions?

Here are two articles you must read.

Interview with Bjarne Stroustrop:
http://www.artima.com/intv/goldilocks.html

"How Non-Member Functions Improve Encapsulation" by Scott Meyers
http://www.cuj.com/documents/s=8042/cuj0002meyers/

The two main points to get from the Stroustrop interview are:

* Use classes to maintain invariants
* Create efficient access to what you need from the class, then write others
  things as non-member functions.

His example of this is the std::vector. There are invariants to be maintained,
such as making sure the 'size' of the vector and the amount of memory allocated
make sense (i.e. you should have enough memory allocated). Since you have
efficient access to the elements of the vector, you should write functions like
find(), sort(), and so on as non-member functions.

Scott Meyers says basically the same thing in his article. His main point is
that when you add more member functions, you reduce the power of the
encapsulation of the class. He says that if you have the choice between writing
a function as a member function or a non-member function, choose to write it as
a non-member function.

Another way to think about this is that you want to minimize the number of
places where the private data is handled. As an example, I used to work at a
library, and for a while we allowed anyone to take payments for lost items,
overdue fees, and other things. The money in the register usually didn't balance
out with what the receipts added up to. They solved the problem by only allowing
two people to take payments. They minimized the number of people who handled the
money, and it solved the problem. If there was a problem, they knew who to talk
to about it, and they could correct the problem easier. If there is a problem
with your board class, and you have minimized the number of places where the
private data is accessed or changed, you will have a much easier time finding
out where the problem is.



This page took 0 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.