Which programming language is more useful?

Discussion of chess software programming and technical issues.

Moderator: Ras

User avatar
michiguel
Posts: 6401
Joined: Thu Mar 09, 2006 8:30 pm
Location: Chicago, Illinois, USA

Re: Which programming language is more useful?

Post by michiguel »

Bo Persson wrote:
michiguel wrote:
Zach Wegner wrote:I think C would be easier for a beginner to learn. No need to learn about namespaces, inheritance, operator overloading, polymorphism, etc. C++ has some nice features, but they won't really be useful until you know a lot about programming (and it would be bad to try and use them before then). It's a classic beginner's mistake to hear about the wonders of object oriented programming and then make every thing an object. This is inevitably slow. IMO you should learn C first and then decide if you want to learn C++.

Also, in my highly subjective opinion, C is a much more elegant language, since it matches in my mind what the CPU is doing. For every piece of code I write, I more or less know what the assembly is going to look like. When you see C++, you start have to dealing with function tables, templates, etc. It gets messy.
I agree 100%.
I disagree about 98%!
Let's focus in the 2% agreement :-)
michiguel wrote: Sherif, I was in your same situation and I have a strong opinion on this ==> Buy the classic "The C programming language" by Kernighan and Ritchie and learn it. It is a short book and if you are a skillful learner, your will be programming in C in no time. Of course, you have to like programming.
That's the 1970's!
Second editions is actually very late 80's (almost by the time the 89 standard was published). Anyway, it is a classic that beat the test of time.
If you want a new book about programming, aimed at beginners, try this:

http://www.research.att.com/~bs/programming.html
Thanks, I may go to the library and see whether I can read something interesting and learn. I mean it. However, I wonder the suitability of this book for a beginner in this hobby. That type of beginner is not the same as a comp. sci. first year college student. Just peeking at the sample pages available in that website I see the typical examples of a display model, oriented to teach OOP. IMHO, that is exactly what a beginner should be avoiding. Paticularly, if the goal is to write later a chess engine.

I would agree that C++ will be better than C if there is a book that teaches it as an "improved C", with better type checking, libraries, and that's it. Is there any? I believe that most of the whistles and bells will be distracting for a hobbyst, at least at the beginning. That was my point.
michiguel wrote: For a chess engine you do not need C++. IMHO, for people like you or me, C++ is the worst decision possible. If you like to go higher level for other reasons, then learn Python. If you are going to make a living out of programming, maybe C++ is for you.

I think it is more important to buy a good book on data structures, than learn specifics of a complex language as C++. Keep the language simple, and go deep into structures and algorithms. Besides, I believe this is more fun.

When I started programming, I started with Turbo Pascal (~80's). Later, I learned the Object oriented features of new versions (early 90's). It was cool to learn, but implied an effort that was not needed. I just did it as a hobby.

Learn C first, and then you will realize that you will never need C++ :-) You may like to learn it as a hobby, though (as I may like to do it some day).
Learn C++ first, and you will realize that you will never need to learn the ugly and very dark corners of the C standard library, with functions like strcpy, malloc, memset, or others with easy to learn names like strpbrk, strxfrm, wcsncpy, and sscanf.

There are large parts of the C runtime that you hardly ever use in a C++ program. Why start out trying to learn that?

Very few C++ programmers find it easier to have functions like abs, labs, cabs, and fabs for the absolute value of different types, when std::abs works for all of them.
You don't need anything of that to write a chess engine. The only thing I use from above is malloc in a couple of places, and that is wrapped in another function.

Miguel

michiguel wrote: Of course, this is my very humble opinion from a non-professional programmer.

Miguel
Of course, this is my not so humble opinion from a professional programmer. :-)
User avatar
michiguel
Posts: 6401
Joined: Thu Mar 09, 2006 8:30 pm
Location: Chicago, Illinois, USA

Re: Which programming language is more useful?

Post by michiguel »

Dann Corbit wrote:
Pradu wrote:
sje wrote:I'll guess that most chess programming efforts started it the past decade have used C++.
Some have started C then switch to C++. 8-)
Some will claim that using anything other than assembly language (or C, the 21st century assembly language) will result in an unnecessarily slow program.
There is some truth to this and without a good understand of how C++ code get translated it becomes very easy to misuse C++ features from a performance perspective. I think for people coming from C, the most important thing to learn is how C++ would be translated equivalently in C. For example C++ classes can be translated as C structs with C++ member functions being equivalent C functions having an extra parameter (the "this" pointer). Indeed, in C++ structs and classes are identical (the difference being that struts default to public access and classes default to private access).

