Expanded Guidelines on Programming Style and Documentation

Similar documents
Supplement D: Expanded Guidelines on Programming Style and Documentation

Chapter 2 Primitive Data Types and Operations

Assignment Marking Criteria

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

Full file at

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

Lecture Notes. System.out.println( Circle radius: + radius + area: + area); radius radius area area value

Chapter 2 Primitive Data Types and Operations

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

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. Chapter 1 Introduction to Computers, Programs, and Java

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

Chapter 2 Primitive Data Types and Operations. Objectives

Computer Programming, I. Laboratory Manual. Experiment #2. Elementary Programming

Pace University. Fundamental Concepts of CS121 1

Chapter 2 Author Notes

CEN 414 Java Programming

Chapter 2 Elementary Programming

Datatypes, Variables, and Operations

PROGRAMMING STYLE. Fundamentals of Computer Science I

Chapter 2 Elementary Programming

Chapter 5 Lab Methods

Introduction to Java Applications

Programming with Java

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

Introduction to Java. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Motivations 9/14/2010. Introducing Programming with an Example. Chapter 2 Elementary Programming. Objectives

SE2205B - DATA STRUCTURES AND ALGORITHMS JAVA BASICS. Kevin Brightwell. Tuesday January 10th, Acknowledgements:Dr.

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 3: SEP. 13TH INSTRUCTOR: JIAYIN WANG

Lab # 2. For today s lab:

JAVA Programming Concepts

Coding Standards for Java

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

Introduction to Java Applications; Input/Output and Operators

Chapter 5 Lab Methods

CSCI 1060U Programming Workshop

CS 251 Intermediate Programming Coding Standards

Software and Programming 1

CS11 Java. Fall Lecture 1

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

Identifiers. Identifiers are the words a programmer uses in a program Some identifiers are already defined. Some are made up by the programmer:

Module 1: Introduction to Computers, Programs, and Java

Documentation Requirements Computer Science 2334 Spring 2016

CS 351 Design of Large Programs Coding Standards

CS 152 Computer Programming Fundamentals Coding Standards

Outline. Parts 1 to 3 introduce and sketch out the ideas of OOP. Part 5 deals with these ideas in closer detail.

CSCI 2101 Java Style Guide

Coding Standard for ECE3090 August 24, 2005

11 Coding Standards CERTIFICATION OBJECTIVES. Use Sun Java Coding Standards

CSCE 120: Learning To Code

Programming Syntax and Style. David Greenstein Monta Vista High School

CONDITIONAL EXECUTION

Java Programming Style Guide

Variables of class Type. Week 8. Variables of class Type, Cont. A simple class:

Java Programming Tutorial 1

What does this program print?

CSCI 262 C++ Style Guide

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

Computer Programming, I. Laboratory Manual. Experiment #3. Selections

CSE 11 Style Guidelines

Eclipse Tutorial. For Introduction to Java Programming By Y. Daniel Liang

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

Software and Programming 1

Introduction. Introduction to OOP with Java. Lecture 01: Introduction to OOP with Java - AKF Sep AbuKhleiF -

COMP 250 Winter 2011 Reading: Java background January 5, 2011

Basic Operations jgrasp debugger Writing Programs & Checkstyle

COMP 110 Project 1 Programming Project Warm-Up Exercise

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

Introduction to OOP with Java. Instructor: AbuKhleif, Mohammad Noor Sep 2017

Chapter 1 Introduction to Computers, Programs, and Java

3 CREATING YOUR FIRST JAVA APPLICATION (USING WINDOWS)

Key Differences Between Python and Java

Programming Language Basics

CS 106 Introduction to Computer Science I

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

Coding Standards for C

PROGRAM READABILITY. A program's readability readability, which is a measure of how

CS 251 Intermediate Programming Java Basics

Data Types and the while Statement

Motivations. Chapter 2: Elementary Programming 8/24/18. Introducing Programming with an Example. Trace a Program Execution. Trace a Program Execution

Introduction to Python Code Quality

