{"id":50536,"date":"2018-04-17T15:52:34","date_gmt":"2018-04-17T10:22:34","guid":{"rendered":"https:\/\/asha24.com\/blog\/?p=50536"},"modified":"2023-02-03T17:56:27","modified_gmt":"2023-02-03T12:26:27","slug":"must-have-java-code-style-guide","status":"publish","type":"post","link":"https:\/\/asha24.net\/blog\/must-have-java-code-style-guide\/","title":{"rendered":"The Must Have Java Code Style Guide"},"content":{"rendered":"\n[et_pb_section bb_built=&#8221;1&#8243; _builder_version=&#8221;3.0.47&#8243;][et_pb_row][et_pb_column type=&#8221;4_4&#8243;][et_pb_post_title _builder_version=&#8221;3.0.106&#8243; title=&#8221;on&#8221; meta=&#8221;off&#8221; author=&#8221;on&#8221; date=&#8221;on&#8221; categories=&#8221;on&#8221; comments=&#8221;on&#8221; featured_image=&#8221;off&#8221; featured_placement=&#8221;below&#8221; text_color=&#8221;dark&#8221; text_background=&#8221;off&#8221; title_font=&#8221;Source Sans Pro||||||||&#8221; title_font_size=&#8221;47&#8243; title_font_size_last_edited=&#8221;on|desktop&#8221; custom_margin=&#8221;|||10%&#8221; \/][\/et_pb_column][\/et_pb_row][et_pb_row _builder_version=&#8221;3.0.47&#8243; background_size=&#8221;initial&#8221; background_position=&#8221;top_left&#8221; background_repeat=&#8221;repeat&#8221;][et_pb_column type=&#8221;4_4&#8243;][et_pb_text _builder_version=&#8221;3.0.106&#8243; background_size=&#8221;initial&#8221; background_position=&#8221;top_left&#8221; background_repeat=&#8221;repeat&#8221; background_layout=&#8221;light&#8221; text_font=&#8221;Titillium Web|300|||||||&#8221; text_font_size=&#8221;20&#8243; text_text_color=&#8221;rgba(0,0,0,0.8)&#8221; text_line_height=&#8221;1.6em&#8221; max_width=&#8221;800px&#8221; custom_margin=&#8221;|||10%&#8221;]\r\n\r\n<strong>Java Code Style Guide<\/strong>\r\n\r\nRefer to the content below for guidance on structuring more readable and consistently formatted Java code, files, and package structures.\r\n<ol>\r\n \t<li><strong> Definitions and abbreviations<\/strong><\/li>\r\n<\/ol>\r\n<table width=\"402\">\r\n<tbody>\r\n<tr>\r\n<td width=\"91\">Term<\/td>\r\n<td width=\"311\">Definition<\/td>\r\n<\/tr>\r\n<tr>\r\n<td width=\"91\">Camel Case<\/td>\r\n<td width=\"311\">All words in the name are joined together. The first word starts off with a lower case letter; every word thereafter begins with a capital letter. (iAmCamelCase)<\/td>\r\n<\/tr>\r\n<tr>\r\n<td width=\"91\">Pascal Case<\/td>\r\n<td width=\"311\">All words in the name are joined together. Each new word begins with a capital letter. (IAmPascalCase)<\/td>\r\n<\/tr>\r\n<tr>\r\n<td width=\"91\">UI<\/td>\r\n<td width=\"311\">User interface<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<ol start=\"2\">\r\n \t<li><strong> Layout of source files<\/strong><\/li>\r\n<\/ol>\r\nThe layout for a class will be broken up into the following main sections:\r\n<ul>\r\n \t<li>File Description<\/li>\r\n \t<li>Package Name<\/li>\r\n \t<li>Imports<\/li>\r\n \t<li>Constants<\/li>\r\n \t<li>Methods<\/li>\r\n \t<li>Protected and private members.<\/li>\r\n<\/ul>\r\nEach section is explained below.\r\n\r\n<strong>File description<\/strong>\r\n\r\nThis is a brief description of what the file is about, what it does and represents. It should describe the overview of how to use the file (or classes therein). Java Doc compatible commenting style is required.\r\n<ol>\r\n \t<li>Brief description about the file<\/li>\r\n \t<li>A list of any modifications made subsequent to the first production release of the application any change request numbers<\/li>\r\n<\/ol>\r\n<blockquote>\/**\r\n\r\n**File description\r\n\r\n<a href=\"mailto:*@Description\">*@Description<\/a> of Modification\r\n\r\n*@Change Request No.\r\n\r\n*\/<\/blockquote>\r\n<strong>Package name<\/strong>\r\n\r\nPackage names should occur on the first non-commented line of the source file, should follow standard naming convention defined, and be separated by a blank line on either side. Packages should include a package-info.java file that provides a repository for package level annotations and documentation.\r\n\r\nImports\r\n\r\nImmediately following the package name should be the imported class names. There should not be any wild card import statements (e.g. import java.util.*). Optimize the imports by removing extraneous include statements.\r\n<ol start=\"3\">\r\n \t<li><strong> Declarations<\/strong><\/li>\r\n<\/ol>\r\n<strong>Packages<\/strong>\r\n\r\nAll projects need to pick a prefix. The project may split itself into any number of packages below this prefix.\r\n<ul>\r\n \t<li>Package names are always lower case<\/li>\r\n \t<li>Package names developed for the client begin with followed by the client organization\u2019s internal convention, UNLESS otherwise specified by the client<\/li>\r\n \t<li>Examples of top level domains currently include com, edu, gov, mil, net, org, or one of the English two-letter codes identifying countries as specified in ISO Standard 3166, 1981<\/li>\r\n<\/ul>\r\n<strong>File names<\/strong>\r\n<ul>\r\n \t<li>File names must always be in Pascal case<\/li>\r\n \t<li>Match the name of the primary public class\/interface contained in the file<\/li>\r\n \t<li>Each class file will be in a separate source file. Note: This does not apply to inner classes and anonymous classes<\/li>\r\n \t<li>Property files for applications should end with .properties<\/li>\r\n<\/ul>\r\nE.g.\r\n<blockquote>WidgetFactory.java\r\n\r\nInvalidTransactionException.java<\/blockquote>\r\n<strong>Classes and Interfaces<\/strong>\r\n<ul>\r\n \t<li>These must follow Pascal case; avoid use of underscores (\u201c_\u201d)<\/li>\r\n \t<li>The first letter of subsequent words in the name should be upper case<\/li>\r\n \t<li>Append \u201cimpl\u201d on classes that implement interfaces, e.g. DataConnectionPoolImpl implements DataConnectionPool.<\/li>\r\n \t<li>Class names should be nouns<\/li>\r\n \t<li>Classes built on arrays, vectors, and collections should include the type at the end of the name<\/li>\r\n<\/ul>\r\nE.g.\r\n<blockquote>WidgetFactory\r\n\r\nWidgetCallbackDelegate\r\n\r\nInvalidTransactionException\r\n\r\n\/\/DAO = Data Access Object\r\n\r\nIlimsDatabaseDAO<\/blockquote>\r\n<strong>Methods<\/strong>\r\n<ul>\r\n \t<li>Methods should use \u201ccamel\u201d case (first letter is lower case)<\/li>\r\n \t<li>Method names should be a verb, verb\/noun, or verb\/adjective\/noun combination<\/li>\r\n \t<li>Method parameter names should use camel case, e.g. private void someMethod(String someString)<\/li>\r\n \t<li>Methods retrieving the value of a variable or object (accessors) should be in the form getName, unless the value is a boolean<\/li>\r\n \t<li>Accessors retrieving the value of a boolean should start with is or has (e.g. isManager, hasBaseAccount)<\/li>\r\n \t<li>Methods updating the value of a variable or object (mutators) should be in the form setName<\/li>\r\n \t<li>Methods returning arrays, vectors, lists, etc. should include the type in the name<\/li>\r\n<\/ul>\r\nNote: In addition to naming conventions, the maintainability of fields is achieved by the appropriate use of accessor member methods, methods that provide the functionality to either update a field or to access its value. There are two types of accessor methods: get methods and set methods (also called mutators). A get method returns a parameter\u2019s value for you a set method modifies it. Although accessor methods previously added overhead to the code, this is no longer true. Java compilers are now optimized for their use. Accessors help to hide the implementation details of the class. By having at most two control points from which a variable is accessed, a get and a set method, you are able to increase the maintainability of your classes by minimizing the points at which changes need to be made in the event of changes to business rules surrounding the attribute.\r\n\r\nE.g.\r\n<blockquote>public void processData(&#8230;)\r\n\r\nprivate void sendEmail(&#8230;)\r\n\r\npublic String[] getStringData(&#8230;)<\/blockquote>\r\n<strong>Variables<\/strong>\r\n<ul>\r\n \t<li>Declare one variable per line<\/li>\r\n \t<li>Whenever possible, initialize a variable when it is created<\/li>\r\n \t<li>Declare variables at the beginning of a block of code<\/li>\r\n \t<li>Use meaningful variable names which reflect the purpose and are unambiguous in the scope in which they reside<\/li>\r\n<\/ul>\r\nE.g.\r\n<blockquote>int indentLevel; \/\/ indentation level\r\n\r\nint numOfRows; \/\/ number of rows in table\r\n\r\nint i, j, k; \/\/ ok, unless used in nested loops<\/blockquote>\r\n<ul>\r\n \t<li>These must follow camel casing<\/li>\r\n \t<li>Variable names should be short, but meaningful, suggesting the intent of the use<\/li>\r\n \t<li>Variable names should not start with an underscore (&#8220;_&#8221;) or dollar sign (&#8220;$&#8221;), though both are allowed<\/li>\r\n \t<li>Underscores are acceptable on private class variables<\/li>\r\n \t<li>Upper-case with underscores are acceptable on static final class variables<\/li>\r\n<\/ul>\r\nE.g.\r\n<blockquote>public class Blah {\r\n\r\nprivate static final int MY_SIZE = 8192;\r\n\r\nprivate string myString;\r\n\r\npublic void someMethod() {\r\n\r\nint recordCount;\r\n\r\n}<\/blockquote>\r\n<strong>Constants<\/strong>\r\n<ul>\r\n \t<li>Names of constants and ANSI characters should be all upper case<\/li>\r\n \t<li>Words are separated by underscores (&#8220;_&#8221;)<\/li>\r\n<\/ul>\r\n<ol start=\"4\">\r\n \t<li><strong> Commenting code<\/strong><\/li>\r\n<\/ol>\r\nIn general code should be readable and self-commenting. Comments should only be provided for additional discussion is warranted.\r\n\r\nFor a detailed reference on commenting in Java Code, refer to JavaDoc (refer to Oracle for guidance on the JavaDoc standard supported by your deployed release of Java).\r\n\r\nSource code can have three styles of implementation comments: block, single-line, and trailing.\r\n\r\n<strong>Block comments<\/strong>\r\n\r\nBlock comments are used to provide descriptions of files, methods, data structures and algorithms. Block comments should be used at the beginning of each file and before each method. They can also be used in other places, such as within methods. Block comments inside a function or method should be indented to the same level as the code they describe.\r\n\r\nA block comment should be preceded by a blank line to set it apart from the rest of the code.\r\n\r\nBlock comments have an asterisk \u201c*\u201d at the beginning of each line except the first.\r\n\r\n\/*\r\n\r\n* Here is a block comment.\r\n\r\n*\/\r\n\r\nBlock comments can start with \/*-, which is recognized by indent(1) as the beginning of a block comment that should not reformatted. Example:\r\n\r\n\/*\r\n\r\n* Here is a block comment with some very special\r\n\r\n* formatting that I want indent (1) to ignore.\r\n\r\n*\r\n\r\n* one\r\n\r\n* two\r\n\r\n* three\r\n\r\n*\/\r\n\r\nNote: If you don\u2019t use indent(1), you don\u2019t have to use \/*- in your code or make any other concessions to the possibility that someone else might run indent(1) on your code.\r\n\r\nSingle-line comments\r\n\r\nShort comments can appear on a single line indented to the level of the code that follows. If a comment can\u2019t be written in a single line, it should follow the block comment format (see section 6.4.1). A single-line comment should be preceded by a blank line. Here\u2019s an example of a single-line comment in Java:\r\n<blockquote>if (\u201dQuarterly\u201d.equalsIgnoreCase(frequency)) {\r\n\r\n\/* Exclude all entitlements that do not require quarterly review *\/\r\n\r\n&#8230;\r\n\r\n}<\/blockquote>\r\n<strong>Trailing comments<\/strong>\r\n\r\nVery short comments can appear on the same line as the code they describe, but should be shifted far enough to separate them from the statements. If more than one short comment appears in a chunk of code, they should all be indented identically, using tabs. When modifying block of code resulting in indentation changes, re-align any comments. Avoid commenting every line of executable code with a trailing comment. These comments can also be written using the \/\/ comment delimiter.\r\n\r\nHere\u2019s an example of a trailing comment in Java code:\r\n<blockquote>\/\/check if the account is active\r\n\r\nif (identity.isActive() == true) {\r\n\r\nreturn true; \/* account is active *\/\r\n\r\n} else {\r\n\r\nreturn false \/* account is disabled *\/\r\n\r\n}<\/blockquote>\r\n<strong>JavaDoc Annotation<\/strong>\r\n\r\n\u200bAll classes and interfaces, and all public and protected class\/interface members, must have Java Doc comments. Example:\r\n<blockquote>\/**\r\n\r\n* Purges old data files.\r\n\r\n*\/\r\n\r\npublic class PurgeOldFiles() {\r\n\r\n\/\/ This is a single line comment.\r\n\r\n\/**\r\n\r\n* A string that says hello.\r\n\r\n*\/\r\n\r\nprotected String myString = &#8220;hello&#8221;;\r\n\r\n\/**\r\n\r\n* Constructor.\r\n\r\n*\r\n\r\n* @param config Configuration parameters for this action\r\n\r\n*\/\r\n\r\npublic PurgeOldFiles(FilePurgeConfiguration config) {\r\n\r\nsuper(config);\r\n\r\n}\r\n\r\n\/**\r\n\r\n* Execute the business logic for this action.\r\n\r\n* @exception Exception If an error occurs while performing the action\r\n\r\n* @return True if the action executed successfully, false otherwise\r\n\r\n*\/\r\n\r\nprotected boolean performAction() throws Exception {\r\n\r\nreturn true;\r\n\r\n}\r\n\r\n}\r\n\r\nMethods that simply expose properties do not require comments.\r\n\r\n\/**\r\n\r\n* This is a class representing a bit of material.\r\n\r\n*\/\r\n\r\npublic class Material\r\n\r\n{\r\n\r\nprivate String compoundNumber = null;\r\n\r\nprivate int counter = 0;\r\n\r\n\/\/no method comments required here\r\n\r\npublic void setCompoundNumber(String compoundNumber) {\r\n\r\nthis.compoundNumber = compoundNumber;\r\n\r\n}\r\n\r\n\/**\r\n\r\n* Non-obvious &#8220;side effects&#8221; must be commented.\r\n\r\n*\/\r\n\r\npublic int setCounter(int counter) {\r\n\r\nthis._counter = counter &#8211; 20;\r\n\r\n}\r\n\r\n}<\/blockquote>\r\nAnnotation gives the ability to provide additional metadata alongside a Java entity (such as classes, interfaces, fields and methods). This additional metadata, called annotation, can be read and interrelated by the compiler or other utilities. They can also be stored in the class files. The runtime can discovered these metadata via the &#8220;reflection&#8221; API. When the Java class extends another class and overrides a method, @Overrride annotation must be used in that method.\r\n\r\nExample:\r\n<blockquote>public class AnnotationOverrideTest {\r\n\r\n@Override\r\n\r\npublic String toString() {\r\n\r\nreturn &#8220;Override the toString() of the superclass&#8221;;\r\n\r\n}\r\n\r\n\/\/ Compilation Error because superclass Object does not have this method\r\n\r\n@Override\r\n\r\npublic String toString123() {\r\n\r\nreturn &#8220;Override the toString123() of the superclass&#8221;;\r\n\r\n}\r\n\r\n}<\/blockquote>\r\n<ol start=\"5\">\r\n \t<li><strong> Exception handling<\/strong><\/li>\r\n<\/ol>\r\n<strong>Unchecked &amp; run-time exceptions<\/strong>\r\n\r\nUse unchecked, run-time exceptions to report serious unexpected errors that may indicate an error in the program&#8217;s logic.\r\n\r\nCatching and handling run-time exceptions is possible, however they are usually of such a severe nature that program termination is imminent. Run-time exceptions are usually thrown because of programming errors, such as failed assertions, using an out-of-bound index, dividing by zero, or referencing a null pointer.\r\n\r\n<strong>Checked exceptions<\/strong>\r\n\r\nUse checked exceptions to report errors that may occur, however rarely, under normal program operation. Checked exceptions indicate a serious problem that should not occur under normal conditions. The caller must catch this exception. Depending upon the application, a program may be able to recover from a checked exception; that is, it does not indicate a fundamental flaw in the program&#8217;s logic. Exceptions should be handled at the correct location in code and not just reported.\r\n\r\n<strong>Example of checked exceptions<\/strong>\r\n<blockquote>class ThrowsDemo {\r\n\r\n&#8230;\r\n\r\npublic String getLanguage(SailPointContext context,\r\n\r\nEmailTemplate template, EmailOptions options)\r\n\r\nthrows GeneralException\r\n\r\n{\r\n\r\nLogger log = Logger.getLogger(ThrowsDemo.class);\r\n\r\nString defaultUserLanguage;\r\n\r\nString recipient;\r\n\r\nString recipientLanguage = null;\r\n\r\nString newTemplateName;\r\n\r\n\/\/Retrieving the default user language from the system configuration.\r\n\r\ndefaultUserLanguage = context.getConfiguration().getString(\r\n\r\n&#8220;llydefaultUserLanguage&#8221;);\r\n\r\nif (template != null) {\r\n\r\nrecipient = template.getTo();\r\n\r\n} else {\r\n\r\nlog.error(&#8220;Default template not found in IIQ&#8221;);\r\n\r\nthrow new GeneralException(&#8220;Default template not found in IIQ&#8221;);\r\n\r\n}\r\n\r\ntry {\r\n\r\nrecipientLanguage = getRecipientLanguage(context, recipient);\r\n\r\n}\r\n\r\ncatch (Exception e) {\r\n\r\nlog.error(&#8220;Recipient&#8217;s user language could not be retrieved. Using default language.&#8221;, e);\r\n\r\n}\r\n\r\nfinally {\r\n\r\nif (recipientLanguage != null) {\r\n\r\nreturn recipientLanguage;\r\n\r\n} else {\r\n\r\nreturn defaultUserLanguage;\r\n\r\n}\r\n\r\nlog.close();\r\n\r\n}\r\n\r\n}\r\n\r\n}<\/blockquote>\r\n<strong>Absorbing exceptions<\/strong>\r\n\r\nDo not silently absorb a run-time or error exception. Breaking this rule makes code hard to debug because information is lost:\r\n<blockquote>try {\r\n\r\nfor(int i = v.size(); &#8211;i &gt;= 0;) {\r\n\r\nostream.println(v.elementAt(i) };\r\n\r\n}\r\n\r\ncatch (ArrayOutOfBounds e) {\r\n\r\n\/\/ Oops! We should never get here. ..\r\n\r\n\/\/. ..but if we do, nobody will ever know!\r\n\r\n}<\/blockquote>\r\nEven if We have coded a catch block simply to catch an exception We do not expect to occur, log the exception. We never know when something &#8220;impossible&#8221; might occur within our software:\r\n<blockquote>try {\r\n\r\nfor (int i=v.size(); &#8211;i&gt;=0;) {\r\n\r\nostream.println(v.elementAt(i));\r\n\r\n}\r\n\r\ncatch (ArrayOutOfBounds e) {\r\n\r\n\/\/ Oops! Should never get here. ..\r\n\r\n\/\/ But print a stack trace just in case&#8230;\r\n\r\nLog.error(e);\r\n\r\n}<\/blockquote>\r\n<strong>Release resources<\/strong>\r\n\r\nThe finally clause can be used to \u201ccleanup\u201d (e.g. release resources, close statements) after the execution of the try catch block. This prevents repetitious code in the catch clauses and prevents error by forgetting to add this functionality while adding code to handle another type of exception. Remember that the finally clause is executed irrespective of whether the try block executed the normal sequence or an exception happened. This makes the finally block a good place to release any resources acquired prior to entering or within the try-block.\r\n\r\nIn this first example, if an exception or return occurs following the creation of the output stream, the function will exit without closing and flushing the stream:\r\n<blockquote>public void logSomeStuff() {\r\n\r\nOutputStream log = new FileOutputStream(&#8220;log&#8221;);\r\n\r\n&#8230;\r\n\r\n\/\/ could get exception here!\r\n\r\n&#8230;\r\n\r\nlog.close();\r\n\r\n}<\/blockquote>\r\nIn this example, we use a finally block to ensure the stream is always closed when the thread of execution exits the try-block. This is done even if the thread exits the block because an exception has been thrown or a return statement was executed:\r\n<blockquote>OutputStream outputStream = null;\r\n\r\ntry {\r\n\r\noutputStream = new FileOutputstream(&#8220;log&#8221;);\r\n\r\n}\r\n\r\nfinally {\r\n\r\nif (outputStream!= null) {\r\n\r\noutputStream.close();\r\n\r\n}<\/blockquote>\r\n<ol start=\"6\">\r\n \t<li><strong> Formatting of statement<\/strong><\/li>\r\n<\/ol>\r\n<strong>Classes and methods<\/strong>\r\n\r\nUse the following format for class and method declaration:\r\n<ul>\r\n \t<li>An open brace &#8220;{&#8221; appears at the end of the same line as the declaration statement<\/li>\r\n \t<li>A closing brace &#8220;}&#8221; starts a line by itself and is indented to the corresponding opening brace<\/li>\r\n \t<li>Methods are separated by a blank line<\/li>\r\n \t<li>No space between the method name and the parenthesis<\/li>\r\n \t<li>Follow the same convention throughout the entire file.<\/li>\r\n<\/ul>\r\n<blockquote>public class MyClass extends SomeClass implements ISomeInterface {\r\n\r\npublic MyClass() { }\r\n\r\npublic MyClass(int myInt) {\r\n\r\nsuper(myInt);\r\n\r\n}\r\n\r\npublic void doSomething () {\r\n\r\nreturn;\r\n\r\n}\r\n\r\n}<\/blockquote>\r\n<strong>Parentheses and braces<\/strong>\r\n\r\nUse either of the following formats for parenthesis and braces:\r\n<ul>\r\n \t<li>An open brace \u201c{\u201c or closing brace &#8220;}&#8221; appears on the same line as the control statement<\/li>\r\n \t<li>The closing brace is indented to the corresponding opening brace.<\/li>\r\n<\/ul>\r\nFollow the same convention throughout the entire file.\r\n<blockquote>if (&#8230;) {\r\n\r\n&#8230;\r\n\r\n} else if (&#8230;) {\r\n\r\n&#8230;\r\n\r\n} else {\r\n\r\n&#8230;\r\n\r\n}\r\n\r\n\u2026or for a do-while loop:\r\n\r\ndo {\r\n\r\n&#8230;\r\n\r\n} while (&#8230;);\r\n\r\nBraces are required if there are one or more statements in the loop or logical control block.\r\n\r\n\/\/acceptable!\r\n\r\nif (noBracesOnNonEmptyBlock)\r\n\r\nfailCodingStandards();\r\n\r\n\/\/unacceptable!\r\n\r\nif (noBracesOnNonEmptyBlock)\r\n\r\nif(x&gt;6)\r\n\r\nfailCodingStandards();\r\n\r\nelse\r\n\r\nfailCodingStandards();\r\n\r\nelse\r\n\r\nfailCodingStandards();\r\n\r\nEmpty blocks may have both braces on the same line\r\n\r\nif (emptyBlock) { }\r\n\r\nLine breaks\r\n\r\nEach line should contain only one statement. The exception is the &#8220;? :&#8221; operator pair.\r\n\r\n\/\/unacceptable\r\n\r\nmyInt++; myOtherInt = 5;\r\n\r\n\/\/OK\r\n\r\nmyDogHasFleas ? batheDog() : hugDog();\r\n\r\nWhen an expression will not fit on a single line, break it after a comma or operator.\r\n\r\nlongMethodCall(expr1, expr2,\r\n\r\nexpr3, expr4, expr5);\r\n\r\nresult = a * b \/ (c &#8211; g + f) +\r\n\r\n4 * z;\r\n\r\nstring s = &#8220;give me &#8221; +\r\n\r\n&#8220;a break&#8221;;\r\n\r\nFor \/ For-each\r\n\r\nA for statement must use the following format:\r\n\r\nfor (int i = 0; i &lt; 5; i++)<\/blockquote>\r\nAn empty for block must use the following format:\r\n\r\nfor (initialization; condition; update) ;\r\n\r\nIn Java, as much as possible use the enhanced for construct introduced in Java 5:\r\n\r\nfor (AccountRequest accountRequest : accountRequestList) ;\r\n\r\nAvoid using indexed for loops with collections without random access (e.g. LinkedList). Use an iterator instead.\r\n<blockquote>While\r\n\r\nAn empty while block must use the following format:\r\n\r\nwhile (condition) ;\r\n\r\nSwitch\r\n\r\nSwitch\/case statements must use the following format:\r\n\r\n\/\/preferred\r\n\r\nswitch (condition) {\r\n\r\ncase A:\r\n\r\nbreak;\r\n\r\ndefault:\r\n\r\nbreak;\r\n\r\n}<\/blockquote>\r\n<ol start=\"7\">\r\n \t<li><strong> Class definition<\/strong><\/li>\r\n<\/ol>\r\nClass header block should be used with following items\r\n\r\n<strong>For CLASS:<\/strong>\r\n<blockquote>\/**\r\n\r\n* The descriptive text that explaining the purpose and use of the class.\r\n\r\n* @version\r\n\r\n* @Class name\r\n\r\n* @ History\r\n\r\n*\/<\/blockquote>\r\nThis section is the actual implementation of the class. Each method (and private function) will be prefaced by the standard documentation header.\r\n\r\n<strong>Methods and functions<\/strong>\r\n\r\nThe following standard has been established for the documentation of methods and functions.\r\n\r\nEach line item begins with an asterisk, and ends with blank space. All subsequent lines in multiline component are to be indented so that they line up vertically with the previous line.\r\n\r\n<strong>For example <\/strong>\r\n<blockquote>\/**\r\n\r\n*\r\n\r\n*\r\n\r\n* @param\r\n\r\n* @return\r\n\r\n* @exception\r\n\r\n**\/<\/blockquote>\r\n<strong>Description <\/strong>\r\n\r\nProvide a detailed description. This may include:\r\n<ul>\r\n \t<li>intent of method<\/li>\r\n \t<li>pre and post conditions<\/li>\r\n \t<li>side effects<\/li>\r\n \t<li>dependencies<\/li>\r\n \t<li>implementation notes<\/li>\r\n \t<li>who should be calling this method<\/li>\r\n \t<li>whether the method should or should not be overridden (if the method cannot be overwritten, it should be marked as final)<\/li>\r\n \t<li>where to invoke super when overriding<\/li>\r\n \t<li>control flow or state dependencies that need to exist before calling this method<\/li>\r\n \t<li>use of variables outside the scope of the function (i.e. global and super scopes in Beanshell)<\/li>\r\n<\/ul>\r\n<strong>Params section <\/strong>\r\n\r\nDescribes the type, class, or protocol of all the method or routine arguments. Should describe the parameters intended use (in, out, in\/out) and constraints.\r\n\r\n<strong>For example <\/strong>\r\n\r\n* @param aSource &#8211; the input source string. Cannot be 0-length.\r\n\r\n<strong>Returns section<\/strong>\r\n\r\nThis section is used to describe the method\/routine return type. Specifically, it needs to detail the actual data type returned, the range of possible return values, and where applicable, error information returned by the method. Also note that if the return value is self, we do not have to specify a type (defaults to ID). All other cases require an explicit return type specification.\r\n\r\n<strong>For example<\/strong>\r\n\r\n* @return Possible values are 1..n.\r\n\r\n<strong>Exception section<\/strong>\r\n\r\nThe purpose of this section is a complete description of all the non-system exceptions that this method throws. A description detailing whether or not the exception is recoverable should be included. If applicable, a recovery strategy for the exception can be described here.\r\n\r\n<strong>For example<\/strong>\r\n\r\n* @exception ResourceNotFoundException. recoverable, try another resource\r\n<ol start=\"8\">\r\n \t<li><strong> Additional guidance<\/strong><\/li>\r\n<\/ol>\r\n<ul>\r\n \t<li>Avoid the use of strings when concatenating.<\/li>\r\n \t<li>It is advisable not to code numerical constants (literals) directly, except for -1, 0, and 1, which can appear in a \u201cfor\u201d loop as counter values. This is advisable but not compulsory.<\/li>\r\n \t<li>Use parentheses liberally in expressions involving mixed operators to avoid operator precedence problems.<\/li>\r\n<\/ul>\r\n[\/et_pb_text][\/et_pb_column][\/et_pb_row][et_pb_row][et_pb_column type=&#8221;4_4&#8243;][et_pb_team_member _builder_version=&#8221;3.0.106&#8243; name=&#8221;Mahesh J&#8221; position=&#8221;Author&#8221; facebook_url=&#8221;&#8221; twitter_url=&#8221;&#8221; google_url=&#8221;&#8221; linkedin_url=&#8221;&#8221; image_url=&#8221;https:\/\/asha24.net\/blog\/\/wp-content\/uploads\/2018\/03\/123.gif&#8221; background_layout=&#8221;light&#8221; header_font=&#8221;Titillium Web|700|||||||&#8221; body_font=&#8221;Titillium Web||||||||&#8221; body_font_size=&#8221;16&#8243;]\r\n\r\nHello all! I\u2019m a nature\u2019s child, who loves the wild, bringing technical knowledge to you restyled.\r\n\r\n[\/et_pb_team_member][\/et_pb_column][\/et_pb_row][\/et_pb_section]\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":4,"featured_media":50618,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"on","_et_pb_old_content":"<form id=\"aspnetForm\" action=\"\/tmc\/tech-coding-stds\/pages\/java-code-style-guide.aspx\" method=\"post\"><div id=\"s4-workspace\"><div id=\"s4-bodyContainer\"><div id=\"contnetwrapper\"><div id=\"centerarea\" class=\"clearfix\"><div id=\"MainRow\"><div id=\"LandingpageWithNav\" class=\"drresponsive\"><div id=\"col2\" class=\"dyncol col-xs-12 col-md-9 pl15 pr15\"><div id=\"c2row1\" class=\"glb-wp glb-wpTitleBorder\"><div class=\"ms-webpart-zone ms-fullWidth\"><div id=\"MSOZoneCell_WebPartWPQ2\" class=\"s4-wpcell-plain ms-webpartzone-cell ms-webpart-cell-vertical ms-fullWidth  firstcontainer\"><div class=\"ms-webpart-chrome ms-webpart-chrome-fullWidth  setborder firstcontainer\"><div id=\"WebPartWPQ2\" class=\"ms-WPBody ms-WPBorder ms-wpContentDivSpace  setpadding\"><div class=\"ms-rtestate-field\"><p><span class=\"ms-rteStyle-BodyText\">Before implementing any code, you should identify an appropriate version control system (VCS), code versioning system (CVS), or other code management repository that enables your team to collaborate on developing code for your implementation. This system should be coupled with a build system or tool, such as gradle, maven, ANT, etc, that automates the process of building code packages targeted to lower and production environments at your client. Note that these tools are often available from the client. If not, you should work with your client to implement or otherwise make such tools available. <\/span><\/p><p><span class=\"ms-rteStyle-BodyText\">Do not collaborate on the same code base without rigorous use of a code management repository and build automation, and especially do not maintain your code in the deployed application versus importing from an external code management system. Doing so has caused significant headaches for teams that not only develop the original solution, but who are also brought in to update the code after it is deployed.<\/span><\/p><\/div><\/div><\/div><\/div><\/div><\/div><\/div><\/div><\/div><\/div><\/div><\/div><\/div><\/form>","_et_gb_content_width":"","footnotes":""},"categories":[61],"tags":[],"class_list":["post-50536","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v17.0 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>The Must Have Java Code Style Guide - Asha24 Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/asha24.net\/blog\/must-have-java-code-style-guide\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"The Must Have Java Code Style Guide - Asha24 Blog\" \/>\n<meta property=\"og:url\" content=\"https:\/\/asha24.net\/blog\/must-have-java-code-style-guide\/\" \/>\n<meta property=\"og:site_name\" content=\"Asha24 Blog\" \/>\n<meta property=\"article:published_time\" content=\"2018-04-17T10:22:34+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-02-03T12:26:27+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/asha24.net\/blog\/wp-content\/uploads\/2018\/04\/pexels-photo-265110.jpeg\" \/>\n\t<meta property=\"og:image:width\" content=\"1953\" \/>\n\t<meta property=\"og:image:height\" content=\"1500\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Mahesh\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"14 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/asha24.net\/blog\/#website\",\"url\":\"https:\/\/asha24.net\/blog\/\",\"name\":\"Asha24 Blog\",\"description\":\"Dedication Towards Learning\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/asha24.net\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/asha24.net\/blog\/must-have-java-code-style-guide\/#primaryimage\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/asha24.net\/blog\/wp-content\/uploads\/2018\/04\/pexels-photo-265110.jpeg\",\"contentUrl\":\"https:\/\/asha24.net\/blog\/wp-content\/uploads\/2018\/04\/pexels-photo-265110.jpeg\",\"width\":1953,\"height\":1500},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/asha24.net\/blog\/must-have-java-code-style-guide\/#webpage\",\"url\":\"https:\/\/asha24.net\/blog\/must-have-java-code-style-guide\/\",\"name\":\"The Must Have Java Code Style Guide - Asha24 Blog\",\"isPartOf\":{\"@id\":\"https:\/\/asha24.net\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/asha24.net\/blog\/must-have-java-code-style-guide\/#primaryimage\"},\"datePublished\":\"2018-04-17T10:22:34+00:00\",\"dateModified\":\"2023-02-03T12:26:27+00:00\",\"author\":{\"@id\":\"https:\/\/asha24.net\/blog\/#\/schema\/person\/f6167b78bbaddfc399ae1154cd5b6759\"},\"breadcrumb\":{\"@id\":\"https:\/\/asha24.net\/blog\/must-have-java-code-style-guide\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/asha24.net\/blog\/must-have-java-code-style-guide\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/asha24.net\/blog\/must-have-java-code-style-guide\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/asha24.net\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"The Must Have Java Code Style Guide\"}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/asha24.net\/blog\/#\/schema\/person\/f6167b78bbaddfc399ae1154cd5b6759\",\"name\":\"Mahesh\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/asha24.net\/blog\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/085c03e75ffb51af2509c1cfad9c7d78b30236d43a008db2e46f96e2f40c67fc?s=96&d=wavatar&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/085c03e75ffb51af2509c1cfad9c7d78b30236d43a008db2e46f96e2f40c67fc?s=96&d=wavatar&r=g\",\"caption\":\"Mahesh\"},\"url\":\"https:\/\/asha24.net\/blog\/author\/mahesh\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"The Must Have Java Code Style Guide - Asha24 Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/asha24.net\/blog\/must-have-java-code-style-guide\/","og_locale":"en_US","og_type":"article","og_title":"The Must Have Java Code Style Guide - Asha24 Blog","og_url":"https:\/\/asha24.net\/blog\/must-have-java-code-style-guide\/","og_site_name":"Asha24 Blog","article_published_time":"2018-04-17T10:22:34+00:00","article_modified_time":"2023-02-03T12:26:27+00:00","og_image":[{"width":1953,"height":1500,"url":"https:\/\/asha24.net\/blog\/wp-content\/uploads\/2018\/04\/pexels-photo-265110.jpeg","path":"\/home\/reviews981\/public_html\/asha24.net\/blog\/wp-content\/uploads\/2018\/04\/pexels-photo-265110.jpeg","size":"full","id":50618,"alt":"","pixels":2929500,"type":"image\/jpeg"}],"twitter_card":"summary_large_image","twitter_misc":{"Written by":"Mahesh","Est. reading time":"14 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebSite","@id":"https:\/\/asha24.net\/blog\/#website","url":"https:\/\/asha24.net\/blog\/","name":"Asha24 Blog","description":"Dedication Towards Learning","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/asha24.net\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"ImageObject","@id":"https:\/\/asha24.net\/blog\/must-have-java-code-style-guide\/#primaryimage","inLanguage":"en-US","url":"https:\/\/asha24.net\/blog\/wp-content\/uploads\/2018\/04\/pexels-photo-265110.jpeg","contentUrl":"https:\/\/asha24.net\/blog\/wp-content\/uploads\/2018\/04\/pexels-photo-265110.jpeg","width":1953,"height":1500},{"@type":"WebPage","@id":"https:\/\/asha24.net\/blog\/must-have-java-code-style-guide\/#webpage","url":"https:\/\/asha24.net\/blog\/must-have-java-code-style-guide\/","name":"The Must Have Java Code Style Guide - Asha24 Blog","isPartOf":{"@id":"https:\/\/asha24.net\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/asha24.net\/blog\/must-have-java-code-style-guide\/#primaryimage"},"datePublished":"2018-04-17T10:22:34+00:00","dateModified":"2023-02-03T12:26:27+00:00","author":{"@id":"https:\/\/asha24.net\/blog\/#\/schema\/person\/f6167b78bbaddfc399ae1154cd5b6759"},"breadcrumb":{"@id":"https:\/\/asha24.net\/blog\/must-have-java-code-style-guide\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/asha24.net\/blog\/must-have-java-code-style-guide\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/asha24.net\/blog\/must-have-java-code-style-guide\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/asha24.net\/blog\/"},{"@type":"ListItem","position":2,"name":"The Must Have Java Code Style Guide"}]},{"@type":"Person","@id":"https:\/\/asha24.net\/blog\/#\/schema\/person\/f6167b78bbaddfc399ae1154cd5b6759","name":"Mahesh","image":{"@type":"ImageObject","@id":"https:\/\/asha24.net\/blog\/#personlogo","inLanguage":"en-US","url":"https:\/\/secure.gravatar.com\/avatar\/085c03e75ffb51af2509c1cfad9c7d78b30236d43a008db2e46f96e2f40c67fc?s=96&d=wavatar&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/085c03e75ffb51af2509c1cfad9c7d78b30236d43a008db2e46f96e2f40c67fc?s=96&d=wavatar&r=g","caption":"Mahesh"},"url":"https:\/\/asha24.net\/blog\/author\/mahesh\/"}]}},"_links":{"self":[{"href":"https:\/\/asha24.net\/blog\/wp-json\/wp\/v2\/posts\/50536","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/asha24.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/asha24.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/asha24.net\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/asha24.net\/blog\/wp-json\/wp\/v2\/comments?post=50536"}],"version-history":[{"count":13,"href":"https:\/\/asha24.net\/blog\/wp-json\/wp\/v2\/posts\/50536\/revisions"}],"predecessor-version":[{"id":52158,"href":"https:\/\/asha24.net\/blog\/wp-json\/wp\/v2\/posts\/50536\/revisions\/52158"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/asha24.net\/blog\/wp-json\/wp\/v2\/media\/50618"}],"wp:attachment":[{"href":"https:\/\/asha24.net\/blog\/wp-json\/wp\/v2\/media?parent=50536"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/asha24.net\/blog\/wp-json\/wp\/v2\/categories?post=50536"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/asha24.net\/blog\/wp-json\/wp\/v2\/tags?post=50536"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}