Which programming language is more useful?

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

User avatar
Bo Persson
Posts: 243
Joined: Sat Mar 11, 2006 8:31 am
Location: Malmö, Sweden
Full name: Bo Persson

Re: Which programming language is more useful?

Post by Bo Persson »

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%!
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!

If you want a new book about programming, aimed at beginners, try this:

http://www.research.att.com/~bs/programming.html
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.
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. :-)
mcostalba
Posts: 2684
Joined: Sat Jun 14, 2008 9:17 pm

Re: Which programming language is more useful?

Post by mcostalba »

Tord Romstad wrote:
mcostalba wrote:Programming languages are different, but the underlying logic is the same for all of them.
Many people think so, but I don't agree, and I think most people who make such claims have never been exposed to any languages apart from descendants of Algol (like C, Pascal, Java, C#, etc.).
Yes, you are right the only language I've learned and used apart from the procedural ones is SQL for querying databases.

It is different because is not procedural...but I would think that for someone who starts from zero it is difficult to spot the differences at a such deep level (I am not talking about syntax of course).

I have learned programming myself when I was very young, a kid, so I take for granted a lot of things. But when, during my life, I have met people starting to learn programming and perhaps not teenagers anymore, I have seen that for them is difficult to graps the concept that a program does exactly what you tell it to do and nothing more or less, the concept of analyze and splitting a problem in small and elemantary enough pieces to be coded, the concept of keeping the stuff in a good order, the concept of choosing proper names for functions and variables and of course, but is not their fault, they completly missed the concept of elegance of code, they hardly could see the difference between good and spaghetti code.

I was referring to this when I was writing that all the languages share the same basic concepts.
Gian-Carlo Pascutto
Posts: 1243
Joined: Sat Dec 13, 2008 7:00 pm

Re: Which programming language is more useful?

Post by Gian-Carlo Pascutto »

Zach Wegner wrote: 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.
In some cases this is not an advantage. I mean, there are parts in my program where I like to know exactly the assembly that is going to be output. But there are also parts where I *really* don't want to know.

If I am doing manipulation of a variable sized list, I'll take std::vector over having to manually manage it any day. I'm also not interested in doing the 100th implementation of a red-black tree. I'll just take std::map.
When you see C++, you start have to dealing with function tables, templates, etc. It gets messy.
All those features are optional.

I am sure that I can code more, faster and with less bugs in C++, despite having learned C++ after I already had years of experience with C. This is particularly true if something like Boost is added to the equation.

C++ is far from perfect. I don't like the fact that you have to worry about compiler standards compliance, that you sometimes have to worry about name mangling, that dynamic libraries are problematic and incompatible, that its hard to interface to the outside, that compile times are so bad, that editing/refactoring tools aren't up to Eclipse+Java standards...

C++ is also too hard to learn. I see people recommending starting with C++ because then you can use iostreams instead of printf. This is all fine and dandy until the beginner gets a compile error. Debugging C++ error messages (particularly involving templates) can be an art in itself. You also HAVE to be able to understand what the compiler is doing sometimes. You don't always want to do that, but you still have to be able to, or you're going to hit a wall sooner or later.
mcostalba
Posts: 2684
Joined: Sat Jun 14, 2008 9:17 pm

Re: Which programming language is more useful?

Post by mcostalba »

Gian-Carlo Pascutto wrote:You also HAVE to be able to understand what the compiler is doing sometimes. You don't always want to do that, but you still have to be able to, or you're going to hit a wall sooner or later.
This is very true, especially when speed is important for you as is the case with chess engines.

Unfortunatly understunding what compiler does is far more broad and tricky with C++ then with C. I would say this is the main point against C++ and in favor of C...perhaps there are no others, at least IMHO.
Pradu
Posts: 287
Joined: Sat Mar 11, 2006 3:19 am
Location: Atlanta, GA

Re: Which programming language is more useful?

Post by Pradu »

