Java programming specifications, java programming

Source: Internet
Author: User
Tags define local unsupported

Java programming specifications, java programming

 

Note:

The code automatically generated by the development tool can be inconsistent.

 

 

Note:

For (...)

{

... // Your code

}

 

 

Note:

If (filename! = Null

& New File (logPath + filename). length () <logConfig. getFileSize ())

{

... // Your code

}

 

Note:

LogFilename wow = null;

LogFilename that = null;

 

Note:

If (writeToFile)

{

WriteFileThread. interrupt ();

}

 

Note:

If (log. getLevel () <log. getRecord ())

{

Return;

}

// Empty rows

LogWrite writer;

 

Note:

In this case, the program layout is not neat due to the number of spaces set by the TAB key in different editors. JBuildr, UltraEdit, and other editing environments support replacing the first TAB with spaces in the line, this option should be enabled

 

Note:

The purpose of writing code in this loose way is to make the program clearer. Since the clarity produced by spaces is relative, there is no need to leave spaces in clear statements, if the statement is clear enough, no space is required for the inside of the brackets (that is, before the Left and Right brackets), and no space is required between the multiple parentheses, because java brackets are clear signs. in a long sentence, if you need to add a lot of spaces, you should keep the overall clarity, but do not add spaces in the part. Do not leave more than two spaces in a row when leaving spaces for operators.

 

Note:

Class Definition :{

Class public attribute definition;

Class protection attribute definition;

Class private property definition;

Class public method definition;

Class protection method definition;

Class private method definition;

}

 

 

Note:

<Html>

<Body>

<P> one-sentence description

<P> detailed description

<P> product Module

<Br> company version information

</Body>

</Html>

 

Note:

/*

* File name

* Copyright

* Description

* Modifier

* Modification time

* Modified content

* Trail Ticket No.

* Change Ticket No.

*/

 

 

Note:

/**

* <One-sentence function description>

* <Function description>

* @ Author

* @ Version

* @ See [related classes/methods]

* @ Since [product/module version]

* @ Deprecated (this indicates that this class or interface is not recommended)

 

 

 

Note:

/**

* @ Param

* @ Return

* @ Exception/throws

*/

 

 

 

 

Note:

In this way, the intention of the program writer is clear, and the break statement is effectively prevented from being omitted for no reason.

 

 

 

Note: Unless necessary, you must describe the abbreviations at or before they are used.

 

Note: Unless necessary, do not insert comments in the middle of the code or expression, otherwise it is easy to make the code understandable.

 

 

The following annotations do not make much sense.

// If receiveFlag is true.

If (receiveFlag)

The following comments provide additional useful information.

// If the message is received from the link

If (receiveFlag)

 

Note: When the code segment is long, especially when multiple nesting occurs, this can make the code clearer and easier to read.

Example: See the following example.

If (......)

{

Program code1

 

While (index <MAX_INDEX)

{

Program code2

} // End of while (index <MAX_INDEX) // specifies that the while statement ends.

} // End of If (......) // Specify which if statement ends.

 

 

Note: It is convenient to use the program during debugging /*...... */Comment out a long program.

 

Note: select the first sentence when collecting the introduction of the JavaDoc tool.

 

Example: The following is a process Comment for setting properties.

// 1. Determine whether the input parameter is valid.

......

// 2. set local variables

......

 

Example: The Leap year algorithm is described here.

// 1. If it can be divisible by 4, it is a leap year;

// 2. If it can be divisible by 100, it is not a leap year;

// 3. If it can be divisible by 400, It is a leap year;

 

Format: com. huawei. Product Name. Module name

Com. huawei. Department name. Project Name

Example: Integrate the WEBSMAP package name com. huawei. iin. websmap

General Message forwarding package name com. huawei. insa2.msgtrans

 

Example: OrderInformation, CustomerList, LogManager, LogConfig, SmpTransation

 

Example: private void calculateRate ();

Public void addNewOrder ();

 

Format: get + non-Boolean attribute name ()

Set + attribute name ()

Is + Boolean attribute name ()

Verb ()

Verb + object ()

Example: public String getType ();

Public boolean isFinished ();

Public void setVisible (boolean );

Public void addKeyListener (Listener );

Example: private mermername;

Private orderNumber;

Private smpSession;

 

Example: public final static int MAX_VALUE = 1000;

Public final static String DEFAULT_START_DATE = "2008-11-06 ";

 

