How beautiful is your code?

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

User avatar
JuLieN
Posts: 2949
Joined: Mon May 05, 2008 12:16 pm
Location: Bordeaux (France)
Full name: Julien Marcel

How beautiful is your code?

Post by JuLieN »

In our little community, sources of engines like Fruit or Stockfish are often praised, the first one for its clarity, the second one for its mastering of all C++ tricks (something that, as a pascal programmer, I have to trust you for). But what is a beautiful code ?

I just read a very interesting article on the subject:
http://kotaku.com/5975610/the-exception ... t=56177550

So how beautiful is your code?
"The only good bug is a dead bug." (Don Dailey)
[Blog: http://tinyurl.com/predateur ] [Facebook: http://tinyurl.com/fbpredateur ] [MacEngines: http://tinyurl.com/macengines ]
jdart
Posts: 4366
Joined: Fri Mar 10, 2006 5:23 am
Location: http://www.arasanchess.org

Re: How beautiful is your code?

Post by jdart »

I'm pretty pragmatic about it. If the code works I don't obsess about how nice it might be in other respects. Once in a while I go through and clean up things: remove warnings, add "const" where it should be, indent the right way, etc. But I'd rather spend my time on the function not the appearance. That is just my way of doing things.

I am not a big STL fan. I wrote some of my own templated containers because in the early C++ days STL was a nightmare from the standpoint of portability and reliability. But I think it's solidified now.

That said, I agree Stockfish is pretty nicely structured and I think Tord took a bit more care about code than I do.

--Jon
User avatar
lucasart
Posts: 3232
Joined: Mon May 31, 2010 1:29 pm
Full name: lucasart

Re: How beautiful is your code?

Post by lucasart »

jdart wrote:That said, I agree Stockfish is pretty nicely structured and I think Tord took a bit more care about code than I do.
To be fair, the credit should go to Marco Costalba on that. In terms of coding style, Glaurung 2.1 and the latest Stockfish are like day and night. Stockfish manages to combine
1/ use of powerful C++ and STL features
2/ readability and simplicity

More often than not, people writing C++ fall into extremes:
- people who don't know much about C++ do not use 1/, effectively writing C code with a little bit of syntaxic sugar around it, to pretend it's C++
- people who know too much about C++ write illegible and highly context sensitive templatized code. The result is illegible to mere mortals, and unmaintainable by anyone other than the guy who wrote it (or a few C++ gurus like Bjarn and his friends)

But Stockfish somehow manages to find the right balance. It uses powerful C++ features, in order to make the code concise and portable. But at the same time it never goes to far in C++ obfuscation.

There are so many things that could be said about coding style, that I really don't even want to start. And others have already done that better than I could. Have a look at what the prophet Linus Torvaldes had to say regarding coding style in Linux:
https://computing.llnl.gov/linux/slurm/coding_style.pdf

The guy is a bit psycho-rigid sometimes (like on his tab=8 spaces idiocy), but he's damned right stuff that really matters.
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: How beautiful is your code?

Post by mcostalba »

JuLieN wrote: But what is a beautiful code ?
IMO the beautiful code has proper named variables and data structures.

This is the difference between a beautiful code and another: in the first case names are up to the point, clear, readable, simple, self-documenting and not ambiguous. It is very hard to chose proper names and you can always do better, or you can always steal names from others, as I did a lot and still I do ;-)

Eventually the "beautiful code" (defined as above) usually it is also simple, well structured and well written. Coincidence? No, I think it is not.
User avatar
Rebel
Posts: 6991
Joined: Thu Aug 18, 2011 12:04 pm

Re: How beautiful is your code?

Post by Rebel »

Beautiful code is that others understand it quickly.
User avatar
michiguel
Posts: 6401
Joined: Thu Mar 09, 2006 8:30 pm
Location: Chicago, Illinois, USA

Re: How beautiful is your code?

Post by michiguel »

JuLieN wrote:In our little community, sources of engines like Fruit or Stockfish are often praised, the first one for its clarity, the second one for its mastering of all C++ tricks (something that, as a pascal programmer, I have to trust you for). But what is a beautiful code ?

I just read a very interesting article on the subject:
http://kotaku.com/5975610/the-exception ... t=56177550

So how beautiful is your code?
I don't believe there is such thing. Beautiful means nothing and it is in the eye of the beholder. Names that I think could be applied to compliment code are "straightforward", "readable", "efficient", "modular", "maintainable" etc., but beautiful is a silly name, the same thing happens when chefs praise food. The food could be "flavorful", "intense", "spicy" etc. but they use poetry instead.

For instance, a pascal programmer could see a goto and vomit. A C programmer could use it in a way that it very convenient to simplify the code and see it beautiful. I grew as a pascal programmer and I developed an aversion to gotos that I believed played against me when I started with C. Still today I do not use any, when my mind is telling me it could be better to do it in certain cases. I think we need to keep our minds open. setjmp longjmp is another case that some people have strong disagreements about it, when it comes to "beauty".

"Beautiful code is for hippies, who could only play frisbee" will say Eric Cartman ;-)

Miguel
mar
Posts: 2554
Joined: Fri Nov 26, 2010 2:00 pm
Location: Czech Republic
Full name: Martin Sedlak

Re: How beautiful is your code?

Post by mar »

My code is ugly.
But beautiful code IMHO is well designed in the first place. Poor design and you are DOOMed :)
You mentioned Doom3, I think Quake3 (plain C) was a masterpiece.
"Beautiful" code makes you think it's actually very simple.
Modern Times
Posts: 3546
Joined: Thu Jun 07, 2012 11:02 pm

Re: How beautiful is your code?

Post by Modern Times »

michiguel wrote: For instance, a pascal programmer could see a goto and vomit. A C programmer could use it in a way that it very convenient to simplify the code and see it beautiful. I grew as a pascal programmer and I developed an aversion to gotos that I believed played against me when I started with C. Still today I do not use any, when my mind is telling me it could be better to do it in certain cases.
Miguel
In my programming class a couple of decades ago, using a GOTO was an instant fail from our lecturer. Things have moved on of course...
User avatar
Kempelen
Posts: 620
Joined: Fri Feb 08, 2008 10:44 am
Location: Madrid - Spain

Re: How beautiful is your code?

Post by Kempelen »

Rebel wrote:Beautiful code is that others understand it quickly.
I suppose modern editors and IDEs with integrate syntax color has something to do also for the subjetive assess of beautiful code.
Fermin Serrano
Author of 'Rodin' engine
http://sites.google.com/site/clonfsp/
User avatar
lucasart
Posts: 3232
Joined: Mon May 31, 2010 1:29 pm
Full name: lucasart

Re: How beautiful is your code?

Post by lucasart »

Modern Times wrote:
michiguel wrote: For instance, a pascal programmer could see a goto and vomit. A C programmer could use it in a way that it very convenient to simplify the code and see it beautiful. I grew as a pascal programmer and I developed an aversion to gotos that I believed played against me when I started with C. Still today I do not use any, when my mind is telling me it could be better to do it in certain cases.
Miguel
In my programming class a couple of decades ago, using a GOTO was an instant fail from our lecturer. Things have moved on of course...
In some extremely rare cases, GOTO is the right way. Stockfish uses a goto in the search, for instance.
In my engine, I never needed to write a GOTO, but if I did, I would do it without shame :D
Theory and practice sometimes clash. And when that happens, theory loses. Every single time.