Cooking became one of my passions through necessity. Eating out can get expensive – fast! It was not until recently that I realized coding and cooking have a lot in common.

MacGyver it

It has been two weeks since you last went food shopping. You find some leftover rice and chicken in the fridge along with some tortillas and diced tomatoes in the pantry. Rather than cook chicken again, use the leftovers to make a burrito.

In coding, this principle is called DRY (Don’t repeat yourself) and is aimed at reducing repetition. The opposite of DRY is WET (Write everything twice) which is messy and wasteful.

Don’t reinvent the wheel

Jarred pizza sauce, ready made pizza crust, grated cheese and sliced pepperoni. These items aren’t tough to make from scratch, but they can be an excellent time saver on a weeknight. Of course, you won’t get the satisfaction that comes from curing your own pepperoni, but there’s somebody else out there who already did it for you.

In coding, this concept is known a package. A package is a bundle of code that meets a specific need. Faker, for example, generates fake data for testing and seeding. These packages are typically installed by a package manager.

There’s more than one way to skin a cat

There are multiple ways to do something. You can use a garlic press, food chopper or chef’s knife on a garlic clove. There are trade-offs: the knife will take slightly longer than the chopper, but the knife takes less time to clean.

Similarly, in programming, you could test a condition with one line of code versus four, but that one line may be more difficult to understand at a glance. Fewer lines are not synonymous with more efficient.

Break it up into small tasks

Recipes are usually broken down into sections such as ingredients, preparation, and cooking. First, you gather all the necessary ingredients, mix them together in a bowl and toss them into a skillet. You may also taste your concoction to see if needs some more salt.

In programming, you code the smallest task at a time and incorporate feedback in order to change requirements. For example, when reading a file, you first have to verify the file exists and open it. While reading lines from the file you also have to continuously check that you have not reached the end of the file.

A sense of community

Cooking with another person can be very helpful. They can chop garlic while you are peeling potatoes. Having an extra set of hands is not only useful, but you can also learn from one another.

The same is true for coding. Peer review of code can help improve code quality and potentially present a different way of accomplishing the same task in a more efficient way.

The finished product

There is nothing quite as satisfying as sinking your teeth into a delicious meal that you prepared from scratch.

The same can be said for coding…

how-i-feel-when-my-code-works