There are no doubt advantages to C++. The code you mention is cleaner (at least the searching/evaluating part, the "back end" of that would be pretty ugly IMO). I'm talking about the C/C++ -> assembly translation. But what you say illustrates the point. With the functors it looks like you are just calling a simple function(), but in reality it is adding a parameter to the call stack that you don't notice. With C you know exactly what calls are being made.
I think it brings about the meaning of the program more clearly because you would do the same thing in C (or assembly) anyways. If you wanted to create evals of different weights you'd do eval(weight,position) in C whereas weight is part of the state of eval (via non-static member variables) in C++ so you can just do eval(position). Usually you wouldn't care if eval took in weights or not in the search, you'd just want the "eval". Also you wouldn't have to be passing the weight pointers around all over the search manually. Here's an example of how easy it is to switch between weighted eval and regular eval in C++:

Code: Select all

#include <stdio.h>

int eval&#40;)
&#123;
    return 0;
&#125;

struct Evaluator
&#123;
    int operator&#40;)() &#123; return 3; &#125;
&#125;;

struct SearchThread
&#123;
    Evaluator eval; //comment uncomment
    SearchThread&#40;)
    &#123; search&#40;); &#125;
    // no need to pass eval thorugh here
    void search&#40;)
    &#123;
        printf&#40;"eval = %d\n", eval&#40;));
    &#125;
&#125;s;

int main&#40;)
&#123; return 0; &#125;
This version of the code doesn't rely on the compiler optimizing away the extra this pointer given to eval.

It very easy to switch between a high performance eval for release builds and an eval with states for things like automated tuning with selfplay matches. I'm not sure how of much performance hit it is to pass in an additional pointer into a function but if it is very little you can also do things like give a pointer to a pawn hash to use for a specific evaluator (it might be better to construct the pawn hash somewhere else).

