PROGRAMMING STYLE. Fundamentals of Computer Science I

Similar documents
Conditionals, Loops, and Style

Conditionals, Loops, and Style. Control flow thus far. if statement. Control flow. Common branching statement Evaluate a boolean expression

Basic Computation. Chapter 2

Assignment Marking Criteria

MODULE 02: BASIC COMPUTATION IN JAVA

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

Chapter 2 Part 2 Edited by JJ Shepherd, James O Reilly

Basic Computation. Chapter 2

Array Basics: Outline

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Java Basics

Full file at

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

CONDITIONAL EXECUTION

Array Basics: Outline

Java Bytecode (binary file)

DATA TYPES AND EXPRESSIONS

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

Lec 3. Compilers, Debugging, Hello World, and Variables

CONDITIONAL EXECUTION

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

Supplement D: Expanded Guidelines on Programming Style and Documentation

Expanded Guidelines on Programming Style and Documentation

Tutorial 1 CSC 201. Java Programming Concepts عؾادئماظربجمةمبادؿكدامماجلاصا

Lab # 2. For today s lab:

CS 251 Intermediate Programming Coding Standards

Key Differences Between Python and Java

CS 152: Data Structures with Java Hello World with the IntelliJ IDE

CS 351 Design of Large Programs Coding Standards

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

Basics of Java Programming

Flow of Control. Chapter 3

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

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

TESTING AND DEBUGGING

M e t h o d s a n d P a r a m e t e r s

Lecture Set 2: Starting Java

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

CS 177 Recitation. Week 1 Intro to Java

Lecture Set 2: Starting Java

Working with JavaScript

COMP 202 Java in one week

Chapter Two Bonus Lesson: JavaDoc

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

Chapter 2: Programming Concepts

COMP-202: Foundations of Programming. Lecture 2: Java basics and our first Java program! Jackie Cheung, Winter 2016

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

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

Object-oriented programming in...

Visual Programming. Lecture 3: Loops, Arrays. Mahmoud El-Gayyar

Software and Programming 1

CEN 414 Java Programming

4 Programming Fundamentals. Introduction to Programming 1 1

CS 106 Introduction to Computer Science I

Advanced Computer Programming

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

A Foundation for Programming

COMP163. Introduction to Computer Programming. Introduction and Overview of the Hardware

Algorithms and Programming I. Lecture#12 Spring 2015

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

Primitive Data, Variables, and Expressions; Simple Conditional Execution

Programming Syntax and Style. David Greenstein Monta Vista High School

CHAPTER 2 Java Fundamentals

Chapter 2 Author Notes

COMP-202: Foundations of Programming. Lecture 2: Java basics and our first Java program! Jackie Cheung, Winter 2015

BlueJ Demo. Topic 1: Basic Java. 1. Sequencing. Features of Structured Programming Languages

Java Programming Fundamentals - Day Instructor: Jason Yoon Website:

Fundamentals of Programming. Lecture 3: Introduction to C Programming

Welcome to Python 3. Some history

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

CMSC 201 Fall 2018 Python Coding Standards

Perl Basics. Structure, Style, and Documentation

CS125 : Introduction to Computer Science. Lecture Notes #6 Compound Statements, Scope, and Advanced Conditionals

CS 152 Computer Programming Fundamentals Coding Standards

class objects instances Fields Constructors Methods static

COMP-202: Foundations of Programming. Lecture 5: Arrays, Reference Type, and Methods Sandeep Manjanna, Summer 2015

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

A Foundation for Programming

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

Chapter 2. Lexical Elements & Operators

Chapter 4 Defining Classes I

Programming Assignment 1: CS11TurtleGraphics

Introduction to C Programming

Introduction to Java & Fundamental Data Types

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

Program Fundamentals

Branching and Boolean Expressions

CPS122 Lecture: Defining a Class

CONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics. COMP-202 Unit 1: Introduction

CS106A, Stanford Handout #30. Coding Style

Anatomy of a Java Program: Comments

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

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

3 CREATING YOUR FIRST JAVA APPLICATION (USING WINDOWS)

CS1150 Principles of Computer Science Loops (Part II)

COMP-202 Unit 2: Java Basics. CONTENTS: Using Expressions and Variables Types Strings Methods

17 Hello world 18 Type: String: literal 19 Standard API: System: out.println() 20 Hello world 21 Statement 22 Statement: simple statements are ended w

Full file at C How to Program, 6/e Multiple Choice Test Bank

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

Mr. Monroe s Guide to Mastering Java Syntax

CS 302: INTRODUCTION TO PROGRAMMING IN JAVA. Chapter 5: Methods. Lecture 10

Transcription:

PROGRAMMING STYLE Fundamentals of Computer Science I

Documentation and Style: Outline Meaningful Names Comments Indentation Named Constants Whitespace Compound Statements

