In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "what are the good habits of writing code". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought. Let's study and learn "what are the good habits of writing code"?
1. After modifying the code, remember to test yourself.
"after changing the code, self-test" is a necessary basic accomplishment of every programmer. In particular, don't take such a fluke: I just changed a variable or I only changed one line of configuration code, so I don't have to test myself. After changing the code, try to ask yourself to test, ha, you can avoid a lot of unnecessary bug.
two。 The methods were tested as much as possible.
Input verification is also a necessary basic accomplishment of every programmer. Your method processing, "you must first verify the parameters." For example, whether the input parameter is allowed to be empty, and whether the length of the input parameter meets your expected length. Try to make a habit of this. Many "low-level bug" are caused by "not checking parameters".
If your database field is set to varchar (16), the other party sends a 32-bit string over, you do not verify the parameters, "insert the database directly abnormal".
3. When modifying the old interface, think about the compatibility of the interface.
A lot of bug is caused by modifying the old foreign interface, but "not compatible". Most of the key problems are serious, which may directly lead to the failure of the system. Novice programmers are easy to make this mistake.
Therefore, if your requirements are modified on the original interface, especially if the interface provides services, be sure to consider interface compatibility. For example, the dubbo interface, for example, used to receive only the parameter Amagine B, but now that you add a parameter C, you can consider doing this.
/ / the old interface void oldService (A _ Magi B); {/ / compatible with the new interface, pass a null instead of C newService;} / / the new interface cannot be deleted temporarily and needs to be compatible. Void newService (A _ Magna B _ M C)
4. For complex code logic, add clear comments
When writing code, there is no need to write too many comments, good method variable naming is the best comments. However, if it is "code with complex business logic", it is really necessary to write "clear comments". Clear comments are more conducive to later maintenance.
5. After using the IO resource stream, you need to close the
Everyone should have experienced that if the windows desktop "opens too many files" or system software, it will feel that the computer is very stuck. Of course, our linux server is the same, usually operating files, or database connections, if the IO resource flow is not closed, then the IO resource will be occupied by it, so that others have no way to use it, resulting in a "waste of resources".
So after using the IO stream, you can use finally to close it.
FileInputStream fdIn = null; try {fdIn = new FileInputStream (new File ("/ jay.txt");} catch (FileNotFoundException e) {log.error (e);} catch (IOException e) {log.error (e);} finally {try {if (fdIn! = null) {fdIn.close ();}} catch (IOException e) {log.error (e);}}
There is a more handsome way to turn off streaming after JDK 7, "try-with-resource".
/ * * the little boy who follows the official account and picks up snails * / try (FileInputStream inputStream = new FileInputStream (new File ("jay.txt")) {/ / use resources} catch (FileNotFoundException e) {log.error (e);} catch (IOException e) {log.error (e);}
6. The code takes steps to avoid run-time errors (such as array boundary overflow, division by zero, etc.)
In daily development, we need to take measures to avoid runtime errors such as "array boundary overflow, zero integer, null pointer" and so on.
Similar code is common:
String name = list.get (1) .getName (); / / list may be out of bounds, because there may not be two elements.
Therefore, we should "take measures to prevent array boundary overflow", as a positive example:
If (CollectionsUtil.isNotEmpty (list) & & list.size () > 1) {String name = list.get (1). GetName ();}
7. Try not to make remote calls or database operations in a loop, giving priority to batch operations.
Remote operations or database operations are "relatively consuming network and IO resources", so try not to call remotely in the loop or operate the database in the loop. You can "check it back in batches and try not to cycle many times". (however, do not check too much data at once, ha, 500 in batches.)
Positive example:
RemoteBatchQuery (param)
Counterexample:
For (int iTuno Bandi)
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.