In my engine, instead of the above approach I'd probably use a separate source file with #ifdefs for including into two different types of classes (regular and weighted search classes) with different names, one that uses a weighted evaluator and one that uses regular evaluator (with different constructors). In C you'd have to preprocess every eval call where as in C++ you'll only have to preprocess the constructor and some member variables of the object.
Also, the search function you mention is IMO bad design. You want all the board data declared once as a class. Including it again in the search function is bad. So maybe you could use board.movestack[] rather than board->movestack[], but thats it. And either way you are still passing around a pointer and doing an indirection. C just makes sure you know it (and also allows you to not pass it to certain functions where you don't need it).
The board can indeed be in it's own struct/class. I consider the move stack as not part of a board but rather the state of search.
playwaycool

Re: Which programming language is more useful?

Post by playwaycool »

It's very interesting to get the opinions from all of you! all of you have valid points from which I can understand. The fact all of you have experience programming in one way or the other. I can fully understand the suggestion about learning C programming language first makes sense due to the similarity with it's C++ and since C is a little easier it would make a great deal of sense to learn the slightly easier one to prepare myself for higher challenge programming language when time comes for C++ and main while to get experience learning two programming languages instead of one!



Also some folks suggested to learn a much easier programming language such as Pascal perhaps even Visual Basic makes sense to get myself into the theory/basics on how programming really works the understanding on how programming works is indeed the trick and not so much for syntax.



Lastly but not least, some suggested to skip chess engine project for now and suggested to learn much easier programming language with an easier project which is perfect suggestion of course, someone can't jump to make complex programming unless he is familiar with how programming language really works!



That all being said, I know myself. I have always been challenged for things to be learn with, for instance I learned Chess all on my own without any assistance from anyone and so far i'm considered an Expert chess player with self taught. I can see the need to start easier for programming language to avoid hair pulling! but I can see myself learning C++ first! I may be one of the exception people find C++ is easy to learn but not so much for C or even slightly easier language. I always have a habit to learn complex materials and find it quite easier to comprehend and may struggle in easier material so I can see why C++ will do me benefits for future reference and I would like to be able to improve the programs I perhaps plan to program with C++ which speed for me will make a difference so I can see C programming language or even easier language is only a way to comprehend how to programming really works to prepare for C++ but what are the chances of using those languages again once i get my hands on C++ probably slim which I can see why folks suggest to learn with what I intend to program with!
Dann Corbit
Posts: 12542
Joined: Wed Mar 08, 2006 8:57 pm
Location: Redmond, WA USA

Re: Which programming language is more useful?

Post by Dann Corbit »

Uri Blass wrote:
MattieShoes wrote:For chess engines specifically, C or C++ as noted. As for programming in general, I'd pick a higher level language (ie. java, C#) to start with.

I suppose it depends on what you want. Higher level languages tend to hide some of the ugliness that goes on under the hood but they tend to be slower. I think this makes learning basic concepts about things like classes, functions, loops, data structures, and so forth much easier (because you're not worrying about things like pointer arithmetic, pointers to pointers, how multiple inheritance works, what #typedef means, annoying treatment of strings, etc.

One could write a very clean, functional engine in high level languages that will beat the pants off most people. It'll never be as fast as ones written in C or C++, but lets be honest, your first attempt at an engine wasn't going to be anyway. :-)
What about writing a chess interface?
What is the simplest chess interface with free source code to learn from it
and which computer language is best to use for it.

What are the computer languages that are practically used for
chess sites like
http://chess.emrald.net/ or
http://chesstempo.com/
For writing a web interface, the languages are probably html, perl and maybe javascript.

For writing another sort of GUI, we have:

SCID == C++ and TCL/TK

Winboard = C (yacc was used somewhere in the process but he never published the grammar -- my opionion)

Arena == Dephi or FreePascal (compressed with pepack.exe)

Jose == Java

If I were going to write one {a chess GUI}, and I was in a hurry, I would use C#.

If I were going to write one, and I had lots of time, I would use C++ and wxWidgets.
Dann Corbit
Posts: 12542
Joined: Wed Mar 08, 2006 8:57 pm
Location: Redmond, WA USA

Re: Which programming language is more useful?

Post by Dann Corbit »

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".

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.
Daniel Shawul
Posts: 4185
Joined: Tue Mar 14, 2006 11:34 am
Location: Ethiopia

Re: Which programming language is more useful?

Post by Daniel Shawul »

For what i usually do, the most efficient high level language is considered c++ and lowest FORTRAN (sometimes C). For academical purposes , i sometimes use high level scripts of some mathematical packages that i would not consider programming at all. For beginners I guess using a language where other chess programs are abundantly available is also a factor.
I rarely find a library I need written in C either because it is commercial or is not written yet. And most of this new libraries are usually written in C++ (not C). The older ones used to have separate subroutines for Single and double precision numbers while the new ones use templates, which is much more comfortable to the user.

YMMV
DAniel
bob
Posts: 20943
Joined: Mon Feb 27, 2006 7:30 pm
Location: Birmingham, AL

Re: Which programming language is more useful?

Post by bob »

playwaycool wrote:I have 0 experience with programming but I have a bit of somewhat of a talent with learning skills. I'm somewhat interested to program a new engine but I'm far from it at this point, I still have to learn a language which will take time and patience. However I'm interested to know which programming language is useful for long term and what is the main advantages? The two language I'm referring too are C & C++



Also is there a list of all chess engines with their language? I'm confused on some few engines whether they're programmed in C or C++ or any other. Hiarcs is one of the programs I'm not sure about to be exact.
The best programming language is the one you know the best. :)

I have written chess programs in Fortran and C. I know plenty of examples in C++, and done correctly there is no speed advantage or disadvantage in using C++. I've known several that were written in assembly language, which is faster in execution, but slower in writing/debugging. Also most every other language known to man, from PL/1 to Pascal, and one brave soul years ago that did one in Cobol of all things.

Use what is comfortable. If none are, pick one that is mainstream with hope for speed, which means no Java. Or Perl. etc...