How beautiful is your code?

Discussion of chess software programming and technical issues.

Moderators: hgm, Dann Corbit, Harvey Williamson

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: 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. :)
Thanks Wylie, I haven't seen it before but I'm very well aware that for example M$ compiler has sizes <= general pointer to member, I used hacks using memcpy/memset, but it simply works like a charm :)
For those interested, my "library" (very hacky code :) is available under zlib license here:
https://github.com/kmar/sig
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 »

Rein Halbersma wrote:Just pointing to a FAQ of corner cases does not mean that the fast majority of template libraries is not enormously useful.
You obviously have not read any of this FAQ. These are not corner cases. I've experienced many of them already. And this is just the template part. There is an entire FAQ/FQA on C++. You should seriously read some of it, as it would open your mind a little bit.

This guys knows the arcanes of C++ better than anyone here, perhaps even you. But, unlike C++ evangelists, he speaks from experience rather than theory, and he also knows a *lot* of other programming languages, so his brain did not rot from years of thinking that C++ is the only right way to do things.
Theory and practice sometimes clash. And when that happens, theory loses. Every single time.
Ron Murawski
Posts: 397
Joined: Sun Oct 29, 2006 4:38 am
Location: Schenectady, NY

Re: How beautiful is your code?

Post by Ron Murawski »

Don wrote: 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.
Hi Don,

Vala/Genie has been ported to Windows, along with much of the necessary Gnome stuff.
http://www.tarnyko.net/en/
But it's only one developer doing the porting and with no 64-bit support.

For "strongly cross-platform" how about the Haxe language?
https://en.wikipedia.org/wiki/HaXe
I've never used Haxe, but it already has a couple of books written about it, so it's becoming more mainstream.

I'm working with other people on a Python-based compiled language that, in theory, can be as fast as C. It will be a transpiler that produces C code, similar to Genie. The language will include an interpreter and a compiler. The intent is that normal Python code should run at the same speed as regular Python (ie: slowly), but once you start specifying types in your code, you can compile those parts of the program into modules (so and dll files) that will be callable from the interpreter. If you specify all types throughout your program (or the compiler can infer types from usage), then your program should run at the speed of C. The intent of the language is to create a superset of Python that supports the full language (the new language has many new keywords and extensions added so you can specify types). More importantly the new language will also support the existing Python libraries (and the intriguing possibility of optimizing them!) But the language implementation will be very tricky and it's many years away from being a practical language.

Ron
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:
Rein Halbersma wrote:Just pointing to a FAQ of corner cases does not mean that the fast majority of template libraries is not enormously useful.
You obviously have not read any of this FAQ. These are not corner cases. I've experienced many of them already. And this is just the template part. There is an entire FAQ/FQA on C++. You should seriously read some of it, as it would open your mind a little bit.

This guys knows the arcanes of C++ better than anyone here, perhaps even you. But, unlike C++ evangelists, he speaks from experience rather than theory, and he also knows a *lot* of other programming languages, so his brain did not rot from years of thinking that C++ is the only right way to do things.
When I first came across that FQA Lite (around a decade ago?) I was still a big fan of C++. "What is this guy talking about?" I thought to myself. "He's comparing it to interpreted languages with garbage collectors, that's not fair." I thought. "Of course it doesn't provide run-time type safety, its a compiled language!" I thought.

