Construction: High quality code for programming in the large

Similar documents
About the required reading. Construction: High quality code for programming in the large. What is high quality code?

the gamedesigninitiative at cornell university Lecture 13 Architecture Design

the gamedesigninitiative at cornell university Lecture 12 Architecture Design

Documentation (and midterm review)

Write for your audience

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

Northeastern University - Seattle. Computer and Information Sciences

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

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

Object-Oriented Software Engineering Practical Software Development using UML and Java. Chapter 2: Review of Object Orientation

Classes, interfaces, & documentation. Review of basic building blocks

CS112 Lecture: Defining Instantiable Classes

CS112 Lecture: Defining Classes. 1. To describe the process of defining an instantiable class

Chapter 4 Defining Classes I

Comp215: Thinking Generically

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

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

CSE 331 Software Design & Implementation

COMP 401 Fall Recitation 7: Factories and Lists

Chapter01.fm Page 1 Monday, August 23, :52 PM. Part I of Change. The Mechanics. of Change

Object-Oriented Programming Classes, Objects, Variables

Client Code - the code that uses the classes under discussion. Coupling - code in one module depends on code in another module

Chapter 1 Getting Started

CS112 Lecture: Working with Numbers

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

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

This assignment requires that you complete the following tasks (in no particular order).

Appendix: Common Errors

Plan. Design principles: laughing in the face of change. What kind of change? What are we trying to achieve?

CMSC 132: Object-Oriented Programming II

1. Write two major differences between Object-oriented programming and procedural programming?

Object-Oriented Software Engineering. Chapter 2: Review of Object Orientation

Introduction to Programming Using Java (98-388)

CMSC 132: Object-Oriented Programming II

1 Getting used to Python

The compiler is spewing error messages.

Comments are almost like C++

Exceptions. References. Exceptions. Exceptional Conditions. CSE 413, Autumn 2005 Programming Languages

This course supports the assessment for Scripting and Programming Applications. The course covers 4 competencies and represents 4 competency units.

Chapter 5 Errors. Bjarne Stroustrup

Our second exam is Thursday, November 10. Note that it will not be possible to get all the homework submissions graded before the exam.

The first program: Little Crab

CPS122 Lecture: Defining a Class

Inf1-OP. Inf1-OP Exam Review. Timothy Hospedales, adapting earlier version by Perdita Stevens and Ewan Klein. March 20, School of Informatics

1B1b Classes in Java Part I

Com S 227 Spring 2018 Assignment points Due Date: Thursday, September 27, 11:59 pm (midnight) "Late" deadline: Friday, September 28, 11:59 pm

EE 382 Style Guide. March 2, 2018

Object-Oriented Design

Tutorial: Functions and Functional Abstraction. Nathaniel Osgood CMPT

getimage getimage Parameters: Returns: See Also:

Introduction to Programming

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

Introduction to Programming Style

OOP Design Conclusions and Variations

Initial Coding Guidelines

CISC-124. Passing Parameters. A Java method cannot change the value of any of the arguments passed to its parameters.

JAVA: A Primer. By: Amrita Rajagopal

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

CSE 11 Style Guidelines

Errors. Lecture 6. Hartmut Kaiser hkaiser/fall_2011/csc1254.html

A few more things about Agile and SE. Could help in interviews, but don t try to bluff your way through

COMSC-051 Java Programming Part 1. Part-Time Instructor: Joenil Mistal

CS1 Lecture 5 Jan. 26, 2018

Racket Style Guide Fall 2017

Software Design and Analysis for Engineers

Collections Chapter 12. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Mock Objects and the Mockito Testing Framework Carl Veazey CSCI Friday, March 23, 12

QUIZ. What are 3 differences between C and C++ const variables?

Drawing Fast The Graphics Pipeline

Chapter Two Bonus Lesson: JavaDoc

CE221 Programming in C++ Part 1 Introduction

All code must follow best practices. Part (but not all) of this is adhering to the following guidelines:

CS 251 Intermediate Programming Java Basics

CS11 Java. Fall Lecture 1

CSCI 2101 Java Style Guide

