Objectives. Coding Standards. Why coding standards? Elements of Java Style. Understand motivation for coding standards

Similar documents
Index. Index. More information. block statements 66 y 107 Boolean 107 break 55, 68 built-in types 107

CS 251 Intermediate Programming Coding Standards

CSE 11 Style Guidelines

CS 351 Design of Large Programs Coding Standards

Chapter 2 Author Notes

QueueBlock, ReversalADT, LinkedList,CustomerAccount, not MaintainCustomerData

Beginning Style. 16-Nov-15

CSCI 1060U Programming Workshop

Expanded Guidelines on Programming Style and Documentation

CS 152 Computer Programming Fundamentals Coding Standards

Java Style Guide. 1.0 General. 2.0 Visual Layout. Dr Caffeine

Supplement D: Expanded Guidelines on Programming Style and Documentation

11 Coding Standards CERTIFICATION OBJECTIVES. Use Sun Java Coding Standards

Assignment Marking Criteria

6.170 Laboratory in Software Engineering Java Style Guide. Overview. Descriptive names. Consistent indentation and spacing. Page 1 of 5.

IT Web and Software Developer Software Development Standards

JOSE LUIS JUAREZ VIVEROS com) has a. non-transferable license to use this Student Guide

CPS122 Lecture: Defining a Class

Week 7 - More Java! this stands for the calling object:

Java Programming Style Guide

Chapter 2 Basic Elements of C++

Identifiers and Variables

What does this program print?

Hotmail Documentation Style Guide

CSCI 2101 Java Style Guide

Documentation Requirements Computer Science 2334 Spring 2016

Classes, interfaces, & documentation. Review of basic building blocks

Guidelines Quick Reference

CS112 Lecture: Defining Instantiable Classes

C # Coding Standards for.net By Lance Hunt. Document Version 1.13a August Copyright Lance Hunt 2004 All Rights Reserved

Index COPYRIGHTED MATERIAL

CSE 142/143 Unofficial Style Guide

A A B U n i v e r s i t y

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

Introduction to Programming Style

Coding Standards for Java

Introduction to Java. Java Programs Classes, Methods, and Statements Comments Strings Escape Sequences Identifiers Keywords

CS112 Lecture: Working with Numbers

Makefiles Makefiles should begin with a comment section of the following form and with the following information filled in:

C # Coding Standards for.net By Lance Hunt. Document Version 1.13 August Copyright Lance Hunt 2004 All Rights Reserved

Maintainability and Readability. What makes code readable? Understandable code. General Routine Structure. General Module Structure

CS112 Lecture: Variables, Expressions, Computation, Constants, Numeric Input-Output

Lecture 27. Lecture 27: Regular Expressions and Python Identifiers

WRITING FOR THE WEB. UIUC Web Governance

Software Implementation (Writing Quality Code)

JAVA: A Primer. By: Amrita Rajagopal

Log4Delphi Coding Standards

Inheritance and Encapsulation. Amit Gupta

UNIT 5 - UML STATE DIAGRAMS AND MODELING

Chapter Two Bonus Lesson: JavaDoc

WBJS Grammar Glossary Punctuation Section

Web Site Documentation Eugene School District 4J

CS112 Lecture: Defining Classes. 1. To describe the process of defining an instantiable class

Full file at

LONGWARE, Inc. Style Guide

Quality of the Source Code Lexicon

Write for your audience

Coding Style Handout #15 February 1, CS106A Winter

EE 382 Style Guide. March 2, 2018

PHP-FIG Home Blog PSRs Personnel Bylaws FAQs Get Involved PSR-2: Coding Style Guide

Code Convention and version control help us to success in Botball

1. General Principles

Correlation to Georgia Quality Core Curriculum

IT 374 C# and Applications/ IT695 C# Data Structures

Coding Standard for ECE3090 August 24, 2005

Pace University. Fundamental Concepts of CS121 1

A3.1 Introduction A3.2 Classification of coding standards A3.3 Typography... 3 A3.3.1 Naming conventions... 3 A3.3.2 Layout rules...

CS106A, Stanford Handout #30. Coding Style

