Monday, June 27, 2011

The "C/C++" myth

Seeing C and C++ referred to as "C/C++" perplexes me to no end. Writing "C/C++" makes no sense at all. You almost always want either C or C++, as the two have widely disparate areas of suitability.

The two languages seem to be shrouded in a good number of myths, that I would like to debunk

Myth: There is a language "C/C++"

There is no language "C/C++". C and C++ have very little in common. Yes, I know C++ evolved from C, but that fact aside, the two languages are very different. C++ has a lot more in common with basically any given object oriented language than it does with C.

Myth: C++ is the next version of C

C++ is C with an object system grafted onto to it (object orientation is not a more evolved form of imperative programming, even though it's more suitable for some tasks).

The next version of C is C1x.

Myth: C++ is backwards compatible with C

At some point, it was. But today, C++ is not backwards compatible with C, with void pointers and whatnot.

Myth: If you know C++, you know C

This is a superficial truth. You may know the syntax of the language, but this does not make you a good C programmer. The C language has a large number of limitations, and to overcome these requires a lot of experience in C.

C++ has other ways of overcoming these limitations, and there generally is no way to directly translate these to C. Finally, imperative C++ code is bad C++ code, not C code.

As a side note, this is why speed comparisons between C and C++ are typically pathological. If you compile a piece of C code in C++, it will run at the same speed as it would in C. But code in the wild is not going to be side-stepping the object system like that.

Thursday, June 16, 2011

Static webpages

If your webpage has nothing but static content, it should not be generated dynamically. There should not be a multi-second load time for a static webpage with 2 kB worth of content.

If you do dynamically generate static content, then you, fine sir, are an idiot.

Thanks for your patience. I needed to vent this, having used a university webpage that is like the antichrist of sensible web application design. Go go application state stored in session variables rather than the URL, so that browser navigation doesn't work and the whole application periodically crashes forcing you to re-start from the main page.

Tuesday, June 14, 2011

The square root of 2, and multiples of 7

The square root of 2 has a mathematical curiosity. It starts with a long string of multiples of 7.

For reference
\[ \sqrt{2} = 1.4142135623730950\dots \]
\[ \frac{\sqrt{2}}{7} = 0.2020305089104421\dots \]

I've known about this for years. Chalked it up to a mathematical coincidence, and not really thought about it. What connects two, seven and ten (where ten comes from the numeric base used)? The answer is so simple

\[ \sqrt{\frac{1}{2}} = 0.7071067811865475 \dots \approx 0.7 \]