CodePlea

Random thoughts on programming
11 Feb 2016

Succinct Code


I like simple things. It should be self-evident - I recently ditched WordPress for a hundred lines of PHP and flat-files. I'm much happier for it.

I used to write a lot of C++ code, but for the last several years I've completely eschewed it in favor of C. I can't ever see myself ever going back to C++, as I feel like C is a better language, and it's much easier to keep a large project nice and neat using it. C++ code has a tendency to grew crufty almost from the start. C++ has more abstractions, and although all abstracts are leaky, I think C++ lends itself to creating especially leaky abstractions. C++ makes it much easier to build yourself into a corner compared to C.

Anyway, a few days ago I witnessed an exchange on Stack Overflow that I thought was interesting. It pretty well illustrated some of the differences I find between C and C++ code. And also showcased some of the problems I have with Stack Overflow in general.

While keeping an eye on the new tab, a question about random number generators in C++ came up. A kind answerer posted the following code as an example of a linear congruential generator.

unsigned int seed = 12345;

unsigned int random() {
    seed = (1664525 * seed + 1013904223);
    return seed;
}

In my opinion, this is excellent code for it's purpose. It's succinct, and it demonstrates exactly the LCG algorithm and nothing more. It's excellent teaching code.

Of course, it seems that my opinion is the minorities'. Almost immediately I watched this answer get down-votes and negative comments. Comments like "Global variables are evil!" "This belongs in a class!" etc.

The question wasn't about global variables. Anyone with a modicum of proficiency in C knows ten different ways to refactor out the global seed variable. And the code has much larger impediments to real world usage than its global variable (such as the inherent limitations of the LCG algorithm). But the code is great for illustration of the algorithm, and I thought that's what Stack Overflow was about.

Another user noticed the trend and decided to capitalize on the opportunity to gain rep. Not long after the original answer, he posted the following code:

class Random
{
    private:
        unsigned int seed;

    public:
        Random(unsigned int s);
        unsigned int next();
};

Random::Random(unsigned int s)
{
    seed = s;
}

unsigned int Random::next()
{
    seed = (1664525 * seed + 1013904223);
    return seed;
}

This longer, more verbose, needlessly abstracted, C++ code quickly gained up-votes, positive comments, and was later accepted as the official answer. It reminded me of why I don't generally like C++. It's all the boilerplate. Making a class is like filling out tax forms. It's tedious and boring. It provides friction to creating anything and especially to refactoring anything. And for what advantage?

Eventually the moderators closed the question as a duplicate, the first answerer deleted his answer, and everybody lost. The end.

01 Feb 2016

Ditching Wordpress


This site has been running WordPress since the beginning.

Today I moved it to my own CMS (which I will open-source one of these days). I've integrated comments using Disqus.

WordPress isn't bad per se, but it tries to do everything. That makes it extremely complex. I like simple things. Now I feel like I can really tweak the site's functionality. I think I'll be much happier going forward with my flat-file CMS.

Hopefully nothing was broken in the transfer. I think all the URLs have stayed the same and all the old comments transferred over.

21 Jan 2016

Math Expression Parsing in C


I published some code over at Github today. It's a tiny library called TinyExpr. It parses, compiles, and evaluates math expressions.

    #include "tinyexpr.h"
    #include <stdio.h>

    int main(int argc, char *argv[])
    {
        const char *expression = "sqrt(5^2+7^2+11^2+(8-2)^2)";
        printf("Result: %f\n", te_interp(expression, 0));
        return 0;
    }

Anyway, it's quite fast, and it has other features like variable binding. It's all implemented in one ANSI C source file with no dependencies. Check it out!

05 Nov 2015

Uploading to GitHub


I've decided to publish some of my side projects over at GitHub. Right now it's just PlusCallback (C++ callback library) and MINCTEST (a tiny ANSI C unit-testing framework). Hopefully this will help me keep a bit more organized, and help me publish some more code.

I'm currently working on several side projects I'd like to open source in the next several months:

  • A tiny PHP CMS
  • A simple artificial neural network library in C
  • A financial technical analysis project
  • A numerical optimization library in C
04 Mar 2015

OpenGL with C and Tcl/Tk


Tcl/Tk has been my language of choice lately for doing GUIs. I often combine this with a C back-end for some of the heavy lifting. A few times I've needed a 3D visualization in C and OpenGL, but was stuck without a good GUI. So the obvious solution was to combine C and Tcl/Tk with OpenGL.

There are a few existing solutions for combining Tcl/Tk with OpenGL (see the Tcl wiki). However, everything I've found has had some shortcoming. Almost all of these projects are old and not maintained. This can make even compiling them a huge pain. Also, I didn't really want to actually do 3D from Tcl. Tcl is great for text processing and glue logic, but it's not my favorite language for math.

It turns out there is a very easy way to combine Tcl/Tk with OpenGL and C on Windows. It involves making a frame widget in Tcl, and then passing its HWND to C to initialize the OpenGL rendering context.

#We'll use a frame control to draw our OpenGL.
set display [frame .opengl -width 800 -height 500 -background white -takefocus 1]
pack $display -fill both -expand 1

#Tell C where to put the OpenGL
SetRenderWindow [winfo id $display]

#Tell C when the window resizes
bind $display  {Resize %w %h}

Both SetRenderWindow and Resize are commands added in C. SetRenderWindow takes the HWND and creates an OpenGL rendering context. Resize uses the new size info from TCL to change the OpenGL viewport.

All of the OpenGL math and rendering is done from C. We use the Tcl_CreateTimerHandler function to create a regular callback for rendering.

This way it's easy to use OpenGL from C with a nice Tcl/Tk GUI.

Tcl/TK app showing 3d cube in OpenGL.

Here is the code: Tcl/Tk OpenGL Basic.