Which programming language and which framework should I use?

Posted on September 19th, 2009 by Chris.
Categories: Chris, Programming.

Frameworks like Ruby on Rails are great because they make it easy to write code – in a way that makes sense for the problem of websites.

Programming languages are great because they make it easy to write code – in a way that makes it easier for us to understand.

In the end though, all of these abstractions were not written with your problem in mind, but a generic, off-the-shelf problem that you don’t actually have.

Thus, write with any language you want. But write in a way that’s appropriate to your problem, and write in a way that’s appropriate to you.

0 comments.

Pseudocode from a practical perspective

Posted on December 13th, 2008 by Chris.
Categories: Chris, General/Misc., Programming.

Designing programs before you write them

Preconditions:

  • Familiarity with writing and compiling a program in Java.
  • Not for people who have had a significant amount of experience in commercial programming.

(more…)

0 comments.

Reversing bits

Posted on November 11th, 2008 by Tim.
Categories: Programming, Tim.

Reversing the bytes of an integer is a reasonably common operation when doing network programming, especially when dealing with possibly differing endianness. Reversing the endianness came up at work today, so I was thinking about a slightly different problem.

Given a 64-bit integer, what is the fastest way to reverse the ordering of the bits? The naive approach is to use a loop getting 1 bit into place each iteration. The result is something like this:

private static ulong RevBits1(ulong p)
{
ulong r = 0;
for (int i = 0; i < 64; i++)
{
r = r >> 1;
r = r | (p & 0x8000000000000000);
p = p << 1;
}
return r;
}

If you don’t count the loop operations, this ends up with 256 operations, 4 per iteration. If you unroll this, you can make one of the terms a constant factor, bringing the operation count down to 192. We can improve on this with a divide and conquer approach:

private static ulong RevBits(ulong p)
{
p = (p << 32) | (p >> 32);
p = ((p << 16) & 0xFFFF0000FFFF0000) | ((p >> 16) & 0x0000FFFF0000FFFF);
p = ((p << 8 ) & 0xFF00FF00FF00FF00) | ((p >> 8 ) & 0x00FF00FF00FF00FF);
p = ((p << 4) & 0xF0F0F0F0F0F0F0F0) | ((p >> 4) & 0x0F0F0F0F0F0F0F0F);
p = ((p << 2) & 0xCCCCCCCCCCCCCCCC) | ((p >> 2) & 0x3333333333333333);
p = ((p << 1) & 0xAAAAAAAAAAAAAAAA) | ((p >> 1) & 0x5555555555555555);
return p;
}

Harder to read, but this version is only 28 operations. In asm we could get this down even farther on 64 bit machines, since the first statement can be written as a 32 bit rotation, and the second statement can be written as two 16 bit rotations, bringing our operation count down to 24. Since each of our other operations is nearly equivalent to an opcode (on 64-bit processors at least) this is pretty close to the actual number of opcodes that could be generated by a good optimizing compiler.

It would be interesting to see if this could be optimized further :-)

-Tim

0 comments.

Don’t Work on the Wrong Machine

Posted on August 28th, 2008 by Chris.
Categories: Business/The Software Industry, Chris, Programming.

Fred Brooks: Build one to throw away

My version: Build every one to throw away.

Fred Brooks’ classic The Mythical Man-Month made this observation developing a new piece of software – inevitably, many of the problems that will need to be solved won’t be known until you try to solve them. The first try will incorporate many feelings of should-have and would-have that will be fixed on the second try.

Carter made a bench!

This is much like a journeyman craftsman building his or her first bench, or planting their first garden. The first try is a learning experience. There is no way around this, nor should there be – learning is often best by doing. The valuable asset is the experience of the builder, not the product.

But we don’t live in the Middle Ages, the age of guilds. We live in the post-Industrial Age. Factory owners don’t consider hoarding all the toys or cars or electronics they produce as an asset. Henry Ford’s genius was to redesign not just the golden eggs, but the machine that lays them.

As programmers, we are not working on a code base. We are working on a machine that produces code. The machine is made up of us, our experiences, the tools we build to make code (which can be made out of code themselves!). This is where results come from, and we should spend our time tuning this machine by producing more, not protecting what we have.

Does this mean that we should throw out old code? By no means! Old code is one of the most efficient resources we have for producing new code. But every process of manufacture in the past has been made into a more automatic and refined process. How could we consider ourselves any different?

Does this mean that we should code like crap? By no means! As was once said, “If you write the first one to throw out, you will end up throwing out the second one as well.” The point is, write good code, but be willing to write new code – the point is to make not good code, but a good code factory.

0 comments.