“Tradecraft” carries a connotation of high-stakes skullduggery, usually by semi-psychopathic agents of various governments, usually up to no damn good at all.

But what a great word, tradecraft.

In programming, tradecraft could be used to refer to all those accessory necessities, in particular, mastery of:

4. The APIs and language features allowing you to trade in your craft.

All these tools are irrelevant to Computer Science.

Mastery of all is critical to mastering programming tradecraft.

Initially, it doesn’t really matter which set of tools are learned, so long as mastery is pursued. It’s the mindset that matters most.

## Simple Object Oriented Unit Testing For C Programming

Recovery from giant blast of wind

Remember that giant blast of wind a few months ago? The wind that launched all my cool plants down the stairs? Well, here’s a picture of the re-potted collection. That’s about half of them, the other half are still languishing. Crassulae are tough as nails. I may re-pot the remaining… but these need to be split out now. They’re a little overcrowded. (Update: I did split that pot and repot the remaining. Many of them ended up at my friend Ben’s house in Berkeley.)

From left to right: kalanchoe longifolia, unknown crassula, kalanchoe tubiflora, unknown sedum, kalanchoe serrata, unknown kalanchoe, another k. longiflora, sedum burrito, kalanchoe glaucesens, kalanchoe gastonis-bonnerai (hardly visible, very sick little plant), kalanchoe tomentosa, kalanchoe pumila. There’s a kalanchoe marmorata buried in there as well, but that one volunteered. Whew!

The important stuff dealt with, let’s get on with some C programming…

### Really simple unit testing for C

Several years ago I found myself needing a simple way to unit test some c code I was working on. I had previously used JUnit, which was relatively new at that time. cppunit either wasn’t released yet, or was very raw, and besides, my code was straight c.

So I wrote my own.

I knew I didn’t want the complexity of the full JUnit framework, but I did want the the convenience of an object-oriented system.

The general design of this simple object-oriented c unit testing code is to keep the test harness very simple. Each class test can be run as it’s own program, allowing invocation from a shell script, or you can stack the tests like brickwork into increasingly more general c programs.

This is in contrast to the JUnit design where you have to set up more framework in advance, and you add tests within their framework.

The JUnit design is probably more powerful in the sense that it scales better.

On the other hand, mine is simple enough that you can design your own system using it as a building block. And small enough that you can very easily embed it into your code and ship it for internal testing if you like.

I’m sure there are other ways to do it.

### Design of a simple unit testing class

The design is simple enough to implement as a class in either C or C++. I’ve done both, used both in production, and will compare the two approaches here. First, an overview on the general idea:

1. In the C code, the unit test struct definition is located in the header file, making it defined for any file including the unit testing header file. It’s very simple, just a table:

/** Each unit test builds a table that can be
* traversed to exercise each function in the
* component.
*/
typedef struct _testfunc {
int (*test)(void);
const char * testname;
} TestFunc;

2. Here’s an example of a table from the demo code, which tests the correctness of a line segment intersection algorithm:

TestFunc tf[] = {
{test_vertical_cross,       "vertical_cross"       },
{test_vertical_parallel,    "vertical_parallel"    },
{test_endpoint_intersection,"endpoint_intersection"},
{test_total_overlap,        "total_overlap"        },
{test_partial_overlap,      "partial_overlap"      },
{test_no_intersection1,     "no_intersection1"     },
{NULL,                      ""                     }
};


These functions could be hashed into a red/black tree or something, but that’s too much work and too much code. It doesn’t really matter how the functions are ordered, and they’re all going to be called one after another, so a dispatch table is just fine.

3. As you will see from the source code, it’s very simple: a loop scans the table and invokes each callback in turn, until it gets to NULL. The callbacks are boolean, which you will see in the C++ code, returning pass or fail:

int
unittest(TestFunc * testfunc) {

int i = 0;
int passed = TRUE;

while (testfunc[i].test != NULL) {

if (testfunc[i].test()) {
fprintf(stdout, "Passed test_%s.n", testfunc[i].testname);
} else {
fprintf(stdout, "Failed test_%s.n", testfunc[i].testname);
passed = FALSE;
}
i++;
}

fprintf(stdout,"n");

return passed;
}


