DEV Community

Justice-hub
Justice-hub

Posted on

5 Tips For Writing Cleaner Code

When you're writing code, it's important to be mindful of your coding practices. This means taking the time to write clean, organized code that is easy for others to read and understand. Here are a five tips to help you get started:

1. Avoid Unnecessary Nesting

Nesting in code is something we do all the time, and although there's nothing inherently wrong with nesting, it can sometimes make code harder to read. One approach to help avoid this is to use the "Return Early" design pattern. It allows us to use the if statement as a guard clause to check for errors and return before executing any further code. It helps avoid the use of if/else and unnecessary nesting.

Like this:

  • Before
function deleteItem(item) {
  if (item != null) {
    console.log("Deleting item");
    item.delete();
  }
}
Enter fullscreen mode Exit fullscreen mode
  • After
function deleteItem(item) {
  if (item == null) return;

  console.log("Deleting item");
  item.delete();
}
Enter fullscreen mode Exit fullscreen mode

As you can see the second implementation is obviously cleaner, this approach can help make your code more linear, cleaner, and more readable. It's a simple technique that is easy to implement.

2. Object Destructuring For Function Parameters

Let's assume we have a function that takes an object as the parameter and performs some kind of operation on that object to return a new value. Without using object destructuring, we might get something like this:

// not so good
function getFullName(person) {
  const firstName = person.firstName;
  const lastName = person.lastName;
  return `${firstName} ${lastName}`;
}
Enter fullscreen mode Exit fullscreen mode

This implementation works just fine, but a better way to implement this is to use object destructuring. We can destruct the person object to get both the first name and last name in one line:

// better
function getFullName(person) {
  const { firstName, lastName } = person;
  return `${firstname} ${lastName}`;
}
Enter fullscreen mode Exit fullscreen mode

3. Using Pure Functions

Pure functions are a great way to write code that is easy to read and understand. By using pure functions, you can avoid creating complex and difficult-to-follow code. Pure functions always return the same result given the same input, which makes them predictable and reliable. Additionally, pure functions are easy to test and debug, making them ideal for use in software development projects.

// bad
let items = 5;
function changeNumber(number) {
  items = number + 3;
  return items;
}
changeNumber(5);
Enter fullscreen mode Exit fullscreen mode
// good
function addThree(number) {
  return number + 3;
}
Enter fullscreen mode Exit fullscreen mode

4. Keep Functions Simple

There is a lot of wisdom in keeping functions simple. When you keep your functions small and focused, it’s easier to understand what they do and how they work. This makes them less error-prone and more maintainable. Additionally, when you keep your code modular, it becomes easier to reuse individual functions in different contexts.

function signUpAndValidate() {
// Do a stuff here
}
Enter fullscreen mode Exit fullscreen mode

It is best to keep functions responsible for one thing only. This is a better approach:

function signUp() {
}
function validate() {
}
Enter fullscreen mode Exit fullscreen mode

In short, keeping your functions simple makes for better code that is easier to understand and maintain. So next time you are faced with the challenge of writing some code, remember to keep things simple!

5. Use Meaningful Variable Names

Use meaningful variable names. It makes your code more readable and easier to debug. For example, don't use x or y as variables; use something that describes what the variable is for, like currentWidth or inputValue.

// bad
let x = 0;
let y = 1;
Enter fullscreen mode Exit fullscreen mode
// good
let currentWidth = 0;
let inputValue = 1;
Enter fullscreen mode Exit fullscreen mode

Thanks for reading

Visit my site for know more about of me.

Top comments (12)

Collapse
 
kithminiii profile image
Bhagya • Edited

Nice one. Thanks for sharing.

Collapse
 
nhshanto profile image
N.H

Thanks man

Collapse
 
justice_hub profile image
Justice-hub

It was a pleasure to help you

Collapse
 
damanita profile image
damanita

Thank you

Collapse
 
whitepassscholarship profile image
White Pass Scholarship

The White Pass Scholarship is a scholarship program open to all students in the White Pass School District. The scholarship is designed to help families with the cost of their student's education.

Collapse
 
aminaraifi profile image
Amina Raifi • Edited

Many of the web app coding can be solved by the stackflow but the things you discussed above really helpful for my develop my webpage of perfect cv maker, its really nice of you to discuss the tips to write cleaner codes.

Collapse
 
justice_hub profile image
Justice-hub

Glad to have helped you :)

Collapse
 
jonrandy profile image
Jon Randy πŸŽ–οΈ

There's some good advice here, but most of it is a matter of opinion. "Clean" code is subjective

Collapse
 
justice_hub profile image
Justice-hub

True, but in this article I have provided some general advice.

Collapse
 
solomondevid profile image
solomondevid • Edited

I have read your article carefully and I have learned a lot and I also want people to benefit from this article thanks to me. So, through this blog of yours, I request people who want to small office trailer for rental in United States. They can contact us.

Collapse
 
hm2hb profile image
Healthy Mind Healthy Body

We believe that health and wellness are dynamic, ever-changing states of being. So we offer a variety of services, based on the latest knowledge in vitamin therapy, to help you feel inspired and ready to take on life's challenges. We pride ourselves on our customer service and our philosophy of wellness.

Collapse
 
devash98 profile image
Ashique Billa Molla

I do Object Destructuring while passing props in react fc.