How beautiful is your code?

Discussion of chess software programming and technical issues.

Moderators: hgm, Dann Corbit, Harvey Williamson

wgarvin
Posts: 838
Joined: Thu Jul 05, 2007 5:03 pm
Location: British Columbia, Canada

Re: How beautiful is your code?

Post by wgarvin »

lucasart wrote:obfuscated templates libraries may be useful for experienced C++ programmers who understand the arcanes of the STL (at best 1% of amateur C++ programmers?). But saying that they make life easier for beginners is such falsehood, it's not evne funny.
http://yosefk.com/c++fqa/templates.html

I have made a choice to use these nasty things to a minimum and only when they are actually useful. There are some many C++ programs that make a point of using all the defective C++ feature there is, as if their code was written to impress someone about their knowledge of the language. What matters is to write simple, maintainable, portable code. The rest is masturbation, IMO.
As a matter of fact, all very large projects written in C++ inevitably become an unmaintainable mess, that only some C++ gurus can fix (at huge cost, especially when the code is written to use all C++ obfuscated STL there is).
I totally agree. I'm not sure what its like outside of the game industry, but I've used C++ for about 20 years now and I am increasingly disappointed with its many failings every year. Its a bloated, complex language full of dangerous, sharp-edged features which often interact badly with each other. It drifts farther and farther from the systems-programming language I actually need, with every generation of the standard.

I know some game developers who like STL, but my own experience has been that only a few parts of it are really worth bothering with.

std::sort and std::lower_bound are great, and std::vector is usable if you don't already have something better (but every game engine I've worked on has something much better, because variable-sized arrays of homogenous data are used everywhere in games because they are cache-friendly, and it pays to have something smaller and faster and easier to debug and less code-bloat-causing than std::vector). Same thing for std::string, although that one is good enough for most purposes. If you wrap them carefully enough, stl::map and whatever hash_map also come in handy. I struggle to think of anything else from the STL or the C++ standard library that I would willingly use, and language misfeatures like exceptions, RTTI, virtual base classes and pointers-to-members are safest to just completely avoid.

Iostreams are a clunky mess, I've never seen a problem where they were the right solution. Same goes for C++ exceptions which are a total disaster-zone. You can throw anything you want, but how to catch it? Interop with C gets hard in the presence of exceptions, and most programmers can't write reliable exception-safe code. (I think most of the ones who think they can, are deluding themselves. I certainly wouldn't claim to be able to write exception-safe code, and if you know you can't do it correctly anyways, I suggest not trying to do it at all!) even if we wanted to use exceptions, game engines have to run on platforms which don't even support them anyways.. None of the built-in smart pointer types are worth trying to build on. Linked-lists are worthless if they aren't intrusive; <algorithm> is a joke, sets are not generally more efficient than maps. At the extreme end of C++ness, you end up with something like boost, which is a serious case of mad-cow disease.

If you want to write high-level programs, write them in ANY other language besides C++. Most languages have decent bindings to C that you can use for high-performance parts. Or you can embed LuaJIT or a JavaScript VM and alternate hard and soft layers. But I think the most useful niche for C++ is still, using it as a better C to write low-level programs. C plus syntactic sugar, virtual methods, simple container classes that manage their own memory, const references for passing large, read-only parameters, and the convenience of RAII. But the deeper you drink the C++ "kool-aid", the more problems you create for yourself that you will then have to solve! The 10th time you get a crash inside a 1KB block of heavily-templated code and the debugger can't even tell you what types the template was instantiated with, you will realize that those "benefits" you thought you were getting from using "proper" C++ can also be serious liabilities. Being able to debug your code is significantly more important than saving 1 line of boilerplate with something from <algorithm> will ever be.
User avatar
Don
Posts: 5106
Joined: Tue Apr 29, 2008 4:27 pm

Re: How beautiful is your code?

Post by Don »

wgarvin wrote:
lucasart wrote:obfuscated templates libraries may be useful for experienced C++ programmers who understand the arcanes of the STL (at best 1% of amateur C++ programmers?). But saying that they make life easier for beginners is such falsehood, it's not evne funny.
http://yosefk.com/c++fqa/templates.html

