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

Similar documents
C++ Style Guide. 1.0 General. 2.0 Visual Layout. 3.0 Indentation and Whitespace

CSE 11 Style Guidelines

QueueBlock, ReversalADT, LinkedList,CustomerAccount, not MaintainCustomerData

CSCI 2101 Java Style Guide

Java Programming Style Guide

Coding Standards for Java

Creating Universally Designed Word 2013 Documents - Quick Start Guide

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

Documentation Requirements Computer Science 2334 Spring 2016

Creating Universally Designed Word 2010 Documents - Quick Start Guide

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

CS 251 Intermediate Programming Coding Standards

Objectives. Problem Solving. Introduction. An overview of object-oriented concepts. Programming and programming languages An introduction to Java

Coding Standards for C

Expanded Guidelines on Programming Style and Documentation

Coding Standards for C

Assignment Marking Criteria

Java Programming Tutorial 1

19. Bulleted and Numbered Lists

Supplement D: Expanded Guidelines on Programming Style and Documentation

EE 382 Style Guide. March 2, 2018

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

CS 351 Design of Large Programs Coding Standards

11 Coding Standards CERTIFICATION OBJECTIVES. Use Sun Java Coding Standards

PROGRAMMING STYLE. Fundamentals of Computer Science I

Chapter Two Bonus Lesson: JavaDoc

For Teachers Engineering Design in Oregon Science Classrooms Page 1 of 6. EDOSC Style Guide. Subtitle

Programming in ROBOTC ROBOTC Rules

Company Design Specifications

3. Formatting Documents

CS 152: Data Structures with Java Hello World with the IntelliJ IDE

Microsoft Word 2007 Lesson 1

Microsoft Word 2007 Final Lesson

Software Implementation (Writing Quality Code)

CSCI 1060U Programming Workshop

C++ Programming Style Guide

Unit D Lecture Notes Word 2003

Write for your audience

Programming. C++ Basics

CS 211 Programming Practicum Fall 2018

MS Word 2013 Accessibility Fundamentals

Initial Coding Guidelines

CSCI 262 C++ Style Guide

Chapter 2 Author Notes

CS106A, Stanford Handout #30. Coding Style

Appendix G C/C++ Notes. C/C++ Coding Style Guidelines Ray Mitchell 475

Setting Up a Paper in APA Style Using Microsoft Word 2008 for MACs

Log4Delphi Coding Standards

MS Word 2010 Accessibility Fundamentals

Additional Support and Disability Advice Centre

Setting Up a Paper in APA Style Using Microsoft Word 2007

Pace University. Fundamental Concepts of CS121 1

CSSE2002/7023 The University of Queensland

CS 315 Software Design Homework 3 Preconditions, Postconditions, Invariants Due: Sept. 29, 11:30 PM

How to Create Chapter Headings in a Microsoft Word 2007Document

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

3. NetBeans IDE 6.0. Java. Fall 2009 Instructor: Dr. Masoud Yaghini

CS 152 Computer Programming Fundamentals Coding Standards

Coding Style Handout #15 February 1, CS106A Winter

Choose a title that captures the interest of an audience and orients the audience to the poster s content.

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

What does this program print?

Running head: WORD 2007 AND FORMATING APA PAPERS 1. A Brief Guide to using Word 2007 to Format Papers in the Publication Style of the American

MULTIMEDIA TRAINING KIT INTRODUCTION TO OPENOFFICE.ORG WRITER HANDOUT

CSE 142 Su 04 Computer Programming 1 - Java. Objects

Skill Exam Objective Objective Number. Setting Page Layout Modify page setup Insert breaks to create sections.

Full file at C How to Program, 6/e Multiple Choice Test Bank

Intermediate Word for Windows

Homework Assignment: Sudoku Board

Create a new document based on default template, other available template like: memo, fax, agenda.

Chapter. Focus of the Course. Object-Oriented Software Development. program design, implementation, and testing

How to Format Modern Language Association (MLA) Style Papers

Coding Guidelines. Introduction. General Points. Avoid redundant initialization/assignment

5 The Control Structure Diagram (CSD)

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

The Java Language Rules And Tools 3

Creating Accessible Word Documents

CHAPTER 3: MICROSOFT OFFICE: WORD 2010

