StyleAbout the bookRule 1: Adhere to the style of the originalRule 3: Do it right the first timeRule 5: Indent nested codeRule 6: Break up long linesRule 8: Don’t use “hard” tabsRule 9:Use meaningful namesRule 10: Use familiar namesRule 11: Question excessively long namesMeaningful names: exceptions IMeaningful names: exceptions IIRule 28: Use standard names for “throwaway” variablesRule 12: Join the “vowel generation”Naming classes and interfacesNaming variablesNaming methodsRule 13: Capitalize only the first letter in acronymsNaming constantsKinds of commentsWhich kind of internal comment?Explaining the code IExplaining the code IIEnd-line comments IEnd-line comments IIFlagging unresolved issuesIntentionally missing breakLabel empty statementsDon’t repeat the codeUse the active voiceDebugging statementsThe EndSlide 33Jan 14, 2019Style2About the bookThis book is a team effort by many good programmers, not just one person’s opinionsThe rules have been widely distributed and commented uponThe rules reflect widespread and accepted practicesAnd no, I don’t agree with everything in the book!3Rule 1: Adhere to the style of the originalConsistent style is very importantMost times, you will enter an ongoing project, with established style rulesFollow them even if you don’t like themDon’t try to establish “better” style rulesIt won’t work anywayThere may be reasons you don’t know aboutIf a project has mixed styles with no consistency, you might try to get people to agree on one4Rule 3: Do it right the first timeYou’re working on a large project, so you use good style......but you need a tool to help you do one little job, so you slap it together quicklyGuess which program will be around longer and used by more people?5Rule 5: Indent nested codeAlways indent statements that are nested inside (under the control of) another statementif (itemCost <= bankBalance) { writeCheck(itemCost); bankBalance = bankBalance - itemCost;}while (seconds > 0) { System.out.print(seconds + "..."); seconds = seconds - 1;}Indentation should be consistent throughout the program4 spaces has become more-or-less standard6Rule 6: Break up long linesScrolling a window horizontally is a pain!When you print on standard paper, long lines are either cut off or wrap in bad placesI have long used a 72 character limitSome editors will show you a limit lineVarious suggestions:Break after, not before, operatorsLine up parameters to a methodDon’t indent the second line of a control statement so that it lines up with the statements being controlled7Rule 8: Don’t use “hard” tabsOnce upon a time, you could depend on tab stops every eight character positionsToday, every editor has its own idea of where and how to set tab stopsIf you change editors, your nice indentation gets ruinedIt’s worse if you use both tabs and spacesI have learned this one the hard way!A hard tab is an actual tab character in your textGood editors can be set to use soft tabs (your tab characters are replaced with spaces)BlueJ uses only soft tabs; Eclipse can do either8Rule 9:Use meaningful namesNames should be chosen very carefully, to indicate the purpose of a variable or methodIf the purpose changes, the variable or method should be renamedIt is worthwhile spending a little time choosing the best nameLong, multiword names are common in JavaEclipse makes it very easy to rename things9Rule 10: Use familiar namesWhere common terminology exists, use it; don’t make up your ownExample from the book: If your users refer to “customers,” your program should use the name Customer, not Client10Rule 11: Question excessivelylong namesVariables should be used for a single purposeMethods should do one simple, clearly defined thingIf a descriptive name is overly long, maybe the variable or method is trying to serve too many purposes11Meaningful names: exceptions IIt is common practice to use i as the index of a for-loop, j as the index of an inner loop, and k as the index of a third-level loopThis is almost always better than trying to come up with a meaningful nameExample:for (int i = 1; i <= 10; i++) { for (int j = 1, j <= 10; j++) { System.out.println(" " + (i * j)); }}12Meaningful names: exceptions IIMethod variables may be given short, simple names, if:The purpose of the variable is obvious from context, andThe variable is used only briefly, in a small part of the programBut never use meaningless names for fields (class or instance variables)13Rule 28: Use standard names for “throwaway” variablesIf variables have no special meaning, you can use names that reflect their typesFor example, if you are writing a general method to work with any strings, you might name them string1, string2, etc.Alternatively, you can use very short namess, t, u, or s1, s2, etc. are often used for Stringsp, q, r, s are often used for booleansw, x, y, z are often used for real numbers14Rule 12: Join the “vowel generation”In more primitive languages, names were often limited to 8 or so charactersThis led to names like maxVolum and lngPlyngThe usual rule was to leave out vowels, starting from the rightSuch names are harder to read and to rememberDo not leave out vowels, or otherwise use unusual abbreviations, in Java!15Naming classes and interfacesRule 18: Capitalize the first letter of each word, including the first: PrintStream, Person, ExemptEmployeeRule 19: Use nouns to name classes: ExemptEmployee, CustomerAccountClasses are supposed to represent thingsRule 20: Use adjectives to name interfaces: Comparable, PrintableInterfaces are supposed to represent features16Naming variablesRule 25: Capitalize the first letter of each word except the first: total, maxValueRule 26: Use nouns to name variables: balance, outputLineVariables are supposed to represent values17Naming methodsRule 22: Capitalize the first letter of each word except the first: display, displayImageMethods are capitalized the same as variablesRule 23: Use verbs when naming methods: displayImage, computeBalanceMethods are supposed to do something18Rule 13: Capitalize only the first letter in acronymsIn names, write acronyms such as GUI and API as Gui and ApiExamples: setDstOffset,, displayAsHtml,,
View Full Document