技术控

    今日:43| 主题:49507
收藏本版 (1)
最新软件应用技术尽在掌握

[其他] The Elements of Modern Java Style

[复制链接]
骑着兔子的萝卜 发表于 2016-10-3 16:18:28
129 3

立即注册CoLaBug.com会员,免费获得投稿人的专业资料,享用更多功能,玩转个人品牌!

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
This article is featured in the new DZone Guide to Modern Java, Volume II. Get your free copy for more insightful articles, industry statistics, and more.   
  Code Style and How to Use It

  Compilers and interpreters require the syntax of code to conform to a given programming language’s grammar. Humans, however, require additional guides to translate the cold instructions of machine code into something that can be followed and understood. Code style can provide these cues, and can include things like basic text formatting, indentation, and small methods.
  Style can also apply to the use of design patterns, such as best practices for things like constructor chaining, exception handling, and synchronization. Well-styled code is easy to read, like well-written prose. And easier-to-read code means it is easier to understand, and this means more robust, error-free software that developers are happy to work with.
  Coding style also works best when it is consistent throughout the whole codebase. Sometimes, however, many years of development may exist where no style or minimal style is applied. In these cases, it is best not to rush in and change everything just for the sake of applying style. Apply changes slowly as code changes, starting first with all new files. Then update existing files only in those places that are changing, such as a method due to a bug fix. And once a particular file passes a threshold (such as 75%), the remaining non-styled sections can be updated.
  It is also important to note that modern IDEs allow for the configuration of formatting rules; take advantage of this assistance where available.
  Principles of Modern Java Style

  The most important aspect of style is simplicity, since the simpler the style the easier it is to remember and apply. No developer wants to memorize hundreds of rules and dozens of exception in order to write software. There is an overall guiding principle that is commonly known as "The Boy Scout Rule for Software Development". This simply states that a developer should leave the code in a better state than when they found it, much like Boy Scouts and their creed to maintain camp sites.
  Highlights of Coding and Formatting Conventions

  Code Formatting

  Formatting consists of all the typological conventions that give code its basic appearance and includes indentation, the use of comments, and where braces appear.
  Some best practices:
  Nest code and use four spaces for indentation. This gives a good trade-off between emphasizing the text indentation at each level and does not push text too far into the line if multiple indentations levels are required. Use spaces for indentation, never tab characters. Tabs are visually indistinguishable from spaces and can cause issues if both are mixed, since tab lengths can vary across environments. Using only spaces ensure that indentation is always consistent.
  Break long lines and margins of 80, 120, or 132 characters. 80 is better for doing side-by-side comparison or multi-way merges, but will lead to more wrapped lines.
  Use white space, blank lines, and comments to improve readability.
  Braces

  Braces have long since become a point of contention in any discussion of coding style. There are two main schools of thought on braces: Cozied and Aligned. Each has its own pros and cons, which are explained below.
  Cozied Braces put the open brace right at the end of the originating statement and the closing brace on its own line. Associated keywords (like “else” to an “if”) cozy up to the end brace on the same line.
  1. if (condition) {
  2.     statement;
  3. } else {
  4.     statement;
  5. }
复制代码
This style allows for reducing the amount of vertical space used by the code, showing more code on the screen at a time. However, compacting information in this way can reduce readability. Aligned braces, alternatively, place the braces directly on top of each other.
  1. if (condition)
  2. {
  3.     statement;
  4. }
  5. else
  6. {
  7.     statement;
  8. }
复制代码
This style sets off statements from the keywords surrounding it by introducing blank lines since each brace is on its own line, instantly improving readability. It also introduces consistency in the location of braces because they are always in the same place, directly below the first character of the statement that introduced them.
  Aligned Braces are better for making the code consistent and symmetrical, and this especially becomes obvious when the statement before the first open brace is longer than a single line.
  1. if (long condition with
  2.     keyword and keyword and
  3.     keyword and keyword)
  4. {
  5.     keyword;
  6. }
复制代码
The cozied brace version of the above requires an additional indentation to differentiate the continuation of the condition with the start of the execution block.
  When using Aligned Braces, the formatting automatically aligns the text blocks, keeps the condition items together, and does not require additional formatting rules or indentation spaces. It is for these reasons that Aligned Braces are preferred.
  Naming Conventions

  Naming is an important part of code writing — picking an appropriate name that conveys meaning and is appropriate for the scope and lifespan of the construct.
  In general, it is better to be too descriptive than too terse, but always consider the scope that the variable will exist in. Short names are preferable in smaller scopes, while longer names are more appropriate for longer-lived objects.
  A short, single character is appropriate for a self-contained loop:
  1. for (int i = 0; i < listSize; i++)
  2. {
  3.     if (condition)
  4.     {
  5.         sum += list.getItemAt(i);
  6.     }
  7. }
复制代码
Larger-scoped variables require longer and more descriptive names:
  1. private CommandProcessor sequentialCommandProcessor =
  2.     new CommandProcessor();
复制代码
This variable may be used throughout the class in various places, where each of the separate invocations are not simultaneously visible on the editor screen at the same time.
  1. sequentialCommandProcessor.init();
  2. ...
  3. sequentialCommandProcessor.addCommand(...);
  4. ...
  5. sequentialCommandProcessor.execute();
  6. ...
  7. sequentialCommandProcessor.cleanUp();
  8. ...
