
I know how to make and sell software online, and I can share my tips
with you.
Email
|
Twitter
|
LinkedIn
|
Comics
|
All articles
The simple and obvious way to walk through a graph
Posted 13 years ago
At some point in your programming career you may have to go through a graph of items and process them all exactly once. If you keep following neighbours, the path might loop back on itself, so you need to keep track of which ones have been processed already.
function process_node( node )
{
if ( node.processed ) {
return;
}
node.processed = true;
var i;
for( i = 0; i < node.neighbours.length; i++ ) {
process_node( node.neighbours[i] );
}
// code to process the node goes here. It is executed only
// once per node.
}
The code works, but it only works once! The next time you try it, all the nodes will already be marked as processed, so nothing will happen.
Here's a neat trick that elegantly solves the problem. Instead of using a boolean flag in each node, use a generation count.
var CurrentGeneration = 0;
function process_node( node )
{
if ( node.generation == CurrentGeneration ) {
return;
}
node.generation = CurrentGeneration;
var i;
for( i = 0; i < node.neighbours.length; i++ ) {
process_node( node.neighbours[i] );
}
// code to process the node goes here. It is executed only
// once per node.
}
function process_all_nodes( root )
{
CurrentGeneration += 1;
process_node( root );
}
It's simple and obvious right? So why didn't I think of it in eight years?
How QBASIC almost got me killed

The day arrived when my project was ready to be unleashed upon the world. I waited until the teacher was hovering nearby and then I started my application, running the FORMAT command on the network drive. Some classmates were watching the screen and she hurried over to see what all the fuss was about.
UMA's dirty secrets
Recently, many carriers have started offering UMA, or WiFi phones. These are cell phones with WiFi capabilites. Don't be fooled -- you won't be able to get free calls and run skype on them. The UMA technology is meant to extend the carrier's cellular network into your home using your broadband internet connection.
Bending over: How to sell your software to large companies

For a micro-ISV, selling to businesses can be more lucrative than selling to consumers. Instead of making a few dollars per sale and hoping for thousands of sales, you sell to only a few customers, and charge much higher rates. But the rates are high for a reason. It takes more time and money to sell to businesses.
Coding tips they don't teach you in school

Some time-saving shortcuts for C code that will make your coworkers scream. In Awe.
Finding great ideas for your startup

"I just don't have any ideas." This is the #1 stumbling block for budding entrepreneurs. Here are a few techniques to get the creative juices flowing.
Why Perforce is more scalable than Git
Branching on Perforce is kind of like performing open heart surgery. But here's why git can't hope to compete with it.
Why don't web browsers do this?

Why don't web pages start as fast as this computer from 1984?
How wide should you make your web page?

Based on 22500 unique IP addresses over the past week.
Comment spam defeated at last
For years when running this blog, I would have to log in each day and delete a dozen comments due to spam. This was a chore, and
I tried many ways to stem the tide.
Rules for Effective C++
The rules for safe C++ code are surprisingly controversial.