The default value in each of the test functions should be FALSE. Can you see why the default return value is TRUE in this function?

### Testing for line intersection

For an example, we’ll be testing a function that determines if two line segments intersect. The code is part of a geometry component for the Discontinuous Deformation Analysis application. The idea is that we need to create geometrical blocks from collections of line segments. Line segments that cross each other need to be partitioned into pieces for use by different blocks, and segments that overlap each other need to be appropriately trimmed or deleted. There’s more to block construction than this, but these tasks are handled by the code we’re going to unit test here.

Here’s the overall plan for testing:

1. Test for intersection
2. Test for non-intersection
3. Test for partial overlapping
4. test for full overlapping

We’ll construct a set of line segments for testing each case.

Remember: unit testing cannot guarantee correctness. It can only find find situations which are specified as incorrect. The specification for correctness and incorrectness has to be determined out of band. In this case, that means drawing out a set of line segments on graph paper, picking off the endpoints, feeding them into the algorithm, and determining whether the algorithm is returning what we think it ought to be returning.

Download the Object Oriented C toolkit and take a look for yourself. The code is LPGL licensed, but truthfully, I’m not that worried about it. If you find it useful, and you can show me something cool you did with it or how you extended it, I’m happy. Alternatively, I’d be happy to add any minor tweaks you might like to use back into the main code base… as long as the extensions are simple. If you want a full-blown framework, use CGreen or Boost.

These are the best links I could find on why unit testing is important:

• JUnit.org: The original home of unit testing. You can learn a lot here.
• cgreen is an interesting tool, but much heavier than the tool outlined in this article.
• ### What’s with the these plants anyway?

Plants are cool. I like them.

I have plenty of spares, mostly succulents. If you’re an East Bay or San Francisco local and would like a cool plant, stop by some time get a couple. Or drop me a line and I’ll drop off a plant for you.

## Programming Is “Actionary” Blogging Is “Reactionary”

I have a problem: I need to maintain and extend two blogs, two WordPress plugins, and start work on a new piece of code for a web application.

I have to blog and program…

…but blogging and programming seem to require slight but important differences in how I work. Blogging requires a different sort of attention than programming. This difference is evident. Most bloggers do not program, and most programmers blog only once in a while, if at all.

### Reactionary blogging

So much of what I read about blogging success falls into a category I call “reactionary.” Successful bloggers have to stay on top of everything, all the time! Having Attention Deficit Disorder (ADD) seems to be an advantage for successful bloggers!

Successful bloggers must:

• Regularly keep an eye on trending topics
• Watch traffic on a regular basis
• Maintain the blogging system

These activities require pounding on different parts of your brain, requiring your to shift context. With more experience, you will build a “blogging context” which will make it easier.

### Actionary programming

Programming requires action, forethought, planning and long spans of concentrated attention. I call this “actionary.” People with ADD, otherwise brilliant, sometimes have a hard time sitting down long enough to be effective on a long term programming project.

### Blog and program if possible!

Blogging and programming together can be very difficult, unless you limit the blogging topics to stay very close to your programming.

Do you have experience doing both? Would you like to discuss it? (I’m thinking of you, JP, and this would make an excellent guest post as well!)

## Filling The Resume Gap With Programming

So you got laid off.

It happens.

This is my 5th recession personally. I saved a bunch of money during the last boom, so I’m fairly well set for this one. I planned – in advance – to take some time off and sharpen up my writing skills… and learn some new programming skills.

### Dig deeper into your current skillset

Now that you have a little time on your hands, you can dig a little deeper into some of the more obscure or powerful parts of your favorite programming language. For example, suppose you’re pretty good at C, but have only written single threaded applications. Right now is an excellent time to dip into multi-threading.

I’ve been digging a little deeper myself, poking at some of the object-oriented C code I have laying around. There are some blog posts under development on this topic too.

### Learn a new programming language

Since most of my experience is with compiled languages such as C, C++ and Java, I decided when I finished my last contract I would learn a scripting language. PHP was an obvious choice. Given my interest in writing, learning PHP would allow me to do some WordPress hacking as well.