Example: public class Person

{

Private String name;

Private static List properties;

 

Public void setName (String name)

{

This. name = name;

}

 

Public void setProperties (List properties)

{

Person. Properties = properties;

}

 

}

 

Example: For the Application type, the name ends with an App --- MainApp;

For Frame type, we recommend that you name it end with Frame --- TopoFrame;

Panel type. We recommend that you name it "CreateCircuitPanel" at the end of the Panel;

Bean type. It is recommended that the name end with Bean --- DataAccessBean;

For the EJB type, it is recommended that the name end with EJB -- DBProxyEJB;

For the Applet type, it is recommended that the name end with the Applet --- PictureShowApplet;

 

Example: Change getCustomerInformation () to getCustomerInfo ()

 

Example: protected void setUserName ()

Private void calculateRate ()

 

Example: customes; orderItems

 

Note: although it seems unnecessary to compile functions that can be completed with only one or two lines, the functions can be clarified by using methods to increase program readability and facilitate maintenance and testing.

 

Note: The legality check of interface method parameters between modules usually has two extreme phenomena: either the caller and the called do not check the legality of the parameters, as a result, the necessary process of checking the legitimacy is omitted, which may cause potential problems. Either the caller or the called user checks the legitimacy of the parameters, however, redundant code is generated to reduce the efficiency.

 

Note: logical processing, data, and display should be separated as much as possible during classification. The singleness of class functions.

Example: The data class cannot contain the logic of Array Processing.

The communication class cannot contain the display processing logic

 

Note: If the parent class implements a reasonable toString (), the Child class can inherit and do not have to be rewritten.

Example: public TopoNode

{

Private String nodeName;

Public String toString ()

{

Return "NodeName:" + nodeName;

}

}

 

Example: try

{

//......

}

Catch (IOException ioe)

{

//......

}

Finally

{

Try

{

Out. close ();

}

Catch (IOException ioe)

{

//......

}

}

100. unfriendly attributes, methods, and classes not used in the package are not defined

101. Friendly classes without subclasses should be defined as final methods as final

102. Database Operations, IO operations, and other objects that need to end close () must be close () in try-catch-finally ().

103. Provide a default option for the switch statement

104. Do not assign values to the counter in the for loop body

105. do not define public constructors for non-public classes

106. Do not compare floating point numbers, for example, =! = Reduce <,> operation

107. When the equals () method is implemented, use getClass () or instanceof to compare the types before continuing the comparison.

108. Do not reload the main () method for usage other than the entry

109. Do not use the same name as the method name in the class after the method parameter

110. Do not use the same method name as the class name except the Builder

111. do not define the subclass of Error and RuntimeException. You can define the subclass of Exception.

112. The run () method needs to be implemented in the thread

113. Use equals () to compare whether the values of the two classes are the same

114. When the string is connected to the numeric operation result, the numeric operation part should be enclosed in parentheses.

115. Do not use non-private (public, protected, and friendly) non-static attributes in the class.

116. The class should be defined as an abstract method for interfaces not implemented, and the class should be defined as an abstract class.

117. Do not display the imported java. lang. * package

118. Do not use non-static attributes of the class during initialization

119. display all local variables during initialization,

120. Sort methods by method name, And put methods of the same name and type together

121. Do not call abstract methods in the constructor of the abstract class.

122. The hashcode () method should be overloaded when the equals () method is overloaded.

123. do not define the constructors including private constructors for the tool class

124. Do not use more than 10 case statements in the switch.

125 put the main () method at the end of the class

126. Do not use Exception when the lifecycle method is in violation. Its subclass should be used.

127. Do not directly throw an error and should throw its subclass.

128. During comparison, constants are always placed on the same side.

129. Whenever possible, a default constructor is always defined for the class

130. Exception, RuntimException, and Throwable are not used to capture exceptions.

131. Define constants in interfaces and tool classes,

132. Use uppercase "L" to represent a long constant

133. The method requires comments of the same number of parameters with @ param

134. Do not use unsupported tags in comments @ unsupported

135. Do not use the Runtime, exec () method

136. Do not use custom local methods

137. initialize the capacity when using the set

 

138. Do not call the synchronization method in a loop or use complex expressions.

139. do not define variables in the loop body,

140. initialize capacity when using StringBuffer

141. Use less!

142. Perform the instanceof operation on the interface as much as possible

 

143. Do not display the call to finalize ()

