Download presentation
Presentation is loading. Please wait.
Published byDrusilla Daniel Modified over 6 years ago
1
Coding #1 if(!(index < 0 || index >= list.size( )-1))
Valid indices between 0…(size-1): if(!(index < 0 || index >= list.size( )-1))
2
Coding #1 if(!(index < 0 || index >= list.size( )-1))
Valid indices between 0…(size-1): if(!(index < 0 || index >= list.size( )-1)) if(index >= 0 && index < list.size( ))
3
Find 4 things to improve here:
Coding #2 Find 4 things to improve here: boolean found; for(String filename : files) { if(filename.contains(searchString)) { found = true; break; } else { if(found == false) { System.out.println(“NO file found”);
4
Find 4 things to improve here:
Coding #2 Find 4 things to improve here: boolean found = false; // initialization for(String filename : files) { if(filename.contains(searchString)) { found = true; break; // bad style } else { // empty block if(found == false) { // unary operator System.out.println(“NO file found”); !found
5
Coding #3 What is wrong here: boolean found = false;
for(String filename : files) { if(filename.contains(searchString)) { found = true; } else { found = false; if(!found) { System.out.println(“NO file found”);
6
What is (1) wrong here logically:
Coding #3 What is (1) wrong here logically: boolean found = false; for(String filename : files) { if(filename.contains(searchString)) { found = true; } else { // negates found=true, found = false; // if this item is AFTER } // an item already found if(!found) { System.out.println(“NO file found”);
7
Find 5 things to fix here:
Coding #4 Find 5 things to fix here: int index; int limit; index = 0; limit = files.size() - 1; while(index < limit) { String filename = files.get(index); System.out.println(filename); }
8
Find 5 things to fix here:
Coding #4 Find 5 things to fix here: // declare & initialize int index = 0; // at the same time int limit = files.size() - 1; if(limit < 0) { // check lower boundary System.out.println(“NO files exists”); } else { // check upper boundary while(index <= limit) { String filename = files.get(index); System.out.println(filename); index++; // increment counter
9
How can this be improved & compiled:
Coding #5 How can this be improved & compiled: if(index < 0) { System.out.println(“Index too SMALL”); return -1; } else if(index >= size) { System.out.println(“Index is too LARGE”); else if(index >= 0 && index < size) { System.out.println(“Index is VALID”); return index;
10
How can this be improved & compiled:
Coding #5 How can this be improved & compiled: int location = -1; // use local variable if(index < 0) { System.out.println(“Index too SMALL”); return -1; // too many returns } else if(index >= size) { System.out.println(“Index is too LARGE”); return -1; // too many returns else if(index >= 0 && index < size) { System.out.println(“Index is VALID”); location = index; // set valid index return location; // 1 return statement
11
How can this be improved:
Coding #6 How can this be improved: if(isValid == true) { return true; } else { return false;
12
How can this be improved:
Coding #6 How can this be improved: BETTER if(isValid == true) { return true; } else { return false; BEST return isValid;
13
How can this be improved or fixed:
Coding #7 How can this be improved or fixed: public int sum(int a, int b) { int sum = 0; while(a <= b) { sum = sum + a; a = a + 1; }
14
How can this be improved or fixed:
Coding #7 How can this be improved or fixed: public int sum(int a, int b) { int sum = 0; int num = a; // use local variable while(num <= b) { // not parameter sum += num; // compound operator num++; // unary operator } return sum; // missing return
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.