In helping you solve code problems remotely , Found that many students have a common code problem : Code writing format is not standardized . Although the code writing specification does not affect the performance and operation of the program , But it affects people's first impression of your programming habits or abilities , At the same time, it will bring readers a different impression .

When I first came into programming , The format of code development is also arbitrary , Add a space here , Add a space there . Later, I was reminded by my friends , I started to follow < Alibaba Java Development Manual > To be strict with your . In the work also gradually discovered that , You're more likely to read code that's written in someone else's standard format . Let's start with a demo Contrast .

Code for getting started :

 int[] a={1,2,3,4,5};
int sum=0;
for(int i=0;i<a.length;i++){

Now write the code :

 int[] a = {1, 2, 3, 4, 5};
int sum = 0;
for (int i = 0; i < a.length; i++) {
sum += a[i];

Maybe based on the few lines of code above , You may think that there is no difference between the two code formats . But in the project , Often a Java There are hundreds of lines of code in the source file . If you write it in the first way , The code will look compact , It's very tiring to read , So in practice , It's basically written in the second way .

Next, take a look at the most basic code writing format specification .

Code format


1.if/for/while/switch/do Spaces must be added between reserved words and brackets

2. Any assignment operator =、 Logical operators &&、 Add, subtract, multiply, divide, etc 、 The left and right sides of the trinary operator should be filled with spaces

3.else And before and after the braces with spaces

4. Brackets ( There is no space between and the right adjacent character ; ) There are no spaces between the left and the adjacent characters ; And the left brace { You need a space before

5. There is only one space between the double slash of the comment and the comment content

6. Method parameters are defined and passed in , Multiple argument commas must be followed by spaces

The above comprehensive examples :

// abc Three parameter commas followed by spaces , Method ) and { There's a space between 
public static void test(int a, int b, int c) {
c = 0;
if (true) {
a = b + c;
} else {
a = a == 1 ? a : 1;

7. use 4 Space indent , No use tab character

If you use tab Indent , You have to set 1 individual tab by 4 A space .IDEA Set up tab by 4 When spaces , Do not tick Use tab character

8. In case of type cast , There is no space between the closing bracket and the cast value

 long first = 1000000000000L;
int second = (int)first + 2;

Line break

1. The number of characters in a single line is limited to 120 individual , Line break required for exceeding

2. Empty in braces , I'll just write it tersely {} that will do , There are no newlines and spaces between the braces

formula : Don't wrap before the left brace , Wrap after left brace ; Wrap before right brace , Right brace followed by else Wait for the code to wrap ; The closing brace representing the termination must be followed by a line break

3. Different logic 、 Different semantics 、 Insert a blank line between different business codes to improve readability

Naming specification

1. Names in the code cannot start and end with underscores or dollar symbols

2. Method name 、 Parameter name 、 Member variables 、 Local variables are used uniformly lowerCamelCase style .

3. It is forbidden to use mixed Chinese and English or Chinese Pinyin in any naming

4. stay long perhaps Long assignment , Use uppercase after value L, Can't be lowercase

Error model :

// 1 Should be name, You can use _ and $
int _name = 1;
int name$ = 2;
// 2 The right one is countSumAll
int countsumall = 0;
// 3 Pinyin should not be used , Should use the price To express the price
int jiage = 10;


Above are some of the most common code writing specifications . Include Java Other aspects, such as set operations, etc , All of these are here < Ali Java Development Manual > There's a trail to follow . So it's suggested that the buddies , At the beginning of learning Java When programming , Just ask yourself according to the specifications in the manual , Develop good coding habits .

Last , I hope you and I can encourage each other .

It's all about personal practice in daily work , From one's own point of view 0 writes 1, Make sure that you can really understand .

The article will be in the official account. [ The road from entry to giving up ] First episode , Looking forward to your attention .

Java The entrant : How to write beautiful Java Code ? More articles about

  1. JAVA The language of how to write high performance Java Code ?

    This article mainly introduces JAVA The language of how to write high performance Java Code ? Through the specific content to show you , I hope to learn from you JAVA Language helps . In this article , We're going to talk about a few things that can help to improve Java How to improve application performance . I ...

  2. 【 original 】 How to write elegant Java Code ? This article tells you the answer !

    This article has been collected from JavaGuide (59k+ Star):[Java Study + Interview guide ] One covers most Java The core knowledge that programmers need to master . This article is relatively short , It's basically to recommend some articles or ...

  3. How to use java Write code without side effects

    To make java The concept of no side effects may not be familiar to the students of our university , This is a concept in functional programming , No side effects means : A function (java It's the method ) In multiple calls of , As long as the values of the input parameters are the same , The value of the output must be the same , And in this letter ...

  4. Please elaborate on calling Activity What are the ways , And write the relevant Java Code

    Please elaborate on calling Activity What are the ways , And write the relevant Java Code . answer : You can call... In two ways Activity: Show calls and implicit calls . The display call directly specifies Activity, The code is as follows : Intent int ...

  5. How to write robust Java Code

    Recently I write code in the company , Written code found BUG quite a lot , In order to achieve a function , The code changed and changed , It affects the efficiency of the work order , It also affects personal performance , So I found some articles about writing robust code on the Internet , Plus some of my own experience . Robust code means ...

  6. Interview questions 28: How to write safe java Code ?

    Yes jdk,jvm,java There are a variety of attacks on Applications ? So from java From the programmer's point of view , How to write secure code ? I'm Li Fuchun , I'm preparing for an interview , Today's topic is : How to write safe java Code ? answer : This needs to go from function design to implementation ...

  7. How to write without BUG Code for

    1947 year 9 month 9 Japan , Brigadier General of the United States Navy Grace Hopper Used in the Harvard computer lab Mark II and Mark III Computers do research . Her team tracked Mark II A mistake on , Operator ...

  8. How to write without bug Code for ?

    source 1947 year 9 month 9 Japan , Brigadier General of the United States Navy Grace Hopper Used in the Harvard computer lab Mark II and ...

  9. 《 The beauty of data structure and algorithm 》 &lt;05&gt; Linked list ( Next ): How to write the correct code of linked list easily ?

    It's not easy to write good linked list code , Especially those complicated linked list operations , For example, reverse the list . Ordered list merging, etc , It's very easy to make mistakes when writing . From my experience in hundreds of interviews , Can put the “ List reversal ” There are not enough people who are right about these lines of code 10%. Why do linked lists replace ...

  10. How to write elegant CSS Code ?( turn )

    For the same project or a web page , Although in the end, every front-end development engineer can achieve the same effect , But the code they write must be different . Some are elegant , It looks clear and easy to understand , The code is extensible , Such code is conducive to team cooperation and later maintenance : And some of them ...

Random recommendation

  1. About xml Loading tips : Error on line 1 of document : No content is allowed in the foreword

    I was in java Related tests done in , First, paste the error report : Read xml The configuration file :xmls\property.xml org.dom4j.DocumentException: Error on line 1 of ...

  2. html5 Canvas foundation

    canvas Element is used to draw graphics on a web page . What is? canvas? HTML5 Of canvas Element usage JavaScript Drawing images on Web pages . The canvas is a rectangular area , You can control every pixel . canva ...

  3. ( turn )DoDataExchange Execution opportunity

    void CRegisterDialog::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DAT ...

  4. JavaScript Introduction to the types of operators and their rules in

    JavaScript There are many operators in , It is mainly divided into arithmetic operators , The identical identical operator , Comparison operator , String operators , Logical operators , Assignment operators, etc . These operators all have their own operation rules , Let's introduce JavaScri ...

  5. php Full captcha code

    <?php require_once 'string.func.php'; // adopt GD Library as verification code /** * Add validation text * @param int $type * @param int $le ...

  6. GCC Command line details -L Specify the path to the library -l Specify the name of the library to connect to ( Reprint )

    Reprinted from : 1.gcc Contains c/c++ compiler gcc,cc,c++,g++,gcc and ...

  7. [RxJS] Changing Behavior with MapTo

    You often need streams to trigger different behaviors on the data based on which streams triggers. T ...

  8. mysql_navicat_ Shortcut key

    Shortcut keys can save a lot of time , I've been studying oracle,plsql There is a custom auto complete , such as sf Directly enter Can appear select * from wait ( reference ...

  9. 【 mathematical modeling 】MATLAB grammar

    One . vector . The representation and use of matrices format long  % There are many decimals format short % Default 4 Decimal place format rat % Show the most recent score format short e % Numbers in exponential form What's the mantissa e ...

  10. java Decimal to binary

    Keep these in mind , Very often ! 1. Decimal to binary String s = Integer.toBinaryString(n)  // Convert a decimal number to a string , for example n=5 ,s = "101" ...