Guide the most simple and effective way to write easy-to-read code

Invite you and TipsMake.com to find out the article to guide the simple and most effective way to write easy-to-read code below!

  1. Instructions for creating a website for beginners
  2. 7 great HTML effects anyone can add to their website
  3. 17 simple HTML codes you can learn in 10 minutes

Most of us know how to write code but only lazy, so we don't follow it. This is one of the most understated ways of writing code and there are some false rumors, but I will mention it later. That method is, converting code into functions!

I know that we all convert code into functions but not too often. Get the code below, a code from my old company for example. Take a look at each of the points mentioned below!

Guide the most simple and effective way to write easy-to-read code Picture 1

1. It looks like it is recovering some groups. Although there are only 2 lines, you can draw a reasonable method from there.

2. Initially it must start with a flag. Programmers must feel uncomfortable about why only one line of code must have a separate method. Then another flag is added and the next one is a flag!

3. It looks like we are creating a container for frames. Why should high-level code pay attention to how it initializes?

4. As soon as I looked at the 4 lines of code, I understood why we should care about device information. I know 4 lines are not many but it can multiply tens or hundreds of lines. It is better to separate a method!

5. Login while reading flow is not effective. Must change now!

6. All listeners should be separated in one method.

After performing a few refactoring operations:

Guide the most simple and effective way to write easy-to-read code Picture 2

The code has become more eye-catching than before. It's easy to flip through without getting unnecessary details. A defined method will not require any comment lines.

Advice:

  1. Do not interfere with reading flow with complex effects or complicated conditions.
  2. Define short methods. It makes the explanation easier, the flow clearer, the scope is shorter, then you will have a better feeling.
  3. Make all declarations of the lecture code.

Adding two lines to a method is always easy. However, you should also note that the code changes above and below require a separate range, if any, to separate the method immediately. In each method, separate logic must be applied, so later contributors will add changes according to each corresponding method.

Conclude

Does increasing the number of methods negatively affect performance as many people find it? This is a disastrous misunderstanding and in most cases these effects are too small to worry about. If you work with the languages ​​on the JVM, congratulations, JVM is a great software with many really great time optimization features. And transform it .

"Code is like humor. Khi bạn có thể tạo nó, nó sai ." - Cory House

Author: Girish Budhwani

Refer to some more articles:

  1. C ++ exercises have solutions (sample code) for variables, data types, and operators
  2. 11 basic principles that every programmer should follow
  3. Form a way of thinking like a programmer

Having fun!

5 ★ | 1 Vote

May be interested

  • How IP addresses workPhoto of How IP addresses work
    any device connected to a network such as a computer, tablet, camera, etc., will need a unique identifier for other devices to know how to access it. in the tcp / ip protocol, that identifier is called an internet protocol address or ip (internet protocol - ip) address.
  • IP indicator for water and dust on smartphone everyone needs to knowPhoto of IP indicator for water and dust on smartphone everyone needs to know
    in the past few years, the market has started to appear smartphones introduced to the ability of dust and water resistance to meet standards such as ip52, ip57, ip68, ... so the nature of these standards is as meaningful as how?
  • What is RAR file?Photo of What is RAR file?
    the rar file (short for roshal archive compressed) is a compressed file or data warehouse that contains one or more other files and folders inside. imagine a rar file as a folder on your computer that has files and other folders inside it.
  • Differences between normal and power surge protectionPhoto of Differences between normal and power surge protection
    voltage-boosting electric drives look like regular electric drives, but not all power outlets are usually voltage-resistant power drives. voltage surge protectors help the device resist surge current. many electric drives are usually just extension cords and do not have any protection.
  • What does the meaning of your phone number say?Photo of What does the meaning of your phone number say?
    please refer to the meaning of the phone numbers below to choose the best sim cards.
  • What is the API Document and why is it important?Photo of What is the API Document and why is it important?
    let's join tipsmake.com to find out what api documentation (api document) is and why it is necessary to have a good api document in this article!