Good Coding Practices Spring 2018

Similar documents
Day 8. COMP1006/1406 Summer M. Jason Hinek Carleton University

Lecture 21: The Many Hats of Scala: OOP 10:00 AM, Mar 14, 2018

Scala Style Guide Spring 2018

Lecture 14: Exceptions 10:00 AM, Feb 26, 2018

CSE 331 Software Design & Implementation

Racket Style Guide Fall 2017

Java Review Outline. basics exceptions variables arrays modulo operator if statements, booleans, comparisons loops: while and for

CS 61B Data Structures and Programming Methodology. July 7, 2008 David Sun

Lecture 5: Implementing Lists, Version 1

Lecture 16: HashTables 10:00 AM, Mar 2, 2018

Lab 10: Sockets 12:00 PM, Apr 4, 2018

CS159. Nathan Sprague

Abstract Classes, Exceptions

CS 251 Intermediate Programming Methods and More

CS 3 Introduction to Software Engineering. 3: Exceptions

Program Correctness and Efficiency. Chapter 2

CS 251 Intermediate Programming Methods and Classes

Lab 2: Object-Oriented Design 12:00 PM, Jan 31, 2018

CS 142 Style Guide Grading and Details

Prelim 1 SOLUTION. CS 2110, September 29, 2016, 7:30 PM Total Question Name Loop invariants. Recursion OO Short answer

Homework 2: Imperative Due: 5:00 PM, Feb 15, 2019

OCaml Style Guide Fall 2017

CMSC131. Exceptions and Exception Handling. When things go "wrong" in a program, what should happen.

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

Administration. Exceptions. Leftovers. Agenda. When Things Go Wrong. Handling Errors. CS 99 Summer 2000 Michael Clarkson Lecture 11

Lab 5: Java IO 12:00 PM, Feb 21, 2018

CSE 143: Computer Programming II Summer 2015 HW6: 20 Questions (due Thursday, August 13, :30pm)

Lecture 8: Iterators and More Mutation

CSC System Development with Java. Exception Handling. Department of Statistics and Computer Science. Budditha Hettige

Exception-Handling Overview

CSE 143: Computer Programming II Spring 2015 HW7: 20 Questions (due Thursday, May 28, :30pm)

Lab 4: Imperative & Debugging 12:00 PM, Feb 14, 2018

Lecture 4: Inheritence and Abstract Classes

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

Project 1 Computer Science 2334 Spring 2016 This project is individual work. Each student must complete this assignment independently.

(800) Toll Free (804) Fax Introduction to Java and Enterprise Java using Eclipse IDE Duration: 5 days

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

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

Lecture 10 Assertions & Exceptions

CS159. Nathan Sprague

Lecture 12 Assertions & Exceptions

Java Programming Style Guide

Errors and Exceptions

CS 351 Design of Large Programs Coding Standards

CPSC 427: Object-Oriented Programming

CSE 331 Software Design and Implementation. Lecture 12 Assertions & Exceptions

Exceptions and Design

Summary. Recursion. Overall Assignment Description. Part 1: Recursively Searching Files and Directories

Defensive Programming

EXCEPTION HANDLING. Summer 2018

CS112 Lecture: Repetition Statements

ITI Introduction to Computing II

Internal Classes and Exceptions

BBM 102 Introduction to Programming II Spring Exceptions

ITI Introduction to Computing II

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Learning objectives. Reading carefully. Managing your time. CS2110 Fall 2017 Assignment A1. PhD Genealogy. See CMS for the due date

Initial Coding Guidelines

엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2017 EOM, HYEONSANG ALL RIGHTS RESERVED

CS61B Lecture #12. Programming Contest: Coming up Saturday 5 October. See the contest announcement page, here.

CS 61B Discussion 5: Inheritance II Fall 2014

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

CS Internet programming Unit- I Part - A 1 Define Java. 2. What is a Class? 3. What is an Object? 4. What is an Instance?

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

ASSIGNMENT 6. COMP-202, Winter 2015, All Sections. Due: Tuesday, April 14, 2015 (23:59)

Homework 6: Heaps Due: 5:00 PM, Apr 9, 2018

Exceptions in Java

Lecture 7: Implementing Lists, Version 2

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

Programming Languages and Techniques (CIS120)

To Think About. MyClass.mogrify(new int[] { 1, 2, 4, 6 }));

6.005 Elements of Software Construction

BBM 102 Introduction to Programming II Spring 2017

A Third Look At Java. Chapter Seventeen Modern Programming Languages, 2nd ed. 1

CS112 Lecture: Defining Instantiable Classes