The project I chose was to modify an existing WordPress plugin for formatting reviews such that it could be used for formatting recipes. The programming has been interesting and rewarding, the hRecipe project has been successful in driving considerable traffic to There Is NO Box. It’s one of the most popular parts of the website, and I look forward to doing considerably more work on the hRecipe project.

It’s worked well for me: I have learned some PHP, and a little Javascript too, while working on my scripting language project.

Learning something new will work well for you, just jump in a go for it! If all you know is scripting, try out a compiled language like Java. It will give a brand new, and valuable, perspective on programming.

### Bring some old code up to date

If you have been seriously programming for any length of time, you’re going to have to code in various states of maintenance. I look back over years and years of programming, and see code dating from the 1990s! Some of this code still interests me. Some of it’s still good. A big chunk of it could be brought up to date with relatively little effort. Build environments and Makefiles need to be updated. Dangerous or stupid code replaced. Poorly written sections rewritten.

Even better, some of this old sludge could be distilled into shiny new code, by porting to new, more modern languages. For example, I have a circular histogram generator written in C, which would be nice to have in Javascript for generating rose diagrams on the fly.

What do you have laying around that would be fun, educational and perhaps profitable to port forward?

Do some excavation in your code archives, let us know what you find!

### Learn to be an MS Office Power User

Suppose you have no programming experience.

This isn’t necessarily a problem. You are using your computer for something besides surfing the internet, right?

Even if you mainly use computers for writing letters in Microsoft Word, or for computing your budget in Excel, or perhaps you’re a graphic designer using Photoshop, whatever application you use almost positively can be programmed with scripts. For example, all of Microsoft’s products can be programmed using built-in macros or Visual Basic. And surely there was something you could have automated at your last job, if you had only had the time.

Now is that time!

If you end up starting your own business, any office automation skills you learn are going to pay you back in spades.

## Ultra-Fast Startup Technology: Bootstrap with free or cheap web applications

So you wanna “do a startup?”

Ok, no problem.

I’ve done a couple myself, and it’s fun. Addictive, even.

Now is a great time, too. Turns out bootstrapping your software startup is dirt cheap compared to what it used to cost 10 years ago. The web has really changed everything: you can handle a vast amount of your business infrastructure on line now.

Any business is going to need some infrastructure, and software startups will need a variety of specialized tools for software development. There’s three main strategies for bootstrapping:

1. Totally free and open source software development to leverage totally free environments such as Sourceforge.
2. Self-hosting your entire project, either with an internal server or purchased server space elsewhere.
3. Mixture of self-hosted and inexpensively purchased services.

If you want a totally free software solution, in the last few years Sourceforge has radically improved it’s offerings. However, freely available Sourceforge accounts require your source code to comply with an Open Source license. If you have need to keep your source code private, you will need to find a different solution.

### Host everything yourself

Another option is self-hosting everything you need, ranging from Sugar CRM, Google Documents, through dotproject, wikimedia, a WordPress, Drupal or Joomla driven website, and much, much more.

The primary disadvantage of going completely self-hosting is that someone will have be take charge as a systems administrator. And that’s not a job too many developers or startup founders are going to willingly assume. It’s thankless and tedious. Nobody cares as along as everything “just works,” but people become irate when it doesn’t!

### Split the difference: self-host and outsource

In my opinion, based on experience, the best option for a small startup is a mixture of freely available, self-hosted, and free or inexpensive web applications.

1. wush.net: $15/month, the best money you will ever spend. You get a subversion repository with integrated Trac, 2G of disk space, unlimited users and excellent customer service. It’s a very good deal. Wush.net doesn’t have an affiliate program, but I like their service so well I won’t promote anyone else. 2. 37 Signals Basecamp: I’ve used Basecamp for a couple of years. Projects always seem to go smoother with Basecamp than without it. If you are purely developers, you may find Basecamp redundant with Trac capabilities, but the moment you add someone not comfortable with hard coding Trac wiki syntax, your team will be more productive moving all non-coding task management to Basecamp projects. (I know this from experience.) Best of all, you can start with a free account, and if you like it, an account upgrade to a small startup level (15 people) are very reasonably priced at$24 per month.
3. WordPress: Host this yourself, use it to build out your company web site. Even if all you want is a static web site, WordPress gets you a lot of bang for your buck. Easy installation, easy maintenance, easy to add material, all your SEO is magically handled because WordPress is so well structured. Check out Website In A Weekend for more information.
4. Google Documents: free, easy, with just enough power to get a startup well underway. Google Docs is my “goto” solution when I need to collaborate on documentation. It’s much easier to collaborate using Google Docs than with MS Word; merges are easier being handled real time, and you don’t have to worry about passing documents around, and who has the latest or most correct version.

