How beautiful is your code?

Discussion of chess software programming and technical issues.

Moderators: Harvey Williamson, bob, hgm

Forum rules
This textbox is used to restore diagrams posted with the [d] tag before the upgrade.
User avatar
JuLieN
Posts: 2948
Joined: Mon May 05, 2008 10:16 am
Location: Nantes (France)
Contact:

How beautiful is your code?

Post by JuLieN » Tue Jan 15, 2013 1:36 am

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)
Image [Blog: http://tinyurl.com/predateur ] [Facebook: http://tinyurl.com/fbpredateur ] [MacEngines: http://tinyurl.com/macengines ]

jdart
Posts: 4014
Joined: Fri Mar 10, 2006 4:23 am
Location: http://www.arasanchess.org

Re: How beautiful is your code?

Post by jdart » Tue Jan 15, 2013 3:08 am

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: 3141
Joined: Mon May 31, 2010 11:29 am
Full name: lucasart
Contact:

Re: How beautiful is your code?

Post by lucasart » Tue Jan 15, 2013 7:44 am

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 7:17 pm

Re: How beautiful is your code?

Post by mcostalba » Tue Jan 15, 2013 7:43 pm

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: 5694
Joined: Thu Aug 18, 2011 10:04 am

Re: How beautiful is your code?

Post by Rebel » Tue Jan 15, 2013 7:54 pm

Beautiful code is that others understand it quickly.

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

Re: How beautiful is your code?

Post by michiguel » Tue Jan 15, 2013 8:13 pm

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: 2195
Joined: Fri Nov 26, 2010 1:00 pm
Location: Czech Republic
Full name: Martin Sedlak

Re: How beautiful is your code?

Post by mar » Tue Jan 15, 2013 8:16 pm

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: 2604
Joined: Thu Jun 07, 2012 9:02 pm

Re: How beautiful is your code?

Post by Modern Times » Tue Jan 15, 2013 9:44 pm

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 9:44 am
Location: Madrid - Spain
Contact:

Re: How beautiful is your code?

Post by Kempelen » Tue Jan 15, 2013 10:36 pm

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: 3141
Joined: Mon May 31, 2010 11:29 am
Full name: lucasart
Contact:

Re: How beautiful is your code?

Post by lucasart » Tue Jan 15, 2013 11:09 pm

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.

Post Reply