Supplement D: Expanded Guidelines on Programming Style and Documentation

Similar documents
Expanded Guidelines on Programming Style and Documentation

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

Chapter 2 Primitive Data Types and Operations

Assignment Marking Criteria

Full file at

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

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

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

Introduction to Java Applications; Input/Output and Operators

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

Chapter 2 Primitive Data Types and Operations

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

Pace University. Fundamental Concepts of CS121 1

CEN 414 Java Programming

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

PROGRAMMING STYLE. Fundamentals of Computer Science I

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

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

Chapter 2 Author Notes

Chapter 2 Elementary Programming

Chapter 2 Elementary Programming

Lab # 2. For today s lab:

Chapter 2 Primitive Data Types and Operations. Objectives

Module 1: Introduction to Computers, Programs, and Java

JAVA Programming Concepts

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

Chapter 5 Lab Methods

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

Documentation Requirements Computer Science 2334 Spring 2016

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

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

Coding Standards for Java

3 CREATING YOUR FIRST JAVA APPLICATION (USING WINDOWS)

Introduction to Java Applications

CSCI 1060U Programming Workshop

Chapter 5 Lab Methods

Topics. The Development Process

Datatypes, Variables, and Operations

CS 251 Intermediate Programming Coding Standards

Programming with Java

11 Coding Standards CERTIFICATION OBJECTIVES. Use Sun Java Coding Standards

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

CS11 Java. Fall Lecture 1

Entry Point of Execution: the main Method. Elementary Programming. Learning Outcomes. Development Process

CS 351 Design of Large Programs Coding Standards

CS 152 Computer Programming Fundamentals Coding Standards

CSCI 2101 Java Style Guide

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

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

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

Coding Standard for ECE3090 August 24, 2005

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

CSCI 262 C++ Style Guide

Data Types and the while Statement

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

II. Compiling and launching from Command-Line, IDE A simple JAVA program

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

Example Program. public class ComputeArea {

Software and Programming 1

CONDITIONAL EXECUTION

What does this program print?

Programming Exercise 7: Static Methods

CSE 11 Style Guidelines

Chapter 1 Introduction to Computers, Programs, and Java

QueueBlock, ReversalADT, LinkedList,CustomerAccount, not MaintainCustomerData

I pledge by honor that I will not discuss this exam with anyone until my instructor reviews the exam in the class.

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

CS 251 Intermediate Programming Java Basics

Tips from the experts: How to waste a lot of time on this assignment

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

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

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

b. Suppose you enter input from the console, when you run the program. What is the output?

OCaml Style Guide Fall 2017

Chapter 1 Introduction to Computers, Programs, and Java

Coding Standards for C

CSCE 120: Learning To Code

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

COMP 110 Project 1 Programming Project Warm-Up Exercise

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

Programming Syntax and Style. David Greenstein Monta Vista High School

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

Date: Dr. Essam Halim

Java Programming Constructs Java Programming 2 Lesson 1

Java Programming Style Guide

Definite Loops. Computer Science S-111 Harvard University David G. Sullivan, Ph.D. Using a Variable for Counting

Coding Standards for C

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

Java Programming Tutorial 1

Using Eclipse Europa - A Tutorial

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

Programming Language Basics

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

CHAPTER 2 Java Fundamentals

Full download all chapters instantly please go to Solutions Manual, Test Bank site: testbanklive.com

Introduction to Python Code Quality

CS Programming I: Primitives and Expressions

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

Chapter 2 ELEMENTARY PROGRAMMING

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

Transcription:

Page 1 of 5 Introduction Supplement D: Expanded Guidelines on Programming Style and Documentation For Introduction to Java Programming Y. Daniel Liang mailto: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, "Elementary Programming." First, here is a sample code: /** * Class: CSCI1301-03 Introduction to Programming Principles <br /> * Instructor: Y. Daniel Liang <br /> * Description: (Give a brief description for Exercise 1) <br /> * Due: 1/18/2013 </ br> * I pledge that I have completed the programming assignment independently. <br /> I have not copied the code from a student or any source. <br /> I have not given my code to any student. <br /> <br /> Sign here: <br /> */ public class Exercise1 { /** The main method displays three message */ // Use the println statemetns to display three messages System.out.println("Programming is fun"); System.out.println("Welcome to Computer Programming"); System.out.println("Java is a programming language"); /** * Class: CSCI1301-03 Introduction to Programming Principles * Instructor: Y. Daniel Liang * Description: (Give a brief description for Exercise 2) * Due: 1/18/2013 * I pledge that I have completed the programming assignment independently. I have not copied the code from a student or any source. I have not given my code to any student. Sign here: */ public class Exercise2 { /**Main method*/ double radius; double area; // Prompt the user to enter radius System.out.print("Enter radius: "); radius = MyInput.readDouble();

Page 2 of 5 // 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 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/. Eclipse 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];

Page 3 of 5 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 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.

Page 4 of 5 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. 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 { 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 {

Page 5 of 5 while (i < n); try-catch Style try { catch (Exception ex) { // Handler Summary 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.