I have made a choice to use these nasty things to a minimum and only when they are actually useful. There are some many C++ programs that make a point of using all the defective C++ feature there is, as if their code was written to impress someone about their knowledge of the language. What matters is to write simple, maintainable, portable code. The rest is masturbation, IMO.
As a matter of fact, all very large projects written in C++ inevitably become an unmaintainable mess, that only some C++ gurus can fix (at huge cost, especially when the code is written to use all C++ obfuscated STL there is).
I totally agree. I'm not sure what its like outside of the game industry, but I've used C++ for about 20 years now and I am increasingly disappointed with its many failings every year. Its a bloated, complex language full of dangerous, sharp-edged features which often interact badly with each other. It drifts farther and farther from the systems-programming language I actually need, with every generation of the standard.

I know some game developers who like STL, but my own experience has been that only a few parts of it are really worth bothering with.

std::sort and std::lower_bound are great, and std::vector is usable if you don't already have something better (but every game engine I've worked on has something much better, because variable-sized arrays of homogenous data are used everywhere in games because they are cache-friendly, and it pays to have something smaller and faster and easier to debug and less code-bloat-causing than std::vector). Same thing for std::string, although that one is good enough for most purposes. If you wrap them carefully enough, stl::map and whatever hash_map also come in handy. I struggle to think of anything else from the STL or the C++ standard library that I would willingly use, and language misfeatures like exceptions, RTTI, virtual base classes and pointers-to-members are safest to just completely avoid.

Iostreams are a clunky mess, I've never seen a problem where they were the right solution. Same goes for C++ exceptions which are a total disaster-zone. You can throw anything you want, but how to catch it? Interop with C gets hard in the presence of exceptions, and most programmers can't write reliable exception-safe code. (I think most of the ones who think they can, are deluding themselves. I certainly wouldn't claim to be able to write exception-safe code, and if you know you can't do it correctly anyways, I suggest not trying to do it at all!) even if we wanted to use exceptions, game engines have to run on platforms which don't even support them anyways.. None of the built-in smart pointer types are worth trying to build on. Linked-lists are worthless if they aren't intrusive; <algorithm> is a joke, sets are not generally more efficient than maps. At the extreme end of C++ness, you end up with something like boost, which is a serious case of mad-cow disease.

If you want to write high-level programs, write them in ANY other language besides C++. Most languages have decent bindings to C that you can use for high-performance parts. Or you can embed LuaJIT or a JavaScript VM and alternate hard and soft layers. But I think the most useful niche for C++ is still, using it as a better C to write low-level programs. C plus syntactic sugar, virtual methods, simple container classes that manage their own memory, const references for passing large, read-only parameters, and the convenience of RAII. But the deeper you drink the C++ "kool-aid", the more problems you create for yourself that you will then have to solve! The 10th time you get a crash inside a 1KB block of heavily-templated code and the debugger can't even tell you what types the template was instantiated with, you will realize that those "benefits" you thought you were getting from using "proper" C++ can also be serious liabilities. Being able to debug your code is significantly more important than saving 1 line of boilerplate with something from <algorithm> will ever be.
LuaJIT is pretty impressive - the speed is absolutely amazing. This is truly a high level scripting language that approaches the speed of C.

I would like to limit myself to 2 languages for 99% of what I need to do but I have not found that ideal second language. It could be LuaJIT except that it's not really designed to be a general purpose language and that usually involves some pain when trying to do something that isn't already in it's limited library.

Of course the dream is to have a single simple high level language that is just as fast as C and gives you complete control like C does and yet can be used for quick and dirty scripting too. The chances of that happening seem nil. I want too much. I also want my language to produce native binaries.

The Go language is one possibility. It's not everything I want but it's a LOT of what I want. It's not as fast as C but maybe someday it will be. I would not be surprised if lauJIT is faster right now for most things.
Capital punishment would be more effective as a preventive measure if it were administered prior to the crime.
mcostalba
Posts: 2684
Joined: Sat Jun 14, 2008 9:17 pm

Re: How beautiful is your code?

Post by mcostalba »

I'm toying with Python these days.

