Java Programming Style Guide

Similar documents
C++ Programming Style Guide

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

CSE 11 Style Guidelines

CSE 142/143 Unofficial Style Guide

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

Coding Standards for Java

CS 351 Design of Large Programs Coding Standards

CS 251 Intermediate Programming Coding Standards

QueueBlock, ReversalADT, LinkedList,CustomerAccount, not MaintainCustomerData

Documentation Requirements Computer Science 2334 Spring 2016

CS 152 Computer Programming Fundamentals Coding Standards

11 Coding Standards CERTIFICATION OBJECTIVES. Use Sun Java Coding Standards

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

Full file at

CSCI 2101 Java Style Guide

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

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

Coding Standards for C

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

Coding Standards for C

CS112 Lecture: Working with Numbers

Software Implementation (Writing Quality Code)

Expanded Guidelines on Programming Style and Documentation

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

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

Computer Programming : C++

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

3. Java - Language Constructs I

COMP 202 Java in one week

CMSC 201 Fall 2018 Python Coding Standards

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

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

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

Our Strategy for Learning Fortran 90

Chapter 2 Author Notes

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

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

Coding Standard for ECE3090 August 24, 2005

What does this program print?

Assignment 5. Introduction

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

Assignment Marking Criteria

Introduction to Python Code Quality

Assignment 4 Publication date: 27/12/2015 Submission date: 17/01/ :59 People in-charge: R. Mairon and Y. Twitto

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Basics of Java Programming

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

IT Web and Software Developer Software Development Standards

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

Programming Style Guide v1.1

Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Supplement D: Expanded Guidelines on Programming Style and Documentation

CSCI 1060U Programming Workshop

SML Style Guide. Last Revised: 31st August 2011

CSCI 200 Lab 3 Using and implementing sets

CS 142 Style Guide Grading and Details

Problem Solving with C++

Program Fundamentals

Differentiate Between Keywords and Identifiers

EE 382 Style Guide. March 2, 2018

Problem 1: Building the BookCollection application

AP Computer Science A

EECS 280 C++ Coding Standards

CS313D: ADVANCED PROGRAMMING LANGUAGE

Program Elements -- Introduction

COMP 202 Java in one week

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

CSE 142 Su 04 Computer Programming 1 - Java. Objects

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

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

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

Chapter 2 Basic Elements of C++

A Fast Review of C Essentials Part I

Identifiers and Variables

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

Scala Style Guide Spring 2018

UNIT- 3 Introduction to C++

Java Primer 1: Types, Classes and Operators

Course Coding Standards

4 Programming Fundamentals. Introduction to Programming 1 1

APPENDIX A : Example Standard <--Prev page Next page -->

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

Outline. Overview. Control statements. Classes and methods. history and advantage how to: program, compile and execute 8 data types 3 types of errors

Coding Convention for ECON 41701

Good Coding Practices Spring 2018

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

Boolean Expressions and if 9/14/2007

Java Coding style guide 1. Java. Coding Style Guide. (July 2015)

UEE1302 (1102) F10: Introduction to Computers and Programming

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

All copyrights reserved - KV NAD, Aluva. Dinesh Kumar Ram PGT(CS) KV NAD Aluva

CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING. Dr. Shady Yehia Elmashad

Pace University. Fundamental Concepts of CS121 1

Chapter 5 Control Statements: Part 2 Section 5.2 Essentials of Counter-Controlled Repetition

Introduction to Visual Basic and Visual C++ Introduction to Java. JDK Editions. Overview. Lesson 13. Overview

Programming Practice (vs Principles)

CS112 Lecture: Defining Instantiable Classes

Conditionals and Loops

Programming Lecture 3

Transcription:

Java Programming Style Guide Computer Science Program Cedarville University Goal: Our goal is to produce well-written code that can be easily understood and will facilitate life-cycle maintenance. These guidelines lay out principles that Java programmers have found useful for producing readable code that contains fewer bugs and is easier to maintain. It is important to remember these are just guidelines, and efficiency and understandability should not be sacrificed to blindly follow them. Remember: there are no style points for slick code. Any fool can write code that a computer can understand. Good programmers write code that humans can understand. - Martin Fowler Layout and Comments: 1. Each public class should be placed in a separate file (of the same name). If you use private classes, they can be located in the same file as the public class they support (the public class should be first in the file). Logically associated public classes should be placed in the same directory and included in the same package. Each file will contain a package statement, necessary import statements, and the code for the class being developed. 2. The first line in the file should be the package statement. This should be followed by any required import statements. The package and import statements should be located before the class header. 3. Each class will begin with a class header block. This block should contain, as a minimum, the name and brief description of the class, the author, the name of the file containing the class, the date created, a summary of modifications, and a copyright statement. The block should also explain the use/purpose of the class. For example, explain the hierarchy it belongs to, what classes derive from it, and how the class is intended to be used. JavaDoc will be used to the maximum extent possible. Mandatory JavaDoc fields include @author and @version. 1

