Essay 47 Code Is the Ultimate Junior Developer

With the advantages we’re afforded in our medium, it’s quick to forget how remarkable code is.

For the time being, forget about the latest jQuery plug-in or Rails patch. Instead, imagine if the great mathematician, Carl Frederick Gauss, had the benefit of a programming language at his disposal back in the 18th century.

What Gauss Could’ve Done with Code

As a famous story goes, one day in grade school, Gauss’s notoriously lazy teacher had the entire class sum all the integers from 1 to 100 in hopes of keeping the class occupied for a long while. Much to the teacher’s chagrin, the young Gauss came back to his teacher after only a few moments with the right answer: 5,050.

How did he come up with the answer so quickly? If Gauss had the tools to program back then, he might have written some code like this to get to the answer:

 
public ​int​ sum_range_of_positive_integers_to_100()
 
{
 
int​ sum;
 
 
for​ (​int​ i = 1; i <= 100; i++)
 
{
 
sum += i;
 
}
 
 
return​ sum;
 
}

After some quick thinking, he might have decided to rewrite his program more generically, in case the teacher was to challenge him again with a different range of numbers:

 
public ​int​ sum_range_of_integers(​int​ first, ​int​ last)
 
{
 
if​ (last < first)
 
{
 
throw new Exception(​"Last must be larger than first!"​);
 
}
 
 
int​ sum;
 
 
for​ (​int​ i = first; i <= last; i++)
 
{
 
sum += i;
 
}
 
 
return​ sum;
 
}

Of course, young Gauss didn’t have such an option at the time. So, how did he get to the answer so quickly?

Rather than adding the numbers one at a time, he approached the problem in a far more clever way. Instead of summing the numbers linearly, he summed the remaining first and last numbers in the sequence instead, starting with 1 and 100, followed by 2 and 99, 3 and 98, and so forth.

 
(1 + 100) + (2 + 99) + ... + (49 + 52) + (50 + 51)

From here, a simple pattern emerges. It turns out there are 50 pairs of numbers that each sum up to the magic number, 101.

 
101 + 101 + 101 + ... + 101 + 101 + 101

The problem of summing all 100 numbers together was reduced to a simple multiplication equation:

 
50 * 101 = 5050

Gauss’s approach was a uniquely human one. He took a problem that appeared tedious at the surface and came up with an elegant way to solve it. He found a better heuristic instead of resorting to the tedious linear approach that most of his classmates would’ve taken. As it turns out, he fell upon a nifty little formula:

 
The sum of all integers from 1 to n = n * (n+1) / 2

How would our code approach this problem? The code written earlier in this chapter would have produced the same answer, only doing it the “brute-force” way—the exact way we told it to do it. Given how fast processors work these days, software running our code would have arrived at the answer much faster than even Gauss did, despite the inefficiencies in its approach.

However, at a certain point, Gauss’s approach would win out. If Gauss were asked to sum all the numbers from 1 to 4,000,000, sum_range_of_integers would take a lot longer to compute.

This means that, at some number, Gauss would likely have been able to beat the code to the answer, because although the prodigy cunningly knew that the answer was to evaluate one simple formula, our poor program would’ve executed it this way:

 
1 + 2 + 3 + .... + 3,999,998 + 3,999,999 + 4,000,000

The Attractive Qualities of Code

Gauss’s tale provides some interesting insight into how code solves problems differently from humans. Code thrives at the tedious stuff—at algorithmic, rules-based problem solving—far better than a human being. It doesn’t just thrive; it has all the ingredients of an incomparably productive and affordable junior developer.

Code Doesn’t Get Lazy

Code will never decide to just take a shortcut or find an easier way of doing things. The code snippet at the beginning of this chapter would sum up all 4,000,000 integers without deciding to just skip one. Code executes with impeccable precision.

Code Doesn’t Get Bored

Imagine writing a small program like this:

 
int​ x = 0;
 
 
while​ (x != x + 1)
 
{
 
// do nothing
 
}

As irrational as this task sounds, code will continue to crank at this nothingness forever, saved only by a runtime engine that would force an abort, sensing this loop would be going nowhere fast. Code doesn’t analyze the importance of a task. It has no interest in its own well-being. It simply executes whatever we tell it to do.

Code Doesn’t Forget

At the beginning of this chapter, we told the engine this:

“Whenever I tell you to sum_range_of_integers(1,100), create an integer called sum. Starting with the number 1, add the value to sum, and then increment the value by 1. Keep doing this until you’ve hit 100. Then, give me back the value of sum.”

Years later, I can go back to my program, call the method again, and expect the same result. Code doesn’t forget what it’s asked to do. Software systems, built upon thousands and thousands of lines of code, are equally adept. Code, no matter its volume, remembers what to do days, weeks, and years later. Could any human do the same?

Code Is Cheap

If a co-worker were asked to sit at his desk and sum all the numbers up from 1 to 4,000,000, we’d understand if he asked for money in return. Maybe we could work out some commission per summation agreement or agree to an hourly rate. Think back to Essay 8, The Perks Are in the Work . For highly tedious tasks like this one, we’re motivated by perks.

Fortunately, we’ve never taught code about money, market economies, or vacation homes. It performs for nothing in return. Once we’ve taught code something, we can take full advantage of it. There are no code-labor laws to get in our way.

Code Is Fast

Code executes at rates incomparable to the way we can finish tasks. It’s metered by the limitations of hardware, which will become less and less limiting over time. Humans are absolutely no match for speed.

What does this all mean? Imagine a Craigslist ad that went like this:

images/cl.png

Code is inarguably the greatest junior developer who ever lived. It is uniquely adept at tedious yet definable tasks. It never complains—unless our instructions don’t make sense. It’s cheap, fast, diligent, consistent, and unemotional. Many companies would hire a dozen such programmers with these qualities in a heartbeat.

The power of code is extraordinary.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.14.251.243