[Words spelled to me when I was in the first grade ⑩] Please understand the minimum Java coding method. (1st: Naming)

――Now, the tenth article that you can just write as a letter that you are using it as a matter of course. ――The adult behavior that stretched beyond the limit, saying, "I never conceit the tenth bullet, which is a very happy number, and just work hard every day." ――I can't see the concept of age anymore in the self-satisfaction paranoia of "I mean, did you do so much fantasy clinging work, which is called jumping this time?" ――OK. This time, let's write what I should have known after joining the company.

Java coding practices

―― "Convention" ... I was careful only about the basic naming of classes, methods, variables, etc. ―― “Method” ・ ・ ・ From the learning stage, I didn't consciously describe it, and I thought it would be good if I could understand the meaning.

→ In team development, etc., the description lacks uniformity and readability, causing a great deal of inconvenience.

――Re-learn from the basics about rules and practices in books and online. --Check the human code while looking at sample programs and open source projects.

Basic

The name is not distinguished by uppercase and lowercase.

「OK」

String firstMessage = "Hello";
String secondMessage = "I'm Bob";
String thirdMessage = "Bye";

「NG」

String message = "Hello";
String MESSAGE = "I'm Bob";
String Message = "Bye";

package

Naming is all lowercase.

"Supplement"

--The name is an easy-to-understand name. --Names should be singular.

「OK」

package lang.java.hello;
package lang.java.name;

「NG」

package lang.java.Hello;
package lang.java.names;

Do not abbreviate the name

「OK」

package lang.java.infomation;

「NG」

package lang.java.info;

Do not omit the import with "*".

"Supplement"

--The order of description is as follows. --Java standard class API --Global (famous or commercially available libraries) --Local (in-house or project library) --In many cases, alphabetical order

class

The class name should be a role name.

「OK」

class SubmitAction {
    
}

「NG」

class Test1 {
    
}

The first letter of the name is capitalized, and in the case of multiple words, the first letter of each word is capitalized.

"Supplement"

--Capitalizing the beginning of each word is called "capitaliza". --The capitalized character string format is called "Pascal format". → Also known as "Upper Camel format".

「OK」

class TwitterApiClient {
    
}

「NG」

class twitterapiclient {
    
}

The class name is completely described.

「OK」

class TwitterApiClient {
    
}

「NG」

class TwApiCli {
    
}

Add "Abstract" to the name of the abstract class

「OK」

abstaract class AbstractShowController{
    
}

class ShowController extends AbstractShowController{

}

「NG」

abstaract class MainShowController{
    
}

class showController extends MainShowController {

}

Add "Exception" to the end of the exception class

「OK」

class TestObjectException extends Exception{
    
}

「NG」

class TestObjectError extends Exception {
    
}

Method

Give a name with a clear purpose

「OK」

public String toString(){

}

「NG」

public String test2(){

}

Described in camel format

"Supplement"

--The first word starts with a lowercase letter. --For multiple words, capitalize the beginning of each subsequent word.

「OK」

public String showInfo(){

}

「NG」

public String showinfo(){

}

The boolean method should have a name that is easy to understand.

「OK」

boolean canRemove()

boolean checkChange()

「NG」

boolean setName()

boolean isRemove()

variable

Make the name understandable.

「OK」

startDate
endDate
maxPrice
minPrice

「NG」

a
num
str

Boolean variables should have names that are easy to understand.

「OK」

boolean canRemove;

boolean checkChange;

「NG」

boolean isRemove;

boolean name;

The component type variable is "purpose of use + component name".

Supplement

--In the example below, if there is a component called "Button".

「OK」

Button cancelButton = new Button():

「NG」

Button cancel = new Button():

constant

All uppercase

「OK」

static final int MODE = 7;

static final string COUNTRY = "JAPAN";

「NG」

static final string country = "JAPAN"

For multiple words, separate them with an underscore.

「OK」

static final int EDIT_MODE = 1;

static final string MY_COUNTRY = "JAPAN";

「NG」

static final string myCountry = "JAPAN"

Summary

――This time, I will write an article while remembering the time when there was no concept of "unity / readability" in group development that also served as learning by grasping Java coding standards. ―― “It's important that there are some restrictions and ethics in everything,” says a man who writes articles that do not feel unified or readable at all. ――When I made an artificial statement of determination, "Let's continue to write with unity and compassion," I started preparing for the second writing.

reference

Recommended Posts

[Words spelled to me when I was in the first grade ⑩] Please understand the minimum Java coding method. (1st: Naming)
[Words spelled to me when I was in the first grade ⑪] Please understand the minimum Java coding method. (2nd: Javadoc edition)
[Words spelled to me when I was in the first grade ⑦] What I want you to include at least with the Visual Studio Code extension
When I wanted to create a method for Premium Friday, it was already in the Java 8 standard API
I want to get the IP address when connecting to Wi-Fi in Java
Java reference to understand in the figure
I was addicted to the roll method
[Promotion of Ruby comprehension (1)] When switching from Java to Ruby, first understand the difference.
The part I was addicted to in "Introduction to Ajax in Java Web Applications" of NetBeans
Java classes and instances to understand in the figure
I tried to implement the Euclidean algorithm in Java
How to batch initialize arrays in Java that I didn't know when I was a beginner
How to get the class name / method name running in Java
I translated [Clone method for Java arrays] as the Clone method in Java arrays.
What I was addicted to when introducing the JNI library
Create a method to return the tax rate in Java
I want to simplify the conditional if-else statement in Java
The story I was addicted to when setting up STS
When I was worried about static methods in java interface, I arrived in the order of name interpretation
I want to know the Method of the Controller where the Exception was thrown in the ExceptionHandler of Spring Boot