Documentation and Style Most programs are modified over time to respond to new requirements. Programs which are easy to read and understand are easy to modify. Even if it will be used only once, you have to read it in order to debug it. And when we talk about javadoc, if your comments are meaningful, your code will write its own documentation!!

Meaningful Variable Names A variable's name should suggest its use e.g. taxrate Boolean variables should suggest a true/false value Choose names such as ispositive or systemsareok. Avoid names such as numbersign or systemstatus.

Style: Naming Things Variable names Begin with lowercase, uppercase each new word int totalwidgets; Class names Begin uppercase, then lowercase except for new words public class InventoryTracker Name exactly as in assignment description Constants All upper case, use _ between words double SPEED_LIGHT = 3.0e8; 5

Comments The best programs are self-documenting. Clean style Well-chosen names Comments are written into a program as needed to explain the program. They are useful to the programmer, but they are ignored by the compiler.

Style: Comments Comments help reader/grader understand your program Good comments explain why something is done Write comments before coding tricky bits Helps you formulate a plan Don't comment the obvious: i++; // Increment i by one 7

Comments A comment can begin with //. Everything after these symbols and to the end of the line is treated as a comment and is ignored by the compiler. double radius; //in centimeters

Comments A comment can begin with /* and end with */ Everything between these symbols is treated as a comment and is ignored by the compiler. /** This program should only be used on alternate Thursdays, except during leap years, when it should only be used on alternate Tuesdays. */

Comments A javadoc comment, begins with /** and ends with */. It can be extracted automatically from Java software. /** method change requires the number of coins to be nonnegative */ We will talk about javadoc later in the semester.

When to Use Comments Begin each program file with an explanatory comment What the program does The name of the author Contact information for the author Date of the last modification. Provide only those comments which the expected reader of the program file will need in order to understand it.

Indentation Indentation should communicate nesting clearly. A good choice is four spaces for each level of indentation. Indentation should be consistent. Indentation should be used for second and subsequent lines of statements which do not fit on a single line. Indentation does not change the behavior of the program. Proper indentation helps communicate to the human reader the nested structures of the program Eclipse will help you indent correctly Eclipse can fix automatically, ctrl-a then ctrl-i

Using Named Constants To avoid confusion, always name constants (and variables). area = PI * radius * radius; is clearer than area = 3.14159 * radius * radius; Place constants near the beginning of the program. Once the value of a constant is set (or changed by an editor), it can be used (or reflected) throughout the program. public static final double INTEREST_RATE = 6.65; If a literal (such as 6.65) is used instead, every occurrence must be changed, with the risk that another literal with the same value might be changed unintentionally.

Declaring Constants Syntax public static final Variable_Type Variable_Name = Constant_Value; Examples: public static final double PI = 3.14159; public static final String MOTTO = "The customer is always right."; By convention, uppercase letters are used for constants.

Style: Whitespace public class StarTriangle { public static void main(string[] args) {int limit = Integer.parseInt(args[0]); for (int i=0;i<limit;i++){ for (int j = 0; j <= i; j++) System.out.print("*");System.out.println(); }}} Indent each level of conditionals/loops Indent a fixed number of spaces (3-4) Use blank lines to separate logical sections Only one statement per line 15

Style: Whitespace for (int i=0;i<limit;i++) vs. for (int i = 0; i < limit; i++) a=b*c/d-(8.12*e); vs. a = b * c / d - (8.12 * e); //this is a comment //describing my code vs. // this is a comment // describing my code Use spaces between Statements in for loop Operators in math expressions After the // starting a comment 16

Style: Whitespace Math. random (); vs. Math.random(); args [0]; vs. args[0]; i = i + 1 ; i = i + 1; Do NOT use spaces between method class, dot, name, or ()'s array name and []'s statement and ending semicolon vs. 17

Style: Whitespace Use spaces to align parallel code if it makes it more readable Often helps to spot mistakes int numpoints = Integer.parseInt(args[0]); int startx = Integer.parseInt(args[0]); int starty = Integer.parseInt(args[2]); double velx = Integer.parseInt(args[3]); double vely = Integer.parseInt(args[4]); int numpoints = Integer.parseInt(args[0]); int startx = Integer.parseInt(args[0]); int starty = Integer.parseInt(args[2]); double velx = Integer.parseInt(args[3]); double vely = Integer.parseInt(args[4]); 18

Style: Curly Bracing public class HelloWorld { public static void main(string [] args) { System.out.println("Hello world!"); } } public class HelloWorld { public static void main(string [] args) { System.out.println("Hello world!"); } } public class HelloWorld { public static void main(string [] args) { System.out.println("Hello world!"); } } 19

Summary Meaningful Names Comments Indentation Named Constants Whitespace Compound Statements