160 likes | 289 Views
Variables (Chapters 10-11 of Code Complete ). Steve Chenoweth CSSE 375, Rose-Hulman Based on Don Bagert’s 2006 Lecture. Today. Your variable results may vary… this Tonight – turn in HW 4 HW 5 – due Mon, Oct 24 (next Mon).
E N D
Variables(Chapters 10-11 of Code Complete) Steve Chenoweth CSSE 375, Rose-Hulman Based on Don Bagert’s 2006 Lecture
Today • Your variable results may vary…this • Tonight – turn in HW 4 • HW 5 – due Mon, Oct 24 (next Mon) Ronald – from http://www.bigbroomfield.com/Restaurants/mcdonalds_menu.htm.
Outline Variables • Maintenance Issue with variables -- in general • The Data Literacy Test • Declarations • “PLC Issues” • Naming Issues • Variables in Maintenance/Open Source
Maintenance issue with variables… • How to make it clear what they’re looking at. Say, you encounter the following variable for the first time: accountindex++ ; • What is going on? -- translates to What are they doing with this variable? Often, have to guess at both pieces of knowledge: • What does accountindex stand for? • What’s the current value now?
An example – Mac’s • I said, “I’ll have a number 1 with a Coke.” • She said, “What size?” • I said, “The 4.95 size.” • She looked back at the menu, said, “Medium or large?” • I said, “How should I know?” What was the problem? Picture from suzero.com/blog/?m=200704 .
A lot of programs have similar problems in explaining vbl meaning… • From an infamous CSSE 374 program (TwentyFour): static final int maxDepth = 10; // How deep is our search table? (see next line) int operandTbl[][] = new int[maxDepth][9]; // for selecting operands & recording answers /* Values of slots in this table are as follows, for each i = 1 to maxDepth: answer = operandTbl[i][0]; // answer, or original operand status flag = operandTbl[i][1]; // 0 = empty, kinda; 1 = ready to use, 2 = used op1 = operandTbl[i][2]; // recover pieces of each calc, for display op2 = operandTbl[i][3]; // 2nd operand or -2000 flag if unary operation opn = operandTbl[i][4]; // operation, or -1 = original operand with no operation …
Quiz Exercise 1:Data Literacy Test(page 238 of Code Complete) -- see also p. 2 of your daily quiz
An interesting style question… • Why do you use enumerated types?
An interesting style question… • Typical example of enumerated types – public enum Day { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY } … public void tellItLikeItIs() { switch (day) { case MONDAY: System.out.println("Mondays are bad."); break; \ case FRIDAY: System.out.println("Fridays are better."); break; …
Declaration Issues • Implicit declarations are bad! (Messed up an early Mars mission, for instance) • Initializing all variables is an example of defensive programming, but which may also decrease readability
Variable “Problems” grow with the system: • As you make a class more and more complex, the variable use becomes messy. • Say, we started with a method that decides if Steve (or whomever) has an account with us: boolean CheckForName(string name){ boolean found = false; while (not db.eof()) { record nextOne = db.getNext(); if nextOne.name == name { found = true; break; } } return found; } • Later, you want to use this same routine to look up their account balance…
“PLC Issues” • Topics that are usually covered in CSSE 304: • Scope • Persistence • Binding Time
How’s this one working out? • In Java, variables created with new persist until they are garbage collected. • In C++, variables created with new persist until you delete them.
Identifier Naming Issues • Mnemonic (meaningful) names • Length of 6-20 characters recommended • Use names for loop indices? • Language-specific conventions (pages 275-279 of Code Complete) • e.g. for Java, variable and method names begin with a small letter, while class names start with a capital
Example • Java names -- notoriously long, even for “trivial” purposes: // Create label for displaying moon phase images and put a border around it. phaseIconLabel = new JLabel(); phaseIconLabel.setHorizontalAlignment(JLabel.CENTER); phaseIconLabel.setVerticalAlignment(JLabel.CENTER); phaseIconLabel.setVerticalTextPosition(JLabel.CENTER); phaseIconLabel.setHorizontalTextPosition(JLabel.CENTER); phaseIconLabel.setBorder(BorderFactory.createCompoundBorder( BorderFactory.createLoweredBevelBorder(), BorderFactory.createEmptyBorder(5,5,5,5))); …
Quiz Question 5:Identifier Programming Style See sample code, pp. 3-4 of your daily quiz. Do with a team mate.