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?
How beautiful is your code?
Moderator: Ras
-
- Posts: 2949
- Joined: Mon May 05, 2008 12:16 pm
- Location: Bordeaux (France)
- Full name: Julien Marcel
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 ]
[Blog: http://tinyurl.com/predateur ] [Facebook: http://tinyurl.com/fbpredateur ] [MacEngines: http://tinyurl.com/macengines ]
-
- Posts: 4396
- Joined: Fri Mar 10, 2006 5:23 am
- Location: http://www.arasanchess.org
Re: How beautiful is your code?
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
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
-
- Posts: 3241
- Joined: Mon May 31, 2010 1:29 pm
- Full name: lucasart
Re: How beautiful is your code?
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 combinejdart wrote:That said, I agree Stockfish is pretty nicely structured and I think Tord took a bit more care about code than I do.
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.
-
- Posts: 2684
- Joined: Sat Jun 14, 2008 9:17 pm
Re: How beautiful is your code?
IMO the beautiful code has proper named variables and data structures.JuLieN wrote: But what is a beautiful code ?
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.
-
- Posts: 7286
- Joined: Thu Aug 18, 2011 12:04 pm
- Full name: Ed Schröder
Re: How beautiful is your code?
Beautiful code is that others understand it quickly.
-
- Posts: 6401
- Joined: Thu Mar 09, 2006 8:30 pm
- Location: Chicago, Illinois, USA
Re: 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.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?
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
-
- Posts: 2641
- Joined: Fri Nov 26, 2010 2:00 pm
- Location: Czech Republic
- Full name: Martin Sedlak
Re: How beautiful is your code?
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.
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.
-
- Posts: 3693
- Joined: Thu Jun 07, 2012 11:02 pm
Re: How beautiful is your code?
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...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
-
- Posts: 620
- Joined: Fri Feb 08, 2008 10:44 am
- Location: Madrid - Spain
Re: How beautiful is your code?
I suppose modern editors and IDEs with integrate syntax color has something to do also for the subjetive assess of beautiful code.Rebel wrote:Beautiful code is that others understand it quickly.
-
- Posts: 3241
- Joined: Mon May 31, 2010 1:29 pm
- Full name: lucasart
Re: How beautiful is your code?
In some extremely rare cases, GOTO is the right way. Stockfish uses a goto in the search, for instance.Modern Times wrote: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...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 engine, I never needed to write a GOTO, but if I did, I would do it without shame

Theory and practice sometimes clash. And when that happens, theory loses. Every single time.