Resist the temptation to quickly find answers to coding software questions and instead invest the time in keeping those fundamentals sharp!

Right now, you’re probably shaking your head thinking, “I don’t ever need to know the complexity of merge sort for the kind of code I write.” That’s certainly true for the vast majority of developers but really misses the point.

Put it all in Perspective

Legend has it that Bill Gates once said, “640K ought to be enough for anybody” when speaking about the memory limitations of the IBM PC. Gates has always denied that he said it. But it is convenient to attribute the quote to him.

Clearly, 640K of memory was not enough for everybody and memory in modern computers is now measured in Gigabytes instead of Kilobytes, a million-fold increase. I claim that this increase in memory and CPU speed has allowed developers to become lazy and forgo writing efficient code.

You might respond with, “Who cares if my code has a two nested for loops and declares a new array within the inner loop, the compiler will optimize it!”

To which I would reply, that’s still lazy and some developers might not even realize that there is even a problem with writing such sub-optimal code, further eroding the fundamental grounds on which computer science was built.

I won’t bore you with all the details of loop unrolling and memory optimization. You know this. Instead, I created a list of things that I pulled from my old university textbooks that help reinforce fundamentals in the code I write.

Here’s the Primer

The table below – which can be found in “Cracking the Coding Interview” by Gayle Laakmann – provides a quick reference for topics worth reviewing if it’s been a while since you have cracked open a computer science textbook.Primer Table

Some Examples

My street cred would be severely diminished if I didn’t provide examples of bad fundamentals and their good counterparts. Below are some of the most common I see in my travels.

The code is written in C; however, many of the concepts also apply to Java, C#, and other modern programming languages. These examples are trivial in nature, but the mistakes are surprisingly not uncommon.

Loop Unrolling

Memory Leaks

Memory Leaks

Let’s Dive Deeper

Granted, the examples above are a bit trivial and don’t represent most of the problems a seasoned developer faces on a daily basic. So let’s explore some tougher issues.

Java String vs. StringBuilder

This one always surprises when I find it in code, since StringBuilder (or StringBuffer) is a pretty basic concept for Java developers.  Yet some developers either don’t know about it or have forgotten how expensive String copying can be.  The underlying concept here is that String is immutable; whereas, StringBuilder is mutable.JavaString vs. StringBuilder

Iteration vs. Recursion

For me, recursion is like an incantation from a wizard: clever, mysterious, and occasionally dangerous.  In the right situation, recursion turns ugly code into simple perfection.  At other times, recursion is wasteful and serves no purpose.  And once in a while, recursion can melt your app and result in the dreaded “stack overflow” when your code gets stuck and can’t escape from itself.  The key concept here is to know when to use it, and more importantly, when NOT to use it.Iteration vs. Recursion

Hashtable (or Hashmap)

Much like recursion, a hashtable is a clever way to solve SOME problems; but not all.  Hashtables work well when your data map easily onto a small, finite set of array indices.  The hashing function is also key to a good behaving (read efficient) hashtable.  If your data is made up of key-value-pairs (KVPs), then you should probably look into using a hashtable.Hashtable (Or Hashmap)

Back to the Basics

If you actually cracked open one of your old university textbooks, bravo! You will not be disappointed and this will pay dividends in your everyday coding efforts.

Here are some books that I keep handy when I’m looking for inspiration:

  • The Art of Computer Programming, 1: Fundamental Algorithms – Donald Knuth
  • The Art of Computer Programming, 3: Sorting and Searching – Donald Knuth
  • Introduction to Algorithms (CLRS) – Thomas Cormen, Charles Leiserson, Ronald Rivest, and Clifford Stein

But the best homage you can pay to this blog is to remind your fellow developers about fundamentals and why they still matter. And remember: think INSIDE the box before venturing outside!