Style Rules II: Names and Naming. Overview Today we will discuss: –Reasons for naming conventions –Rules for naming variables, classes, and methods.

Slides:



Advertisements
Similar presentations
Copyright © 2014 Dr. James D. Palmer; This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
Advertisements

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.
Coding Standards for Java An Introduction. Why Coding Standards are Important? Coding Standards lead to greater consistency within your code and the code.
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.
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.
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.
Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.
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.
Introduction to Computers and Programming Lecture 15: Arrays Professor: Evan Korth New York University.
22-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
Loops – While, Do, For Repetition Statements Introduction to Arrays
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.
Lecture 2: Variables and Expressions Yoni Fridman 6/29/01 6/29/01.
While Loops and Do Loops. Suppose you wanted to repeat the same code over and over again? System.out.println(“text”); System.out.println(“text”); System.out.println(“text”);
Identifiers and Assignment Statements. Data structures In any programming language you need to refer to data The simplest way is with the actual data.
Cs205: engineering software university of virginia fall 2006 Semantics and Specifying Procedures David Evans
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Java. Why Java? It’s the current “hot” language It’s almost entirely object-oriented It has a vast library of predefined objects It’s platform independent.
Style It's still hard to give rules, but we can give some examples. Remember: this is to some extent a matter of taste.
Programming Logic and Design Sixth Edition Chapter 2 Working with Data, Creating Modules, and Designing High-Quality Programs.
Programming.
(1) Coding Standards Philip Johnson Collaborative Software Development Laboratory Information and Computer Sciences University of Hawaii Honolulu HI
Detailed design – class design Domain Modeling SE-2030 Dr. Rob Hasker 1 Based on slides written by Dr. Mark L. Hornick Used with permission.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
Java Data Types Data types, variable declaration, and initialization.
The Java Programming Language
CPS120: Introduction to Computer Science
Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.
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
IB Computer Science Unit 1 – Introduction to Java Variables.
Primitive Variables.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs.
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.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Primitive Data Types. Identifiers What word does it sound like?
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs -
CMP-MX21: Lecture 5 Repetitions Steve Hordley. Overview 1. Repetition using the do-while construct 2. Repetition using the while construct 3. Repetition.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Primitive Variables.
Code Conventions Tonga Institute of Higher Education.
Copyright Curt Hill Variables What are they? Why do we need them?
Even More Java. Java Packages What is a package? Definition: A package is a grouping of related types providing access protection and name space management.
Working With Objects Tonga Institute of Higher Education.
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.
Values, Types, and Variables. Values Data Information Numbers Text Pretty much anything.
Variables. Some Rules of Variable Naming Convention : Variable names are case-sensitive. A variable’s name can be any legal identifier. It can contain.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
Primitive Data Types. int This is the type you are familiar with and have been using Stores an integer value (whole number) between -2,147,483,648 (-2.
Today’s Agenda ML Development Workflow –Emacs –Using use –The REPL More ML –Shadowing Variables –Debugging Tips –Boolean Operations –Comparison Operations.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Introduction to Computer Science / Procedural – 67130
Variables ICS2O.
Style 5-Dec-18.
Style 5-Dec-18.
Chapter 2: Java Fundamentals
Style 22-Feb-19.
Beginning Style 27-Feb-19.
Style 4-Apr-19.
Style 9-Apr-19.
Style 26-Apr-19.
Primitive Types and Expressions
Chap 2. Identifiers, Keywords, and Types
Variables and Constants
Presentation transcript:

Style Rules II: Names and Naming

Overview Today we will discuss: –Reasons for naming conventions –Rules for naming variables, classes, and methods

Why have naming conventions? A program is written once, but read many times –During debugging –When adding to the program –When updating the program –When trying to understand the program Anything that makes a program more readable and understandable saves lots of time, even in the short run

Rule 9:Use meaningful names Names should be chosen very carefully, to indicate the purpose of a variable or method If the purpose changes, the variable or method should be renamed It is worthwhile spending a little time choosing the best name Long, multiword names are common in Java

Rule 10: Use familiar names Where common terminology exists, use it; don’t make up your own Example from the book: If your users refer to “customers,” your program should use the name Customer, not Client

Rule 11: Question excessively long names Variables should be used for a single purpose Methods should do one simple, clearly defined thing If a descriptive name is overly long, maybe the variable or method is trying to serve too many purposes

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); } }

Meaningful names: exceptions II Method variables 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 nonmeaningful names for class or instance variables

Rule 28: Use standard names for “throwaway” variables 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

Rule 12: Join the vowel generation Despite the cutesy name, this rule is important In more primitive languages, names were often limited to 8 or so characters –This led to names like maxVolum and lngPlyng –The usual rule was to leave out vowels, starting from the right –Such names are harder to read and to remember Do not leave out vowels, or otherwise use unusual abbreviations, in Java!

Naming classes Rule 18: Capitalize the first letter of each word, including the first: PrintStream, Person, ExemptEmployee Rule 19: Use nouns to name classes: ExemptEmployee, CustomerAccount –Classes are supposed to represent things

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

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

Rule 13: Capitalize only the first letter in acronyms In names, write acronyms such as GUI and API as Gui and Api Examples: setDstOffset, displayAsHtml, loadXmlDocument Since capital letters are used to separate names, this rule helps avoid confusion

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; Rule 31: Constants are written in ALL_CAPITALS, with underscores between words Exception: color names, such as Color.pink –Colors were defined before conventions were established

Will you be held responsible for following these rules? Yes

The End