VARIABLES. Aim Understanding how computer programs store values, and how they are accessed and used in computer programs.

Lesson 1A - First Java Program HELLO WORLD With DEBUGGING examples. By John B. Owen All rights reserved 2011, revised 2015

Java EE 7: Back-end Server Application Development 4-2

Project 1. Java Control Structures 1/17/2014. Project 1 and Java Intro. Project 1 (2) To familiarize with

INTRODUCTION 1 AND REVIEW

Chapter 1. Introduction to Computers and Java Objects. Background information. » important regardless of programming language. Introduction to Java

Problem 1: Building the BookCollection application

The Elements of Java Style. Al Vermeulen Scott W. Ambler Greg Bumgardner Eldon Metz Trevor Misfeldt Jim Shur Patrick Thompson

Chapter 4 Defining Classes I

Access Intermediate

WYSE Academic Challenge Computer Science Test (Regional) 2015 Solution Set

C++ Coding Standards and Practices. Tim Beaudet March 23rd 2015

User Interface Programming OOP/Java Primer. Step 3 - documentation

Google Java Style. 1 Introduction. Last changed: March 21, Introduction 1.1 Terminology notes 1.2 Guide notes

Javadoc. Computer Science and Engineering College of Engineering The Ohio State University. Lecture 7

User Defined Types. Babes-Bolyai University Lecture 06. Lect Phd. Arthur Molnar. User defined types. Python scope and namespace

Programming in ROBOTC ROBOTC Rules

CSSE2002/7023 The University of Queensland

Database Programming Style Guidelines

Generating/Updating code from whole project

The Elements. Java Style