144. Do not use static sets. There is no limit on its memory usage growth.

145. Do not call a method repeatedly to retrieve objects. Use Partial Variables to reuse objects.

146. Use policyall () in the thread to replace policy ();

147. avoid deadlocks caused by calling another synchronous method in the synchronous method,

148. The wait () notify () method cannot be called in non-synchronous Methods

149. Do not use synchronization blocks.

150. Use wait () notify () instead of while () sleep (),

151. Define all public methods as Synchronization Methods

152. The Runable and run () methods must be synchronous methods.

153. The clone () method should and must use super. clone () instead of new

154. Use StringTokenizer instead of indexof () and substrin ()

155. Do not assign values to parameters

156. Do not use the compareTo () equals () method of the String class

157. Do not use the SingletThreadModel of servler, which will consume a large amount of resources.

158. In the Switch, each Case statement should contain either break or return.

159. Do not use NULL for, if, and while statements.

160. do not reduce the data precision in operations.

161. The Case keyword in the Switch statement must be kept with a space with the subsequent constant. do not define useless tags other than Case in the Switch statement.

162. Do not use equal sign = in the if statement to assign values.

163. Static members or methods are accessed using class names without using handles.

164. During method overloading, be sure to have the same method name to avoid using two very similar method names in the class.

165. Do not call the serResize () method in the ComponentListener or ComponentResized () method.

166. Do not overwrite the static and private methods of the parent class.

167. Do not overwrite the attributes of the parent class.

168. Do not use internal classes of two or more levels.

169. Define internal classes as private classes.

170. Remove unnecessary definitions in the interface (do not use public, abstrac, static, final, etc., which is the default definition in the interface ).

171. do not define local variables that will not be used, class private attributes, class private methods and method parameters.

172. explicitly lie all static attributes.

173. Do not use the System getenr () method.

174. Do not hard encode '\ n' and' \ R' as line breaks.

175. Do not directly use the interfaces in java. awt. peer.

176. Use System. arraycopy (). Do not use loops to copy arrays.

177. Avoid unnecessary instenceof comparison and class modeling operations.

178. Do not delete the listeners in the finalize () method ).

179. In the finalize () method, you must call the super finalize () method.

180. Call the super finalize () method in finally of the finalize () method.

181. Use as few temporary variables as possible during character conversion.

182. Call reset () to release an Object after using the Object stream method.

183. In thread synchronization, use conditional testing in the loop (use while (isWait) Wait () instead of if (isWait) Wait ()).

184. Do not use the resume (), suspend (), stop () Methods of the Thread class.

185. Reduce the complexity of a single method. The if, while, for, and switch statements must be less than 10.

186. Reuse the data source connected to JDBC in Servlet.

187. Reduce the Synchronization Methods Used in Servlets.

188. Friendly attributes, methods, and classes that are not used in the package are not defined.

189. Friendly classes without subclasses should be defined as final.

190. Non-covered friendly methods should be defined as final.

191. Provide a default option for the Switch statement.

192. Do not assign values to the counter in the for loop body.

193. do not define a public builder for non-public classes.

194. Do not compare floating point numbers, especially do not perform = ,! = Operation, reduce> <operation.

195. When the equals () method is implemented, the getclass () or instanceof method is used for type comparison before the comparison can continue.

196. Do not reload the main () method for usage other than the open port.

197. The parameter name of the method should not be the same as the Chinese method name of the class.

198. Do not use the same method name as the class name except the builder.

199. do not define the subclasses of Error and RuntimeException. You can define the subclasses of Exception.

200. run () must be implemented in the thread.

201. Use equals () to compare whether the values of the two classes are the same.

202. When the string is connected with the numeric operation result, enclose the numeric operation part in parentheses.

203. Do not use non-private (public month, protected and friendly) non-static attributes in the class.

204. For interfaces not implemented in a class, they should be defined as abstract methods, and classes should be defined as abstract classes (Level 5)

205. Do not display the imported Java. lang. * package.

206. Do not use non-static attributes of the class during initialization.

207. display all local variables initialized.

208. Sort methods by method name. Methods of contract types with the same name should be put together.

209. Do not use nested values, that is, use multiple = values in an expression.

210. Do not call abstract methods in the abstract class builder.

211. When the equals () method is overloaded, the hush code () method should also be overloaded.

212. The tool class (utility) should not define the builder. Includes a private builder.

213. Do not use more than 10 case statements in the switch.

