Moderator: Andres Valverde
Sven Sch?le wrote:Hi Uri and all others,
one problem of putting comments into separate files might be the maintenance problem. Separate documentation has to be changed synchronously to changes in the source code; do you think this would really work? Today you only think of Fruit 2.1 but what about future versions?
Sven Sch?le wrote:
My experience over years is that commenting directly within the source code is the only acceptable approach while detailled separate documentation (I don't call it "comments" due to its different style of being an external document) is outdated most of its lifetime.
Sven Sch?le wrote:That's why I fully agree with Dann in that this can only be done by the author himself, provided he wants to do it. Commenting done by anyone else is a quite dangerous task, as Dann already stated, because it may produce comments looking like an explanation of what the author had in mind when writing a piece of code - which is impossible to tell by someone else than himself.
Sven Sch?le wrote:Btw I don't think that Fruit 2.1 source code really needs much more commenting; actually it depends on the level of understanding you want to achieve: understanding the higher-level, functional aspects of some code or understanding lower-level, internal implementation details. For the latter, there may be a need to add a few comments but I am not sure if this is important.
Sven Sch?le wrote:
I want to add a personal note on code commenting.
My experience in software development taught me that commenting is essential for "surviving". If you work on different tasks over a certain period of time, or even if different people work on the same task (at the same time or sequentially), you absolutely need excellent code commenting, otherwise you will be lost at some point.
However, most computer chess projects are single person projects, and for many authors it is their main project for a longer period, so things may be different here.
Sven
if (UseKingAttack)
for (colour = 0; colour < ColourNb; colour++)
for (colour = White; colour <= Black; colour++)
if ((mat_info->cflags[colour] & MatKingFlag) != 0)
if (bq >= 1 && bq+br+bb+bn >= 2) cflags[White] |= MatKingFlag;
if (wq >= 1 && wq+wr+wb+wn >= 2) cflags[Black] |= MatKingFlag;
Sven Sch?le wrote:
- Code: Select all
if ((mat_info->cflags[colour] & MatKingFlag) != 0)
This is an example where I admit that comments are necessary.
An inline function taking mat_info and colour as arguments and returning bool, with an appropriate name, would avoid the need for comments, though.
Tord Romstad wrote:Sven Sch?le wrote:
- Code: Select all
if ((mat_info->cflags[colour] & MatKingFlag) != 0)
This is an example where I admit that comments are necessary.
I think it is quite easy to guess what this code does, but as always, YMMV.
Tord
Uri Blass wrote:I see no way to understand that
((mat_info->cflags[colour] & MatKingFlag) != 0) if and only if the opponent has at least 2 pieces and at least a queen.
It is not obvious that king safety is irrelevant if the opponent has no queen and I think that it is wrong.
I also do not like to guess without being sure because even if I am right in 90% of the cases then the 10% of the cases that I am wrong may lead to misunderstanding of the code.
Tord Romstad wrote:Uri Blass wrote: It is not obvious that king safety is irrelevant if the opponent has no queen and I think that it is wrong.
This is very possible, and it is actually one of the reasons why I don't think it is very important to be able to see immediately exactly how much material Fruit thinks a player needs to produce a dangerous attack. As you point out, the exact condition chosen is likely to be sub-optimal anyway, and has little value to the reader.
Tord
Tord Romstad wrote:Sven Sch?le wrote:An inline function taking mat_info and colour as arguments and returning bool, with an appropriate name, would avoid the need for comments, though.
Unfortunatly inline function are not very useful in C, because the compiler is free to ignore them. GCC never inlines calls to a function defined in a different source file, for instance. For this reason, I normally use #define macros instead, as much as I hate them.
inline bool f(SomeType t)
{
return someExpr(t);
}
Sven Sch?le wrote:{snip}
I know, of course, that some compilers do not inline some functions even if you use the "inline" keyword, but often the compiler has good reasons not to do it.
Sven
Dann Corbit wrote:So you have to resort to the cheesy #include source files trick:
Dann Corbit wrote:Sven Sch?le wrote:{snip}
I know, of course, that some compilers do not inline some functions even if you use the "inline" keyword, but often the compiler has good reasons not to do it.
Sven
Not always.
Sometimes, it will refuse just because it can't see the implementation (GCC does this).
So you have to resort to the cheesy #include source files trick:
#include "prjhdr.h"
#include "foo.c" /* First source file in project */
#include "bar.c"
#include "boo.c"
...
#include "end.c" /* Last source file in project */
However, using macros for functions has some very, very ugly side effects including undefined behavior if you look sideways at it.
Sven Sch?le wrote:Putting inline implementations into .h files instead, not .cpp files, will work.
Tord Romstad wrote:Sven Sch?le wrote:Putting inline implementations into .h files instead, not .cpp files, will work.
I suspect that GCC would complain that the same function in multiple source files in that case (I haven't tried, though). Perhaps defining the functions as 'static inline' and placing them in .h files would work.
Tord
mathmoi wrote:Tord Romstad wrote:Sven Sch?le wrote:Putting inline implementations into .h files instead, not .cpp files, will work.
I suspect that GCC would complain that the same function in multiple source files in that case (I haven't tried, though). Perhaps defining the functions as 'static inline' and placing them in .h files would work.
Tord
Hi Tord,
In fact inline function must be implemented in the .h file. The compiler will not complain. Since the function is inlined it's definition must be know while compiling a call to it, this is why you must put the definition of the function in the header file. Otherwise inline is ignored
Return to Programming and Technical Discussions
Users browsing this forum: No registered users and 23 guests