Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write good code

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the knowledge of "how to write good code". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Write a good note

The matter of writing code is probably a moment of programming, and then debugging the crematorium.

Ah Fan had an experience, that is, the development task was tight at that time, and after the function was realized, it was done, and he did not write notes properly. As a result, two or three months later, the function changed into bug because of the change in demand, and solved it when it came out of bug.

But! Because A Fan did not write a note, the realization logic at that time, why did he forget all about it? so it is equivalent to going through the logic all over again. The taste is really sour.

Picture

So A Fan wants to put this suggestion first!

When writing comments, if the logic is more complicated, you can first write down the general idea, and then write in detail when you implement it, such as the code that Ah Fan once wrote (desensitized, let's not take it too seriously). The key point is the comments written by Ah Fan.

/ * this method is called during XX operation Used to insert data into the database * @ param saveModel the data to be inserted * @ return is saved successfully * * @ author a'fen * @ date 2020-10-17 23:50:45 * / public Boolean isSaveSuccess (SaveModel saveModel) {/ * overall idea: 1 receive all incoming id/name information 2 insert related information 2.1 after the XX information is inserted successfully Then insert the relevant information into the YY table 3 when inserting the YY table, first determine whether there is relevant information, if so, update it, if not, directly insert 4 whether to add AA, if the business is configured with AA, you need to update the relevant content to the ZZ table Note: click next in the BB interface The relevant information has been inserted into the ZZ table, so you can just update here * / 1 to receive all incoming id/name information / / create xxList to receive all id/name information List xxList = new ArrayList () / / A pair of incoming information is sorted by id to facilitate the next operation saveModel.getIdList (). Stream.collect (Collectors.groupingBy (SaveModel::getId)) .forEach ((String mapKey, List save)-> {xxList.addAll (save);}) / / 2 insert relevant information / / Circular xxList for (XXModel model: xxList) {/ / insert information into XX table successfully, go to YY table to insert relevant information implementation code} / / 3 when inserting YY table, first judge whether there is any relevant information, if so, update it, if not, insert the implementation code / / 4 directly whether to add AA If AA is configured on the business, you need to update the relevant content to the ZZ table implementation code}

Before you write the code, think about possible application scenarios

Sometimes after getting the demand, if you are in a hurry to do it, you may spend a lot of energy in the later stage.

Ah Fan once did a business, similar to the scene of user recharge. At that time, after A Fan got this demand, he thought that it was just recharge money, so it was enough to define an Integer type, and then the operation was as fierce as a tiger, and there was no problem after being online.

As a result, on one occasion, the company's operation cooperated with some other third parties, using this interface to recharge, and then because the operation continued to recharge in large quantities, it directly exceeded the allowable scope of Integer, resulting in the failure of the whole business line.

Then check the problem to the end, it is found that it is caused by a powder, but also caused by a data type. Ah Fan really wanted to go underground at that time.

The code is written for people to read, so keep your code clean and tidy

The code is written for people, not for machines. Do you think, if you want to show it to the machine, it doesn't smell good to write 0110001 directly! After all, the machine only recognizes 0 or 1.

So the code is written for people, so keep it clean and tidy when you write it.

For example, for the indent that should be indented, the blank line is empty, such as the following:

Public static void sleep (long millis, int nanos) throws InterruptedException {if (millis)

< 0) { throw new IllegalArgumentException("timeout value is negative"); } if (nanos < 0 || nanos >

999999) {throw new IllegalArgumentException ("nanosecond timeout value out of range");} if (nanos > = 500000 | | (nanos! = 0 & & millis = = 0) {millis++;} sleep (millis);}

To indent there is indentation, the blank line on the empty line, do not you praise, I think this code is written, really good! (Thread source code who dares to say bad?!

It's all right. Just talk to Ah Fan about the source code.

In fact, if you want to write good code, the most important thing is to read more and write more yourself.

What are you looking at? Fan thinks it is enough to look at the source code. The JDK source code is used by so many people, which shows how well the JDK source code is written.

So, just talk more about the source code with Ah Fan, and then go to the library to study the source code. Usually, when you write your own code, you should pay more attention to learn from the implementation ideas and programming skills learned from the source code. Over time, you will definitely have a good improvement in programming.

This is the end of "how to write good code". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report