I didn't know this incredible glue language, ok I knew it exists but I never used it before a couple of weeks ago, and I have to say I am amazed !!

- It is very easy to learn, or at least is easy to start writing stuff

- The interactive interpret is really powerful and very useful for debugging

- It has thousands of libraries for almost anything, it is a snap to install and use them

- And last, but not the least, the language is expressive and simple

For anything that does not involve extreme performance (so not for chess engines) it really deserves a try.
User avatar
Don
Posts: 5106
Joined: Tue Apr 29, 2008 4:27 pm

Re: How beautiful is your code?

Post by Don »

mcostalba wrote:I'm toying with Python these days.

I didn't know this incredible glue language, ok I knew it exists but I never used it before a couple of weeks ago, and I have to say I am amazed !!

- It is very easy to learn, or at least is easy to start writing stuff

- The interactive interpret is really powerful and very useful for debugging

- It has thousands of libraries for almost anything, it is a snap to install and use them

- And last, but not the least, the language is expressive and simple

For anything that does not involve extreme performance (so not for chess engines) it really deserves a try.
Ruby is even better. I like both languages but I believe Ruby is a better designed language. In many ways they are very much the same.

There are some advantages to Python that goes beyond the language itself though.
Capital punishment would be more effective as a preventive measure if it were administered prior to the crime.
Tom Likens
Posts: 303
Joined: Sat Apr 28, 2012 6:18 pm
Location: Austin, TX

Re: How beautiful is your code?

Post by Tom Likens »

mcostalba wrote:I'm toying with Python these days.

I didn't know this incredible glue language, ok I knew it exists but I never used it before a couple of weeks ago, and I have to say I am amazed !!

- It is very easy to learn, or at least is easy to start writing stuff

- The interactive interpret is really powerful and very useful for debugging

- It has thousands of libraries for almost anything, it is a snap to install and use them

- And last, but not the least, the language is expressive and simple

For anything that does not involve extreme performance (so not for chess engines) it really deserves a try.
Python is fantastic. I use it all the time at work. Once you combine Python with Numpy and Matplotlib you have an amazing powerful tool to do scientific computing. If you have any background with Matlab, this is a natural (and more powerful) replacement.
Tom Likens
Posts: 303
Joined: Sat Apr 28, 2012 6:18 pm
Location: Austin, TX

Re: How beautiful is your code?

Post by Tom Likens »

Don wrote:
mcostalba wrote:I'm toying with Python these days.

I didn't know this incredible glue language, ok I knew it exists but I never used it before a couple of weeks ago, and I have to say I am amazed !!

- It is very easy to learn, or at least is easy to start writing stuff

- The interactive interpret is really powerful and very useful for debugging

- It has thousands of libraries for almost anything, it is a snap to install and use them

- And last, but not the least, the language is expressive and simple

For anything that does not involve extreme performance (so not for chess engines) it really deserves a try.
Ruby is even better. I like both languages but I believe Ruby is a better designed language. In many ways they are very much the same.

There are some advantages to Python that goes beyond the language itself though.
Ruby is interesting. What support facilities does it have for scientific analysis, (i.e. how easy would it be for me to do an FFT or something similar without writing my own routines)?
mar
Posts: 2552
Joined: Fri Nov 26, 2010 2:00 pm
Location: Czech Republic
Full name: Martin Sedlak

Re: How beautiful is your code?

Post by mar »