I believe careful use of C++ should be as fast if not faster (eg. function pointers vs inlinable Functors although you would never use function pointers in a chess program...) as C.
Use of function pointers is an underused idea that translates well to chess. The simplest example is Bruce Moreland's Gerbil. Other more sophisticated programs also use this technique. Ernst Heinz champions the idea in his book "Scalable Search in Computer Chess".

A little piece of Gaviota's evaluation.

Code: Select all

		psq = po->bi.dyn[OWN].list;
		pth = po->bi.dyn[OWN].attack;

		for (; *psq != NOSQUARE; psq++, pth++) {
			square = *psq;
			pc = PIECE_MASK & chessboard[square];
			accum_score += (*piecef[pc]) (square, *pth, &prm);  <== HERE
			accum_score += psq_table(pc, OWN, ph, square);
		}
I loop over the list of [OWN] pieces with their attacks, and evaluate the mobility and other things with the function (*piecef[pc])

I have this type of things scattered all over my code. I really like it.
For instance, in the functions that parse the ini files. Some inputs are integer, some are "on/off" etc. Each parameter has their own type of parser (proc_cstr, proc_onoff, proc_int, proc_mem, see below). I find it easy to incorporate new commands.

For some reason, the tabs are messed up here.

Code: Select all

	struct ini_command		cmdlist[] = {

	{
"name",			&Program_name,		proc_cstr,	"Error in initialization of the Program Name"	},{
"book_file",	&Book_filename,		proc_cstr,	"Error in book name at initialization"			},{
"script_file",	&Input_filename,	proc_cstr,	"Error in input filename at initialization"		},{
"learn_file",	&Learn_filename,	proc_cstr,	"Error in Learn File Name at initialization"	},{
"param_file",	&Param_filename,	proc_cstr,	"Error in Parameters File at initialization"	},{
"ponder",		&Ponder,			proc_onoff,	"Error with ponder initialization"				},{


etc.
etc.
etc.
					},{

NULL, NULL, NULL, NULL
}

};
Parsing the file is just looping through the lines of the files, loop the name of the command, which identifies the proper function, and returns a value (or error) that is saved in the pointer that the table indicates.

Miguel

For learning C++, I'd recommend with learning C first, then try out C++. "Write great code" volume 1 and 2 by Randall Hyde is a great book to help learn a little on how to translate C++.
To learn C:
Start with "The C Programming Language":
http://en.wikipedia.org/wiki/The_C_Prog ... age_(book)

For C++, get Stroustrup's book:
http://www.research.att.com/~bs/3rd.html

Nothing like getting it from the horse's mouth.
MattieShoes
Posts: 718
Joined: Fri Mar 20, 2009 8:59 pm

Re: Which programming language is more useful?

Post by MattieShoes »

I wrote a board representation in perl to parse pgn games a long time ago. I think perft() was getting around 2000 nps. :-) This was back on a 133MHz pentium if I remember right, but... ouch! Definitely not worth writing an engine in perl except for the "yeah, I did that once" geek cred.
Charles B.

Re: Which programming language is more useful?

Post by Charles B. »

playwaycool wrote:Hiarcs is one of the programs I'm not sure about to be exact.
playwaycool wrote: Yeah, I tried that but some programs are not listed and some are outdated! Hiarcs for instance is not listed among some others.

I remembered having read this interview with Mark Uniacke a few years ago. For your benefit I will post a link to the interview here.

If the information in Question 13 on the interview page is still valid, it would look like Hiarcs is programmed in the C language.


http://www.exactachess.com/modules.php?name=Interviews&page=3



Question 13.

Which is the primary program language that you use to program Hiarcs and what are the reasons why?

I use the C language because it's efficient and comes naturally to me.




Good luck to you Sherif. A short time ago I purchased the book "Programming in C" by Stephen Kochan. With me it is just a learning experience and I have no desire to write a chess engine, but I am still a beginner just like you.
edlich

Re: Which programming language is more useful?

Post by edlich »

Dar Sherif,

