Code convention document Argus-viewer



Similar documents
Java Code Conventions. September 12, 1997

Taxi Service Coding Policy. Version 1.2

C Coding Style Guide. Technotes, HowTo Series. 1 About the C# Coding Style Guide. 2 File Organization. Version 0.3. Contents

Software Development (CS2500)

CSE 308. Coding Conventions. Reference

AppendixA1A1. Java Language Coding Guidelines. A1.1 Introduction

Official Android Coding Style Conventions

egovernment Authority [Sharmila Naveen] [07/04/2010]

Example of a Java program

Chapter 2 Introduction to Java programming

C# Code Style Guide. Version 1.2 Scott Bellware

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

Building a Multi-Threaded Web Server

PROBLEM SOLVING SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON

Object Oriented Software Design

A Coding Style Guide for Java WorkShop and Java Studio Programming

Moving from CS 61A Scheme to CS 61B Java

JavaScript: Control Statements I

Java Coding Style Guide

An Incomplete C++ Primer. University of Wyoming MA 5310

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

JavaScript: Introduction to Scripting Pearson Education, Inc. All rights reserved.

Introduction to Python

Object Oriented Software Design

JAVA - QUICK GUIDE. Java SE is freely available from the link Download Java. So you download a version based on your operating system.

Java Interview Questions and Answers

Perl in a nutshell. First CGI Script and Perl. Creating a Link to a Script. print Function. Parsing Data 4/27/2009. First CGI Script and Perl

Coding Standards for C#

Debugging. Common Semantic Errors ESE112. Java Library. It is highly unlikely that you will write code that will work on the first go

9 Control Statements. 9.1 Introduction. 9.2 Objectives. 9.3 Statements

Computer Programming C++ Classes and Objects 15 th Lecture

CS170 Lab 11 Abstract Data Types & Objects

Creating a Simple, Multithreaded Chat System with Java

CS 111 Classes I 1. Software Organization View to this point:

Coding conventions and C++-style

Applied Informatics C++ Coding Style Guide

Lecture 5: Java Fundamentals III

Java Application Developer Certificate Program Competencies

Some Scanner Class Methods

Basic Java Constructs and Data Types Nuts and Bolts. Looking into Specific Differences and Enhancements in Java compared to C

Basic Object-Oriented Programming in Java

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

ECE 341 Coding Standard

Enumerated Types in Java

Chapter 1 Java Program Design and Development

Introduction to Python

Conditional Statements Summer 2010 Margaret Reid-Miller

AP Computer Science Java Subset

CS 241 Data Organization Coding Standards

Hands-on Exercise 1: VBA Coding Basics

Java Program Coding Standards Programming for Information Technology

Bachelors of Computer Application Programming Principle & Algorithm (BCA-S102T)

Statements and Control Flow

Programming Languages CIS 443

Communicating with a Barco projector over network. Technical note

java.util.scanner Here are some of the many features of Scanner objects. Some Features of java.util.scanner

Introduction to Java Lecture Notes. Ryan Dougherty

Bash shell programming Part II Control statements

Week 1: Review of Java Programming Basics

Retrieving Data Using the SQL SELECT Statement. Copyright 2006, Oracle. All rights reserved.

Install Java Development Kit (JDK) 1.8

Unix Shell Scripts. Contents. 1 Introduction. Norman Matloff. July 30, Introduction 1. 2 Invoking Shell Scripts 2

The University of Alabama in Huntsville Electrical and Computer Engineering CPE Test #4 November 20, True or False (2 points each)

Lesson: All About Sockets

Android Studio Application Development

Designing with Exceptions. CSE219, Computer Science III Stony Brook University

JavaScript Introduction

The C Programming Language course syllabus associate level

Informatica e Sistemi in Tempo Reale

SQL Server Database Coding Standards and Guidelines

CSS 543 Program 3: Online Tic-Tac-Toe Game Professor: Munehiro Fukuda Due date: see the syllabus

Pemrograman Dasar. Basic Elements Of Java

Third AP Edition. Object-Oriented Programming and Data Structures. Maria Litvin. Gary Litvin. Phillips Academy, Andover, Massachusetts

Explain the relationship between a class and an object. Which is general and which is specific?

6. Control Structures

