In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what are the pits of Java development and programming". 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 to study and learn what are the pits of Java development and programming.
Null values that can be seen everywhere
I have seen a lot of code will take the Null value as the return value, when you expect a string, you accidentally get a Null value; when you expect a List, you accidentally get a Null value, if you do not deal with it, then you will accidentally get NullPointerException. Just like down there.
/ / case 1 String userTag = getUserTag (); if (userTag.equals ("admin")) {/ / NullPointerException / /...} / / case 2 List carList = getCarList (); for (String car: carList) {/ / NullPointerException / /.}
To prevent this, you can give an empty collection instead of Null when List returns. If it is a string, you can put the object of value to be determined before the comparison.
If ("admin" .equals (userTag)) {/ /...} / / or if (Objects.equals (userTag, "admin")) {/ /.}
There is no null check.
Maybe you take into account the above Null value, but do not take into account the null value in the actual processing, such as the empty string "", or the collection is empty. Then it is possible to get a NullPointerException in subsequent processing. So you should make a null judgment.
String userTag = getUserTag (); if (userTag! = null & & userTag.trim ()! = ") {/ /...} List carList = getCarList (); if (carList! = null & &! carList.isEmpty ()) {/ /.}
Ignored exception handling
Exception handling is always annoying, and ignoring exceptions always seems to have an attractive magic. I've seen code like this.
Try {List result= request (); / /...} catch (Exception e) {}
You read it correctly, there was nothing in catch, and then there was a problem and there was no trace of the log file. Exceptions are thrown on purpose, and you should handle them correctly or continue to throw them. And at the same time, you should output a line of logs to record this exception to facilitate future problem tracking.
No resources are released
It is important that close is always required when reading files or requesting network resources, otherwise it may block the use of other threads. But beginners may forget this step. In fact, since the beginning of Java 7, try-with-resources has provided the feature of automatically closing resources, only need to put the open resources into the try.
Try (FileReader fileReader = new FileReader ("setting.xml")) {/ / fileReader.read (); / /...} catch (Exception e) {e.printStackTrace ();}
Like the above, there is no need to manually call the close method of fileReader in finally to close the resource, because the resource call placed in try will automatically call close when it is finished. And whether there is an exception thrown or not, this is practical.
ConcuretModificationException
One day you will encounter ConcuretModificationException and start searching for its solution. The most common scenario for this exception is that you update as you traverse a collection, such as the following.
List list = new ArrayList (); list.add ("A1"); list.add ("b1"); list.add ("b2"); list.add ("C1"); for (String s: list) {if ("b1" .equals (s)) {list.remove (s);}}
This exception is very useful because ArrayList is not a thread-safe collection, assuming that one side of your traversal is constantly updated by another thread, and the non-thread-safe collection will cause your traversal results to be incorrect, so the existence of this exception is reasonable. The same is true of HashMap.
Missing comment
It is sometimes no exaggeration that accurate notes can save people from fire and water. Although good code itself is a very good comment, this rarely happens when it comes to practical development. Comments do not require you to record everything in detail, but you should add proper comments to the core logic, such as the implementation ideas of complex logic and current logic business requirements. The reason for the addition of a judgment, the occurrence of an exception, and so on. This allows you to thank yourself when you need to look back at your current code at some point in the future. What's more, it will make it easy for you to win in the pot one day.
No code testing
I have seen some colleagues throw it directly to their docking colleagues after the function has been developed, but they have not passed any tests, or just tested a simple situation. Testing is an important part of the development process, it is difficult to say that there is no problem with the code without strict testing. I think at least unit testing, special use case testing, integration testing and other forms of testing are needed after functional development. Rigorous testing can not only find problems in the first place, but also reduce unnecessary docking and debugging time later.
Repeat the wheel
You know, the Java community is very active, there are a large number of third-party libraries, open source authors may have spent years to maintain and improve class libraries, these libraries are excellent. At the same time, JDK also provides a large number of commonly used functional encapsulation. All these can add wings to our development speed. So, when you need a feature, you should first check to see if the same feature already exists in JDK and the class library that has been introduced, instead of making your own wheels, and in most cases your wheels are not as good as others.
Here are some examples.
If you need logging, you can use logback.
If you need network operation, you can use netty.
If you need to parse JSON, you can use gson.
If you need to parse the table, you can use apache poi.
If you need general operation, you can use apache commons.
Another situation is that you may not know that a feature has been implemented in JDK, so you should check JDK Document. I have seen a colleague handwritten string split in my work, converting Date objects to Calendar in order to obtain timestamps.
Lack of necessary communication
This part has nothing to do with development, but this link often affects the final development results. Before specific development, you should communicate and understand the functional requirements in detail, so that you can write code that does not deviate from the actual needs. Sometimes you may misunderstand the requirements because of the lack of necessary communication, and eventually find that the features you write are completely useless after development.
There is no code specification
Code standardization is very important, and it can be a headache for maintainers if a project is full of bizarre code specifications. And with the rapid development of the software industry, the demand for the comprehensive quality of developers is getting higher and higher, and excellent programming habits can also improve the final quality of the software. For example, the maverick naming style challenges reading habits; a variety of error codes artificially increase the difficulty of troubleshooting problems; messy engineering structures lead to difficulties in the maintenance of subsequent projects; vulnerability codes without authentication are vulnerable to hackers; low-quality code is full of loopholes after being online, and so on. Because there is no unified code specification, problems in development may emerge one after another.
The following is a brief list of development specifications that should be unified. Such as naming style is good; constant name structure; code format how to unify; date and time format how to deal with; set processing matters needing attention; log printing specification; before and after interaction specific specifications.
Thank you for your reading, these are the contents of "what are the pits of Java development and programming?" after the study of this article, I believe you have a deeper understanding of what the pits of Java development and programming have, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.