Clean Formatting. Christopher Simpkins Chris Simpkins (Georgia Tech) CS 2340 Objects and Design CS / 1

Similar documents
Clean Comments and Formatting

Clean Classes. Christopher Simpkins Chris Simpkins (Georgia Tech) CS 2340 Objects and Design CS / 11

CS 2340 Objects and Design

CS 2340 Objects and Design - Scala

Clean Boundaries. Christopher Simpkins Chris Simpkins (Georgia Tech) CS 2340 Objects and Design CS / 10

Introduction to Object-Oriented Programming

Introduction to Object-Oriented Programming

CS 2340 Objects and Design

Introduction to Object-Oriented Programming

Maintainability and Readability. What makes code readable? Understandable code. General Routine Structure. General Module Structure

Introduction to Object-Oriented Programming

Clean Functions. Christopher Simpkins Chris Simpkins (Georgia Tech) CS 2340 Objects and Design CS / 1

CS 1331 Introduction to Object Oriented Programming

Testing. Christopher Simpkins Chris Simpkins (Georgia Tech) CS 2340 Objects and Design CS / 13

Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous

Functional Objects. Christopher Simpkins CS 3693, Fall Chris Simpkins (Georgia Tech) CS 3693 Scala / 1

Introduction to Object-Oriented Programming

(7-2) Operator Overloading D & D Chapter 10. Instructor - Andrew S. O Fallon CptS 122 (February 23, 2018) Washington State University

Introduction to Object-Oriented Programming

COMP 202 Java in one week

CS106A, Stanford Handout #30. Coding Style

Intermediate Code Generation

11 Coding Standards CERTIFICATION OBJECTIVES. Use Sun Java Coding Standards

Computer Programming C++ Classes and Objects 6 th Lecture

CS 2340 Objects and Design

CSE 431S Type Checking. Washington University Spring 2013

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

Stacks II. Adventures in Notation. stacks2 1

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

CS 142 Style Guide Grading and Details

Java Fall 2018 Margaret Reid-Miller

CS 106A Midterm Review. Rishi Bedi, adapted from slides by Kate Rydberg and Nick Troccoli Summer 2017

Java Programming Style Guide

JAVA REVIEW cs2420 Introduction to Algorithms and Data Structures Spring 2015

CS112 Lecture: Working with Numbers

Chris Simpkins (Georgia Tech) CS 2316 Data Manipulation for Engineers Python Overview 1 / 9

Operators DECREASE in strength toward the Bottom

CS159. Nathan Sprague

Introduction to Object-Oriented Programming

Advanced Computer Programming

The Program Specification:

public class Foo { private int var; public int Method1() { // var accessible anywhere here } public int MethodN() {

CPS311 Lecture: Procedures Last revised 9/9/13. Objectives:

Chapter 2: Basic Elements of Java

Review main idea syntax-directed evaluation and translation. Recall syntax-directed interpretation in recursive descent parsers

G52CPP C++ Programming Lecture 17

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

CPS122 Lecture: Defining a Class

Announcements. CS18000: Problem Solving And Object-Oriented Programming

Cpt S 122 Data Structures. Introduction to C++ Part II

Do these criteria apply to work in this course?

Programming Languages and Techniques (CIS120)

Ch. 12: Operator Overloading

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

Friends and Unary Operators

CSc 453 Intermediate Code Generation

There are four numeric types: 1. Integers, represented as a 32 bit (or longer) quantity. Digits sequences (possibly) signed are integer literals:

HOW TO WRITE USER STORIES (AND WHAT YOU SHOULD NOT DO) Stuart Ashman, QA Director at Mio Global Bob Cook, Senior Product Development Manager, Sophos

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

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

GOING IN STYLE (#3): ON TYPOGRAPHY, PART 2

EE 382 Style Guide. March 2, 2018

Practice Midterm Sample Solutions

Software Coding Guidelines

Logistics. Final Exam on Friday at 3pm in CHEM 102

register lock_guard(mtx_); string_view s = register to_string(42); We propose register-expression to grant the temporary objects scope lifetimes.

Code Convention and version control help us to success in Botball

CPS122 Lecture: From Python to Java

Written by John Bell for CS 342, Spring 2018

Documenting Java Code. Javadoc: The Tool and the Legend

Programming Languages and Techniques (CIS120e)

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

1. General Principles

CS 3 Introduction to Software Engineering. 3: Exceptions

Last Time. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

CS260 Intro to Java & Android 03.Java Language Basics

QueueBlock, ReversalADT, LinkedList,CustomerAccount, not MaintainCustomerData

CS 152 Computer Programming Fundamentals Coding Standards

How about them A s!! Go Oaktown!! CS61C - Machine Structures. Lecture 4 C Structures Memory Management Dan Garcia.

A PROGRAM IS A SEQUENCE of instructions that a computer can execute to

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Section we will not cover section 2.11 feel free to read it on your own

Pythonic Coding Style. C-START Python PD Workshop

CS240: Programming in C

Overloaded Operators, Functions, and Students

CS11 Java. Fall Lecture 1

Kinds Of Data CHAPTER 3 DATA REPRESENTATION. Numbers Are Different! Positional Number Systems. Text. Numbers. Other

The Composite State Visitor Pattern

Mehran Sahami Handout #7 CS 106A September 24, 2014

Assignment #1: /Survey and Karel the Robot Karel problems due: 1:30pm on Friday, October 7th

What is a Pattern? Lecture 40: Design Patterns. Elements of Design Patterns. What are design patterns?

One of Mike s early tests cases involved the following code, which produced the error message about something being really wrong:

Racket Style Guide Fall 2017

Casting -Allows a narrowing assignment by asking the Java compiler to "trust us"

13 th Windsor Regional Secondary School Computer Programming Competition

CS61C : Machine Structures

Coding Style Handout #15 February 1, CS106A Winter

TinyVM: User's Guide

CSSE 220. Event Based Programming. Check out EventBasedProgramming from SVN

Iterator: A way to sequentially access ( traverse ) each object in a container or collection. - Access is done as needed, not necessarily at once.

Transcription:

Clean Formatting Christopher Simpkins chris.simpkins@gatech.edu Chris Simpkins (Georgia Tech) CS 2340 Objects and Design CS 1331 1 / 1

Formatting Code should be written for human beings to understand, and only incidentally for machines to execute. Hal Abelson and Gerald Sussman, SICP The purpose of a computer program is to tell other people what you want the computer to do. Donald Knuth The purpose of formatting is to facilitate communication. The formatting of code conveys information to the reader. Chris Simpkins (Georgia Tech) CS 2340 Objects and Design CS 1331 2 / 1

Vertical Formatting Newspaper metaphor Vertical openness between concepts Vertical density Vertical distance Vertical ordering Chris Simpkins (Georgia Tech) CS 2340 Objects and Design CS 1331 3 / 1

Vertical Openness Between Concepts Notice how vertical openness helps us locate concepts in the code more quickly. package fitnesse.wikitext.widgets; import java.util.regex.*; public class BoldWidget extends ParentWidget { public static final String REGEXP = ".+? "; private static final Pattern pattern = Pattern.compile(" (.+?) ", Pattern.MULTILINE + Pattern.DOTALL ); public BoldWidget(ParentWidget parent, String text) throws Exception { super(parent); Matcher match = pattern.matcher(text); match.find(); addchildwidgets(match.group(1)); Chris Simpkins (Georgia Tech) CS 2340 Objects and Design CS 1331 4 / 1

Vertical Openness Between Concepts If we leave out the blank lines: package fitnesse.wikitext.widgets; import java.util.regex.*; public class BoldWidget extends ParentWidget { public static final String REGEXP = ".+? "; private static final Pattern pattern = Pattern.compile(" (.+?) ", Pattern.MULTILINE + Pattern.DOTALL ); public BoldWidget(ParentWidget parent, String text) throws Exception { super(parent); Matcher match = pattern.matcher(text); match.find(); addchildwidgets(match.group(1)); It s harder to distinguish the package statement, the beginning and end of the imports, and the class declaration. It s harder to locate where the instance variables end and methods begin. Chris Simpkins (Georgia Tech) CS 2340 Objects and Design CS 1331 5 / 1

Vertical Density Openness separates concepts. Density implies association. Consider: public class ReporterConfig { /** The class name of the reporter listener */ private String m_classname; /** The properties of the reporter listener */ private List<Property> m_properties = new ArrayList<Property>(); public void addproperty(property property) { m_properties.add(property); The vertical openness (and bad comments) misleads the reader. Better to use closeness to convey relatedness: public class ReporterConfig { private String m_classname; private List<Property> m_properties = new ArrayList<Property>(); public void addproperty(property property) { m_properties.add(property); Chris Simpkins (Georgia Tech) CS 2340 Objects and Design CS 1331 6 / 1

Vertical Distance and Ordering Concepts that are closely related should be vertically close to each other. Variables should be declared as close to their usage as possible. Instance variables should be declared at the top of the class. Dependent functions: callers should be above callees. Chris Simpkins (Georgia Tech) CS 2340 Objects and Design CS 1331 7 / 1

Horizontal Openness and Density Keep lines short. Uncle Bob says 120, but he s wrong. Keep your lines at 80 characters or fewer if possible (sometimes it is impossible, but very rarely). Put spaces around = to accentuate the distinction between the LHS and RHS. Don t put spaces between method names and parens, or parens and paramter lists - they re closely related, so should be close. Use spaces to accentuate operator precedence, e.g., no space between unary operators and their operands, space between binary operators and their operands. Don t try to horizontally align lists of assignments it draws attention to the wrong thing and can be misleading, e.g., encouraging the reader to read down a column. Always indent scopes (classes, methods, blocks). Chris Simpkins (Georgia Tech) CS 2340 Objects and Design CS 1331 8 / 1

Team Rules Every team should agree on a coding standard and everyone should adhere to it. Don t modify a file just to change the formatting, but if you are modifying it anyway, go ahead and fix the formatting of the code you modify. Code formatting standards get religious. My rule: make your code look like the language inventor s code. If the language you re using has a code convention (like Java s), use it! Chris Simpkins (Georgia Tech) CS 2340 Objects and Design CS 1331 9 / 1