PHP Tutorial From beginner to master

Coding Conventions for C++ and Java applications

PRI-(BASIC2) Preliminary Reference Information Mod date 3. Jun. 2015

C Language Tutorial. Version March, 1999

13 Classes & Objects with Constructors/Destructors

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Software Engineering Techniques

KC Data Integration Web Service Developer Guide

Topics. Parts of a Java Program. Topics (2) CS 146. Introduction To Computers And Java Chapter Objectives To understand:

Chapter 2: Elements of Java

Web development... the server side (of the force)

C++ INTERVIEW QUESTIONS

Introduction to Java

WA2099 Introduction to Java using RAD 8.0 EVALUATION ONLY. Student Labs. Web Age Solutions Inc.

Recognizing PL/SQL Lexical Units. Copyright 2007, Oracle. All rights reserved.

Dinopolis Java Coding Convention

CS106A, Stanford Handout #38. Strings and Chars

Lecture 2 Notes: Flow of Control

D06 PROGRAMMING with JAVA

Coding Rules. Encoding the type of a function into the name (so-called Hungarian notation) is forbidden - it only confuses the programmer.

Scoping (Readings 7.1,7.4,7.6) Parameter passing methods (7.5) Building symbol tables (7.6)

Transcription:

Code convention document Projectnaam: Auteur(s): Reviewer(s): Software Process case voor master-studie Software Engineering 2007/2008. Robert Baarda (Q&A) Studentnr.: 5821258 Martijn van Beest (Q&A) Ka-Sing Chou (Development) Datum: 12-02-2008 Kenmerk: code_convention_0.5.pdf Status / versie: Versie 0.5 Distributie: Contact: Universiteit van Amsterdam R.J.Baarda@student.uva.nl M.vanBeest@student.uva.nl Yao.Chou@student.uva.nl Copyright 2008 Universiteit van Amsterdam 1 / 14

Inhoudsopgave 1.1 Java source files 4 1.1.1 Beginning Comments 4 1.1.2 Package and Import Statements 4 1.2 Declarations 5 1.2.1 Number Per Line 5 1.2.2 Placement 5 1.2.3 Class and Interface Declarations 6 1.2.4 Enum declarations 6 1.2.5 Commenting 7 1.3 Statements 7 1.3.1 Simple Statements 7 1.3.2 Compound Statements 7 1.3.3 return Statements 7 1.3.4 if, if-else, if else-if else Statements 8 1.3.5 for Statements 8 1.3.6 while Statements 9 1.3.7 do-while Statements 9 1.3.8 switch Statements 9 1.3.9 try-catch Statements 10 1.3.10 Casting 10 1.4 Naming 11 1.5 Example 13 Copyright 2008 Universiteit van Amsterdam 2 / 14