1007 Imperative Programming Part II

CS Problem Solving and Object-Oriented Programming

Example Program. public class ComputeArea {

Adam Blank Lecture 3 Autumn 2016 CSE 143. Computer Programming II

CSE 143. More ArrayIntList; pre/post; exceptions; debugging. Computer Programming II. CSE 143: Computer Programming II

Exceptions. CSE 142, Summer 2002 Computer Programming 1.

Exceptions. Readings and References. Exceptions. Exceptional Conditions. Reading. CSE 142, Summer 2002 Computer Programming 1.

JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB PROGRAMMING, X452.1)

Java Object Oriented Design. CSC207 Fall 2014

Program Correctness and Efficiency. Chapter 2

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

How We Refactor, and How We Know It

CS61B, Spring 2003 Discussion #17 Amir Kamil UC Berkeley 5/12/03

Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p.

Welcome to Design Patterns! For syllabus, course specifics, assignments, etc., please see Canvas

Inf1-OOP. OOP Exam Review. Perdita Stevens, adapting earlier version by Ewan Klein. March 16, School of Informatics

CS 215 Software Design Homework 3 Due: February 28, 11:30 PM

Mobile App:IT. Methods & Classes

CS125 : Introduction to Computer Science. Lecture Notes #4 Type Checking, Input/Output, and Programming Style

CS Exam 1 Review Suggestions

Pointers and References. 8-Aug-11

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

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

CSE331 Winter 2014, Midterm Examination February 12, 2014

Transcription:

Construction: High quality code for programming in the large Paul Jackson School of Informatics University of Edinburgh

What is high quality code? High quality code does what it is supposed to do...... and will not have to be thrown away when that changes. Obviously intimately connected with requirement engineering and design: but today let s concentrate on the code itself. 2 / 20

What has this to do with programming in the large? Why is the quality of code more important on a large project than on a small one? Fundamentally because other people will have to read and modify your code even you in a year s time count as other people! E.g., because of staff movement for code reviews for debugging following testing for maintenance (Exercise. Dig out your early Java exercises from Inf1. Criticise your code. Rewrite it better without changing functionality.) 3 / 20

How to write good code Follow your organisation s coding standards - placement of curly brackets, indenting, variable and method naming... 4 / 20

Coding standard example Suppose you are used to... public Double getvolumeasmicrolitres() { if (m_volumetype.equals(volumetype.millilitres)) return m_volume * 1000; return m_volume; }... and you see public Double getvolumeasmicrolitres() { if(m_volumetype.equals( VolumeType.Millilitres)) { return m_volume*1000; } return m_volume; } Even worse: mixed styles in one file - inevitable without standards! 5 / 20

How to write good code Follow your organisation s coding standards - placement of curly brackets, indenting, variable and method naming... Use meaningful names (for variables, methods, classes...) If they become out of date, change them. Avoid cryptic comments. Try to make your code so clear that it doesn t need comments. Balance structural complexity against code duplication: don t write the same two lines 5 times (why not?) when an easy refactoring would let you write them once, but equally, don t tie the code in knots to avoid writing something twice. Be clever, but not too clever. Remember the next person to modify the code may be less clever than you! Don t use deprecated, obscure or unstable language features unless absolutely necessary. Remove dead code, unneeded package includes, etc. 6 / 20

Which of these fragments is better? 1. for(double countery = -8; y < 8; countery+=0.5){ x = counterx; y = countery; r = 0.33 - Math.sqrt(x*x + y*y)/33; r += sinanim/8; g.fillcircle( x, y, r ); } 2. for(double countery = -8; y < 8; countery+=0.5){ x = counterx; y = countery; r = 0.33 - Math.sqrt(x*x + y*y)/33; r += sinanim/8; g.fillcircle( x, y, r ); } 3. They are both fine. Be consistent about indentation. Don t use TABs 7 / 20

Which of these fragments is better? 1. c.add(o); 2. customer.add(order); 3. They are both fine. Use meaningful names. A good length for most names is 8-20 chars Follow conventions for short names e.g. i, j, k for loop indexes 8 / 20