SAMPLE PAPER FOR AN IGBR JOURNAL OR PROCEEDINGS PUBLICATION

COMP 110 Project 1 Programming Project Warm-Up Exercise

Microsoft Word 2016 LEVEL 1

Chapter 2: Introduction to C++

How to Make Headers for APA, Section Breaks, Line Spacing, and Hanging Indents

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

LAT-TD-xxxxx-xx May 21, 2002

Here are all the comments that your Udacity Code Reviewer had about your code...

Rule 1-3: Use white space to break a function into paragraphs. Rule 1-5: Avoid very long statements. Use multiple shorter statements instead.

SML Style Guide. Last Revised: 31st August 2011

Style guide for Department for Education research reports and briefs

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

Creating Web Pages with SeaMonkey Composer

Coding Standard for ECE3090 August 24, 2005

Programming Assignment Comma Separated Values Reader Page 1

Using Styles In Microsoft Word 2002

Word Tutorial 3. Creating a Multiple- Page Report COMPREHENSIVE

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Make Your Word Documents Accessible

Scala Style Guide Spring 2018

Creating a C++ Program

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

Transcription:

September 25, 2002 Java Style Guide Dr Caffeine This document defines the style convention the students must follow in submitting their programs. This document is a modified version of the document originally written by LCDR Chris Eagle. 1.0 General The purpose of the style guide is not to restrict your programming, but rather to establish a consistent format for your programs. Consistency leads to easier comprehension and maintenance. The style guide presented in this document is based from the Java coding convention. The guide is available from http://java.sun.com/docs/codeconv/html/codeconventionstoc.doc.html 2.0 Visual Layout You should format your program source code making use of page boundaries and blank lines to separate code groups, functions and groups of logically related blocks. As a general guideline, no block should be longer than one page. It is generally better to have some blank space at the end of a page than to start a code block and run it over onto the next page. Use a lot of blank spaces. Output from a program should be both informative and formatted to improve the readability. A person reading the output should be able to determine whether the program works reasonably, without knowing the internal details of the program. A printed page must be 8.5 x 11 inches. Margins must be set between ½ and 1 inch all around. You must use a fixed-width font (Courier, or Courier New 1

