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

Slides:



Advertisements
Similar presentations
Variables in C Amir Haider Lecturer.
Advertisements

C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
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.
Road Map Introduction to object oriented programming. Classes
Introduction to Computers and Programming Lecture 9: For Loops New York University.
Introduction to Computers and Programming Lecture 4: Mathematical Operators 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.
Loops – While, Do, For Repetition Statements Introduction to Arrays
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
1 Introduction to Computers and Programming Quick Review What is a Function? A module of code that performs a specific job.
Introduction to Computers and Programming Strings Professor: Evan Korth New York University.
Introduction to Computers and Programming Lecture 13: User defined methods Instructor: Evan Korth New York University.
Introduction to C Programming
Programming Part 1 Armond R. Smith Zhenying Wu. Overview of this Class ● Transition from FTC -> FRC ● Using Your Resources ● Java Keywords o Data Types.
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
By: Mr. Baha Hanene Chapter 3. Learning Outcomes We will cover the learning outcome 02 in this chapter i.e. Use basic data-types and input / output in.
CSCI 1100/1202 January 16, Why do we need variables? To store intermediate results in a long computation. To store a value that is used more than.
C Programming Tutorial – Part I CS Introduction to Operating Systems.
CS1 Lesson 2 Introduction to C++ CS1 Lesson 2 -- John Cole1.
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.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
© 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)
CIS 234: Project 1 Issues Dr. Ralph D. Westfall April, 2010.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
1 Week 5 l Primitive Data types l Assignment l Expressions l Documentation & Style Primitive Types, Assignments, and Expressions.
 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
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
Chapter 1.2 Introduction to C++ Programming
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Chapter 2 - Introduction to C Programming
Introduction to Computer Science / Procedural – 67130
Road Map Style Principle of proximity Naming variables if / else.
Chapter 2 - Introduction to C Programming
IDENTIFIERS CSC 111.
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Introduction to C++ Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Subject:Object oriented programming
Variables in C Topics Naming Variables Declaring Variables
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
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?)

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 = ; Start out with lower case letter and capitalize first letter of words myTotalTaxDue

Good Names cont’d Bad 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 ;

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

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

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

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

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.

20 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… … }