Common problems for beginners in Java [3]: lack of good programming habits

Source: Internet
Author: User

 

The last time I talked about "lack of basic object-oriented skills", I would like to talk about programming habits today. Most of the bad habits we talk about today are cross-language (for beginners of C ++ and Python), and most of them require constant efforts to get rid of them.

Random name
Some new handwritten programs, when you need to define a variable name (or function name, class name, package name, etc.), randomly tap the keyboard, the name is ready ...... several weeks later, when I encountered a bug and looked at my own code, I secretly muttered: "Is this code written by me? Why can't I understand it ?"
 
So I often tell New cainiao that naming is not standardized and killing people! In view of this problem is quite common, I have compiled several typical textbooks as a negative textbook, specifically: Using Single letters to name variables; using some of them is not very interesting
Variable names (such as S1, S2, and S3); use different terms/abbreviations for the same business concept (which can easily split the person who reads the Code ); use Pinyin name (if your team contains Hong Kong and Taiwan people or old people
).

Get used to code Copy & Paste
This is a common problem. When writing a lot of new code, if you find that a function to be written is similar to a function written a few days ago, paste the original function and change it a few times, in my heart, I also secretly said, "A feature has been quickly completed "......
If you like this, you should pay attention to it. This method is the main source of code odor (by referring to refactoring-improving the design of existing code), leading to a significant reduction in code maintainability. When you need to add features or modify bugs in the future, you need to change multiple places at the same time. At that time, you may not be able to remember that there are several clones in the ghost code.

Magic number
If you have never heard of "magic number", read "here" first.
To illustrate the magic number problem, let's look for an example: Suppose there is a business logic that requires 10 seconds of timeout, how do you write this sleep statement? I guess most people write the following three statements.
1. Write sleep (10*1000) directly.
2. Define a constant timeout_xxx = 10*1000; then sleep (timeout_xxx );
3. Add a timeout item to the preparation file, then the program reads the configuration file to obtain the timeout value, and then calls sleep. (Configuration FileIn a broad sense, it refers to various mechanisms that can be used to store configuration information, such as XML files and databases)
If your approach is similar to writing 1, you may prefer hard coding. Hard coding is not only readable, but also smelly code similar to "copy and paste code" (there may be multiple magic number clones), which is not conducive to future maintenance.
As for writing 2, it is better (at least readable) than writing 1 ). However, once a demand change occurs in the futureRuntimeAdjust the timeout interval (or even ask the user to configure the timeout interval), then the disadvantage of Writing 2 is immediately exposed.

Code Coupling is too high
 
When talking about MVC or design patterns, almost every Java developer can say the first thing? But in other words, when writing code, there are few clearly defined codes. Speaking of code
What are the causes of coupling? What is orthogonal design? (For coupling and orthogonal design, I will discuss it later) there will be fewer people who can fully understand it.
Therefore, it is not surprising that many new Java beginners have a high degree of code coupling. I once checked the code of the employees during the trial period. Various business logic had to be entangled, and the Code had to smell bad. There was no way to start refactoring, so he had to overwrite it.

Spoiled by GC
Because Java provides a garbage collection mechanism for memory at the language level, programmers only apply for memory and do not need to worry about the issue of release. Therefore, many new users have developed a bad habit. For other resources (such as database connections), they only apply for not release (some people even naively think that JVM will help you with resource recovery ).
Even though some people know that resources need to be released, they often forget (for example, they have written the database connection and related code,Coming soonWhen writing and closing the database connection, someone suddenly asks you to go to lunch and forget this problem when you come back ).
This bad habit may lead to resource leakage, which is often worse than memory leakage. If the program you write runs for a long time (such as running on webserver), the entire process may fail due to resource depletion.

Next post, talk about "improper exception handling ".

Http://program-think.blogspot.com/2009/02/defect-of-java-beginner-3-code-style.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.