Netscape rewrote Netscape 4.0 and released it after three years as Netscape 6.0
Borland rewrote dBase and Quattro Pro
Microsoft rewrote Word
The Boy Scout Rule Leave the campground cleaner than you found it
OOP Principles
Solid Principles S.o.l.i.d. Principles
Single Responsibility Principle Only one reason to change Robustness Focus
public class PrintServerImpl extends ServiceAdvanced implements PrintServer , JobListener { public synchronized String createJob ( Object data ) { //... } public int getStatus ( String jobId ) { //... } public void print ( String jobId , int startPage , int endPage ) { //... } public byte [] getPreview ( String jobId , int pageNum ) { //... } public IRawData getData ( String jobId ) { //... } public void abortAction ( String jobId ) { //... } public Vector getPrinterList () { //... } public synchronized void setPrinterList ( Vector printerList ) { //... } public void statusChanged ( JobEvent jobEvent ) { //... } public void pageComputed ( JobEvent jobEvent ) { //... } // ... }
public class PrinterServerJob { public synchronized String createJob ( Object data ) { //... } public int getStatus () { //... } public void addDataToJob () { //... } public void print (){ //... } public void print ( int startPage , int endPage ){ //... } public byte [] getPreview ( int pageNum ){ //... } // ... } public class PrinterList { public Vector getPrinterList (){ //... } public synchronized void setPrinterList ( Vector printerList ){ //... } } public class JobEventListener { public void statusChanged ( JobEvent jobEvent ){ //... } public void pageComputed ( JobEvent jobEvent ){ //... } }
OpenClose Principle open for extension close for modification abstraction
public static final int TYPE_UNDEFINED = 0 ; public static final int TYPE_LEGAL_AG = 1 ; public static final int TYPE_LEGAL_PG = 2 ; public static final int TYPE_TEMPORARY = 3 ; //... boolean ok = false ; String buildType = m_cdInfo . buildType . toUpperCase (); String prefix = "" ; switch ( archType ) { case TYPE_LEGAL_AG : if ( buildType . equals ( "LEGAL_AG" ) || buildType . equals ( "LEGAL_AGPG" )){ ok = true ; } break ; case TYPE_LEGAL_PG : if ( buildType . equals ( "LEGAL_PG" ) || buildType . equals ( "LEGAL_AGPG" )){ ok = true ; } break ; case TYPE_TEMPORARY : if ( buildType . equals ( "TEMPORARY" ) || buildType . equals ( "PRV" )){ ok = true ; } prefix = "AP " ; break ; } if (! ok ) { BurnerHelper . showError (...); }
public interface ArchiveType { public boolean isOk ( String buildType ); public String getPrefix (); } public class Undefined implements ArchiveType { public boolean isOk ( String buildType ){ return false ; } public String getPrefix () { return "" ; } } public class LegalAg implements ArchiveType { public boolean isOk ( String buildType ){ return buildType . equals ( "LEGAL_AG" ) || buildType . equals ( "LEGAL_AGPG" ) } public String getPrefix () { return "" ; } }
public class LegalPg implements ArchiveType { public boolean isOk ( String buildType ){ return buildType . equals ( "LEGAL_PG" ) || buildType . equals ( "LEGAL_AGPG" ) } public String getPrefix () { return "" ; } } public class Temporary implements ArchiveType { public boolean isOk ( String buildType ){ return buildType . equals ( "TEMPORARY" ) || buildType . equals ( "PRV" ) } public String getPrefix () { return "AP" ; } }
//... archTypes . put ( 0 , new Undefined ()); archTypes . put ( 1 , new LegalAg ()); archTypes . put ( 2 , new LegalPg ()); archTypes . put ( 3 , new Temporary ()); //... String buildType = m_cdInfo . buildType . toUpperCase (); boolean ok = archTypes . get ( archType ). isOk ( buildType ); String prefix = archTypes . get ( archType ). getPrefix (); if (! ok ) { BurnerHelper . showError (...); } //...
Liskov Substitution Principle If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of P is unchanged when o1 is substituted for o2 then S is a subtype of T
Liskov Substitution Principle Subtypes must be substitutable for their base types Inheritance and polymorphism
public class Rectangle { protected int _width ; protected int _height ; public int Width { get { return _width ; } } public int Height { get { return _height ; } } public virtual void SetWidth ( int width ){ _width = width ; } public virtual void SetHeight ( int height ){ _height = height ; } } public class Square : Rectangle { public override void SetWidth ( int width ){ _width = width ; _height = width ; } public override void SetHeight ( int height ){ _height = height ; _width = height ; } }
[ TestFixture ] public class RectangleTests { private void CheckAreaOfRectangle ( Rectangle r ){ r . SetWidth ( 5 ); r . SetHeight ( 2 ); Assert . IsEqual ( r . Width * r . Height , 10 ); } [ Test ] public void PassingTest (){ Rectangle r = new Rectangle (); CheckAreaOfRectangle ( r ); } [ Test ] public void FailingTest (){ Rectangle r = new Square (); CheckAreaOfRectangle ( r ); } }
public class Rectangle { protected int _width ; protected int _height ; public int Width { get { return _width ; } } public int Height { get { return _height ; } } public virtual void SetWidth ( int width ){ _width = width ; } public virtual void SetHeight ( int height ){ _height = height ; } } public class Square { protected int _side ; public int Side { get { return _side ; } } public void SetSide ( int side ){ _side = side ; } }
Interface Segregation Principle Don’t be force to implement unused methods Avoid “Fat Interfaces” High cohesion - better understandability, robustness Low coupling - better maintainability, high resistance to changes
public interface CartographyListener { public void poisChanged ( Locations pois ); public void cellsChanged ( Locations cells ); public void mapChanged ( ImageIcon map ); public void updateZoomLevel ( int level ); public void updateGeoArea ( GeoArea ga ); public void updateGridPosition ( Point2D . Double gridPosition ); public void updateMousePosition ( Point position ); } public class CellLayer extends Layer { /* Methods from CartographyListener interface */ public void cellsChanged ( Locations cells ) { setLocations ( cells ); } //..... } public class PoiLayer extends Layer { /* Methods from CartographyListener interface */ public void poisChanged ( Locations locations ) { setLocations ( locations ); } //..... }
public abstract class Layer implements CartographyListener , DrawingInterface { /* Methods from CartographyListener interface */ // Metto qui un'implementazione vuota (una sorta di adapter) così // non sono costretta a sovrascrivere i metodi in tutte le specializzazioni. public void poisChanged ( Locations pois ) {} public void cellsChanged ( Locations cells ) {} public void mapChanged ( ImageIcon map ) {} public void updateZoomLevel ( int level ) { m_zoomLevel = level ; } public void updateGeoArea ( GeoArea ga ) { m_geoArea = ga ; } public void updateGridPosition ( Point2D . Double gridPosition ) {} public void updateMousePosition ( Point position ) {} /* End of methods from CartographyListener interface */ //..... }
public class CartographyUI extends JPanel { public void addCartographyListener ( CartographyListener listener ) { if ( m_listeners == null ) { m_listeners = new ArrayList (); } m_listeners . add ( listener ); } public void setCelles ( Locations celles ) { Iterator listeners = m_listeners . iterator (); while ( listeners . hasNext ()) { CartographyListener listener = ( CartographyListener ) listeners . next (); listener . cellsChanged ( celles ); } updateViewFromLocations (); } public void setPois ( Locations pois ) { Iterator listeners = m_listeners . iterator (); while ( listeners . hasNext ()) { CartographyListener listener = ( CartographyListener ) listeners . next (); listener . poisChanged ( pois ); } updateViewFromLocations (); } //..... }
public interface CartographyListener { } public interface CellListener extends CartographyListener { public void cellsChanged ( Locations cells ); } public interface PoiListener extends CartographyListener { public void poisChanged ( Locations locations ); }
public class CartographyUI extends JPanel { public void setCelles ( Locations celles ) { Iterator listeners = m_listeners . iterator (); while ( listeners . hasNext ()) { CellListener listener = ( CellListener ) listeners . next (); listener . cellsChanged ( celles ); } updateViewFromLocations (); } public void setPois ( Locations pois ) { Iterator listeners = m_listeners . iterator (); while ( listeners . hasNext ()) { PoiListener listener = ( PoiListener ) listeners . next (); listener . poisChanged ( pois ); } updateViewFromLocations (); } // ... } public void addCartographyListener ( CartographyListener listener ) { Class < ? > c = genericObj . getClass (); Class < ? > interfaces [] = c . getInterfaces (); for ( Class < ? > implementedIntf : interfaces ) { if ( implementedIntf . getName (). equals ( urmetcns.mito.ui.cartography.ui_components.PoiListener" )) poiListeners . add (( PoiListener ) listener ); if ( implementedIntf . getName (). equals ( "urmetcns.mito.ui.cartography.ui_components.CellListener" )) cellListeners . add (( CellListener ) listener ); // ... } }
Dependency Injection Principle Hollywood Principle: Inversion of Control "don't call us, we will call you" Dip <> Spring
private boolean retriveCallSideInfo ( String Side ) { //... DeterminationMethod = getXmlOption ( "Method" ); // specify which method to be used //... if ( DeterminationMethod . equals ( "PhoneMatch" )) { //... } if ( DeterminationMethod . equals ( "Exist" )) { //Query to che the existence of a specified dictionary element sqlString = "SELECT count(*) AS Cnt FROM iri_dictionary " " WHERE iri_id = ? and key_dictionary = ?" ; pstmt = ( OraclePreparedStatement ) assocInfo . conn . prepareStatement ( sqlString ); pstmt . setLong ( 1 , assocInfo . myIRIId ); pstmt . setString ( 2 , Dictionary ); } if ( DeterminationMethod . equals ( "Compare" )) { //... } if ( DeterminationMethod . equals ( "InOnly" )) { //Query alwais true for the //provider Telecom Internazionale sqlString = "SELECT 1 As Cnt FROM Dual" ; //... } //... //return true if the info has been found return ( itemFound == 1 ); }
public abstract class CallSideDeterminator { public abstract boolean findItem (); //... } public class CallSideDeterminatorCompare extends CallSideDeterminator { @Override public boolean findItem (){ //... } } public class CallSideDeterminatorDFDM extends CallSideDeterminator { @Override public boolean findItem (){ //... } } public class CallSideDeterminatorPhoneMatch extends CallSideDeterminator { @Override public boolean findItem (){ //... } }
public class AsExecFindCallSide extends AsCommandExec { HashMap < String , CallSideDeterminator > strategies = new HashMap < String , CallSideDeterminator >(); private void init () { strategies = new HashMap < String , CallSideDeterminator >(); strategies . put ( "PhoneMatch" , new CallSideDeterminatorPhoneMatch ()); strategies . put ( "Compare" , new CallSideDeterminatorCompare ()); strategies . put ( "DFDM" , new CallSideDeterminatorDFDM ()); //... } protected boolean retrieveCallSideInfo ( String side ) { CallSideDeterminator determinator = null ; if (( determinator = strategies . get ( getXmlOption ( "Method" ))) != null ) { determinator . initDeterminator ( assocInfo , side ); if ( determinator . findItem ()) { return determinator . storeCIN ( side ); } } return false ; } //... }
The Others Principles
Reuse Release Equivalency Principle The granule of reuse is the granule of release A package can be considered unit of distribution A release should have a version number Black-box, package that is to be used but not changed
Common Closure Principle Maintainability is more important than reusability If code must change, all changes should be in the same package Common changing classes, should be in the same package
Common Reuse Principle The classes in a package are reused together If you reuse one of the classes in a package, you reuse them all
Acyclic Dependencies Principle Avoid cyclic dependencies Nightmare to compile Nightmare to deploy
Least Astonishment Principle The result of some operation should be obvious, consistent, predictable Occam’s Razor: The simplest answer is usually the correct answer
int multiply ( int a , int b ) { return a + b ; } int write_to_file ( const char * filename , const char * text ){ printf ( "[%s]
" , text ); /* Note that 'filename' is unused */ }
Law of Demeter Encapsulation An object should avoid invoking methods of a member object returned by another method “ Don’t talk to stranger" An object A can request a service (call a method) of an object instance B, but object A cannot “ reach through” object B to access yet another object, C, to request its services “ Use only one dot"
Obsolete Comments Old comments that have lost their meaning
//*********************************************************************************** //! Initalize procedure /*! * This methos is called from the main in order to initialize all the thinks<br> * that the plugin need. * * param inLog log that the plugin can use for its own purpose *
eturn true = all ok false = intialization failed */ //*********************************************************************************** public bool init ( log4net . ILog inLog ) { this . log = inLog ; //log.Debug("======================================================="); log . Debug ( "============ INIT Module " + getModuleName () + " =========" ); return true ; }
/** * * * 03 Oct 2005 - AB - Added the isSameCIDinSameLiuID() function to avoid different CID in the same LIU (Ticket#2564) * 09 Sep 2005 - AB - fixed the retriveCallSideInfo() for the PhoneMatch method (Ticket#2381) * 06 Sep 2005 - AB - Fixed the SearchProviderDate() to properly work with the 'DATA' association technique * 01 Sep 2005 - AB - Added the dupval index exception handling in saveInHiddenJournal() function * 27 Jul 2005 - AB - changed the isInformationInDb() to avoid exiting with assocInfo.lemfList == null * 27 Jul 2005 - AB - removed the updateJournal() function because not needed * 26 Jul 2005 - AB - Now mergeJournal() saves a copy of the two lius ti be merged in the hidden_journal table * 26 Jul 2005 - AB - Added the saveInHiddenJournal() function to enhance the mergeJournal() function * 05 Jul 2005 - AB - Changed the retriveCallSideInfo queries to select the correct liu_id in every situation. … * 23 Mar 2005 - AB - Added the ORA-00001 error handling in the AddIRI2Journal * 9 Mar 2005 - AB - moved the queryExec body function to a generic queryExec function in the IRITools * 11 May 2004 - AB - Started **/
svn ci -m ‘Added the isSameCIDinSameLiuID() (Ticket#2564)’ AssociationFunction.java
Redundant Comments Repeating the variable name or condition in the comment
// // list on a file all the Extractors extensions // if ( param == "-LISTEXTRACTORS" ) //...
Redundant Comments Repeating the called method name in a comment after the call
int abs = x . abs (); // Get the absolute value of x int x = point . getX (); // Get the value of x
private int part ( HttpServletRequest request ) { String partNum = request . getParameter ( "PART" ); int part = 1 ; if ( partNum != null ) { part = Integer . valueOf ( partNum ). intValue (); } return part ; } private String fileId ( HttpServletRequest request ) throws ServletException { String fileId = request . getParameter ( "FILEID" ); if ( fileId == null ) { throw new ServletException ( "Invalid FileId" ); } return fileId ; }
General
Duplication Dry “ Once, and only once” Duplication is a missed opportunity for abstraction
Dead Code Code that isn’t executed if statement that checks for a condition that can’t happen Private method never called switch / case conditions that never occur Do the right thing: give it a decent burial
Vertical Separation Variables and function should be defined close to where they are used Local variables should be declared just above their first usage
Clutter Default constructor without implementation Variables not used Functions never called Useless comments Kill’em all!
Selector Arguments Avoid boolean arguments Breaks SRP Split method in two methods
getLocalPrintService ( jobId , data , false ); public static PrintServiceInterface getLocalPrintService ( String jobId , IRawData data , boolean isStandAlone ) throws Exception { //... }
public static PrintServiceInterface getEmbeddedLocalPrintService ( String jobId , IRawData data ) throws Exception { //... } public static PrintServiceInterface getStandAloneLocalPrintService ( String jobId , IRawData data ) throws Exception { //... }
Explanatory Vars Avoid ‘artistic’ programming Break the calculations up
Matcher match = headerPattern . matcher ( line ); if ( match . find ()) headers . put ( match . group ( 1 ), match . group ( 2 ));
Matcher match = headerPattern . matcher ( line ); if ( match . find ()){ String key = match . group ( 1 ); String value = match . group ( 2 ); headers . put ( key . toLowerCase (), value ); }
Magic Numbers Replace Magic Numbers with Named Constants
Negative Conditionals Negatives is harder to understand than positives Avoid negatives Conditionals
if (! buffer . shouldNotCompact ()) { //... } if (! envWarrantsEnabled ) sqlSelect . addWhere ( new SqlNot ( new SqlEq ( "LI_TYPE" , "ENV" )));
if ( buffer . shouldCompact ()) { //... } if ( envWarrantsDisabled ) sqlSelect . addWhere ( new SqlNot ( new SqlEq ( "LI_TYPE" , "ENV" )));
Names
Descriptive Names Choice descriptive names Reevaluate the appropriateness of the names
public int x () { int q = 0 ; int z = 0 ; for ( int kk = 0 ; kk < 10 ; kk ++) { if ( l [ z ] == 10 ) { q += 10 + ( l [ z + 1 ] + l [ z + 2 ]); z += 1 ; } else if ( l [ z ] + l [ z + 1 ] == 10 ) { q += 10 + l [ z + 2 ]; z += 2 ; } else { q += l [ z ] + l [ z + 1 ]; z += 2 ; } } return q ; }
Unambiguous Names Choose names that make the workings of a function or variable unambiguous
public boolean stateMachine ( int smStatus ) { //... } public boolean doAction () { //... }
public boolean moveStateMachineToStatus ( int smStatus ) { //... } public boolean doNextStepInProviderTechnique () { //... }
Avoid Encodings Names should not be encoded with type or scope information Hungarian Notation as obsolete legacy
protected int m_FilesCount ; // Numero files contenuti nel job protected int m_VolumesCount ; // Numero dischi richiesti dal job (1 sola copia) protected long m_TotalSize ; // Size totale in byte del job protected int m_VolumesDone ; // Numero dischi completati protected String m_VolDoneList ; // Lista dischi completati (durante esecuzione) protected ArrayList m_labelFields ; // Nomi/valori campi da utilizzare in label private long m_LastModTime ; // Data/ora modifica file informativo del job protected boolean isOnDisk ;
protected int filesCount ; // Numero files contenuti nel job protected int volumesCount ; // Numero dischi richiesti dal job (1 sola copia) protected long totalSize ; // Size totale in byte del job protected int volumesDone ; // Numero dischi completati protected String volDoneList ; // Lista dischi completati (durante esecuzione) protected ArrayList labelFields ; // Nomi/valori campi da utilizzare in label private long lastModTime ; // Data/ora modifica file informativo del job protected boolean isOnDisk ;
Describe Side Effects Names should describe everything that a function, variable, or class is or does
public DiskWriterJobs getJobs () { if ( m_Jobs == null ) { m_Jobs = new DiskWriterJobs ( this ); } return m_Jobs ; }
public DiskWriterJobs createOrReturnJobs () { if ( m_Jobs == null ) { m_Jobs = new DiskWriterJobs ( this ); } return m_Jobs ; }
Tests
Insufficient Tests How many tests? A test suite should test everything that could possibly break Use a Coverage Tool:
Java
EMMA
Cobertura
Clover
.NET PartCover C/C++ BullseyeCoverage gcov Beware of Coverage Results
Don’t Skip Trivial Tests They are better than documentation Easy today, maybe hard tomorrow
Exhaustively Test Near Bug Bugs tend to congregate
Tests Should Be Fast If slow, launched less frequentely If launched less frequentely, big change between launches and difficult to know where and when an error was introduced
Good code is a golden treasure often buried under a more
Good code is a golden treasure often buried under a lot of mess. In this presentation we recall the basic principles and the smells and heuristics to find it less
0 comments
Post a comment