3.0 Indentation and Whitespace works well) with a font size between 10 and 12 points. 3.0 Indentation and Whitespace Indentation is used to highlight the block structure of the code and should be used consistently. The proper use of whitespace and indentation makes the structure of the program evident from the layout of the code. You must use proper indentation. The recommended indentation is four spaces. You may vary it from 2 to 4 spaces. Use spaces rather than tabs, because printers and monitors usually have different tab spacing. Change one of the options of the IDE that replaces the tabs with a designated number of spaces. It is critical to replace tabs with spaces so you can print the source code nicely. Set you IDE to insert spaces for tabs. Aligning the declarations make them more readable. Use MainWindow FileInputStream File String mainwindow; fileinput; inputfile; stmt1, stmt2; instead of MainWindowmainWindow; FileInputStreamfileInput; File inputfile; Stringstmt1, stmt2; Vertical whitespace is the use of blank lines to help set off blocks of code and increases readability of the program. Code with no vertical whitespace is difficult to follow. No line of code should wrap beyond the right margin. If a line of code becomes too long, break it up into pieces with carriage returns. Remember the compiler largely ignores extra whitespace in your programs (but be careful with strings). The only thing that may follow an opening brace ( { ) on a line of code is a 2 Java Style Guide

3.0 Indentation and Whitespace comment. The block of code enclosed by a set of braces must be indented one level (four spaces) inside the enclosing braces. A closing brace must always be on a line by itself and must be at the same level of indentation as the line containing the corresponding opening brace. Lines of code may be indented more than four spaces when the readability of the code is improved. 3.1 COMMENTS Comments should be indented consistently with the block of code they describe. Comments can be divided into two general categories, strategic and tactical. Strategic comments are used to give the reader a general overview of what is going on. These comments appear at the beginning of files and preceding class, method, and class variable declarations. Strategic comments tend to be written in sentences with an emphasis on explaining the big picture of what the block of code is designed to do. Tactical comments are designed to explain tricky areas of code, what parameters do, and hints about the control flow of the program. These comments should be intermixed in the code when needed. They should be shorter and in bullet format, and may be in inline format If you use sensible and informative naming, block structure, indentation, and straightforward programming, your code will be mostly self documenting. This is what you should strive for. Keep comments simple. Do not explain what is obvious from the code itself. Instead of commenting constants in the code, define symbolic constants. Do final int MAX_WINDOW_WIDTH = 450;... width = Math.min(userInput, MAX_WINDOW_WIDTH); instead of width = Math.min(userInput, 450); //450 is max window width Java Style Guide 3

3.0 Indentation and Whitespace 3.2.1 File Headers. Each file must have a descriptive comment header block at the beginning as follows: //-------------------------------------------------------- // Filename:.java <name of this file> // Date: // Course: // Project # // Compiler: <JDK 1.4.0 etc> //-------------------------------------------------------- 3.2.2 Header Comments -- classes. Each class declaration must have an appropriate comment header giving a brief description using the following format: /** * A single sentence describing the purpose of the class. * Any additional info describing the class * * @author <your name> */ The javadoc documentation tool that can automatically generate html documentation pages for your source code recognizes this style of comment. Sun s Java API documentation was created using the javadoc tool. 3.2.3 Header Comments -- methods. Each method declaration must have an appropriate comment header giving a brief description using the following format: /** * A single sentence describing the purpose of the method. * Any additional info describing the method * * @param <param-name> <param purpose>. * @return <describe the value returned>. * @exception <any exception thrown by this method> */ 4 Java Style Guide

4.0 Control Structures EXAMPLE: /** * Return the square root of the specified value. * * @param value the value to take the square root of. * @return the square root of the number. * @exception ArithmeticException */ public float squareroot(float value) throws ArithmeticException { 3.2.4 Header Comments data members. Each class declaration must have an appropriate comment header giving a brief description using the following format: /** * A single sentence describing the purpose of the variable. */ If the comment fits in a single line, do this: /** Date of employment */ private Date begindate; 4.0 Control Structures The use of braces is required (for style purposes, not syntax purposes) for all control structures, including those with a single statement in the body. Avoid the overuse of continue and break in loops. Always use a default case in switch statements. Use a break statement to end all cases (including default) in a switch statement. The use of // end if, // end while, and // end switch comments after the corresponding ending right brace is optional. Indentation and formats for control structures are as follows (opening brace on the same line): Java Style Guide 5

4.0 Control Structures The if statement: if (boolean expression) { else if (boolean expression) { else { The for statement: for (expression; expression; expression) { The do/while statement: do { while (boolean expression); The while statement: while (boolean expression) { 6 Java Style Guide

5.0 Naming The switch statement: switch (expression) { case <constant expression>: break; case <constant expression>: break; case <constant expression>: break; default: //all switch statement should //have default break; 5.0 Naming Variables must have meaningful names/identifiers. This must be balanced against using names which are too long. Fifteen or so characters approaches the too long limit. Single letter variables or constants should not be used. An exception to this rule is when it is common practice to identify some thing with a single letter. An example of this is the coordinate system (x, y, and z). A second exception occurs in the use of loop counter variables where it is common practice to use variables like i and j as counters in for loops Only class names should begin with a capital letter, all other identifiers (package names, method names, and variable names) should begin with a lower case letter. An identifier consisting of multiple names shall have each name distinguished by making the first letter of each name part upper case (e.g. redcar- Color). Package names should be all lower case. Java Style Guide 7

6.0 Classes Examples: package redcarcolor; class RedCarColor {... int redcarcolor; Names of constant (i.e. final) data members should be in all capital letters. Do not use the lower case letter `L, or the letter `O in names unless they are part of normal words. This is to avoid confusion with the numbers 1 and 0. For example, is Cel1 C- e - l ONE or C e l l? Avoid names that differ only in case, look similar, or differ only slightly. For example, InputData, InData and DataInput will certainly be confusing if used in the same program. Names of methods should reflect what they do (printarray), or what they return (getage). Boolean variable names should sound like Yes/No things isempty and isfinished are possible examples. 6.0 Classes Classes are declared in the following order: public data members protected data members private data members constructors public methods protected methods private methods nested class definitions 8 Java Style Guide