Class Note for CMPSCI 220 at UMass(28)
Class Note for CMPSCI 220 at UMass(28)
Popular in Course
Popular in Department
This 8 page Class Notes was uploaded by an elite notetaker on Friday February 6, 2015. The Class Notes belongs to a course at University of Massachusetts taught by a professor in Fall. Since its upload, it has received 15 views.
Reviews for Class Note for CMPSCI 220 at UMass(28)
Report this Material
What is Karma?
Karma is the currency of StudySoup.
You can buy or earn more Karma at anytime and redeem it for class notes, study guides, flashcards, and more!
Date Created: 02/06/15
Highly Skilled Programming CMPSCI 220 291A I Pro cient with Programming Methodology Tools Spring 2009 Methods I The two are often interrelated 22 Design Coding Standards and Names I E39gquot deSign methOdS and Programming language I Speci cally 00 language features and design UNIVERSITY OF MASSACHUSETTS AMHERST Department of Computer Science I UNIVERSITY OF MASSACHUSETTS AMHERST Department of Computer Science Tools and Methods What s the Bigger Picture I Thus far we ve focused on I What makes code goodbetter I Tools I Are there dimensions of code qualitygt I Java W Effective Java 2 usage guidelines I Can we measure code quality 39 ECHPSQ SUbVefSiOmJUnit and EZSYMOCk I Which tactics design patterns or others lead to I Methods improvements on which dimensionsgt 39 DeSig Patterns and deSig PrinCiPleS I What strategy guides choice use of tacticsgt l Refactormg I How do we decide what code to writegt I Tactical approaches to good better code I HOW do we go about writing it UNIVERSITY OF MASSACHUSETTS AMHERST Department of Computer Science 3 V UNIVERSITY OF MASSACHUSETTS AMHERST Department of Computer Science CMPSCI 220 Picture Code Quality I Programming in the medium I One dimension focuses on overall system function Reliability I Assigned task evolving code base I Small groups 173 of programmers I Efficiency I Evolvrng program I I I Maintainability I Skills needed in future courses I Usability I These and similar referred to as ilities I Primarily relevant to prograrnmingeinetheelarge UNIVERSITY MAsSAEIITsrns AMHERST Department of Computer Science 1 UNIVERSITY MAsSAc sms AMHERST Department of Computer Science Maintainability Some Facets I Abstraction eases understanding I Hidden dependencies inhibit change I Premature commitment limits choices I Consistency eases understanding I Similar things are done in similar ways I Role expressiveness eases understanding I Purpose of a component readily inferred I Measuring any of these is dif cult Code Quality The Design Dimension I Three standard facets I Simplicity vs complexity measurable I Modularity coupling and cohesion I Information hiding I Strong connection to maintainability I And especially to abstraction I How can these be measured I How can they be achieved I Design principles design patterns refactoring UNIVERSITY OF MASSACHUSEI39rS AMHERST Department or Compmer Science Design Principles I Enragmkll e aspects of your application that vary I Program to z39m e ares not an implementations I Favor romgoiitz39on over inheritance I Classes should be 013m for extemion but closed for modi ralion I Strive for loosey ragaed designs between objects that interact I Degend 112071 abstrarlion not concrete classes Design Patterns I Strategy Defines a family of encapsulated algorithms I Decorator Attaches additional responsibilities to an object dynamically Observer Defines a onertoemany publishsubscribe dependency between objects Factory Method Defines an interface for creating an object I Abstract Factory Method Pattern Provides an interface for creating families of related or dependent objects UNIVERSITY orNIASSACHuSETrs AMHERST Department of Computer science More Design Principles I Talk only with your immediate friends I Don t call us we ll call you I Allow lowrlevel components to hook into a system I But highrlevel components determine when and how they are needed I A class should have only one reason to change I More generally Strive for high cohesion I Module or class designed around a set of related functions has high cohesion UNIVERSITY OF MASSACHUSETTS AMHERST Department of Computer Science UNIVERSITY oEiIIASSAcNuSErTS AMHERST Department of Computer Science More Design Patterns I Adapter Converts the interface of a class into another interface the clients expect I Facade Provides a unified interface to a set of interfaces in a subsystem I Template Method Defines the skeleton of an algorithm in a method deferring some steps to subclasses I Iterator Provides a way to access the elements of an agregate object sequentially I Composite Allows composing objects into tree structures to represent partrwhole hierarchies UNIVERSITY or MASSACHUSETTS AMHERST Department of Computer Science Still More Design Patterns Where s the Strategy I State Encapsulates state based behaviors and I Design principles and Patterns are good tactics uses delegation to decide which one to use I Motivated by code quality goals I Flyweight One instance of a Class Provides I But what strategy guides their application many Virtual instances I How do we decide what code to write I I I 3 I Memento Returns an obect to one of its I HOW do we go ZbOUt meng 1t39 I When and how do we apply design principles previous states as for an zmdo and patterns I Refactoring is one answer to last question I Software design methodologies address all UNIVERSITY OF MASSACHUSETTS AMHERST Department of Computer Science I UNIVERSITY OF MASSACHUSETTS AMHERST Department of Computer Science Software Design Methodologies Extreme Programming I Many methodologies have been proposed I Very code centric like code and X I We summarized four representative examples 39 BUt imposes mUCh 111016 diSCiplinEli I Code and fix I Pair programming I Waterfall I Continuous refactoring I Unit tests written before code untested code I Unified process does not eXist testing is heaVily automated I Etheme Progfmming I Only build what is needed try not to anticipate future extensions no borrowing trouble I Some evidence Applicable for small projects UNIVERSITY OF MASSACHUSETTS AMHERST Department of Computer Science 5 V UNIVERSITY OF MASSACHUSETTS AMHERST Department of Computer Science Thirteen Practices of XP Thirteen Practices of XP I Whole Team I Pair Programming I Metaphor I Test Driven Development I Planning Game I Design Improvement formerly Refactoring I Simple Design I Collective Code Ownership I Small Releases I Continuous Integration I Client Tests Sustainable Pace formerly 40 hour week I Coding Standards UNIVERSITY MASSAcuusn rs AMHERST Department of Computer Science Thirteen Practices of XP Coding Standards I Coding Standards I Many Aspects l Developed code should have a consistent style I Formatting I Particular standard used is less important than I Use of comments adhering to one consistent standard I Language constructs and usage I Loops For vs For Each I Exceptions I Names and naming UNIVERSITY OF MASSACHUSETTS AMHERST Department of Computer Science E I UNIVERSITY OF MASSACHUSETTS AMHERST Department of Computer Sciense Typographical Naming Effective Java Item 56 Conventions for Java I Adhere to generally accepted naming l Highly skilled programmers should follow them conventions I Violate them only rarely and With good reason I Java platform follows a set ofwelleestablished I APIS Violating them may be dif cult to use naming conventions I I I Implementations violating them may be difficult to I Many are spec1fied in The ava Language maintain Specification ULS 68 I Sensible for other Java programmers to follow same conventions I So violations can confuse irritate lead to errors I OtherJava programmers generally rely on them I Two categories typographical and grammatical UNIVERSITY OF MASSACHUSETTS AMHERST Department of Computer Science 2 V UNIVERSITY OF MASSACHUSETTS AMHERST Department of Computer Science Typographical Naming Typographical Naming Conventions for Java Conventions Packages I Packages I Hierarchical names I classes I Components separated by periods I Components in lowercase alphabetical characters and rarely digits I Interfaces I MethOdS I For exported packages organization s 39 Fields Internet domain name rst top level down I Type Variables Eg comgoldengamedev I Standard libraries and optional packages java and javax are exceptions W users should never create I acka s startin With these UNIVERSITY OF MASSACHUSHTS AMHERST Department Of Computer Science UNIVERSITY MASSAEHTISEWS AMHERST Department of Computer Science Typographical Naming Typographical Naming Conventions Packages Conventions Classes amp Interfaces I Remainder of name one or more I One or more words each capitalized components describing package I Eg PowerUp PowerUpStrategy I Short eight or fewer characters I Abbreviations should be avoided I Abbreviations OK eg UT I Acronyms and common abbreviations OK I Acronyms OK eg awt eg max min I Generally just one word or abbreviation 39 Acronyms 2H Uppercase Of mixed I But for large packages informal hierarchies 39 Eg HTTPURL VS Httpurl eg javaxswingplafmetal UNIVERSITY OF MASSACHUSETTS AMHERST Department of Computer Science UNIVERSITY OF MASSACHUSETTS AMHERST Department of Computer Science Typographical Naming Typographical Naming Conventions Methods amp Fields Conventions Methods amp Fields I One or more words all but rst capitalized 39 Exception consmnt elds I Eg activatePowerUp powerUp I Static final elds with immutable values I Abbreviations should be avoided 39 IUCIUdeS enum Consm ts I Acronyms and common abbreviations OK I These ShOUId be all UPPEICQSE SEPQIatEd by eg max min underscores I Acronyms as first word should be lowercase I Eg VKLEFT TOPBOTTOMCOLLISION I Eg httpURL or httpUrI UNIVERSITY OF MASSACHUSETTS AMHERST Department of Computer Science 2 V UNIVERSITY OF MASSACHUSETTS AMHERST Department of Computer Science Typographical Naming Typographical Naming Conventions Local Variables Conventions Type Variables I One or more words all but rst capitalized I Usually a single letter I Abbreviations OK 39 T for arbitrary WPC I Single characters and short sequences OK 39 E for element W96 Of 2 COHCCUO I E g i message I K and V for key and value types of a map I Meaning can depend on context Where local 39 X for an exception variable occurs I For a sequence of arbitrary types I T U V I T1 T2 T3 UWERSITY 0E MASSAcHusnrs AMHERST Department of Computer Science 2B quot 7 UMVERSITY 0E MASSAcHusrrTs AMHERST Department of Computer Science Grammatical Naming Conventions Grammatical Naming Conventions for Java for Java Methods I More exible and controversial than I Single verb or verb Phrase typographical conventions I Eg losePowerUp I Packages no grammatical naming conventions I If returning a boolean usually IS or has I Classes Singular noun or noun Phrase followed by a noun or noun phrase or word or I Including enum types Phrase acumg as an adlecuve Egg powerUp MyEvent I Eg isKeyPressed shouldUpdate I Interfaces like classes or adjective followed by able or ible I Eg PowerUpStrategy Serializable Cloneable 7 UNIVERSITY OF MASSACHUSEWS AMHERST Department OF Computer Science V UNIVERSITY OF MASSACHUSHTS AMHERST Department Of Grammatical Naming Conventions Grammatical Naming Conventions for Java Methods for Java Methods I If returning a non boolean attribute or I Methods that convert the type of an object function usually a noun or noun phrase or returning independent object of different type verb phrase starting with get are often called IOUpg I Eg speed getType I Eg toString I If class contains method to set same attribute I Methods that return a view of a different type then geMmibm setAmz39bzze are Often called astg I Eg getType setType I Eg asList UNIVERSITY OF MASSACHLSETEAMHERST Department of Computer Science V v UNIVERSITY OF MASSACHLlSFlE AMHERST Department of Computer Science Grammatical Naming Conventions Grammatical Naming Conventions for Java Methods for Java Fields amp Local Variables I Methods that return a primitive with the same I Less well established and less important value are Often CQHEd pevalue I Few if any exposed fields in wellrdesigned APIs 39 Eggt intvalue I Boolean eld names like boolean accessors I Common names for static factory methods But without initial is 63 keyPressed 39 conventional 2mng inCIUded in Item 1 I Fields of other types nouns or noun phrases I valueOf same value as parameters type conversion I get nstance 77 not same value as parameters I E39gquot type powerUp new nstance guumtees distinct instances I Local variables similar but even less important I getType W when factory method in a different class I newUpe when factory methodin a different class UNIVERSITY OF MASSACHUSETTS AMHERST Department of Computer Science i39 UNIVERSITY OF MASSACHUSETTS AMHERST Department of Computer Science Beyond Typography and Grammar Meaningful Names Meaningful Names Use IntentionRevealing Names I Use Intention Revealing Names I Name not comment should reveal intent I Avoid Disinformation int d elapsed time in days I Make Meaningful Distinctions VS39 int elapsedTimelnDays int daysSinceCreation int daysSinceModification int fileAgeInDays I Use Pronounceable Names I Use Searchable Names I Don t Be Cute I Don t Pun t Based on Meaningful Names by Tim Dttlnger e C39 H V in Robert c Martin39s Clean Code UNIVERSITY OF MASSACHLlSElTS AMHERST Department OF Computer Science 7 UNIVERSITY OF MASSACHUSETTS AMHERST Department of Computer Solemn Meaningful Names Meaningful Names Use IntentionRevealing Names Use IntentionRevealing Names public Listltint7gt getThem Listltintgt ist1 new ArrayListltintgt puphc IT39Stlt39ntgt getFlaggedceHsO for int X theList LIstltIntgt flaggedCells new ArrayLIstltIntgt if xo 4 for int cell gameBoard ist1 addX if ceSTATUSVALUE FLAGGED return list391 I flaggedCellsaddcell I return flaggedCells oWhat s in theList oMeaningful names clarify what s going on oWhat s special about 0 and 4 Only the names have been changed Umvtnsm or MAssXCHUSE AMthsf Department of Computer science V v Umvtnsm or MASSACHUSHE AMHERST Department or Computer science Meaningful Names Meaningful Names Use IntentionRevealing Names Avoid Disinformation public ListltCegt getFIaggedCellsO I Avoid misleading names I ListltCegt flaggedCells new ArrayListltCegt 39 accountGroup VS accountI St lf ot 2 L39St for Cell cell gameBoard I Avoid names that vary in small ways if ceisFagged flaggedCellsaddcell return flaggedCells ControlIerForEfficientHandlingOfStrings ControlerForEfficientStorageOfStrings I Similar spellings for similar related things I Helps if similar things sort together for Repacing the int arrays with class instances automatic completion etc enables more use of meanin ful names g I Avold lowercase L uppercase O 7 T00 21 confused with 1 and 0 UNIVERSITY or MASSACllLISFrTS AMHERsr Department of Computer Science UNIVERSITY or MASSACHUSETTS AMHERST Department of Computer Science Meaningful Names Make Meaningful Distinctions I Wanting to use same name for two different things in same scope I Avoid arbitrary respellings goods vs goodz l Avoid number series naming a1 82 I Avoid noise word distinctions ProductData vs Productlnfo getActiveAccount getActiveAccounts getActiveAccountlnfo UNIVERsmr or MAssAcHusH Es AMHERST Depantmemg of Computer Science
Are you sure you want to buy this material for
You're already Subscribed!
Looks like you've already subscribed to StudySoup, you won't need to purchase another subscription to get this material. To access this material simply click 'View Full Document'