复制代码
Having a descriptive name reduces the time spent attempting to figure out the intention of the variable. As in the example above, longer variable names use Camel Caps to join words, and never exclude vowels for brevity. Acronyms should only have their first letter capitalized, such as parseXml().
  To maintain consistency across the code base, use the following naming conventions:
  
       
  •       Capitalize the first letter of Classes and Interfaces.
       
  •       Start with a lower case letter for methodNames and variableNames.
       
  •       Constants are in all UPPERCASE_WITH_UNDERSCORES.
       
  •       Use single words in all lowercase for package names.
      
  Highlights of Programming and Design

  Conventions

  Programming conventions cover aspects of implementation, including items like Type Safety, Statements and Expressions, Chaining Constructors, Exception Handling, Assertions, Concurrency, Synchronization, and Efficiency.
  Some general conventions:
  
       
  •       Always use braces for block statements, even if they are empty or a single line; this improves readability and prevents issues if those code blocks are changed in the future.
       
  •       Use parenthesis to clarify the order of operations.
       
  •       Use polymorphism to reduce the need for switch statements or the expensive operation and can lead to performance issues if done repeatedly in a loop structure.
       
  •       If using switch statements, always use a default: case and put break; statements at the end of each block, including the default.
      
  Chaining Constructors

  Object construction occurs frequently, and often times with various parameters to help simplify creation. The best practice in this case is to not write duplicate code and instead make each constructor do only the minimum work necessary, passing the remaining processing to the other constructors.
  1. public ChainedConstructor()
  2. {
  3.     // Common setup
  4.     ...
  5. }
  6. public ChainedConstructor(ObjectTypeA a)
  7. {
  8.     ChaintedConstrucor();
  9.     this.a = a;
  10. }
  11. public ChainedConstructor(ObjectTypeA a, ObjectTypeB b)
  12. {
  13.     ChainedConstructor(a);
  14.     this.b = b;
  15. }
复制代码
Exception Handling

  One of the most important things a developer can do is ensure that the software never crashes, even in the event of unexpected circumstances. At run-time, many things can go wrong, from invalid user input to network interruptions. It is for this reason that all potential exception cases must be handled.
  At the very least, run-time exceptions should always be logged. It is very rare that there is an exception that will truly never occur and can be ignored.
  1. try
  2. {
  3.     ...
  4. }
  5. catch (IOException e)
  6. {
  7.     // Should never reach here
  8.     logger.debug(“Unexpected I/O exception:”);
  9.     logger.logStackTrace(e);
  10. }
复制代码
Catch exceptions in as small an exception scope as possible. Do not wrap a try block around a section of code and then only catch java.lang.Throwable. Some exception cases are easier to recover from than others; it is best not to lump non-recoverable errors (such as java.lang.OutOfMemoryError) with more reasonable expected exceptions (such as java.lang.NumberFormatException when converting a String into an Integer).
  Synchronization

  Synchronization is the enforcement that only a single thread shall have access to a particular portion of code or an object at one moment. The most important rule of maintaining data integrity in a threaded environment is to always allocate and synchronize on an object that is used exclusively for synchronization purposes. Java does provide a mechanism to apply synchronization to classes or methods, but this implicitly or explicitly uses the instance of the class object itself as the synchronization object. That means that all synchronized methods in the class will be blocked if a thread is locked in one of them.
  As such, to prevent unintended consequences, always use an Object other than the current object (this) as the synchronization lock token.
  1. private String fileLock = “token”;
  2. public void writeDataFile(String data)
  3. {
  4.     synchronized(fileLock)
  5.     {
  6.         dataFile.write(data);
  7.     }
  8. }
  9. public String readDataFile(int lineNumber)
  10. {
  11.     String result;
  12.     synchronized(fileLock)
  13.     {
  14.         result = dataFile.read(lineNumber);
  15.     }
  16.     return result;
  17. }
复制代码
Synchronization is an expensive operation that will slow down the execution of a code block, so only apply synchronization where necessary to avoid thread collisions or potential data corruption.
  Conclusion

  Code style is an important aspect of software development. Judiciously and consistently applying a well-defined style will produce code that is simpler to read, understand, and debug, and with fewer defects.
      For more insights on Jigsaw, reactive microservices, and more get your free copy of the new DZone Guide to Modern Java, Volume II!
友荐云推荐




上一篇:Python 邮箱爆破
下一篇:谈谈 DSL 以及 DSL 的应用(以 CocoaPods 为例)
酷辣虫提示酷辣虫禁止发表任何与中华人民共和国法律有抵触的内容!所有内容由用户发布,并不代表酷辣虫的观点,酷辣虫无法对用户发布内容真实性提供任何的保证,请自行验证并承担风险与后果。如您有版权、违规等问题,请通过"联系我们"或"违规举报"告知我们处理。

jkng4519 发表于 2016-10-3 18:10:40
无论是不是沙发都得回复下
回复 支持 反对

使用道具 举报

壞pī氣づ 发表于 2016-10-3 18:25:58
顶起顶起顶起
回复 支持 反对

使用道具 举报

南霜 发表于 2016-10-9 15:34:57
有空一起交流一下
回复 支持 反对

使用道具 举报

*滑动验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

我要投稿

推荐阅读

扫码访问 @iTTTTT瑞翔 的微博
回页顶回复上一篇下一篇回列表手机版
手机版/CoLaBug.com ( 粤ICP备05003221号 | 文网文[2010]257号 )|网站地图 酷辣虫

© 2001-2016 Comsenz Inc. Design: Dean. DiscuzFans.

返回顶部 返回列表