15-Jun-15 Beginning Style. 2 Be consistent! Most times, you will enter an ongoing project, with established style rules Follow them even if you don’t.

Slides:



Advertisements
Similar presentations
1-May-15 Style. 2 About the book This book is a team effort by many good programmers, not just one person’s opinions The rules have been widely distributed.
Advertisements

1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Introduction to C Programming
10-Jun-15 Just Enough Java. Variables A variable is a “box” that holds data Every variable has a name Examples: name, age, address, isMarried Variables.
Introduction to Programming with Java, for Beginners Primitive Types Expressions Statements Variables Strings.
10-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
Variables Pepper. Variable A variable –box –holds a certain type of value –value inside the box can change Example –A = 2B+1 –Slope = change in y / change.
Access to Names Namespaces, Scopes, Access privileges.
CIS 234: Using Data in Java Thanks to Dr. Ralph D. Westfall.
15-Jun-15 Style Consolidated Lectures. 2 About the book This book is a team effort by many good programmers, not just one person’s opinions The rules.
Style Rules II: Names and Naming. Overview Today we will discuss: –Reasons for naming conventions –Rules for naming variables, classes, and methods.
Arrays. A problem with simple variables One variable holds one value –The value may change over time, but at any given time, a variable holds a single.
18-Jun-15 Arrays. 2 A problem with simple variables One variable holds one value The value may change over time, but at any given time, a variable holds.
19-Jun-15 Access to Names Namespaces, Scopes, Access privileges.
Outline Java program structure Basic program elements
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Style Rules I. Style is important Everyone agrees that good style is important –Everyone agrees on most of the essentials –But some people have “religious.
26-Jun-15 Methods. About methods A method is a named group of declarations and statements If a method is in the same class, you execute those declarations.
26-Jun-15 Beginning Style. 2 Be consistent! Most times, you will enter an ongoing project, with established style rules Follow them even if you don’t.
28-Jun-15 Access to Names Namespaces, Scopes, Access privileges.
Introduction to C Programming
Algorithm Programming Coding Advices Bar-Ilan University תשס " ו by Moshe Fresko.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
Chapter 2 How to Compile and Execute a Simple Program.
Prepared by Uzma Hashmi Instructor Information Uzma Hashmi Office: B# 7/ R# address: Group Addresses Post message:
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
1 Identifiers  Identifiers are the words a programmer uses in a program  An identifier can be made up of letters, digits, the underscore character (
Java Language and SW Dev’t
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
© 2006 Pearson Education 1 Obj: cont 1.3 and 1.4, to become familiar with identifiers and to understand how programming languages work HW: p.51 #1.8 –
07 Coding Conventions. 2 Demonstrate Developing Local Variables Describe Separating Public and Private Members during Declaration Explore Using System.exit.
The Java Programming Language
CSC204 – Programming I Lecture 4 August 28, 2002.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Classes CS 21a: Introduction to Computing I First Semester,
Java Syntax and Style JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin,
CIT 590 Intro to Programming First lecture on Java.
23-Oct-15 Style. Why style matters Good style isn’t just to make your code “look pretty” The most critical factor in style is readability If a program.
Chapter 2: Java Fundamentals
Introduction to programming in the Java programming language.
Java The Java programming language was created by Sun Microsystems, Inc. It was introduced in 1995 and it's popularity has grown quickly since A programming.
A Simple Guide to Using SPSS ( Statistical Package for the Social Sciences) for Windows.
Asking the USER for values to use in a software 1 Input.
Code Conventions Tonga Institute of Higher Education.
Chapter 2 Variables.
Asking the USER for values to use in a software 1 Input.
1 Week 5 l Primitive Data types l Assignment l Expressions l Documentation & Style Primitive Types, Assignments, and Expressions.
Aside: Running Supplied *.java Programs Just double clicking on a *.java file may not be too useful! 1.In Eclipse, create a project for this program or.
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
Today… Style, Cont. – Naming Things! Methods and Functions Aside - Python Help System Punctuation Winter 2016CISC101 - Prof. McLeod1.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 1: Computer Systems Presentation slides for Java Software Solutions for AP* Computer Science.
Quiz 1 A sample quiz 1 is linked to the grading page on the course web site. Everything up to and including this Friday’s lecture except that conditionals.
Conditionals, Block Statements, Style
Working with Java.
Some Eclipse shortcuts
Chapter 1: Computer Systems
Style 5-Dec-18.
Style 5-Dec-18.
Style 22-Feb-19.
Beginning Style 27-Feb-19.
Style 4-Apr-19.
Style 9-Apr-19.
Introduction to Primitives
Introduction to Primitives
Style 26-Apr-19.
Unit 3: Variables in Java
Presentation transcript:

15-Jun-15 Beginning Style

2 Be consistent! Most times, you will enter an ongoing project, with established style rules Follow them even if you don’t like them In this course you will be working in teams with various other people We’ll all use the same set of style rules

3 Do it right the first time You only write code once, but you read it many times while you’re trying to get it to work Good style makes it more readable and helps you get it right! You’re working on a large project, so you use good style......but you need a tool to help you do one little job, so you slap it together quickly Guess which program will be around longer and used by more people?

4 Indent nested code Always indent statements that are nested inside (under the control of) another statement if (itemCost <= bankBalance) { writeCheck(itemCost); bankBalance = bankBalance - itemCost; } The open brace always goes at the end of a line The matching close brace lines up with the statement being closed Indentation should be consistent throughout the program 4 spaces has become more-or-less standard

5 Break up long lines Keep your lines short enough to be viewed and printed Many people use 72 or 80 character limits Suggestions on where to break a long line: It’s illegal to break a line within a quoted string Break after, not before, operators Line up parameters to a method Don’t indent the second line of a control statement with a long test so that it lines up with the statements being controlled

6 Don’t use “hard” tabs A hard tab is an actual tab character in your text It tells the program to go to the next tab stop (wherever that is) Not every program puts tab stops in the same place If you use hard tabs to indent, sooner or later your nice indentation will be ruined Good editors can be set to use soft tabs (your tab characters are replaced with spaces) When you hit the tab key, the editor puts spaces into your file, not tab characters With soft tabs, your indentation is always safe BlueJ uses only soft tabs

7 Using spaces Use spaces around all binary operators except “dot”: if (n > 1 && n % 2 == 1) n = 3 * n + 1; Do not use spaces just within parentheses: if ( x < 0 ) x = -x; // don’t do this Use a space before and after the parenthesized test in a control statement: if (x < 0) {...} while (x < 0) {...} Do not use a space between a method name and its parameters; do put a space after each comma: int add(int x, int y) {...} a = add(3, k);

8 Use meaningful names Names should be chosen very carefully, to indicate the purpose of a variable or method If the purpose changes, the name should be changed Spend a little time to choose the best name for each of your variables and methods! Long, multiword names are common in Java However, if a name is too long, maybe you’re trying to use it for too many purposes Don’t change the name, separate the purposes Don’t abbreviate names But very common abbreviations, such as max for “maximum”, are OK

9 Meaningful names: exceptions I It is common practice to use i as the index of a for- loop, j as the index of an inner loop, and k as the index of a third-level loop This is almost always better than trying to come up with a meaningful name Example: for (int i = 1; i <= 10; i++) { for (int j = 1, j <= 10; j++) { System.out.println(" " + (i * j)); } }

10 Meaningful names: exceptions II Local variables in methods may be given short, simple names, if: The purpose of the variable is obvious from context, and The variable is used only briefly, in a small part of the program But never use meaningless names for fields (class or instance variables) or classes or methods

11 Meaningful names: exceptions III If variables have no special meaning, you can use names that reflect their types For example, if you are writing a general method to work with any strings, you might name them string1, string2, etc. Alternatively, you can use very short names s, t, u, or s1, s2, etc. are often used for Strings p, q, r, s are often used for booleans w, x, y, z are often used for real numbers

12 Naming classes and interfaces Capitalize the first letter of each word, including the first word: PrintStream, Person, ExemptEmployee Use nouns to name classes: ExemptEmployee, CustomerAccount Classes are supposed to represent things Use adjectives to name interfaces: Comparable, Printable Interfaces are supposed to represent features

13 Naming variables Capitalize the first letter of each word except the first: total, maxValue Use nouns to name variables: balance, outputLine Variables are supposed to represent values

14 Naming methods Capitalize the first letter of each word except the first: display, displayImage Methods are capitalized the same as variables Use verbs when naming methods: displayImage, computeBalance Methods are supposed to do something

15 Naming constants A constant is an identifier whose value, once given, cannot be changed Constants are written with the keyword final, for example: final int FIVE = 5; final float AVOGADROS_NUMBER = 6.022E23; Constants are written in ALL_CAPITALS, with underscores between words

16 Correct style made easy In Eclipse, go to Window  Preferences  Java  Code Style  Formatter, and under Select a profile: choose Java conventions [built-in] Select some or all of your code and choose Source  Format To simply indent correctly, without reformatting, select some lines and choose Source  Correct Indentation or just type ctrl-I.

17 The End “Where a calculator on the ENIAC is equipped with vacuum tubes and weighs 30 tons, computers of the future may have only vacuum tubes and perhaps weigh 1½ tons.” —Popular Mechanics, March 1949