Cooking Up Code: A Beginner's Guide to Programming Concepts
Published: 2023-06-28 10:09:44

Are there any parallels between cooking and programming? On the surface, they may seem vastly different, but in reality, they share many similarities.

The basic concept of programming revolves around defining a set of instructions to complete some specific task. As simple as it sounds, these basic principles have helped NASA launch rockets into space, automobile companies have created self-driving cars, and biomedical engineers have developed nanobots that help increase the success rate of critical surgeries - the list is endless!

A century ago, humans didn't bother about the internet, TikTok, or smart AI Chatbots that could write poems and create art. Today, we live and breathe tech, and these computer programs have become interwoven in our daily lifestyle that it's almost impossible to imagine life without them.

In this article, we will explore the basic concepts of programming and relate them to the art of cooking, using the metaphor of culinary arts to make the complex world of coding more relatable and understandable. So, put on your chef's hat and get ready to cook up some code!

## Programming Is Like Following a Recipe

Just as a recipe is a set of instructions that, when followed correctly, results in a delicious dish; similarly, a computer program is a set of instructions that, when executed correctly, results in a specific task or computation.

If a cook forgets an ingredient or skips a step in a recipe, the dish may not turn out as desired. Likewise, if a programmer makes a mistake in their code, the program may not perform as intended.

For example, let's consider a simple set of programming instructions written in python:
```python
# Guess a number

print("Guess the secret number: ")
secret_number = '3'
    
while True:
    user_guess = input()
    if user_guess == secret_number:
        print("You guessed right!")
        break
    else:
        print("Try again...")
```
Python is a programming language that finds its application in several fields. The execution of a python program typically starts from the top and executes line by line until the end of the program is reached (or it encounters an interrupt that stops the flow).

By merely looking at the block of code above, you can somewhat make an educated guess of what the program is trying to achieve. Let's break it down.

### Code breakdown:
The first line prints the string in quotation marks to the screen, prompting the user to guess the secret number.
Then, we set the secret number to '3' (hoping the user makes the right guess). The third line declares a never-ending loop with a while statement which allows the program to continue receiving input from the user until the secret number is guessed.

Next, we prompt the user to input a number, then, the numbers are compared. If they are the same, the program prints "You guessed right!" and the program escapes the infinite loop using the break statement. If the guess was wrong, the program takes input until the correct number is provided.

**Output:**
```bash
Guess the secret number:
1
Try again...
2
Try again...
3
You guessed right!
```

We'll use this piece of code throughout this article to explain other essential programming concepts like variables, conditional statements, flow control, and functions in programming (with a slight modification of the program).

## Variables Are Like Ingredients
In cooking, ingredients like flour, sugar, and eggs naturally store or possess unique qualities that can be used to make a cake or other foods, depending on how you combine them.

Likewise, in programming, variables are used to store data that are crucial in the execution of a program. For example, a variable can be used to store a person's name, age, or address.

Just as ingredients can be combined and used in different ways to create different dishes, variables can also be manipulated and used in different ways in a program. A cook might use flour, sugar, and eggs to make a cake, but also use the same ingredients to make cookies or bread.

Similarly, a programmer might use a variable to store a person's name in one part of a program, and then use that same variable to store their age in another part of the program.

It's also worth mentioning that ingredients have different properties, such as sugar being sweet or flour being a thickening agent, and some ingredients are used in small quantities and some are used in large quantities. Similarly, variables in programming also have different properties. Some are used in small quantities and some are used in large quantities.

Our guessing program used variables 'secret_number' and 'user_guess' to store the secret number we chose and the number the user guessed, respectively, which were later compared. In this case, these variables were used to store string data types. Variables and data types go hand-in-hand, you can follow the link to learn more about data types.
```python
...
user_guess = input()
    if user_guess == secret_number:
...
```
A programmer can utilize several variables to create various programs, just as a cook can use various ingredients to create different dishes. Additionally, a chef might experiment with various ingredients to create new dishes, also, a programmer can experiment with different variables to create new programs.

## Conditional Statements Are Like Taste Testing

Conditional statements are like taste testing in the sense that they both allow for decisions to be made based on certain conditions. While cooking, you may taste-test a dish to determine if it needs more seasoning.