Project #1 rev 2 Computer Science 2334 Fall 2013 This project is individual work. Each student must complete this assignment independently.

Lab 2: File Input and Output

COMP-202 Unit 9: Exceptions

Inheritance. SOTE notebook. November 06, n Unidirectional association. Inheritance ("extends") Use relationship

Soda Machine Laboratory

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

Exceptions. CSE 142, Summer 2002 Computer Programming 1.

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

SML Style Guide. Last Revised: 31st August 2011

COMP 213. Advanced Object-oriented Programming. Lecture 17. Exceptions

Assertions and Exceptions Lecture 11 Fall 2005

Exceptions and Libraries

COMP-202 Unit 9: Exceptions

F1 A Java program. Ch 1 in PPIJ. Introduction to the course. The computer and its workings The algorithm concept

Lecture 23: Priority Queues 10:00 AM, Mar 19, 2018

Chapter 13 Exception Handling

Homework 4: Hash Tables Due: 5:00 PM, Mar 9, 2018

Pages and 68 in [JN] conventions for using exceptions in Java. Chapter 8 in [EJ] guidelines for more effective use of exceptions.

Prelim 1. CS 2110, September 29, 2016, 7:30 PM Total Question Name Loop invariants

CSC207H: Software Design. Exceptions. CSC207 Winter 2018

JAVA REVIEW cs2420 Introduction to Algorithms and Data Structures Spring 2015

Data Structure. Recitation IV

Absolute C++ Walter Savitch

C++ Programming Style Guide

ACORN.COM CS 1110 SPRING 2012: ASSIGNMENT A1

Transcription:

CS18 Integrated Introduction to Computer Science Fisler, Nelson Contents Good Coding Practices Spring 2018 1 Introduction 1 2 The Don ts 1 3 The Dos 4 4 CS 18-Specific Practices 5 5 Style 6 1 Introduction This document is the CS 18 good coding practices guide. Writing code that follows these practices will serve you well beyond CS 18, because when your code is well structured and well organized, it is easier to read, debug, test, optimize, etc. Please read this guide carefully. In addition to functionality, your assignments will be graded on how well you abide by these good practices, so this guide should be a valuable tool to you while coding. Note that this guide is an evolving document, subject to revision as the semester progresses. If we observe any particularly good or bad coding practices in the assignments you turn in, we will extend this document so that we can share those practices with other students. Finally, the examples in this document are written in Java, but they apply equally well to Scala, and all languages which support the features mentioned. These are language-independent good coding practices; practices you should embrace in all your future programming endeavors. 2 The Don ts This list is a compilation of what not to do while writing code in CS 18 (and beyond). Repeat Code It is bad practice to repeat code. Instead, repeated code should be abstracted out to a helper method, an abstract class, or a utility class so that it can be shared. Write Overly Complicated Logic You should always aim to avoid using overly complicated logic in your code. The simplest solution is best! If you find yourself using convoluted logic in an assignment during CS 18, you are probably not approaching the problem correctly. You should go back to the drawing board.

