While I'm learning new things, I like to draw parallels to seemingly unrelated stuff I already know, to help my brain store the information. Today: the similarities I see between programming best practices and the konMari method of tidying.

Over the last few days, I've been diving deeper into the fundamentals of Javascript programming, following this course by Jonas Schmedtmann (highly recommended). It's been quite humbling to say the least. Because even after having completed quite a few projects on my own, I'm still finding lots of things I didn't know in the basics section of this course.

Other than new features of the Javascript programming language, I'm also learning about best practices to structure code in such a way that it's more readable and easier to understand.

You may think: why does that matter? Does the computer care if things are tidy?

No, of course, a computer doesn't care about anything. But you don't only write code so the computer can read it. Human readability is also incredibly important for maintenance and bug fixing purposes.

This shirt has good advice

So it's always good practice to tidy up your code to optimize its readability. And it's in this tidying that I see similarities with the KonMari method of tidying.

1) Decluttering

The way the Marie Kondo (KonMari) tidying process starts, is that you first make a giant pile of all the clothes you have, on the bed for example, so that you can see just how much you really have.

Then, you go through each and every item, asking yourself: does this spark joy? If the answer is yes, you keep the thing. If the answer is no, you thank the item for its service in the past, and then get rid of it.

The point of this is that you only keep the stuff you're actually going to use. Ask yourself if you really have a use for this item. Maybe you already have a similar item that provides the same function. If so, keep only one. No keeping stuff around for just in case. That's how you get cluttered. Keep only the stuff that is going to be part of your future.

In a similar way, in coding, you need to keep your code DRY.
DRY is an acronym for Don't Repeat Yourself.

You need to go through your code to see if you're not using the same code in multiple places; and if you are, then you need to refactor: abstracting this code away using functions or variables, so that you can use those instead.

This process is referred to as drying up your code; and it will make sure that you don't end up with a cluttered code base full of unnecessary code.

2) Everything has its own home

If you're doing the KonMari method, you need to tidy up in such a way that you can also find your stuff back. For this reason each item needs to have its own 'home'.

This means that similar things need to be stored in the same place, and each item's home needs to be as specific as possible. No catch-all receptacles.

Marie Kondo herself is famous for bringing all kinds of little boxes and other receptacles in different sizes, so that even the smallest things can have their own little place to be stored away.

I see a lot of parallels here with object-oriented programming.

Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects", which can contain data, in the form of fields (often known as attributes), and code, in the form of procedures (often known as methods).

OOP is a way of programming where the data and the functions that are performed on that data are contained in the same data structure: the object. If this sounds a little abstract, here's an example:

var familyJohn = {
  bills: [124, 48, 268, 180, 42],
  tips: [],
  finalPay: [],
  calcTips: function(){
    for (var i = 0; i < this.bills.length; i++) {
      var tip
      var bill = this.bills[i]
      if (bill < 50) {
        tip = bill * .2
      } else {
        tip = bill * .15
      this.finalPay.push(bill + tip)

In this example, John's family is visiting a few restaurants. The object features an array bills that contains all the bills they have to pay for their meals (the data) and also a method calcTips that contains the code to calculate how much John is going to tip on each meal, filling the tips and finalPay arrays with the calculated amounts.

It may seem obvious to structure code like this, but it's really not. If you opted to store all the data in a separate data variable and all the functions as their own variables in some dedicated file full of functions, the code would still work perfectly fine.

So object-oriented programming like this, choosing to organize code around its function, rather than type, is an architectural choice and best practice, to clean up your code. OOP makes it easier to understand what's happening, because it's all in the same place.

This ties in well with the last point:

3) Order at first glance

One of the staples of the KonMari method of tidying is storing things in such a way that you can see everything at once. So you don't stack your folded shirts in a drawer anymore. Instead, you fold them in such a way that they can stand up by themselves, vertically.

Tossing your shirts on top of the pile of folded shirts is of course a lot easier, but using the KonMari method guarantees that you can actually see all the clothes you have at first glance, after opening the drawer.

KonMari way of storing shirts in a drawer

In a similar way it's good practice to write and structure you code in such a way that you can immediately see what's going on. A big part of this is naming your variables.

It may be quicker to just call that helper variable in your function foo instead of sumOfQuarterlyAverages, but you will always find that the few seconds you saved there are not worth it. In the long run, you or somebody else is going to read your code and take forever to figure out what it's doing.

One of the companies I work with actually has the policy that there should be no comments in the code. The reasoning here is that if you need comments to explain what the code is doing, the variables have apparently not been named correctly.

There you have it, the similarities between coding best practices and the KonMari method of tidying. Keep your code dry, organize code around its function and name your variables for readability.

If you do this, you will end up with concise, elegant code that will definitely spark joy in everyone who sees it.