CSE 142/143 Unofficial Style Guide

Similar documents
CSE 11 Style Guidelines

C++ Programming Style Guide

CS 251 Intermediate Programming Coding Standards

Java Programming Style Guide

CS 351 Design of Large Programs Coding Standards

CSCI 2101 Java Style Guide

Rule 1-3: Use white space to break a function into paragraphs. Rule 1-5: Avoid very long statements. Use multiple shorter statements instead.

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

IT Web and Software Developer Software Development Standards

EECS 280 C++ Coding Standards

11 Coding Standards CERTIFICATION OBJECTIVES. Use Sun Java Coding Standards

What does this program print?

Perl Basics. Structure, Style, and Documentation

Course Coding Standards

Working with JavaScript

Enums. In this article from my free Java 8 course, I will talk about the enum. Enums are constant values that can never be changed.

Conditionals, Loops, and Style

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

CS 152 Computer Programming Fundamentals Coding Standards

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

Programming Style Guide v1.1

Formatting & Style Examples

Documentation Requirements Computer Science 2334 Spring 2016

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

CSE332: Data Abstrac0ons Sec%on 1. Hye In Kim Winter 2014

Hello, World and Variables

Chapter 2 Author Notes

C Formatting Guidelines

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Programming for Engineers Introduction to C

class objects instances Fields Constructors Methods static

Lecture 27. Lecture 27: Regular Expressions and Python Identifiers

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

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING

Introduction to Python Code Quality

Coding Standard for ECE3090 August 24, 2005

CS 231 Data Structures and Algorithms, Fall 2016

VARIABLES. 1. STRINGS Data with letters and/or characters 2. INTEGERS Numbers without decimals 3. FLOATING POINT NUMBERS Numbers with decimals

Introduction to Python

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

Full file at

Key Differences Between Python and Java

Programming Syntax and Style. David Greenstein Monta Vista High School

CS Problem Solving and Object-Oriented Programming

Code Convention and version control help us to success in Botball

Introduction to C Programming

CS Introduction to Programming Midterm Exam #1 - Prof. Reed Spring What is your name?: (4 points for writing it on your answer sheet)

Overview of the Ruby Language. By Ron Haley

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

Scala Style Guide Spring 2018

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

COMP 202 Java in one week

Information Science 1

Assignment Marking Criteria

Coding Standards for Java

If you don t, it will return the same thing as == But this may not be what you want... Several different kinds of equality to consider:

CSE 341, Autumn 2015, Ruby Introduction Summary

EE 382 Style Guide. March 2, 2018

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

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

QUIZ on Ch.5. Why is it sometimes not a good idea to place the private part of the interface in a header file?

Expanded Guidelines on Programming Style and Documentation

Identifiers and Variables

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

Coding Workshop. Learning to Program with an Arduino. Lecture Notes. Programming Introduction Values Assignment Arithmetic.

Data Structure and Programming Languages

DaMPL. Language Reference Manual. Henrique Grando

Introduction to Java & Fundamental Data Types

Quiz 1: Functions and Procedures

Math Modeling in Java: An S-I Compartment Model

printf( Please enter another number: ); scanf( %d, &num2);

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Fundamentals of Programming Session 23

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

Pace University. Fundamental Concepts of CS121 1

Coding Standards for C

COMP-202 Unit 4: Programming with Iterations

Information Science 1

Coding Standards for C

GOLD Language Reference Manual

Supplement D: Expanded Guidelines on Programming Style and Documentation

APCS Semester #1 Final Exam Practice Problems

C++ Style Guide. 1.0 General. 2.0 Visual Layout. 3.0 Indentation and Whitespace

The C++ Language. Arizona State University 1

OCaml Style Guide Fall 2017

Variables One More (but not the last) Time with feeling

Java Bytecode (binary file)

CS 142 Style Guide Grading and Details

CSCI 262 C++ Style Guide

SML Style Guide. Last Revised: 31st August 2011

Style and Submission Guide

9/5/2018. Overview. The C Programming Language. Transitioning to C from Python. Why C? Hello, world! Programming in C

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

Boolean Expressions. Is Equal and Is Not Equal

Lecture 10 Declarations and Scope

The C Programming Language. (with material from Dr. Bin Ren, William & Mary Computer Science)

CS 211 Programming Practicum Fall 2018

ECE 122 Engineering Problem Solving with Java

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

Transcription:

CSE 142/143 Unofficial Style Guide Below, things in GREEN are GOOD; things in RED are to be AVOIDED. Commenting Comment well. Follow the commenting rules for header, method, field, and inside-method comments in the accompanying commenting guide. Naming Follow the following naming conventions for different types of variables. Type of variable Formatting Examples Class name Capitalized (first letter of each word is uppercased) MyProgram CritterMain Method/variable name camelcase (first letter lowercase, each new word greatmethod1() String firstname Class constant uppercase) UPPER_CASE (words separated by underscores) int countofx MY_CONSTANT MAX_SIZE Name your variables and methods well. Try to be descriptive while also being concise. Avoid generic names such as x, func, and stuff when a better name exists. Also try to avoid many variables with similar names, such as temp1, temp2, and temp3. Single letter names are almost always bad; however, there are certain situations, such as the i counter in a for loop, where single-letter variables are the best option. If you have a variable that holds the name of a musician, for example, then look at the following table of possible names: Variable Good or bad? Why? n Bad Too short text Bad Not descriptive thenameofthepersonwhowritesthemusic Bad Too long/complex musicanname Good Descriptive, short Formatting Keep lines under 100 characters in length, preferably under 80 characters. It is very difficult to read (and grade) code when it is too long. This can mean that you have to split up a line of code, or that you must put a comment on a line by itself rather than on the same line as the code it refers to.

