goto thread (split)

Discussion of chess software programming and technical issues.

Moderator: Ras

lucasart
Posts: 3243
Joined: Mon May 31, 2010 1:29 pm
Full name: lucasart

Re: Sample code

Post by lucasart »

sje wrote:Here is some sample code; Symbolic's move maker routine. Notice how null moves are handled without fuss.
Yes, I have the same strcuture:

Code: Select all

// 1. code before
if (not null move) {
    // 2. code to play a real move
}
// 3. code after
The only difference is the relative size of (2) compared to (1) and (3). In my case, 2 was the biggest chunk and (1) and (3) were smaller in comparison, so the goto to reduce the indentation of (2) made more sense.

But notice how your code in (2) uses 5 indentation levels! Some people consider this a criminal offense... I don't, although I try as much as reasonably practical to obey to the rule of thumb from Linus Torvalds: no more than 3. Often trying to obey to that rule has meant rewriting the code in a clearer way and breaking it down in small helper functions (and we know that it's better to have lots of small functions rather than few big ones). Again, it's a rule of thumb, nothing should be set in stone (I violate it quite often too).
Theory and practice sometimes clash. And when that happens, theory loses. Every single time.
mcostalba
Posts: 2684
Joined: Sat Jun 14, 2008 9:17 pm

Re: Sample code

Post by mcostalba »

lucasart wrote:and we know that it's better to have lots of small functions rather than few big ones
During the years I found myself prefering the search to be in a single big chunk.

When I started I reformat the Glaurung search code in small functions (update_killers, do_null, rzor, etc) the search was a skeleton from where all the small functions where called.

Gaining experience with the engine I found this scheme poor for me because it hides the real code and the real logic, I needed to look up a function to check what actually code was doing.

So, very naturally, and even without noticing it, I started to revert all the re-factoring, one by one, till returning to a big single chunk of search.This is the solution that is more productive for me.

But this is not a rule that applies everywhere, for instance in evaluation I still prefer to have a central skeleton that dispatches to single per-term evaluation functions.
User avatar
sje
Posts: 4675
Joined: Mon Mar 13, 2006 7:43 pm

Re: Sample code

Post by sje »

lucasart wrote:But notice how your code in (2) uses 5 indentation levels! Some people consider this a criminal offense... I don't, although I try as much as reasonably practical to obey to the rule of thumb from Linus Torvalds: no more than 3.
Torvalds has his head wedged on this one. Modern source editors (like XCode's) do auto-indent and can collapse/expand blocks with a single mouse click. There is no need to artificially limit indentation.

I have a copy of the source for the original 1970s Zurich Pascal compiler, written in Pascal. The indentation goes very deep, but it makes the code easy to read. The source layout is nearly perfect. And this was all done by hand and with punch cards. Rare is the coder today who has the skill and patience to accomplish such a feat, even with modern tools.