In like manner, conditional statements, in programming, are used to make decisions based on certain conditions. For example, a program may check if a user's input is a number, if a file exists or if a certain condition is met.

Just as a cook may add more salt or pepper to a dish based on their taste testing, a program can execute different instructions based on the outcome of a conditional statement.

For example, if a user enters a correct password, the program may proceed to the next step, but if the user enters an incorrect password, the program may display an error message.

This allows for greater flexibility and adaptability in both cooking and programming. In cooking, a cook can adjust the seasoning of a dish based on personal preference, or to suit different dietary restrictions. 

Similarly, in programming, a program can adapt to different inputs, or change its behavior based on the outcome of a conditional statement.
```python
...
if user_guess == secret_number:
        print("You guessed right!")
        break
    else:
        print("Try again...")
```

The number of keywords available for conditional statements depends on the programming language. Some languages like C and JavaScript feature Switch statements in addition to the more common if and else statements.

Furthermore, Conditional statements in programming also allow for the creation of branches and loops, giving the programmer the ability to change the flow of the program, which is essential for creating complex and dynamic applications. 

In the same way, a cook can make multiple variations of a dish by adjusting the recipe accordingly, a programmer can create multiple outcomes by adjusting the code accordingly.

## Loops Are Like Mass Production
Similar to mass production methods, loops allow a set of instruction(s) to be executed multiple times. For example, our guessing game sets up a while loop that prompts the user to keep guessing until the secret number is guessed.

```python
while True:
    user_guess = input()
    if user_guess == secret_number:
        print("You guessed right!")
        break
    else:
        print("Try again...")
```
Moreover, a program might use a loop to display a list of items or to perform a calculation multiple times.
Furthermore, mass-production techniques in a kitchen ensure consistency in the dishes produced. 

Similarly, loops in programming ensure that the same set of instructions is executed multiple times in the same way, which helps to maintain consistency in the results obtained.

Also,loops in programming can also be nested, where one loop can be inside another, this produces even more complex and dynamic operations, similar to how a kitchen can have multiple stations, each with its specialized tasks, that work together to produce a final dish.

## Functions Are Like Specialized Tools

In a kitchen, specialized tools like a mixer, a food processor, or a whisk are used to organize and compartmentalize specific tasks. Likewise, in programming, functions are used to organize and compartmentalize a program by breaking it down into smaller, reusable chunks of code that play a preset role in the program.

To illustrate the use of functions, we've modified our guessing program to include a function:
```python
# Guess a number

print("Guess the secret number: ")
secret_number = '3'

def try_again():
    print("You guessed wrong! Try again...")
    
while True:
    user_guess = input()
    if user_guess == secret_number:
        print("You guessed right!")
        break
    else:
        try_again()
```
If you examine the program, you'd notice two lines of code have been added. The first line; "def try_again()", is a python declarative that introduces a function named "try_again" in our program. The specific method of declaring a function depends on the programming language.

The function we have defined performs one specific task. It prints "You guessed wrong! Try again…", where ever it is called in our program. 

In this case, we've called our function just after the else statement because we want our program to print a preset text to the screen whenever the user enters the wrong number.
A function can be called multiple times throughout a program to perform the same task. This allows for reusability in both cooking and programming.

Furthermore, specialized tools in a kitchen can be used in combination with other tools to create different dishes.

Comparably, functions in programming can be used in combination with other functions and code to create different programs. This introduces modularity programming. If a section of your code fails, you can easily spot the function causing the problem and fix it without having to examine the entire code.

It's also worth mentioning that functions in programming can take inputs and return outputs, similar to how specialized tools in a kitchen take ingredients and produce dishes. Programmers can use this to create more complex and dynamic operations.

## Wrapping Up

Although programming may seem like a complicated activity only understood by a select few, we've been able to see that it's not the case. The art of programming finds similarities in many activities we indulge in daily.

It's important to note that while the comparison between cooking and programming is not direct, it serves as a way to help understand and grasp the concepts that can be difficult to understand at first glance.

We've been able to highlight the similarities between these two fields and how they are both based on following instructions, using logic and some problem solving techniques to achieve a desired outcome. Readers are also encouraged to consider a career in programming or to pursue it as a hobby.