But then I used C++ for more years, and I eventually came to realize that I now agree with everything in that FQA Lite. Most of the criticisms are spot-on, and many annoying things about the language and about its actual implementations (such as long compile/link times, undecidable grammar of the language, etc.) are covered in there. The lack of a proper module system means large C++ programs can take orders of magnitude longer to compile than equivalent-sized programs in a language such as Java, C# or D (or Modula-3 or Ada or ...) The conscious lack of a garbage collector, makes exceptions very tedious to use (and the reluctance of the standards committee to enshrine a "magic" class such as "std::throwable" or something, as the base of all exceptions, makes its pretty much impossible to catch and sensibly handle all of the things that might be thrown.) The language syntax and type system, is so complicated that tools (IDEs, refactoring tools, debuggers, etc.) require like five times as much effort to build as comparable tools for a simpler language like Java do. Visual Studio represents MAN-CENTURIES of effort to try and make a useful IDE and toolchain for C++, and it still sucks donkey-balls compared to Java IDEs. It still can't even reliably do some of the basic stuff that the Smalltalk refactoring browser could do in the 1980's, and that was in dynamically-typed language! Visual Studio is the pinnacle of C++ tooling; it might someday be supplanted by some LLVM-based tool, but for now VS is the state-of-the-art IDE. The best tool that money can buy, and it still sucks for C++. Java/C# IDEs are way better. Consider Eclipse for Java, for example. It has reliable Go To Definition/Find References that work 100% of the time, and automated refactoring support to rename things, move them around in the class hierarchy, etc. Eclipse had all of this stuff in its first year of development, and Visual Studio still can't do it even after like 15 years of development. The reason is simple: C++ is basically impossible to make good tools for.

C++ is a language so vast and complicated, so full of thorny edge cases and incompatible features, that everyone sticks to just a subset of it. Virtually no one uses "the whole language", because a program doing that would be completely unmaintainable. But everyone uses slightly different subsets... some people use exceptions, some people (me) avoid them like the plague. Some people drink the iostreams kool-aid and use << to print out text, other people (me) prefer to use vendor extensions like _vsnprintf. Insane people use virtual base classes and pointer-to-member-functions and always use <algorithm> crap like "replace_copy_if" and "adjacent_find" to avoid writing a trivial 3-line loop that any programmer can read. Other people (me) prefer to write that loop and not have to google the algorithm name 3 months from now to find out WTF it does. :P

Anyway, its late and I'm tired, and thus prone to ranting.. Suffice it to say that if you are forced to do things in C++ under time pressure for a few years, and you constantly have to debug your screwups (and especially other people's screwups) you learn to have a healthy fear of multi-layered templates, of multiple inheritance, of type-casts of all kinds, and super anger about the stupid textual include model and lack of a proper module system even after *decades* of C++ use. C++ programs can now easily be 500x as big as typical C programs were back when that separate compile-and-link model was first developed. Hardware gets faster every year, and we're still saddled with compile times measured in minutes (or even in hours for some extremely large codebases). And IDEs still struggle to understand which macro definitions are effective in a certain source file, or which one of 12 different definitions of a method named "Serialize" is the one I want it to display.
wgarvin
Posts: 838
Joined: Thu Jul 05, 2007 5:03 pm
Location: British Columbia, Canada

Pop-quiz for die-hard C++ fans !

Post by wgarvin »

Pop Quiz time! :

(1) Explain what argument-dependent lookup is (ADL, also known as Koenig lookup).
(2) Explain the "Substitution failure is not an error" rule (SFINAE).
(3) Why does C++ not allow partial specialization of function templates? What is an equivalent mechanism you can use instead?
(4) What is the "curiously-recurring template pattern" (CRTP)?
(5) What is the earliest point at which an anonymous temporary bound to a const reference might be destroyed by the compiler? What about a non-const reference?

Remember, these are super-important things that all C++ programmers need to know! (ha ha)

At one time I could have answered all of these, and plenty of other "bad interview questions" about C++, but I've managed to start forgetting some of the gory details in order to make room in my brain for more practical things.

If you ever need to look up the exact ADL rules or work out when "two-phase lookup" applies and when it doesn't, or google a strange error message about a base class being inaccessible from a template "due to ambiguity"... then you should stop and ask yourself, "Why am I making my own life so complicated? What possible reason could justify adding this complexity to my codebase? Why am I spending effort on using stupid, arcane, dangerous language features of C++ instead of spending that effort making my program simple and easy to maintain?"

Oh, I forgot to list the most important question:

(6) Why should programmers care what the standard actually says, when actual compilers are always buggy, and most programs aren't standard-conforming anyways? The C99 standard defines almost 200 kinds of undefined behaviour, and C++ presumably has at least as many, since the standard is over 850 pages long (the index alone is 33 pages!). How many programmers can claim to know them all? Very few, I'm guessing.

But compiler writers are determined to exploit these undefined behaviours to do better on benchmarks, even if it breaks real-world programs.

Someday I hope a good systems-programming language will become popular and well-enough supported to replace C/C++ in that role... but I guess it won't happen anytime soon. :lol:
User avatar
lucasart
Posts: 3232
Joined: Mon May 31, 2010 1:29 pm
Full name: lucasart

Re: Pop-quiz for die-hard C++ fans !

Post by lucasart »

I don't even want to try and answer your Quizz, because I don't want to damage my brain learning the answers...

After switching from C to C++, I really want to rewrite my chess engine in pure C again. Frankly, after forcing myself to drink the C++ kool-aid, reading large portions of Bjarn Stroustrup's book, my brain has an indigestion and wants to throw up and forget all about C++. The fact is that there was absolutely nothing useful in C++ for my chess engine.

Keep your mental sanity and say NO to C++
Image
Theory and practice sometimes clash. And when that happens, theory loses. Every single time.
Rein Halbersma
Posts: 741
Joined: Tue May 22, 2007 11:13 am

Re: Pop-quiz for die-hard C++ fans !

Post by Rein Halbersma »

lucasart wrote: Keep your mental sanity and say NO to C++
Coming to a city near you: C++Anonymous, for all disgruntled, frustrated programmers who temporarily bound themselves by non-const reference to this temporary language.
Michel
Posts: 2271
Joined: Mon Sep 29, 2008 1:50 am

Re: How beautiful is your code?

Post by Michel »

I'm working with other people on a Python-based compiled language that, in theory, can be as fast as C. It will be a transpiler that produces C code, similar to Genie. The language will include an interpreter and a compiler. The intent is that normal Python code should run at the same speed as regular Python (ie: slowly), but once you start specifying types in your code, you can compile those parts of the program into modules (so and dll files) that will be callable from the interpreter.
It looks to me as if you are describing cython,,,,

http://www.cython.org/

Many of the modules in the computer algebra package SAGE are written in cython.
mcostalba
Posts: 2684
Joined: Sat Jun 14, 2008 9:17 pm

Re: How beautiful is your code?

Post by mcostalba »

wgarvin wrote:Suffice it to say that if you are forced to do things in C++ under time pressure for a few years, and you constantly have to debug your screwups (and especially other people's screwups) you learn to have a healthy fear of
I fully second this, C++ is not the kind of language to write fast stuff under time pressure...even in the unlucky case the 'thing' you wrote _seems_ to work, you will pay for the 'quick and dirty' hack for many years to come, and people after you will pay even a bigger cost.

C++ is the kind of language it requires you to iterate over and over your code if you really want to get the best out of it. This is because it is so vast and full of possibilities that it is very difficult to write 'the best code for the task' at first attempt.

C++ is like a big and powerful truck! If you need a fast car or even a bicycle then better looking somewhere else.
Ron Murawski
Posts: 397
Joined: Sun Oct 29, 2006 4:38 am
Location: Schenectady, NY

Re: How beautiful is your code?

Post by Ron Murawski »

Michel wrote:
I'm working with other people on a Python-based compiled language that, in theory, can be as fast as C. It will be a transpiler that produces C code, similar to Genie. The language will include an interpreter and a compiler. The intent is that normal Python code should run at the same speed as regular Python (ie: slowly), but once you start specifying types in your code, you can compile those parts of the program into modules (so and dll files) that will be callable from the interpreter.
It looks to me as if you are describing cython,,,,

http://www.cython.org/

Many of the modules in the computer algebra package SAGE are written in cython.
Hi Michel,

Cython is a great package for creating C-compiled files that can be called from Python. But that is all it does.

The new language is called Mypy.
http://mypy-lang.org/

The FAQ and the Overview page can give you a hint. Our goals are quite high. We need more developers!

Ron