wgarvin wrote:I struggle to think of anything else from the STL or the C++ standard library that I would willingly use, and language misfeatures like exceptions, RTTI, virtual base classes and pointers-to-members are safest to just completely avoid.
I agree, I was toying with pointers-to-members recently in my signal-slot library but that's as far as I go. It's awesome that I can connect member functions of any class to a signal exported from another (providing they are compatible), in fact this allows to glue two totally unrelated classes together without having to know anything about each other - it's probably not useful for a chess engine but it is for larger projects, where you can sync for example two GUI objects without having to write tons of useless code (which is why message-based GUI framewoks suck). It also allows you to handle callbacks in one place - I have seen lots of bad code because of that where the code paths are too complex and lead to x places - this asks for nasty bugs and maintenance problems.
wgarvin wrote: Or you can embed LuaJIT or a JavaScript VM and alternate hard and soft layers.
Yes Lua is awesome language, very well designed, lightweight, the problem with LuaJIT is that you are of out luck on iOS and have to use an interpreter - just because of stupid restrictions imposed by Apple. It's not because of safety - you can export system-level calls to interpreted language anyway so I don't understand these restrictions - except to make developer's life harder.
Perhaps AngelScript is worth to be mentioned here but I never used it so can't judge.
The real power of a scripting language IMHO is in that you don't have to rebuild your binary to change scripts and that you can usually easily serialize its internal state.
User avatar
Don
Posts: 5106
Joined: Tue Apr 29, 2008 4:27 pm

Re: How beautiful is your code?

Post by Don »

Tom Likens wrote:
Don wrote:
mcostalba wrote:I'm toying with Python these days.

I didn't know this incredible glue language, ok I knew it exists but I never used it before a couple of weeks ago, and I have to say I am amazed !!

- It is very easy to learn, or at least is easy to start writing stuff

- The interactive interpret is really powerful and very useful for debugging

- It has thousands of libraries for almost anything, it is a snap to install and use them

- And last, but not the least, the language is expressive and simple

For anything that does not involve extreme performance (so not for chess engines) it really deserves a try.
Ruby is even better. I like both languages but I believe Ruby is a better designed language. In many ways they are very much the same.

There are some advantages to Python that goes beyond the language itself though.
Ruby is interesting. What support facilities does it have for scientific analysis, (i.e. how easy would it be for me to do an FFT or something similar without writing my own routines)?
I couldn't tell you. I know that it has a very extensive library though, it's certainly quite mature, just like python is It's a dream to program in this powerful language.

There are tons of projects directly involving each languages as well such as JVM versions of them such as Jruby and many others. Both languages are pretty slow as they are super-high level but many projects seek to improve the speed such as Iron Python.

A really interesting project is called Genie - it's a compiled language based on gnome with very Ruby/Python -ish syntax. But for any language I would adapt it must be strongly cross-platform. Yes, I no big fan of windows, but I still see great value in cross-platform development. Obviously, Komodo would not do well running only on Linux so you can see where I am coming from.
Capital punishment would be more effective as a preventive measure if it were administered prior to the crime.
wgarvin
Posts: 838
Joined: Thu Jul 05, 2007 5:03 pm
Location: British Columbia, Canada

Re: How beautiful is your code?

Post by wgarvin »

mar wrote:
wgarvin wrote:I struggle to think of anything else from the STL or the C++ standard library that I would willingly use, and language misfeatures like exceptions, RTTI, virtual base classes and pointers-to-members are safest to just completely avoid.
I agree, I was toying with pointers-to-members recently in my signal-slot library but that's as far as I go. It's awesome that I can connect member functions of any class to a signal exported from another (providing they are compatible), in fact this allows to glue two totally unrelated classes together without having to know anything about each other - it's probably not useful for a chess engine but it is for larger projects, where you can sync for example two GUI objects without having to write tons of useless code (which is why message-based GUI framewoks suck). It also allows you to handle callbacks in one place - I have seen lots of bad code because of that where the code paths are too complex and lead to x places - this asks for nasty bugs and maintenance problems.
If you haven't seen it before, make sure to read this 2005 article by Don Clugston. One eye-opening thing is the table about 1/3rd through it that lists all of the different sizes that pointer-to-members have on various different compilers. Its implemented in a variety of ways, but it turns out that with a bit of compiler-specific hackery you can actually build a robust and efficient delegate mechanism out of them. Its the only use I've ever seen of them that I actually liked. :)
Gusev
Posts: 1476
Joined: Mon Jan 28, 2013 2:51 pm

Re: How beautiful is your code?

Post by Gusev »

I'm toying with Python these days.

I didn't know this incredible glue language, ok I knew it exists but I never used it before a couple of weeks ago, and I have to say I am amazed !!

- It is very easy to learn, or at least is easy to start writing stuff
This is what my 13-year-old daughter is learning.