DEV Community

Josh Lee
Josh Lee

Posted on

How to Use Your "Brain RAM" to Write Better Software

Short term memory is a lot like computer RAM. Here's why that's important to writing good software…

A brain has two types of memory: short-term memory and long-term memory.

Long-term memory is similar to a hard drive while short-term memory is like RAM.

More on exactly why that's important for software developers in a second….

When focusing on something, we typically use our short term memory by loading up the information in our head, using it, and then quickly discarding it.

But most people's short-term memory isn't really that great…

In fact, the average person can only hold about 5 to 7 items in their short term memory. We often use a technique called 'chunking' to hold more information. So instead of remembering a phone number like 5015551234 (10 items) we remember it as [501]-[555]-[12]-34

And when writing code, this is much more important than you'd initially think.

Let's say we're writing code for a shopping cart and store items in a nested array like so:

cart = [ ['apples', 2], ['bananas', 3], ['cake', 2]]
Enter fullscreen mode Exit fullscreen mode

When using that code, we have an extra mental step of converting indexes into what they represent. So, while we're coding we may think "Grab the first item and access the value at index 0 to get the product name" when reading cart[0][0].

Instead, if we wrote out that same code as:

cart = [ { name: 'apples', quantity: 2}, { name: 'bananas', quantity: 3}, {name: 'cake', quantity: 2}]
Enter fullscreen mode Exit fullscreen mode

When we read code like cart[0][:name], our internal voice is saying "Grab the first item and get the name."

Now that might not seem like much in this example, but imagine you're also have to keep up with other items like remembering the q variable means "quantity" instead of just using "quantity" for the variable name, you don't remember what method "calcAv" does, and you're not sure if you're expecting to return a float or an integer.

Am I saying you need to name methods like 'sumUpAllTheItemsInTheCart'? No, but writing cleaner code instead of trying to write smart one-liners can make your software much more maintainable.

Top comments (0)