Example Program. public class ComputeArea {

CS Programming I: Primitives and Expressions

Topics. The Development Process

COSC 123 Computer Creativity. Introduction to Java. Dr. Ramon Lawrence University of British Columbia Okanagan

Lab 9: Creating a Reusable Class

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

CMSC 150 LECTURE 1 INTRODUCTION TO COURSE COMPUTER SCIENCE HELLO WORLD

Supplement H.1: JBuilder X Tutorial. For Introduction to Java Programming, 5E By Y. Daniel Liang

OCaml Style Guide Fall 2017

Supplement II.B(1): JBuilder X Tutorial. For Introduction to Java Programming By Y. Daniel Liang

Programming Exercise 7: Static Methods

QueueBlock, ReversalADT, LinkedList,CustomerAccount, not MaintainCustomerData

Introduction to the C++ Programming Language

How to Create Chapter Headings in a Microsoft Word 2007Document

Coding Standards for C

CSE 142/143 Unofficial Style Guide

Introduction to Computers, Programs, and Java. CSE 114, Computer Science 1 Stony Brook University

Perl Basics. Structure, Style, and Documentation

4 Programming Fundamentals. Introduction to Programming 1 1

Using Eclipse Europa - A Tutorial

Transcription:

Page 1 of 5 Expanded Guidelines on Programming Style and Documentation Introduction Introduction to Java Programming, 5E Y. Daniel Liang liang@armstrong.edu Programming style deals with the appearance of your program. If you were to write an entire program on one line, it would compile and run properly, but doing this would be bad programming style because the program would be hard to read. Documentation consists of explanatory remarks and comments for the program. Programming style and documentation are as important as coding. Good programming style and appropriate documentation reduce the chance of errors and make programs easy to read. This handout provides more detailed guidelines on programming style and documentation that supplements the brief guidelines presented in Chapter 2, "Primitive Data Types and Operations." First, here is a sample code: /** * Class: CSCI1301-03 Introduction to Programming Principles * Instructor: Y. Daniel Liang * Description: (Give a brief description for Exercise 1) * Due: 1/18/2001 * @author John F. Smith * @version 1.0 */ public class Exercise1 { /**Main method*/ public static void main(string[] args) { double radius; double area; // Prompt the user to enter radius System.out.print("Enter radius: "); radius = MyInput.readDouble(); // Compute area area = radius * radius * 3.14159; // Display results System.out.println("The area for the circle of radius " + radius + " is " + area); NOTE: This guideline is consistent with the Java API source code. Appropriate Comments and Comment Styles Include a summary at the beginning of the program to explain what the program does, its key features, its supporting data structures, and any unique techniques it uses. In a long program, you should also include comments to introduce each major step and to explain anything that is difficult to read. It is important to make your comments concise so that you do not crowd the program or

Page 2 of 5 make it difficult to read. Use the javadoc comments (/**... */) for commenting an entire class and an entire method. These comments must precede just before the class or the method header and they can be extracted in a javadoc HTML file. For commenting the steps inside a method, use line comments (//). For information on javadoc comments, see http://java.sun.com/j2se/javadoc/. JBuilder TIP: Use Ctrl+/ as a line comment toggle. Ctrl+/ adds comment tags (//) to the line of code the cursor is on or removes existing comment tags. Highlight blocks of code and use Ctrl+/ to comment or remove existing comments on an entire block. Naming Conventions Make sure that the meanings of the descriptive names you choose for variables, constants, classes, and methods are straightforward. Names are case-sensitive. Listed below are the conventions for naming variables, methods, classes, and packages. For variables and methods, always use lowercase. If the name consists of several words, concatenate them into one, making the first word lowercase and capitalizing the first letter of each subsequent word in the name; for example, the variables radius and area and the method readdouble. For class names, capitalize the first letter of each word in the name; for example, the class name ComputeArea. All letters in constants should be capitalized, and underscores should be used between words; for example, the constant PI and constant MAX_VALUE. Use singulars for variables representing single items such as student and count. Use plurals for arrays or collections. For example, Student[] students = new Student[4]; and Count[] counts = new Count[10]; TIP: It is important to become familiar with the naming conventions. Understanding them will help you to understand Java programs. If you stick with the naming conventions, other programmers will be more willing to accept your program. TIP: Do not choose class names that are already used in the Java standard packages. For example, since the Math class is defined in Java, you should not name your class Math. Package-Naming Conventions

Page 3 of 5 Packages are hierarchical, and you can have packages within packages. For example, java.lang.math indicates that Math is a class in the package lang and that lang is a package within the package java. Levels of nesting can be used to ensure the uniqueness of package names. Choosing a unique name is important because your package might be used on the Internet by other programs. Java designers recommend that you use your Internet domain name in reverse order as a package prefix. Since Internet domain names are unique, this avoids naming conflicts. Suppose you want to create a package named mypackage.io on a host machine with the Internet domain name liang.cs.armstrong.edu. To follow the naming convention, you would name the entire package edu.armstrong.cs.liang.mypackage.io. Java expects one-to-one mapping of the package name and the file system directory structure. For the package named edu.armstrong.cs.liang.mypackage.io, you must create a directory, as shown in the Figure 1. In other words, a package is actually a directory that contains the bytecode of the classes. Figure 1: The package edu.armstrong.cs.liang.mypackage.io is mapped to a directory structure in the file system. Proper Indentation and Spacing A consistent indentation style makes programs clear and easy to read. Indentation is used to illustrate structural relationships among the program s components or statements. Java can read the program even if all of the statements are in a straight line, but it is easier to read and maintain code that is aligned properly. You should indent each subcomponent or statement two spaces more than the structure within which it is nested. Use a space to separate parameters in a method. Do not leave spaces before or after parentheses in a method. For example, amethod(a1, a2) is preferred, whereas amethod ( a1, a2 ) is not a good style. A single space should be added on both sides of a binary operator, as shown in the following statement: boolen b = 3 + 4 * 4 > 5 * (4 + 3) - ++i; A single space line should be used to separate segments of the code to make the program easier to read.

Page 4 of 5 Block Styles A block is a group of statements surrounded by braces. A block can be written in many ways. For example, the following are equivalent: public class Test { public static void main(string[] args) { System.out.println("Block Styles"); public class Test { public static void main(string[] args) { System.out.println("Block Styles"); The former is referred to as the next-line style, and the latter, as the end-of-line style. The end-of-line block style is used in this book. if-else Style if (cond1) { else if (cond2) { else if (cond3) { else { for loop Style for (int i = 1; i < n; i++) { while loop Style while (i < n) { do-while loop Style do { while (i < n);

Page 5 of 5 try-catch Style Summary try { catch (Exception ex) { // Handler Use javadoc comments at the beginning of the program to state your name, class, and give a brief description for the program. Use javadoc comments to describe an entire method. Use line comments to describe steps inside a method. Indent your statement two spaces. Use Next-Line style. Leave a blank line before a comment line or a comment paragraph.