Managing ComplexityLearning things that matterPrograms should workReadabilityAnother readable methodA less readable methodWhat makes a method “readable”?What makes a program “modifiable”?ExampleInformation hidingThe EndManaging ComplexityProgramming is more than just syntaxLearning things that matterThings change rapidly in computer scienceLanguages go in and out of popularity, operating systems change, even programming styles changesHalf of what you learn in my class will be outdated in five yearsCIT591 is primarily an introductory programming courseIt uses this year’s popular programming languageUnfortunately, that language is so complex that learning the syntax takes time away from learning to programIn this slide set I’m talking about things that will not be outdated in five yearsBut my examples will be from JavaPrograms should workPrograms should work correctlyMany programs must be highly reliableMedical programs, space vehicle control programs, sales programs, income tax programsHousehold robots, self-driving automobilesThe need for correctness isn’t going to change any time soonPrograms should continue to work correctly after they are modified, or updated, or had new features addedThus, it is important to be able to modify programs safelyThis means:Clear, concise, readable programsGood tests, especially regression testsReadabilityCan we read a program, or do we have to decipher it?Here’s a method I would consider readable:public boolean isLeapYear(int year) { if (year % 400 == 0) return true; if (year % 100 == 0) return false; return year % 4 == 0;}At this point, you may feel that all programs have to be decipheredI feel the same way when I try to read GermanWith practice, deciphering changes to reading—mostly Another readable methodvoid playGame() { boolean playAgain = true; while (playAgain) { int computersScore = 0; int usersScore = 0; boolean nobodyHasWonYet = true; while (nobodyHasWonYet) { computersScore = computersScore + resultOfComputersTurn(); usersScore = usersScore + resultOfUsersTurn(); printCurrentScores(computersScore, usersScore); nobodyHasWonYet = computersScore < WINNING_SCORE && usersScore < WINNING_SCORE; } printFinalScores(computersScore, usersScore); playAgain = askUser("Do you want to play again?"); } }A less readable methodprivate static int giveRandomNumber(int minValue, int maxValue) { if (minValue>maxValue){ int temp=maxValue; maxValue=minValue; minValue=temp; } Random random=new Random(); int temp; if(maxValue<0&&minValue<0){ temp=0-random.nextInt(minValue)-1; while(maxValue<temp){ temp=0-random.nextInt(minValue)-1; }//w }else{ temp=random.nextInt(maxValue+1); while (minValue>temp){ temp=random.nextInt(maxValue+1); }//w }//e return temp; }What makes a method “readable”?Short enough to see the entire method at once, without scrollingDoes a single thingHas a meaningful, descriptive nameIs properly formatted, and follows established conventionsHas comments that further clarify what the method doesCalls methods with meaningful, descriptive namesUses established idiomsVery idiomatic: for (int i = 0; i < array.length; i++)Less idiomatic: for (row = 0; row <= array.length - 1; ++row)Has a short, memorable parameter list, with well-chosen parameter namesDoesn’t do “weird” thingsDoesn’t change parameter objects unnecessarilyIf available outside the class, works for any valid objectThat is, it doesn’t depend on some other method being called firstWhat makes a program “modifiable”?Good tests are essentialMore bugs are introduced when “correcting” a program than at any other timeIf you have a complete set of tests, you can do this much more safelyFrequently, in order to introduce new features, you have to refactor (reorganize) a programIf you have a complete set of tests, you can do this much more safelyYou can add features, but you cannot change features that other people (that is, other parts of the project) depend uponAt least, not without an ex tremely convincing reasonYou can’t change what methods do, but you can change how they do itYou can only change how methods work if nothing else depends on itThis is why you must hide as much as possible of your implementationExampleA modifiable program:public class Lexicon { private String[] words; private int[] counts; private int numberOfWords = 0; // etc.}An unmodifiable program:public class Lexicon { String[] words; int[] counts; int numberOfWords = 0; // etc.}Information hidingWhen you provide a class to a project,You should provide everything that is needed by the projectYou should not provide anything that isn’t neededIf you do, someone, somewhere, will take advantage of itIf you then change it, you will get the blameThere is a lot more to be said on the topic of information hiding, but I don’t have the time right now to say it allI will add this much:Information hiding also applies to your JUnit testsIf you don’t want your tests to break when you make correct changes to your program, don’t depend on features that should be hiddenThe
View Full Document