What else is wrong with this? r = 0.33 - Math.sqrt(x*x + y*y)/33; r += sinanim/8; g.fillcircle( x, y, r ); Use white space consistently 9 / 20

Use comments when they re useful if (moveshapemap!=null) { // Need to find the current position. All shapes have // the same source position. Position pos = ((Move)moveShapeSet.toArray()[0]).getSource(); Hashtable legalmovestoshape = (Hashtable)moveShapeMap.get(pos); return (Move)legalMovesToShape.get(moveShapeSet); } 10 / 20

and not when they re not // if the move shape map is null if (moveshapemap!=null) { Too many comments is actually a more common serious problem than too few. Good code in a modern high-level language shouldn t need many explanatory comments, and they can cause problems. If the code and the comments disagree, both are probably wrong (Anon) But there s another use for comments... 11 / 20

Javadoc Any software project requires documenting the code by which we mean specifying it, not explaining it. Documentation held separately from code tends not to get updated. So use comments as the mechanism for documentation even if the reader won t need to look at the code itself. Javadoc is a tool from Sun. Programmer writes doc comments in particular form, and Javadoc produces pretty-printed hyperlinked documentation. E.g. Java API documentation at http://docs.oracle.com/javase/7/docs/api/ See Required reading for tutorial. 12 / 20

Javadoc example from tutorial /** * Returns an Image object that can then be painted on the screen. * The url argument must specify an absolute {@link URL}. The name * argument is a specifier that is relative to the url argument. * <p> * This method always returns immediately, whether or not the * image exists. When this applet attempts to draw the image on * the screen, the data will be loaded. The graphics primitives * that draw the image will incrementally paint on the screen. * * @param url an absolute URL giving the base location of the image * @param name the location of the image, relative to the url argument * @return the image at the specified URL * @see Image */ public Image getimage(url url, String name) { try { return getimage(new URL(url, name)); } catch (MalformedURLException e) { return null; } } 13 / 20

Rendered Javadoc (Eclipse) 14 / 20

Coding style Good naming, commenting and formatting are vital components of a good coding style. But good coding is about much more too. E.g. Declaration and use of local variables Good to try keeping local variable scope restricted How conditional and loop statements are written With if-else statements, put normal frequent case first Use while, do-while, for and for/in loops appropriately Avoid deep nesting How code is split among methods Good to try avoiding long methods, over 200 lines Defensive programming using assertions and handling errors appropriately Use of OO features and OO design practices (e.g. patterns) Use of packages 15 / 20

The relevance of object orientation Construction is intimately connected to design: it is design considerations that motivate using an OO language. Key need: control complexity and abstract away from detail. This is essential for systems which are large (in any sense). Objects; classes; inheritance; packages all allow us to think about a lot of data at once, without caring about details. Interfaces help us to focus on behaviour. Revise classes, interfaces and inheritance in Java. 16 / 20

A common pattern in Java interface Foo {... } class FooImpl implements Foo {... } Why is this so much used? 17 / 20

Use of single implementations of interfaces All about information hiding Expect that users of FooImpl objects always interact with them at type Foo. Maybe they are not even aware of FooImpl name. Alternative means of constructing FooImpl objects can be set up Can use access control modifiers (e.g. private, public) on FooImpl members to define its interface However interface and implementation still mixed together in one class definition This was an early criticism of the OO take on the ADT paradigm With distinct interface foo, users need see nothing of FooImpl 18 / 20

Packages Recall that Java has packages. Why, and how do you decide what to put in which package? Packages: are units of encapsulation. By default, attributes and methods are visible to code in the same package. give a way to organize the namespace. allow related pieces of code to be grouped together. So they are most useful when several people must work on the same product. But the package hierarchy is not really a hierarchy as far as access restrictions are concerned the relationship between a package and its sub/superpackages is just like any other package-package relationship. 19 / 20

Reading Required: something that makes you confident you completely understand Java packages (e.g., see course web page). Required: the JavaDoc tutorial Suggested: Code Complete 2nd Ed. Steve McConnell 20 / 20