DEV Community

Cover image for Comedy Techniques Every Programmer Should Know
Vieri Lusen
Vieri Lusen

Posted on

Comedy Techniques Every Programmer Should Know

Comedy and programming may seem like two very different fields. However, like with anything else, as programmers, we can also learn from seemingly unrelated fields, such as comedy.

From comedy, we can learn some techniques that are also applicable in the world of programming, and these techniques have actually been used by programmers and can be applied in application development.

Rule of Three

In comedy, the Rule of Three is a technique where a punchline or tagline is repeated three times, making the joke more effective and memorable.

In programming, the Rule of Three refers to a principle that states that if a piece of code is used three or more times in a program, it's better to refactor that code into a separate function or method.

Before applying the Rule of Three, let's say we have three functions with the same logic for calculating the volume of a rectangular object. We can apply the rule of three by refactoring the logic into a separate reusable function:

// Object 1
let lengthObject1: number = 10.0;
let widthObject1: number = 5.0;
let heightObject1: number = 2.5;
let volumeObject1: number = lengthObject1 * widthObject1 * heightObject1;
console.log("Volume of object 1 is:", volumeObject1);

// Object 2
let lengthObject2: number = 2.0;
let widthObject2: number = 8.0;
let heightObject2: number = 3.5;
let volumeObject2: number = lengthObject2 * widthObject2 * heightObject2;
console.log("Volume of object 2 is:", volumeObject2);

// Object 3
let lengthObject3: number = 15.0;
let widthObject3: number = 3.0;
let heightObject3: number = 2.5;
let volumeObject3: number = lengthObject3 * widthObject3 * heightObject3;
console.log("Volume of object 3 is:", volumeObject3);
Enter fullscreen mode Exit fullscreen mode

After separating the logic into a separate function, we can use these functions more easily and efficiently in our program:

function calculateVolume(length: number, width: number, height: number): number {
   return length * width * height;
}

// Object 1
let lengthObject1: number = 10.0;
let widthObject1: number = 5.0;
let heightObject1: number = 2.5;
const volumeObject1: number = calculateVolume(lengthObject1, widthObject1, heightObject1);
console.log("Volume of object 1 is:", volumeObject1);

// Object 2
let lengthObject2: number = 2.0;
let widthObject2: number = 8.0;
let heightObject2: number = 3.5;
const volumeObject2: number = calculateVolume(lengthObject2, widthObject2, heightObject2);
console.log("Volume of object 2 is:", volumeObject2);

// Object 3
let lengthObject3: number = 15.0;
let widthObject3: number = 3.0;
let heightObject3: number = 2.5;
const volumeObject3: number = calculateVolume(lengthObject3, widthObject3, heightObject3);
console.log("Volume of object 3 is:", volumeObject3);
Enter fullscreen mode Exit fullscreen mode

One Liner

In comedy, a one-liner is a joke delivered in a concise and short sentence. One-liners are usually compact, meaningful, and contain an element of surprise or confusion that triggers laughter from the audience.

In programming, the term "one-liner" refers to a coding technique where the code is written in a single line or a few lines that are concise and readable, without sacrificing code clarity and safety.

Here's an example of using a one-liner in an if-else conditional statement in Python:

#Before using a one-liner
x = 10
y = 5
if x > y:
  print("x is greater than y")
else:
  print("y is greater than x")

#After using a one-liner
x = 10
y = 5
print("x is greater than y" if x > y else "y is greater than x")
Enter fullscreen mode Exit fullscreen mode

However, the use of one-liners should be done carefully and only in specific situations, especially if the code needs to be accessed and modified by others in the future.

Callback

In comedy, a callback is a technique where a comedian refers back to a previous joke or punchline in their performance to create a new and funnier punchline. It is done to strengthen the impact of the previous joke and create a stronger effect on the audience.

In programming, a callback is a piece of code (usually a function, but not always) that is passed as an argument to another function and then invoked by that function when a certain task is completed. With callbacks, we can perform an action or run code after a specific task is finished by another function.

Callback is often used in JavaScript because JavaScript is event-driven, meaning that many tasks performed by JavaScript need to wait for events to occur, such as server requests or user interactions with a web page. In this case, callbacks are used to handle those events when they happen, allowing JavaScript to continue running without waiting for tasks that are waiting for events to complete.

Here's an example of a callback in a simple function:

function greeting(name, callback) {
   console.log("Hello, " + name);
   callback();
}

function sayGoodbye() {
   console.log("Goodbye!");
}

greeting("John", sayGoodbye);
Enter fullscreen mode Exit fullscreen mode

In the example above, the callback parameter is sayGoodbye(). Therefore, the sayGoodbye() function will be executed after the greeting() function finishes executing. The output will be as follows:

Hello, John
Goodbye!
Enter fullscreen mode Exit fullscreen mode

Another example of using a callback in JavaScript is handling the response of a server request using the fetch() method:

fetch('https://www.example.com/data')
   .then(response => response.json())
   .then(data => console.log(data))
   .catch(error => console.log(error));
Enter fullscreen mode Exit fullscreen mode

In the above example, callbacks are used in the .then() method to handle the response from a request made with the fetch() method. After the request is successfully made, the callback will be invoked to handle the received response from the server. Additionally, if there's an error in the request, the callback in the .catch() method will be called to handle the error. By using callbacks, we can effectively and structurally handle response results from requests.

Conclusion

Although they may seem very different, comedy and programming have similarities in some techniques that can be learned and applied by programmers in application development. By observing and understanding techniques from different fields, programmers can expand their problem-solving skills and improve the quality and effectiveness of their code.

This article is translated using AI from the original article written in Indonesian language : Medium

Top comments (0)