Road Map Style Principle of proximity Naming variables if / else.

Slides:



Advertisements
Similar presentations
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
Advertisements

Lecture 2 Introduction to C Programming
Introduction to C Programming
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
Java Syntax Part I Comments Identifiers Primitive Data Types Assignment.
Introduction to Computers and Programming Lecture 11: Introduction to Methods Professor: Evan Korth New York University.
Introduction to Computers and Programming Lecture 9: For Loops New York University.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
Introduction to Computers and Programming Style Lecture Professor: Evan Korth New York University.
Declaring Variables You must first declare a variable before you can use it! Declaring involves: – Establishing the variable’s spot in memory – Specifying.
Introduction to Computers and Programming Lecture 15: Arrays Professor: Evan Korth New York University.
Introduction to Computers and Programming Lecture 3: Variables and Input Professor: Evan Korth New York University.
Introduction to Computers and Programming Lecture 5 Boolean type; if statement Professor: Evan Korth New York University.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Introduction to C Programming
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Introduction to Computers and Programming Lecture 14: User defined methods (cont) Professor: Evan Korth New York University.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
1 Types of Programming Language (1) Three types of programming languages 1.Machine languages Strings of numbers giving machine specific instructions Example:
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Bill Tucker Austin Community College COSC 1315
Last week: We talked about: History of C Compiler for C programming
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
4. Java language basics: Function
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
COMP 170 – Introduction to Object Oriented Programming
Chapter 2 - Introduction to C Programming
Introduction to Computer Science / Procedural – 67130
C Programming Tutorial – Part I
Testing and Debugging.
Chapter 2 - Introduction to C Programming
Arrays, Part 1 of 2 Topics Definition of a Data Structure
IDENTIFIERS CSC 111.
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Introduction to C++ Programming
Chapter 2 - Introduction to C Programming
TIPS: How to Be Successful
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Chapter 2 - Introduction to C Programming
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Building Java Programs
Arrays I Handling lists of data.
Suggested self-checks: Section 7.11 #1-11
Chapter 2 - Introduction to C Programming
Building Java Programs
Unit 3: Variables in Java
Subject:Object oriented programming
Variables in C Topics Naming Variables Declaring Variables
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Introduction to C Programming
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Presentation transcript:

Introduction to Computers and Programming Style Lecture Professor: Evan Korth New York University

Road Map Style Principle of proximity Naming variables if / else

Initialization (revisited) When you declare a variable, you do not know the value stored in that variable until you place something there. The language specification does not guarantee any initial value. Until the user initializes the value, the value stored in that memory is called a garbage value. Java will not allow you to use the garbage value in a calculation or for output. If it is possible that a variable has not been initialized when you try to use it, you will get a compilation error. So you must initialize the variable before you use it on the right hand side of a calculation or output it to the screen.

Principle of Proximity Advanced tip for initialization Initialize your variables close to where you use them if possible Avoid mistakes about value when using late in code (something modified it?)

Stay away from variable names ending in a number Choosing Good Names Stay away from variable names ending in a number integer1, month1, etc. Unclear, and can be confusing when doing mathematical computations (also could be confused with an array) integer1 = 2 + 3 ; Start out with lower case letter and capitalize first letter of subsequent words myTotalTaxDue

Good Names cont’d Bad variable names Good variable names Y = Y – YY ; YYY = John + SalesTax ; Y = Y + LateFee * X1 + XXX ; Y = Y + sales_tax + late_fee ; Good variable names balance = balance – lastPayment ; monthlyTotal = newPurchases + salesTax ; balance = balance + lateFee * balance + newCharges ; balance = balance + newCharges + salesTax * newCharges ;

Should be all caps – underscores can be used to delimit words CONSTANTS Should be all caps – underscores can be used to delimit words E.g. HOUR_DIVISOR

The most important naming consideration The name should fully and accurately describe the entity the variable represents! Try stating in words what the variable represents E.g. bottomTimeWithoutDivePenalty A good name not easily confused with something else Easier to remember because it corresponds to subject

Avoid cryptic abbreviations and ambiguous names runningTotal is better than runningTtl Put modifiers at the end of your variable expenseAverage not averageExpense expenseTotal not totalExpense Keeps more important info first (all related to Expense, no?) Easier to visualize group and add later expenseAverage, expenseTotal versus averageExpense, totalExpense Later you can add other Expense modifiers and keep them in alphabetical order within Expense “grouping” expenseAverage, expenseBudget, expenseTotal

Some abbreviations are okay (but still unnecessary) Usually, that is, depending on your environment avg for average max for maximum min for minimum Fairly clear as to what they stand for, given most people’s general experience The abbreviation for number is interesting...

Num – the exception The use of num in some cases is an exception to the suggestion of putting modifiers at the end numStudents is a variable for the total number of students studentNum is a variable for the number of the current student

Count and Index instead To avoid confusion, may use Count and Index instead studentCount is a variable for the total number of students studentIndex is a variable for the number of the current student

How long is too long for names? Studies show programs with identifiers that are on average of 10 to 16 characters long are easier to debug Programs with names averaging 8 to 20 characters in length are just about as easy to debug

Misleading names or abbreviations Names to avoid Misleading names or abbreviations E.g. TRUE would be a bad abbreviation for “Total Returns Under Expectations” Names with similar meanings E.g. input and inputVal Should rename both to avoid confusion and subtle errors Different meanings but similar names clientRecs and clientReps Make them more different, e.g. clientRecords and clientReports

Similar sounding names More Names to avoid Similar sounding names wrap, rap problem when discussing code with others Misspelled words Don’t use highlite instead of highlight Hard to remember HOW it was misspelled Words that are easy for you or others to misspell definite, absence, receipt, etc. Don’t rely on capitalization to distinguish Input and input, FRD and frd

What? Even More Names to avoid? reserved words (you can’t use these) int, double, if [etc…] API names print, System [etc…] Names totally unrelated to what variables represent John, Suzy Names with hard-to-distinguish characters head2Toe headZToe Ttl5 TtlS

All this work for names? Why?! Can actually save you work by reducing the amount of comments necessary Save you deciphering time (adds up fast!) Lets you concentrate on problem-solving not problem-creation

Pick good spots to break up a long line Line Continuations Pick good spots to break up a long line Break so it’s obvious from reading a line there should be more E.g. if ( (totalSaleBeforeTax > 100) && (isPreferredCustomer) ) System.out.print(""); Note, the dangling && should alert reader to a break

Put a comment after the right brace! Braces Whenever adding a left brace to denote a block or method start, add the right brace right away and then fill between them Put a comment after the right brace! if ( MyGrade < YourGrade ) { } /* end if (MyGrade < YourGrade) */ else { } /* end else of (MyGrade < YourGrade) */ } /* end program */ Use curly braces even if you only have one statement as the body of a control structure.

indentation Place the brace associated with a control statement (or method / class header) on the next line, indented to the same level as the control statement. Statements within the braces are indented to the next level. For example: public class { public static void main… … }