Leave a blank line between methods. Don t be afraid of the white space. public void calculateprime() { plusbyone(); <-- Put a blank line here public void plusbyone() {... Put only one statement on a line. int x = 3; int y = 4; if (x == y) { foo(); Good: int x = 3; int y = 4; if (x == y) { foo(); Leave spaces around all operators (+, -, *, /, =, &&,,!=, >, >=, <, <=). This makes your code much easier to read. Good: int x = 4 + 5; boolean b = 4 > (4 * 2); int x=4+5; boolean b =4>(4*2); Leave no spaces before the parentheses in a method call. Good: method1(); method1 (); Leave a space before opening curly braces. Good: public static void calculateprimes() { if (true) { public static void calculateprimes(){ if (true){ Indent every code block (the stuff inside any curly braces { ). Indent after every if, while, and for statement even if you don't use curly braces. An indent is usually 3 or 4 spaces (choose one and stick with it). public void addodds(int times) { int sum = 0; for (int i = 0; i < times; i++) {

if (times % 2 == 0) sum += i; You can omit the curly braces after an if, while, or for if there is only one statement inside it. However, I strongly discourage this. For consistency, always use curly braces. This also helps prevent bugs that occur when you try to add a statement to a for-loop and forget to add the curly braces. But overall: BE CONSISTENT. If for some reason you don't agree with my conventions (which you should - these are generally accepted), for goodness sake be consistent within your code! Boolean zen Booleans are cool because you don't always have to do all the testing with == and!= that you do with other types. Treat them like the true and false that they are. Remember that you can use! to negate a boolean value, as in example 2 below. If you have declared boolean isalive; then... BAD if (isalive == true) { eatfood(); return isalive == false; if (isalive) { return true; else { return false; GOOD if (isalive) { eatfood(); return!isalive; return isalive; Variable/field guidelines Fields should always be private, unless specifically stated otherwise. However, class constants (which have the keywords static final in their declaration and cannot be changed) can be public.

Good: private String name; public static final String NAME; public String name; If a value doesn t change when your code runs, or you find a number that appears repeatedly in your code and doesn t change, declare a class constant and give it a name. This improves reusability. Avoid static global variables. For the vast majority of purposes, you will never need static global variables. Instead, use either fields or constants. public static String bad; Always declare fields above the first constructor. Initialize them in the constructor, not outside it. Good: private String name; private int count; public Counter(String givenname) { count = 1; private String name = "bob"; private int count = 1; public Counter(String givenname) { Localize variables as much as possible. This means that if something is only needed in one method, it should be a variable in that method and not a field. Only data that is part of your object s state and are needed in several methods should be stored in fields. If a variable is only needed inside a loop, declare it inside the loop and not before. Good: public int addtwos(int times) { int sum = 0; for (int i = 0; i < times; i++) { int temp = 2 * times; sum += temp;

return sum; public int addtwos(int times) { int sum = 0; int temp; for (int i = 0; i < times; i++) { temp = 2 * times; sum += temp; return sum; Redundancy Redundancy is the most subtle style issue, so here are a few guidelines for avoiding it. However, use your best judgment and common sense as a reliable guide. If you find yourself doing the same thing multiple times, make a method out of it and call it whenever you need it. Use parameters to generalize the code. public void printall() { System.out.println("x = 500, y = 100"); System.out.println("x = 400, y = 200"); System.out.println("x = 300, y = 300"); System.out.println("x = 200, y = 400"); System.out.println("x = 100, y = 500"); Good: use a helper method with one parameter to capture the redundancy of the printed format "x = something, y = something" public void printall() { printline(500); printline(400); printline(300); printline(200); printline(100); public void printline(int x) { int y = 600 x; System.out.println("x = " + x + ", y = " + y);

Even better, if you find a pattern, use a loop to reduce the number of manual method calls or operations you do. Great: public void printall() { for (int i = 500; i > 0; i -= 100) { printline(i); public void printline(int x) { int y = 600 x; System.out.println("x = " + x + ", y = " + y); Don t over-use methods. A one-line method that is called only once is probably not useful. Make new methods only where it makes sense to divide your code for reuse or readability. Avoid redundancy in constructors: use the this notation to have one constructor call another. Probably only one constructor should be doing major work. private String name; private int count; public Counter(String givenname) { count = 0; public Counter(int currentcount) { count = currentcount; givenname = null; public Counter(String givenname, int currentcount) { count = currentcount; Good: private String name; private int count;

public Counter(String givenname) { this(givenname, 0); public Counter(int currentcount) { this(null, currentcount); public Counter(String givenname, int currentcount) { count = currentcount; Other Remove any debugging code from your program before you turn it in. Make helper methods private. The client does not need to see them. In general, avoid break or continue statements in loops unless absolutely necessary. If you don t know what those are, then don t worry about it. The homework specifications are the ultimate guide on what we expect, so refer to them for any special requirements.