The Java Language Rules And Tools 3

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Computer Components. Software{ User Programs. Operating System. Hardware

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

A JavaBean is a class file that stores Java code for a JSP

CSE 115 / 503 INTRODUCTION TO COMPUTER SCIENCE I. Dr. Carl Alphonce Dr. Jesse Hartloff

Enums. In this article from my free Java 8 course, I will talk about the enum. Enums are constant values that can never be changed.

Introduction to C++ Programming Pearson Education, Inc. All rights reserved.

CS112 Lecture: Making Choices

Errata for Effective Java, Third Edition. p. 8, par. 4: In the first sentence, the word produce should be produces.

Getting Started With Python Programming

Transcription:

Objectives Coding Standards Understand motivation for coding standards Be able to write code that conforms to JOSSE coding standards. Philip Johnson Collaborative Software Development Laboratory Information and Computer Sciences University of Hawaii Honolulu HI 96822 Be able to recognize and repair non-compliant code. Be able to apply automated tools: Checkstyle ant task JRefactory pretty printer (1) (2) Why coding standards? Coding standards: improve readability by ensuring a common look and feel to the code regardless of how many people have worked on it. Elements of Java Style Consolidates the best practices of the Java community gained from the past five years of language use (and experience with other languages before that). improve understandability by ensuring that basic documentation is always present. improve maintainability by improving the predictability of the code. The authoritative source for coding style in this class. Additional coding standards available at the JOSSE web site. (3) (4)

Coding standard principles and conventions General Principles Formatting conventions Programming conventions Packaging conventions The next sections overview important rules from Elements of Java Style. However, these slides overview the rules, they do not contain enough information to implement them! You must still study the book. General principles 1. Adhere to the style of the original. 2. Adhere to the principle of Least Astonishment. Simplicity, clarity, completeness, consistency, robustness 3. Do it right the first time. 4. Document any deviations. (5) (6) Formatting conventions 5. Indent nested code. 2 spaces Open brace at end of line. Close brace appears by itself on a line. 6. Break up long lines. 100 characters per line maximum. 7. Include whitespace. 8. Do not use tabs. 9. Use meaningful names. No one character names (except index vars). 10. Use familiar names. Learn the application domain, talk to users. 11. Question excessively long names. May indicate need to refactor/redesign. 12. Join the vowel generation. putsoundfile, not ptsndfl (7) (8)

13. Capitalize only the first letter in acronyms. getxmlnode, not getxmlnode 14. Do not use names that differ only in case. theinstance and TheInstance 17. <binary compatibility constraint on new package names> Not appropriate for pre-release development. Omit for JOSSE. 15. Use reversed internet domain names as package prefix. edu.hawaii. for this class 18. Capitalize the first letter of each word in a class or interface name. ex: ServerProperties (9) 16. Use a single lowercased word as the root name for each package. not edu.hawaii.serverkernel; (10) 19. Use nouns when naming classes. CustomerAccount, not MaintainCustomerData 20. Pluralize class names that group related attributes. ex: ServerProperties 23. Use verbs when naming methods. ex: withdraw, reset, clear, 21. Use nouns or adjectives when naming interfaces. ActionListener, or Runnable 24. Follow JavaBeans conventions for property accessor methods. is/get/set 22. Use lowercase first word and capitalize first letter of subsequent words in method names. getserverhostname 25. Lowercase first word and capitalize remaining words in variable names daytimephone (11) (12)

26. Use nouns to name variables daytimeaddress, not getdaytimeaddress 27. Pluralize names of collection references. Customer [] customers; 28. Use standard throwaway variable names. index: i, j, k exception: e 29. Quantify field variables with this to distinguish them from local variables. ex: this.address = address; 30. When a parameter assigns a value to a field, use the same name for the parameter and the field. ex: this.address = address; 31. Use uppercase for words and separate with underscore for naming constants. ex: MAX_VALUE (13) (14) 32. Write comments for those who use and for those who maintain your code. Assume familiarity with Java, but not with your application. 33. Keep comments and code in sync. 34. Use the active voice and omit needless words. Use forceful, clear, concise language. 35. Use documentation comments to describe the programming interface. Defines a contract between a client and a supplier of a service. 36. Use standard comments to hide code without removing it. 37. Use one-line comments to explain implementation details. Assume the reader knows Java. Do not repeat what the code does. (15) (16)

38. Describe the programming interface before you write the code. This can be your design phase. 39. Document public, protected, private, and package private members. Private documentation comments not currently enforced by Checkstyle settings in Ant. 40. Provide a summary description and overview of each package. package.html 41. Provide a summary description for your application. overview.html 42. Use a consistent format and organization for all JavaDoc comments. JRefactory pretty printer helps with this! 43. <Wrap keywords with <code> tags.> Optional for JOSSE (17) (18) 44. Wrap code with <pre> tags. 45. Consider using @link tags. Optional for JOSSE. 46. Use a fixed ordering for JavaDoc tags. Use default JRefactory pretty printer order. 47. Write in third-person narrative form. ex: Gets..., Sets..., Allocates... 48. Write summary descriptions that stand alone. First sentence must summarize behavior. 49. Omit the subject in summary descriptions. Not: This method clears the foo. Instead: Clears the foo. 60. Describe why the code is doing what it s doing, not what it does. (19) (20)

Programming conventions 71. Make all fields private. 74. Encapsulate enumerations as classes. Google typesafe enum for a good pattern. 75. Always use block statements in control flow constructs. Not: if (foo) bar( ); Instead: if (foo) { bar( ) }; Programming conventions 79. Use equals, not ==, to test for equality. 80. Always construct objects in a valid state. See Dr. Hacker (1/27/2002) 82. Use nested constructors to eliminate redundant code. (JOSSE) Do not use wildcards in import. Not: import java.util.*; Instead: import java.util.set; Enforced by Checkstyle. (21) (22) Programming conventions 83. Use unchecked, runtime exceptions to report serious unexpected errors that may indicate an error in the program s logic. 84. Use checked exceptions to report errors that may occur, however rarely, under normal program operation. 85. Use return codes to report expected state changes. More on programming conventions See Effective Java, by Joshua Bloch, for a spectacular treatment of best practices in Java programming. I sure wish I had this book 10 years ago. Some might think that I don t need any Java books, but I need this one. - James Gosling (Inventor of Java) 87. Do not silently absorb a runtime or error exception. (23) (24)

Packaging conventions 104. Place types that are commonly used, changed, and released together into the same package. Observe JOSSE build packaging conventions. (25)