### Scaling up

All of the tools I recommend scale well. What’s of more importance is that you plan to for scaling with your business practices. No matter what, if you want to scale, process matters.

And the bigger you get, the more it matters.

Creating task lists to accomplish project requirements should be a given. But there’s more that you can do to get a grip on your processes. For example:

1. Make a practice to keep a log of what got done and when. Checking off tasks is only half of the battle. There’s always crises that pop up unexpectedly. Keep a daily a log that keeps track of these as well.
2. Consider tracking your time very tightly. Use a web application such as Clicktime which will let you connect your task list to the appropriate Quickbooks accounts. Once a month, go back over your time sheets and do a little post-mortem analysis. Did your forecasts pan out? Or did you seriously under- or over-estimate how long tasks would require for completion? Use this information to calibrate forward for the following month’s tasking.

These techniques are probably overkill for very small startups… but get in the habit now, you won’t have to worry about it when you’re under the gun later.

## Two Traps For The Technicals

I just left the following as a comment on Mark Cuban’s Blog Maverick, as a response to his article on success and motivation.

Many of us on the technical side have a build it and they will come mentality. This works once in a while. But not very often. It´s probably a result of sticking with what one knows best, rather sticking your neck in areas you don’t understand. In my case it’s sales and marketing.

Another trap for the technically-minded is the notion of being so good at what you do, you don´t have to look for work. The work finds you. This is true for an extremely small number of people… who, perhaps not coincidentally, are extremely well-networked. The networking, essentially, is the sales and marketing.

I’d also like to take this opportunity to introduce to you the new noun “technicals.” Ordinarily I loathe nounification (and verbification) with a white-heat passion. But with all this bandying about of the word “creatives” I have to admit… I’ve been feeling left out and lonely… isn’t programming creative? I think it is. No, I feel that it is. Sadly, those who cannot program don’t feel the same way. Since I can’t be a “creative,” I’ll just have to be a “technical.” Booyah.

## Going fractal

IBM lost a sale of PurifyPlus this morning.

What happened is I got into a process I call “going fractal.”

This is where doing something that ought to be fairly straightforward (spending \$1500) becomes nightmarishly complex as you have to jump through hoops of ever-increasing complexity taking you further and further away from whatever it is you wanted to get done.

In the linux world, this happens when you have a “pressing need” for new trivial feature X in special program Y, which in the end turns out to require recompiling the kernel.

In this case, once I was shunted into IBM’s explanation of corporate ID management strategy, I realized IBM was going to make it difficult for me to purchase. While this is a tried and true strategy (prescreen customers by requiring large time, and if, possible emotional commitment), I just have other things to do this morning. I’m not on salary after all.

In any case, I’ll probably pick it up before Jan 1, but it’s still annoying to have to “task out” a purchase in stages.

## Accurate, easy labeling for LaTeX references

Consider the following:

begin{figure}
begin{center}
includegraphics[width=4.5in]{figs/flight_setheight.pdf}
caption{Computational flow for computing stairs.
This case represents the actions on
creating a new flight of stairs.}
label{fig:flight_setheight}
end{center}
end{figure}


#### Bit literate labels

The \label command is one key: it reflects the file name of the figure that will be displayed.

The other key is that the figure is named semantically. At the moment, this figure is displayed as “Figure 5,” which I am sure is going to change as I add figures to the document. With a semantically meaningful figure name, I can easily label and reference the figure without engaging in wasteful calisthentics of manual counting, grepping through the document, etc.

#### Tables, chapters, sections, etc.

I do it this way:

label{tab:some_table}
label{sec:some_section}
label{chap:some_chapter}


You get the idea.