Good Java style: the first part

Source: Internet
Author: User

Brief introduction

Over the years, as a software developer and consultant, I've seen a lot of code written in various programming languages. Among them is the writing elegant, also has the writing ugly, unfortunately many code all belongs to the latter. I want to convince you and my development partners that we should pay more attention to the code style, as we focus on the user interface and other parts of the application. This series is made up of two parts, in the first part, I'll explain why we should be concerned about what the code looks like, and show you some basic elements of a good Java style.

Why style is so important

Although Java is used to write programs rather than prose, it is also used to express ideas and ideas. Moreover, in addition to conveying information, these ideas and ideas must indeed solve some problems. Perhaps you may be concerned that a good style may seem like a waste of time, but it can make the thoughts and ideas that we express in our code especially clear.

Here are some reasons to use good style [from "Java Code conventions," Sun Microsystems]:

80% of the cost of a software product in a lifetime is maintenance.

It is almost impossible for any software to be maintained by the original author of the Software for life.

Good style can improve the maintainability of software code.

If the software comes with the source code, it should be as well structured, clear, and professional as the rest of the product.

Writing code in a good style also has the following benefits:

Improve the readability, consistency, and consistency of your code, making your code easier to understand and maintain.

Easy to track and debug because the code is clear and coherent.

Easy to continue writing code from where you or another programmer stops, especially after a long period of time.

Improve the value of code guidance because participants can focus more on what the code is doing.

General principles

Writing Java in a good style is not difficult, but it does require attention to detail. Here are some general principles that should be followed:

Make the code clear and easy to read.

Make the code consistent.

Use an obvious identifier.

Logically organize files and classes.

There is only one class per file (excluding internal classes).

The maximum ruled is between 80-90 characters.

Use an empty Geote (or) other easily judged separator character.

Indents use spaces rather than tabs.

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.