I really wonder why nobody seems to think about D as a language
for chess engines (http://www.digitalmars.com/d).

My opinions:

* C and C++ might be preferred by chess programmers but these
languages are not so good to program, refactor, maintain, if the codebase
gets bigger and bigger.

On the other hand D seems to have some good points:

* It is nearly as fast as C or C++ (I thought I had seen some benchmark comparisons here: http://shootout.alioth.debian.org/)

* D has implicit and explicit memory control. As you wish.

* D seems to be a modern language with Ruby or Python alike productivity (It even has closures).

* All the inconvenient things from C and C++ have been eliminated.

Unfortunately there are not yet so much books out for D, but the
TIOBE index shows D on place 12 which is quite stunning.
http://www.tiobe.com/index.php/content/ ... index.html

So what do you think. Am I missing something or is D the next language for chess programmers?

Best Regards to you all
Stefan Edlich
Aleks Peshkov
Posts: 893
Joined: Sun Nov 19, 2006 9:16 pm
Location: Russia

Re: Which programming language is more useful?

Post by Aleks Peshkov »

D is vendor specific language with single existing compiler. And this vendor is not even Microsoft or Sun.

I one more Delphi? No thanks.
edlich

Re: Which programming language is more useful?

Post by edlich »

Aleks Peshkov wrote:D is vendor specific language with single existing compiler. And this vendor is not even Microsoft or Sun.
I one more Delphi? No thanks.
What you say is wrong. There is a second compiler GDC for the GNU Compiler collection.

And a compiler has to be from Microsoft or Sun? Otherwise it's bad? I don't get your point here.

Is python bad because it's guido / google? Ruby because it's Matz? Delphi bad because it's Borland?

Still looking for more arguments why C++ is preferred over D for a chess engine.

Regards
Stefan Edlich
P.S.: I really like Sun
Tord Romstad
Posts: 1808
Joined: Wed Mar 08, 2006 9:19 pm
Location: Oslo, Norway

Re: Which programming language is more useful?

Post by Tord Romstad »

Aleks Peshkov wrote:D is vendor specific language with single existing compiler.
Unless I'm mistaken, there is not just one, but three implementations: The Digial Mars compiler, GDC, and LDC.

I am also very unexcited about the D programming language, though. Like C++, it suffers too much from trying to resemble C. It might be slightly more elegant and convenient than C++ in a few ways, but it's nowhere near enough to justify changing languages.

A much more interesting attempt at making a good, modern low-level programming language is BitC. Given a sufficiently mature optimizing compiler for all the major platforms, that's probably what I would use in my next chess program.

Tord
andretaff

Re: Which programming language is more useful?

Post by andretaff »

edlich wrote:And a compiler has to be from Microsoft or Sun? Otherwise it's bad? I don't get your point here.
I think he means Microsoft or Sun are _big_ companies and the world will end before they give up on 'their' languages. Maybe not.


Is python bad because it's guido / google? Ruby because it's Matz? Delphi bad because it's Borland?
From wikipedia: "The language has an open, community-based development model managed by the non-profit Python Software Foundation, which maintains the de facto standard definition of the language in CPython, the reference implementation."

Delphi is dead. Borland will get rid of it as soon as they can.
Still looking for more arguments why C++ is preferred over D for a chess engine.
I don't know about C++, but C is a standardized language with a great history and with _many_ compilers. With C, using only a few defines on specific items (like threads), you can run your code in any system you like.

I'm sorry, but what you said, "these languages are not so good to program, refactor, maintain, if the codebase gets bigger and bigger" is just wrong. I don't know what is happening to Windows nowadays, but the whole Linux codebase is written in C/C++, as an example. So, if they mantain it such a complex, long code, how could you say you shouldn't write a chess engine in C because you won't be able to mantain it?
edlich

Re: Which programming language is more useful?

Post by edlich »

andretaff wrote:I think he means Microsoft or Sun are _big_ companies and the world will end before they give up on 'their' languages. Maybe not.
OK I agree if it's meant like this.

andretaff wrote:Delphi is dead. Borland will get rid of it as soon as they can.
Your point.

OK even I wouldn't take delphi as the language for a chess engine.

andretaff wrote:I'm sorry, but what you said, "these languages are not so good to program, refactor, maintain, if the codebase gets bigger and bigger" is just wrong. I don't know what is happening to Windows nowadays, but the whole Linux codebase is written in C/C++, as an example. So, if they mantain it such a complex, long code, how could you say you shouldn't write a chess engine in C because you won't be able to mantain it?
No it's not wrong. Perhaps we mean something different here. Of course I know that big systems in C/C++ do exist and of course a million strong engines. So I do say that C / C++ is a good choice for a chess engine.

But what I differ in is the code effort you have when it get's bigger and bigger. And if you look at large C / C++ codebases (like kernels), the owners maintain a huge amount of extra testing, tools and persons to handle such large codebases. Simply because the language requires much much work (you have pointers, no packages, visibility, etc. etc.).

So C / C++ is a great choice but you have to decide if you are willing to invest time to maintian the code which is much easier if you have e.g. java (even if java is slower and has no unsigned long, and other chess quirks). I am not willing to invest and look forwad to see a great language that is fast, well threaded and has modern concepts.

Regards
Stefan Edlich