package mathfunctions; import java.util.; / This class implements the Rational ADT, providing support for manipulation of fractions. @author Joe Smith @version 1.0 File: rational.java Created: Jun 2000 Copyright Cedarville University, its Computer Science faculty, and the authors. All rights reserved. Summary of Modifications: 14 Jul 2000 JMS corrected bug in Multiply routine 24 Jul 2000 JMS added Rational::Reduce method Description: This class provides an ADT for fractions. Users of this class are provided methods to manipulate fractions in an intuitive manner. For example, arithmetic (+, -,, /), comparison (<=, ==), and I/O (<<, >>) operators work for Rationals. Rational does not inherit from other classes, and is not anticipated to be a base class for other classes. / 4. An appropriate amount of comments should be used throughout your code. Although it is most common for programmers to provide too few comments, over-commenting can also negatively impact the readability of the code. It is important to comment on any sections of code whose function is not obvious (to another person). For algorithmic-type code which follows a sequence of steps, it may be appropriate to summarize the algorithm at the beginning of the section (e.g., in the method header) and then highlight each of the major steps throughout the code. Either style (C or C++) comments may be used; however, the same style should be used throughout your program. Comments should be indented at the same level as the code they describe, and should appear before the code to which they refer. Naming Conventions: 1. Use descriptive names for variables and methods in your code, such as xposition, distancetogo, or findlargest(). The only exception to this may be loop iteration variables, where no descriptive name makes sense. In this case, use the lower case letters beginning with i (i, j, k, etc). 2. For variables, the first word in the name should be lower case; the first letter of subsequent words in the name should be capitalized (sometimes referred to as camelcase ). For example, upperlimit, averagevalue, makeconnection(), addbody(). This distinguishes function and variable names from class names at quick 2

glance. Whenever possible, method names should be verbs: draw(), getx(), setposition(). 3. For constants, capitalize all letters in the name, and separate words in the name using an underscore, e.g., PI, MAX_ARRAY_SIZE. Any numeric constants needed in your code (other than very simple ones like 1, 0, and 1) should be replaced by a named constant. 4. For boolean variables and functions, the name should reflect the boolean type, e.g., isempty (), islastelement, haschanged. Names should always be positive; i.e., use haschanged rather than hasnotchanged. 5. Do not use the name of a class instance variable as the name for method formal parameter or for local variables within a method. This causes ambiguity which can confuse the reader and promote bugs. 6. Package names should be all lowercase letters. Statements: 1. Only one statement is allowed per line, and each line of code will be no more than 80 characters in length (to prevent line-wrap). If a statement requires more than one line, subsequent lines will be indented to make it obvious that the statement extends over multiple lines, as shown below. The general rules for line breaks are to break after a comma or before an arithmetic operator. int myfunction (int variablea, int variableb, int variablec, int variabled, int variablee, int variablef ) { } 2. A single declaration per line is preferred. Never mix multiple types on the same line, or initialized and uninitialized variables. int id, grades[10]; int height, weight = 5; // bad // bad 3. Braces can be done in one of two styles: the opening brace can be put on the end of the line defining the block, or it can be on a separate line by itself, as shown below. Code inside the braces will always be indented. for (i = 0; i <= maxsize; i++) { for (i = 0; i <= maxsize; i++) { } } 3

4. Make consistent use of horizontal white space. For example, include spaces after commas, and between operands and operators in expressions. Avoid using tabs to create white space. Note the spacing in the for-loop example in paragraph 3 above. 5. Use vertical white space to separate your code into paragraphs of logically connected statements. 6. Use parentheses as needed for clarity. 7. Do not compare a Boolean variable explicitly against true/false. OK: if (isempty) if (counter == 0) Avoid: if(!counter) if (ifempty == true) 8. Write straightforward code and avoid unnecessarily complicated code. For example, for statements should typically only include initialization, test, and increment of iteration variable, not additional statements which belong in the loop body. Avoid: for (i = 0, sum=0; i <= maxcount; i++, sum+=i4) { } 9. For if statements, the nominal or more frequent case should typically be placed in the then block, and the exceptional or less frequent case in the else block. 10. Looping: Kava provides two primary statements for iteration: the while statement and the for statement. The while statement should be preferred when the number of loop iterations is unknown at the outset of the loop or when early termination from the loop is a possibility. In addition, while loops are preferred when the exit condition is a non-counting condition. The for statement should be preferred when the number of loop iterations is known, i.e., they usually can be counted. Early termination of for loop is discouraged. Using do while loops is discouraged. 11. The break and continue statements should be used sparingly, and only if another more straightforward approach results in less readable/maintainable code. 12. Structured programming encourages a single exit point (i.e., a single return statement) from a function. Multiple return statements from a function should be used sparingly. 13. The goto statement is not to be used. 14. Enumerations should be used when you need to specify a set of possible integer values. Classes: 1. Within the class, items will be listed in the following order: a. Static data members. 4

b. Instance data members. c. Constructors. d. Static methods (except main() ). e. Instance methods. f. main(). Note: alternatively, you can put main() right after the constructors; i.e., make it the first method in the file. 2. Member data should usually be private, and public or protected accessor methods [e.g., getx(), setx()] provided. 3. Use of modifiers should be maximized. 4. Methods which are helpers for other methods and are not part of the interface of the class should be declared private. 5. Significant methods (public methods where are part of the interface and other major methods) should provide JavaDoc documentation. These comments should include a brief description, and specify parameters, return type, and any exceptions thrown. For example: / Main entry point to begin simulation of the circuit. @param FileName The name of the file containing the circuit description @return true if the simulation runs to completion @throws FileNotFoundException If the specified file could not be opened / 5