- vừa được xem lúc

Blog#170: Say bye bye to bad javascript practices👋

0 0 9

Người đăng: NGUYỄN ANH TUẤN

Theo Viblo Asia

Hi, I'm Tuan, a Full-stack Web Developer from Tokyo 😊. Follow my blog to not miss out on useful and interesting articles in the future.

Programming is a powerful tool that makes life easier for many people. However, it comes with a responsibility to write code that is easy to test and maintain over time. There are some bad habits in programming that can harm the quality of our code and the final product. It's important to know what these habits are and avoid them.

1. Using var instead of let and const

It’s time to say goodbye to using var.

You should use only let and const for these reasons:

● The scope is clearer (between braces).

● It does not create global objects.

● It flags errors if you redeclare them.

If you are not using an old web browser like Internet Explorer 11, it is best to stop using the "var" keyword and use "let" and "const" instead.

2. Using comments to explain the code

Comments are important when writing software because they help us understand the code better. However, we should not use comments to explain every step of the code; instead, we should write code that is easy to read and use comments to provide additional context.

Here are a few tips and reminders to help you write code comments like a pro:

● Avoid redundancy in your comments; don’t write what you do, write why you do it.

● Descriptive variable/function/class names are better than descriptive comments.

● Summarize as much as possible; do not write paragraphs if it is not absolutely necessary.

● Try to always use the same language and style of comments.

● Over time, comments are not usually maintained (modified) code is.

3. Using === instead of ==

The regular equality operator (==) compares two values to see if they are the same, but it does not check if the data types are the same. The strict equality operator (===) compares two values to see if they are the same and also checks if the data types are the same.

The strict equality operator checks that two values are exactly the same, including their type and value.

4. Forgetting to use optional chaining

The optional chaining operator (?) allows us to access a property located deep in a chain of connected objects without having to check each reference in the chain. This helps us avoid errors when trying to access a non-existing property. For example, if we try to access a property that is not defined in a Pokémon object, Javascript will generate an error and our application will break. However, when using optional chaining (?), Javascript will tell us that the property is undefined but will not generate any error. This can help us avoid errors that are out of our control.

5. Not using magic strings and magic numbers

Magic numbers or strings are values that are used directly in the code without any explanation of what they mean. It is better to assign these values to constants so that they are easier to understand and debug.

6. Improperly handling API call errors

We should always handle errors with a try/catch in our async/await.

If we don't take care of the mistakes in our promises, it is very likely that our application will crash, and we definitely don't want that to happen.

7. Using an object as a single parameter

It is better to use multiple parameters when creating a function that requires multiple values from an object. This makes the code easier to read and test, and helps the product stay maintainable. It also improves performance by avoiding collecting garbage or creating unnecessary objects. Additionally, if you use TypeScript, it is easier to define the interface of the parameters and benefit from type checking and auto-suggestions, which helps to avoid errors.

8. Forgetting the power of abbreviations

We have all been in a situation where we have wondered if a variable exists or if it has a value other than nothing or undefined. To make sure, we often have to do a lot of checks.

Abbreviations allow us to avoid making long and complicated sentences. We can make the same sentence shorter and more concise by using abbreviations.

Conclusion

We are responsible for writing code that is easy to read and maintain. I have learned from my experience as a developer that it takes less time to read code than to write it. I hope these tips will help you create great products. If you have any ideas, please share them in the comments so we can all learn from each other.

And Finally

As always, I hope you enjoyed this article and learned something new. Thank you and see you in the next articles!

If you liked this article, please give me a like and subscribe to support me. Thank you. 😊

Resource

Bình luận

Bài viết tương tự

- vừa được xem lúc

Giới thiệu Typescript - Sự khác nhau giữa Typescript và Javascript

Typescript là gì. TypeScript là một ngôn ngữ giúp cung cấp quy mô lớn hơn so với JavaScript.

0 0 528

- vừa được xem lúc

Bạn đã biết các tips này khi làm việc với chuỗi trong JavaScript chưa ?

Hi xin chào các bạn, tiếp tục chuỗi chủ đề về cái thằng JavaScript này, hôm nay mình sẽ giới thiệu cho các bạn một số thủ thuật hay ho khi làm việc với chuỗi trong JavaScript có thể bạn đã hoặc chưa từng dùng. Cụ thể như nào thì hãy cùng mình tìm hiểu trong bài viết này nhé (go).

0 0 436

- vừa được xem lúc

Một số phương thức với object trong Javascript

Trong Javascript có hỗ trợ các loại dữ liệu cơ bản là giống với hầu hết những ngôn ngữ lập trình khác. Bài viết này mình sẽ giới thiệu về Object và một số phương thức thường dùng với nó.

0 0 158

- vừa được xem lúc

Tìm hiểu về thư viện axios

Giới thiệu. Axios là gì? Axios là một thư viện HTTP Client dựa trên Promise.

0 0 149

- vừa được xem lúc

Imports và Exports trong JavaScript ES6

. Giới thiệu. ES6 cung cấp cho chúng ta import (nhập), export (xuất) các functions, biến từ module này sang module khác và sử dụng nó trong các file khác.

0 0 113

- vừa được xem lúc

Bài toán đọc số thành chữ (phần 2) - Hoàn chỉnh chương trình dưới 100 dòng code

Tiếp tục bài viết còn dang dở ở phần trước Phân tích bài toán đọc số thành chữ (phần 1) - Phân tích đề và những mảnh ghép đầu tiên. Bạn nào chưa đọc thì có thể xem ở link trên trước nhé.

0 0 249