This document is heavily based on: Code Conventions for the JavaTM Programming Language (http://java.sun.com/docs/codeconv/html/codeconvtoc.doc.html). Parts of this document are based on ideas and information presented in the book: Code Complete 2nd Ed., Steve McConnel Copyright 2008 Universiteit van Amsterdam 3 / 14

Java 1.1 Java source files Each Java source file contains a single public class or interface. When private classes and interfaces are associated with a public class, you can put them in the same source file as the public class. The public class should be the first class or interface in the file. Java source files have the following ordering: Beginning comments Package and Import statements Class and interface declarations 1.1.1 Beginning Comments All source files should begin with a c-style comment that lists the class name, date, and copyright notice: /* * Classname * * Date * * Copyright notice 1.1.2 Package and Import Statements The first non-comment line of the Java source files is the package nl.dancingbear.argusviewer; statement. After that, import statements can follow. For example: package nl.dancingbear.argusviewer; import java.awt.peer.canvaspeer; The software will be developed under the nl.dancingbear.argusviewer.* package. Copyright 2008 Universiteit van Amsterdam 4 / 14

In import statements the * is never to be used. Every class used in the source file is imported and grouped together by the package of origin. For example: import java.io.ioexception; import java.net.inetsocketaddress; import java.net.serversocket; import java.net.socket; import java.net.socketexception; import java.nio.bytebuffer; import java.nio.channels.serversocketchannel; 1.2 Declarations 1.2.1 Number Per Line One declaration per line is recommended since it encourages commenting. In other words, int level; // indentation level int size; // size of table is preferred over int level, size; Do not put different types on the same line. Example: int foo, fooarray[]; // WRONG! Note: The examples above use one space between the type and the identifier. Another acceptable alternative is to use tabs, e.g.: int level; // indentation level int size; // size of table Object currententry; // currently selected table entry 1.2.2 Placement Put declarations only at the beginning of blocks. (A block is any code surrounded by curly braces "{" and "".) Don't wait to declare variables until their first use; it can confuse the unwary programmer and hamper code portability within the scope. void mymethod() { int int1 = 0; // beginning of method block if (condition) { int int2 = 0;... // beginning of "if" block Copyright 2008 Universiteit van Amsterdam 5 / 14

1.2.3 Class and Interface Declarations When coding Java classes and interfaces, the following formatting rules should be followed: No space between a method name and the parenthesis "(" starting its parameter list Open brace "{" appears at the end of the same line as the declaration statement Closing brace "" starts a line by itself indented to match its corresponding opening statement, except when it is a null statement the "" should appear immediately after the "{" class Sample extends Object { int ivar1; int ivar2; Sample(int i, int j) { ivar1 = i; ivar2 = j; int emptymethod() {... Methods are separated by a blank line. Methods are declared in the following order: - Class constructor - Public methods - Protected methods - Private methods - Mutators (getters/setters) Overloaded methods will always be ordered by their amount of arguments, from less to many. See chapter 1.5 for an example about class layout. 1.2.4 Enum declarations Enum declarations look a lot like class declarations. Every constant inside the enum is declared on a seperate line and in capital letters. See the following example: public enum Color { RED, GREEN, BLUE Copyright 2008 Universiteit van Amsterdam 6 / 14

1.2.5 Commenting All class and method declarations should be commented. See paragraph 1.5 on how to do this. 1.3 Statements 1.3.1 Simple Statements Each line should contain at most one statement. Example: argv++; // Correct argc--; // Correct argv++; argc--; // AVOID! 1.3.2 Compound Statements Compound statements are statements that contain lists of statements enclosed in braces "{ statements ". See the following sections for examples. The enclosed statements should be indented one more level than the compound statement. The opening brace should be at the end of the line that begins the compound statement; the closing brace should begin a line and be indented to the beginning of the compound statement. Braces are used around all statements, even single statements, when they are part of a control structure, such as a if-else or for statement. This makes it easier to add statements without accidentally introducing bugs due to forgetting to add braces. 1.3.3 return Statements A return statement with a value should not use parentheses unless they make the return value more obvious in some way. Example: return; return mydisk.size(); return (size? size : defaultsize); Copyright 2008 Universiteit van Amsterdam 7 / 14

1.3.4 if, if-else, if else-if else Statements The if-else class of statements should have the following form: if (condition) { if (condition) { else { if (condition) { else if (condition) { else { Note: if statements always use braces {. Avoid the following error-prone form: if (condition) //AVOID! THIS OMITS THE BRACES {! statement; 1.3.5 for Statements A for statement should have the following form: for (initialization; condition; update) { An empty for statement (one in which all the work is done in the initialization, condition, and update clauses) should have the following form: for (initialization; condition; update); When using the comma operator in the initialization or update clause of a for statement, avoid the complexity of using more than three variables. If needed, use separate statements before the for loop (for the initialization clause) or at the end of the loop (for the update clause). Copyright 2008 Universiteit van Amsterdam 8 / 14

1.3.6 while Statements A while statement should have the following form: while (condition) { An empty while statement should have the following form: while (condition); 1.3.7 do-while Statements A do-while statement should have the following form: do { while (condition); 1.3.8 switch Statements A switch statement should have the following form: switch (condition) { case ABC: /* falls through case DEF: break; case XYZ: break; default: break; Every time a case falls through (doesn't include a break statement), add a comment where the break statement would normally be. This is shown in the preceding code example with the /* falls through comment. Every switch statement should include a default case. The break in the default case is redundant, but it prevents a fall-through error if later another case is added. Copyright 2008 Universiteit van Amsterdam 9 / 14

1.3.9 try-catch Statements A try-catch statement should have the following format: try { catch (ExceptionClass e) { A try-catch statement may also be followed by finally, which executes regardless of whether or not the try block has completed successfully. try { catch (ExceptionClass e) { finally { 1.3.10 Casting Casts should be followed by a blank space. Examples: mymethod((byte) anum, (Object) x); mymethod((int) (cp + 5), ((int) (i + 3)) + 1); Copyright 2008 Universiteit van Amsterdam 10 / 14

1.4 Naming Identifier Type Rules for Naming Examples Packages The prefix of a unique package name is always written in all-lowercase ASCII letters. Subsequent components of the package name vary according to an organization's own internal naming conventions. Such conventions might specify that certain directory name components be division, department, project, machine, or login names. will be developed in the nl.dancingbear.argusviewer package. nl.dancingbear.argusviewer Classes Class names should be nouns, in mixed case with the first letter of each internal word capitalized. Try to keep your class names simple and descriptive. Use whole wordsavoid acronyms and abbreviations (unless the abbreviation is much more widely used than the long form, such as URL or HTML). class Raster; class ImageSprite; Interfaces Interface names should be capitalized like class names. interface RasterDelegate; interface Storing; Methods Methods should be verbs, in mixed case with the first letter lowercase, with the first letter of each internal word capitalized. run(); runfast(); getbackground(); Local Variables Except for variables, all instance, class, and class constants are in mixed case with a lowercase first letter. Internal words start with capital letters. Variable names should not start with underscore _ or dollar sign $ characters, even though both are allowed. Variable names should be short yet meaningful. The choice of a variable name should be mnemonic- that is, designed to indicate to the casual observer the intent of its int i; char c; float mywidth; Copyright 2008 Universiteit van Amsterdam 11 / 14

Member Variables use. One-character variable names should be avoided except for temporary "throwaway" variables. Common names for temporary variables are i, j, k, m, and n for integers; c, d, and e for characters. Member variables that are available to multiple routines within a class, but only within a class, are prefixed with an m_. (Source: page 277, Code Construction, Steve McConnel). private int m_classvariable Constants The names of variables declared class constants and of ANSI constants should be all uppercase with words separated by underscores ("_"). (ANSI constants should be avoided, for ease of debugging.) static final int MIN_WIDTH = 4; static final int MAX_WIDTH = 999; static final int GET_THE_CPU = 1; Copyright 2008 Universiteit van Amsterdam 12 / 14

1.5 Example /* * @(#)Blah.java 1.82 99/03/18 * * Copyright (c) 1994-1999 Sun Microsystems, Inc. * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A. * All rights reserved. * * This software is the confidential and proprietary information of Sun * Microsystems, Inc. ("Confidential Information"). You shall not * disclose such Confidential Information and shall use it only in * accordance with the terms of the license agreement you entered into * with Sun. package nl.dancingbear.argusviewer; import java.blah.blahdy.blahblah; * Class description goes here. * * @date 18 Mar 1999 * @author Firstname Lastname * @author Firstname Lastname (In case of pair programming a second author is needed). public class Blah extends SomeClass { /* A class implementation comment can go here. classvar1 documentation comment public static int classvar1; instancevar1 documentation comment public Object instancevar1; instancevar2 documentation comment protected int m_instancevar2; * classvar2 documentation comment that happens to be * more than one line long private static Object m_classvar2; instancevar3 documentation comment private Object[] m_instancevar3; *...constructor Blah documentation comment... public Blah() { //...implementation goes here... *...method dosomething documentation comment... Copyright 2008 Universiteit van Amsterdam 13 / 14

public void dosomething() { //...implementation goes here... *...method dosomethingelse documentation comment... * @param someparam description public void dosomethingelse(object someparam) { //...implementation goes here... *...method returnsomething documentation comment... * @param someparam description * * @throws IOException * * @return return-description public int returnsomething(object someparam) throws IOException { // implementation goes here return stuf; *...method protectedmethod documentation comment... protected void protectedmethod() { //...implementation goes here... *...method privatemethod documentation comment... private void privatemethod() { //...implementation goes here... *...method setclassvar2 documentation comment... * @param instancevar description public void setclassvar2(object instancevar) { m_classvar2 = instancevar; Copyright 2008 Universiteit van Amsterdam 14 / 14