DEV Community

Cover image for AYDT - 02 ( Are You Doing This ❓ ) - One step towards Ninja Land.
Karthikeyan Dhanapal
Karthikeyan Dhanapal

Posted on • Edited on

AYDT - 02 ( Are You Doing This ❓ ) - One step towards Ninja Land.

There's this famous Yoda quote,

yoda quote

Take away from this quote for a developer, I would say: "Practice a language until you no longer have to think twice before coding in it."

Before moving on to this week's AYDT, there's a gentle reminder. This is a part of "Are You Doing This?" series and I would highly recommend you to have a look at the previous articles to learn more. Happy Coding 👨🏻‍💻

Previous week link :
https://dev.to/chintukarthi/aydt-01-are-you-doing-this-one-step-towards-ninja-land-3eb8

With this quote turning the fiery phoenix 🔥 from within you let's look into this week's AYDT!!

What are we waiting for? Let's dive in 🏄🏻‍♂️.

Are You Doing This❓- Self Explanatory Functions!

Writing a function is cool. We can call it across the file or even globally depending on the necessity. But, there's a catch. Most of us write comments either above or within the function for the developers or anyone who would likely to have a look at it in the future.

Even though writing comments is a good practice, but comments alone won't make your function more readable.

Let's a have look at the below function.

# This function performs the addition of two numbers.

function add(a, b) {
  sum = a+b;
  return sum
}
Enter fullscreen mode Exit fullscreen mode

Nothing fishy? Hmm, 🤔 Let's have a look at another function.
Assume that "fibonacci" is a function that is being called within another function called "fib".

# This function gets two input a and b and performs addition operation. 
# After Addition the sum is used to find the fibonacci series

function fib(a, b) {
  sum = a+b;
  fibonacci = fibonacci(sum); 
}
Enter fullscreen mode Exit fullscreen mode

Found something already? Yes. This code sucks. 😖

But for beginners, this won't ring any bell and most of you will think that it is doing exactly what it was expected to do. And that's alright.

But why this isn't a good practice?

Because when it comes to coding, we should not only look at the functionality but also the code readability.

This is where most of the beginners make mistakes. So did I.

While writing a function have this in mind.

"No one has time to read your lines and lines of mumbo jumbo comments."

So what can you do in order to make your function more developer friendly?
You should write a function in such a way that, even after years when a developer looks at your code they should understand the functionality without looking at the comments and that's what we call as 'Self Explanatory Functions'

So how does a self-explanatory function should look like?

It doesn't have a standard. Do your best to make the function explain itself than trying to explain it in the comments.

I can provide with some examples of self-explanatory functions below.

Be Warned:
These examples aren't some kind of ideal scenarios.
The functions mentioned here are based on javascript.

Here you go,

1) The first function can be written more like this :

# Performs addition operation.

function addition(variable_1, variable_2) {
  sum_of_variables = variable_1 + variable_2;
  return sum_of_variables;
}
Enter fullscreen mode Exit fullscreen mode

You may find that this new function looks more complicated than the previous one. But trust me, it isn't. Just because the words increases it doesn't mean the code is complicated.

But on the bright side, "It's much more readable now and self-explanatory than the previous one!"

2) The second function can be written like this :

# Performs addition operation followed by finding fibonacci of the resulted sum.

function addition_with_fibonacci(variable_1, variable_2) {
  sum_of_variables = variable_1 + variable_2;
  fibonacci_series = find_fibonacci_series(sum_of_variables);
  return fibonacci_series;
}
Enter fullscreen mode Exit fullscreen mode

These are some example of how your function can be a self-explanatory one. If you came across or knew a better version of it, please let us know in the comments section. After all, I am one among the fellow developers who love to learn new things every day.

Bonus tip:

Use snack case instead of camel case for naming convention whenever and wherever needed in a function, just like I did it in the above functions.

If you want to know more about snake case and camel case and their use cases in a better way, please wait till next week. 🤪

So that's a wrap for this week. See you people around 🧙🏻‍♂️.

Cover image courtesy: https://www.codeninja.com.sg/

Top comments (0)