214 put the main () method at the end of the class.

215. Do not use Exception when declaring a method violation. Use its subclass.

216. Do not directly generate an Error and should throw its subclass.

217. During comparison, constants are always placed on the same side (both on the left or on the right ).

218. Whenever possible, a default constructor is always defined for the class.

219. Exception, runtimeException, and Throwable are not used to capture exceptions.

220. Define constants (Level 5) in interfaces or tool classes ).

221. Use uppercase "L" to represent a Long constant (Level 5 ).

222. The main () method must be public static void main (String []).

223. The return type is boolean and the return type starts with "is". Other types cannot be used.

224. Start with get for a non-boolean type value method (getter). Other types cannot be used.

225. The setter method starts with set. Other types cannot be used.

226. The method requires the annotation @ param of the same number of parameters.

227. Do not use unsupported tags in comments, such as @ unsupported.

228. Do not use the Runtime, exec () method.

229. Do not customize native methods ).

230. Use operators that are as concise as possible.

231. Set the initial capacity when using the set.

232. Do not use startsWith () for the comparison of a single first character ().

233. Use the shift operator >,< for the multiplication and division operations of the power N of the divisor or the multiplier of 2.

234. Use ''for a character connection instead of" ", for example, String a = B + 'C '.

235. Do not call the synchronization method in the loop body or use the try-catch Block.

236. Do not use unnecessary boolean values for comparison, such as if (a equals (B) instead of if (a equals (B) = true ).

237. The constant String uses String, and the constant String uses StringBuffer.

238. Do not use complex expressions when determining cyclic conditions.

239. For the "if (condition) do1; else do2;" Statement, use the conditional operator "if (condition )? Do1: do2 ".

240. do not define variables in the loop body.

241. Set the initial capacity when using StringBuffer.

242. Use local variables as much as possible for calculation.

243. Use "!" as few as possible Operator.

244. Perform the instanceof operation on the interface as much as possible.

245. Do not use Date [] instead of long.

246. Do not explicitly call finalize ().

247. Do not use static sets. There is no boundary for its memory usage growth.

248. Do not call a method repeatedly to retrieve objects. Use local variables to reuse objects.

249. In thread synchronization, replace policyall () with policy ()

250. Avoid deadlock caused by the inability to call another synchronous method in the synchronous method.

251. The wait () and notify () methods cannot be called in non-synchronous methods.

252. Use wait (), notify () instead of while (), sheep ().

253. Do not use the synchronization method. Use the synchronization block (Level 5 ).

254. Define all public methods as synchronous methods (Level 5 ).

255. the Runnable run () method must be a synchronous method (Level 5 ).

256. A class with only the final static attribute of the abstract method should be defined as an interface.

257. The clone () method should and must use super clone () instead of new.

258. constants must be defined as final.

259. Provide termination conditions in the for loop.

260. Use incremental count in the for and while loops.

261. Use String Tokenizer instead of indexof () and substring.

262. Do not use non-final methods in the constructor.

263. Do not assign values to parameters.

264. Do not compare the classes of two objects by name. getclass () should be used ().

265. Security: Do not use internal classes whenever possible.

266. Security: Do not use classes to clone.

267. Security: Do not use interfaces for serialization.

268. Security: Try not to use friendly methods, attributes, and classes.

269. servlet: Do not use the Javabeans Beans instantiate () method.

270. servlet: Do not use the inralidate method to release Http sessions as soon as possible.

271. servlet: Use the close () method to release JDBC resources.

272. servlet: Do not use servlet's Single Thread Monde, which consumes a lot of resources.

273. Internationalization: Do not use one character for logical operations. Use mescript Format

274. Internationalization: Do not use the Date to String (), Time to String () methods.

275. Internationalization: character and string constants should be placed in the resource file

276. Internationalization: The character uses the toString () method.

277. Internationalization: Do not use the StringBuffer String Tokenizer class.

278. Internationalization: Do not use the compareTo () and equals () Methods of the String class.

279. Complexity: Recommended maximum scale:

280. Inheritance layer 5

281. 1000 rows of the class (including {})

282. 10 class attributes

283. 20 class methods

284. 10 methods for class friends

285. 15 private class methods

286. 10 class protection methods

287. 10 public class methods

288. 20 class call Methods

289. Five method parameters

290. Return Statement 1

291. 30 rows of methods

292. Method Code 20

293. Comment rate 30% ''50%

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.