If you must hand in complicated code, be sure to add clear and concise comments, and, as always, give your variables meaningful names. Overuse Public It is bad practice to declare member variables public, as they can then be accessed/modified by other code. Instead, they should be declared private (or protected), and accessed via getters and setters. Use instanceof When programming, you should (almost) always know the type of your data. Needing to use instanceof implies a bad design and should make you rethink your class heirarchy. An exception to this rule is the use of instanceof when overriding the equals method. Boolean Logic The following are similar to CS 17 guidelines on the correct use of booleans: ˆ A variable should never be directly compared to a boolean, as doing so is redundant. For example, this condition if (x == true) { evaluates to true if x is true, and false if x is false. Thus, the above statement should instead be written as: if (x) { ˆ An if/else statement should never set a variable s value to a boolean, as the variable can be set to the value of the predicate instead. For example, this statement if (x == y) { mybool = true; else { mybool = false; returns true if x is equal to y, and false otherwise. Thus, the above statement should instead be written as: mybool = (x == y); ˆ It is bad practice to leave a section of an if/else statement empty. For example: if (x == y) { else { myfunction(); 2

Instead, this statement should be rewritten as: if (x!= y) { myfunction(); Exceptions The following good coding practices pertain to throwing and handling exceptions. ˆ You should never catch an unchecked exception. Unchecked exceptions, like ArrayOutOfBoundsException and NullPointerException, which inherit from RunTimeException, represent things that should never happen (e.g., accessing the 1st item in an array; getting the first item of an empty collection; etc.). When your program encounters an unchecked exception, it exits ungracefully, dumping error information to the console and confusing your users. If your program is throwing an unchecked exception, don t catch it: fix your code! Instead of writing this: catch (NullPointerException npe) { you should aim to write code such that does not encounter NullPointerExceptions! ˆ You should never catch Exception. The Exception class is the root of the exception hierarchy the class from which all exceptions (including RunTimeException) inherit. You should never catch Exception, as it is unchecked (it is a superclass of the class of all unchecked exceptions!). Regardless, you should always catch the most specific exception possible. If you catch only very general exceptions, catastrophic behavior can occur without explanation, making your code difficult to debug. Instead of writing this: catch (Exception e) { you should catch the specific checked exceptions that you are expecting to encounter and handle them appropriately, like this: catch (FileNotFoundException fnfe) { catch (IOException ioe) { 3

Furthermore, you should prefer a custom exception to a built-in exception, because you can include information specific to the state of your program in a custom exception, whereas you can only include an error message in a built-in one. ˆ You shouldn t write useless try/catch blocks. Instead, your try/catch blocks should do some useful work, like printing out an informative error message or handling the error. Printing out a stack trace is generally not all that useful to a user. Another pattern to avoid is catching an exception just to throw it again. For example: catch (FileNotFoundException fnfe) { throw new FileNotFoundException(); You should either not catch the FileNotFoundException in the first place or handle it gracefully. Note: Printing a stack trace or catching and re-throwing an exception may be helpful while debugging, but should be removed before making code available to other users (or graders). 3 The Dos This list is a compilation of what to do while writing code in CS 18 (and beyond). Be Lavish with Interfaces An interface is a set of behaviors. Interfaces are not essential to a program s functionality, but they are useful (perhaps even essential) to debugging because they create requirements for programmers. As a rule of thumb, you should be lavish with interfaces. More interfaces make your code more readable, more extensible, and more easily testable. When in doubt, use an interface! Properly Format Code Poorly-formatted code can be difficult to read and should be avoided. In CS 18, we provide you with an Eclipse autoformatter to help make sure that all code you write is nicely formatted. Instructions for installing it can be found in Lab 1. If you use our formatter, you won t lose points for poorly-formatted code. Constants Constants should be declared at the top of a class rather than hardcoded throughout. For example: int[] array1 = new int[10]; int[] array2 = new int[10]; int[] array3 = new int[10]; should be rewritten as: 4

int ARRAY_SIZE = 10; int[] array1 = new int[array_size]; int[] array2 = new int[array_size]; int[] array3 = new int[array_size]; In this latter code snippet, it is much easier to understand the choice of the value 10. In addition, it is much easier to change the size of the arrays from 10 to 100 a year from now, when you have long since forgotten where all the hardcoded instances of 10 appear in your code base. (Likewise, it makes it easier for someone other than you to make this change.) Use Brackets While your Java code may well compile without brackets, you should always use them when writing if statements, for loops, and while loops. 1 For example: if (x == y) { return "x equals y, yay!"; is preferred to if (x == y) return "x equals y, yay!"; Use Descriptive Variable Names Your variable names should be as informative as possible. The following are examples of poorly-named variables: p var array16 These names do not help the reader (often you!) understand what they represent. The following are examples of well-named variables: counter inputfile gradearray 4 CS 18-Specific Practices Support Code Never copy and paste support code from the CS 18 directory into your own code. Instead, you should import support code as directed in our handouts. Before turning in an assignment, be sure to run your code on a deparment machine (either in person or through ssh ) to ensure that any support code imports correctly. 1 Tim once wasted an entire day debugging because he had taken this shortcut and not grouped statements correctly. 5

Delete Debugging Comments You should not turn in code that includes code you write for debugging purposes only, rather than functionality. For example, do not include code that prints stack traces or contains TODOs, println statements, commented out code, method stubs, or methods that are never called. All of the above are useful tools to have at your disposal while debugging, but you must delete them before turning in an assignment, as they complicate the grading process. You will be penalized if you do not follow this instruction. 5 Style Another good coding practice is to abide by the conventions of the language in which you are programming. These conventions are usually outlined in a style guide. Indeed, two such guides have been developed expressly for CS 18. You should read these style guide to better understand the conventions enforced by the CS 18 Java and Scala autoformatters. If your computer science career extends beyond Brown, you are certain to encounter a style guide. So one last good coding practice is to get in the habit of following one now! Please let us know if you find any mistakes, inconsistencies, or confusing language in this or any other CS18 document by filling out the anonymous feedback form: http://cs.brown.edu/ courses/cs018/feedback. 6