SlideShare una empresa de Scribd logo
1 de 6
Descargar para leer sin conexión
Essential	
  OO	
  Best	
  Practices	
  	
  
1. Provide a consistent and intuitive class interface.
!Clients of the class should only need to know how to use a class and should not be forced to know how it provides
the functionality. To achieve this, a class should provide a consistent interface that there is no ambiguity in using the
class interface; it should also be intuitive to understand to avoid mistakes by the clients.
!2. Provide common properties of classes in a base class.
!The common properties of various classes should be identified and should be provided in a common base class
(which can be an abstract class, concrete class, or interface). Providing methods in the base class interface is the
basis of runtime polymorphism; it also avoids code duplication and unnecessary downcasts.
!3. Do not expose implementation details in the public interface of the class.
!The public interface is meant for external users. If the class interface exposes only implementation details of the
class, it violates the rules of class abstraction; also any changes to internal details of the class can affect the interface
of the class, which is highly undesirable.
!4. Consider providing helper classes while designing large classes.
!Consider separating the logical parts of the implementation detail or functionality from larger class and provide
them as smaller helper classes. Also, when a small helper class is meaningful only in a limited context, and is used
by a single class, prefer making that class as an inner/nested class of the class that uses it.
!5. Keep the data members private.
!Every object has an identity, state and behaviour. For objects, name and type provides identity, data members
provide state and methods provide behaviour. Data members (that is, the state of the object) are considered as an
implementation detail; so it is better to keep them private and expose only methods (that is, behaviour) as methods
are preferable to be part of the public interface of a class.
!6. Provide lowest possible access to methods.
!Providing minimal access to the members of a class promotes encapsulation by hiding implementation details. By
providing lowest possible access to the class members, the class is more cohesive and more decoupled from other
classes; the internal implementation can also be more easily changed if the limited access is provided to the
members.
!7. Strive for loose coupling between classes.
!Classes should either be independent of other classes or should use only the public interface of other classes. Strive
for such loose coupling between classes because such classes are easy to understand, use, maintain and modify.
!8. Beware of order of initialization problems.
!Many subtle problems can happen because of order of initialization issues. Avoid code that depends on particular
order of implementation as provided by the compiler or the implementation.
!9. Write unit tests for classes.
!An application that is built using various classes can be expected to work fine only if it is ensured that the classes
work fine in isolation. Write unit tests to ensure robust implementation of individual classes.
!10. Avoid low-level code.
©2006-­‐2014.	
  All	
  rights	
  reserved.	
  Ganesh	
  Samarthyam.	
  
Based	
  on	
  "60	
  Tips	
  on	
  Object	
  Oriented	
  Programming",	
  Tata	
  McGraw-­‐Hill,	
  2006. 	
  
!Using low-level code is convenient for many situations, such as making use of legacy code, accessing platform
specific features, efficiency etc. However, low-level code compromises portability, hampers software maintenance
and reuse, and makes the application unsafe and un-secure. So, avoid writing or using low-level code.
!11. Avoid calling virtual functions of the same class from constructors.
!Constructors do not support runtime polymorphism fully as the derived objects are not constructed yet when base
class constructor executes. So, avoid calling virtual functions from base-class constructors, which might result in
subtle bugs in the code.
!12. Consider providing factory methods.
!Factory methods have many advantages over constructors. Depending on the situation, consider providing factory
methods instead of constructors or in addition to existing constructors.
!13. Make constructor private if there are only static members in the class.
!When there are only static members in the class, there is no need to instantiate it; enforce this by making the default
constructor of that class private.
!14. Avoid creating unnecessary temporary objects.
!Programming in object oriented way tends to introduce lots of temporary objects. To a large extent, programmers
can consciously avoid creating such temporary objects. The techniques that can be used to avoid such temporary
object creation are mostly language specific as languages differ on the ways and situations in which temporary
objects are created.
!15. Prefer creating immutable objects.
!Immutable objects help us in avoiding bugs because of modifying the state, when it shouldn’t be done. Immutable
objects have many advantages over the usual mutable objects, so it is recommended to use immutable objects
whenever possible.
!16. Consider creating and using null objects.
!Providing sanity checks to avoid null pointer access or special casing the code to check for null condition can be
avoided for container/utility classes by creating and using null objects.
!17. Provide special objects with read only access in the class itself.
!Consider providing specific special objects that are often required by the application readily in the class itself as
read-only static fields. There is no need to create new objects of such special objects provided in the class and those
objects can be used all over in the application safely.
!18. Overload functions only if it is intuitive and unambiguous to the users.
!Function overloading should improve the readability of the code and make the programming convenient. If the users
find the overloading ambiguous or unintuitive, then it is not recommended to overload the functions.
!19. Overload functions only if the functions do semantically the same thing.
The methods that are semantically same should have syntactically same name, so use overloading; methods that
differ semantically should have syntactically different names, so provide descriptive names.
!20. Overload operators only if it is natural and improves the readability of code.
!Operator overloading is meant for convenience in using user defined types like primitive types and it is also meant
for improving the readability of the code. Overload operators only if the meaning of the operator is evident, un-
©2006-­‐2014.	
  All	
  rights	
  reserved.	
  Ganesh	
  Samarthyam.	
  
Based	
  on	
  "60	
  Tips	
  on	
  Object	
  Oriented	
  Programming",	
  Tata	
  McGraw-­‐Hill,	
  2006. 	
  
ambiguous, natural, and intuitive for a given user-defined type; otherwise it will adversely affect the readability of
the code.
!21. Overload symmetrical operators or family of operators together.
!When overloading an operator, if there are any related operators, overload those operators also.
!22. Do not use inheritance when structure and behaviour are same for related classes.
!Often beginners make the mistake of using inheritance relationship to model relationship of classes that differ in
some value, when the class structure and behaviour remain the same. The correct solution is to keep that value as a
data member in a class.
!23. Prefer object composition over inheritance.
!In many cases, inheritance and composition can provide equivalent functionality. Class inheritance is white-box
reuse whereas object composition is black-box reuse. Since object composition is a better reuse model than class
inheritance, favour composition over inheritance.
!24. Avoid inheriting from multiple classes.
!Understanding, supporting and using multiple inheritance to solve design problems can be complicated. The
situations in which multiple inheritance is required are less-common and it is possible to arrive at equivalent
solutions with single class inheritance itself. It is better to avoid using multiple inheritance.
!25. Prohibit inheriting from a standalone class if the class is not meant for inheritance.
!It is a good practice to make stand-alone utility classes as non-inheritable if the classes are not designed with
inheritance in mind.
!26. Consider making concrete leaf classes non-inheritable.
!Base classes (or root classes) are designed having inheritance in mind for providing a common interface to the
inheritance hierarchy whereas concrete (or leaf classes) are written for providing implementation and hence not for
inheritance or for providing an interface. It is a well-known practice to consider making base classes abstract, but it
is also equally important to consider making leaf classes as non-inheritable.
!27. Write code referring to generic interfaces of classes.
!Keep generality in mind and write the code for interfaces, rather than specific implementations; in this way, the code
becomes extensible. In specific, when there is an abstract class or an interface available in a class hierarchy, prefer
writing code in terms of referring to that interface instead of using any of the concrete classes directly in the code.
!28. Beware of versioning problems in evolving base classes.
!Evolving base classes can introduce versioning problems: Later versions of a base class might end up inadvertently
overriding a method in the derived class in the client, which will result in breaking the client code. Beware of
possible versioning problems in evolving base classes and avoid introducing virtual methods in later versions of
base classes.
!29. Avoid deep inheritance hierarchies.
!It might be sometimes convenient to develop deep inheritance hierarchies. However, it is very difficult to
understand, use, maintain and test classes in such hierarchies, so creating such deep inheritance hierarchies should
be avoided.
!30. Consider making common base classes as abstract classes.
!
©2006-­‐2014.	
  All	
  rights	
  reserved.	
  Ganesh	
  Samarthyam.	
  
Based	
  on	
  "60	
  Tips	
  on	
  Object	
  Oriented	
  Programming",	
  Tata	
  McGraw-­‐Hill,	
  2006. 	
  
The abstract classes and interfaces help us to think about design in conceptual terms rather than worrying about
implementation details. Instead of thoughtlessly mapping the application requirements into concrete classes to get
the work done, it is better to spend time in finding the common properties in the concrete classes and map them into
a abstract base class design and create reusable, extensible and flexible software.
!31. Use abstract classes for modelling abstract properties and sub-classing.
!Sub-classing refers to reusing interface and the implementation of a class; use abstract classes for modelling sub-
classing. For related classes, provide the common functionality in an abstract base class and provide different
implementations in separate concrete classes inheriting from it.
!32. Provide default implementation class(es) with abstract classes or interfaces.
!Abstractions have their own place in object oriented analysis and design. But without concrete classes, abstract
classes or interfaces are not of much use. If you’re providing abstract classes or interfaces, ensure that you are
providing at least one default implementation to make use of the abstract class or interface.
!33. Use interfaces for sub-typing and multiple inheritance.
!Sub-typing refers to reusing only the interface of a class; use interfaces for modelling sub-typing. Use pure interface
classes to bind unrelated classes for some common functionality. Also prefer interfaces for supporting multiple
inheritance.
!34. Consider providing adaptor classes for interfaces.
!Concrete classes that implement interfaces should provide the implementation of 'all' the methods, which is
inconvenient in many situations. Provide adaptor classes for such interfaces providing the default implementation
for the methods; this is for convenience as those adaptor classes will be easier to use.
!35. Keep in mind that interfaces are difficult to evolve.
!Pure class interfaces are difficult to evolve: Once clients have started using a pure interface class by implementing it,
it is not possible to add or delete method declarations or make any modifications to the old method signatures; any
such changes will break the client code. So strive for designing interfaces with an emphasis on establishing stability
in the contract it provides.
!36. Use virtual functions instead of chained if-else or switch statements.
!Programmers from structured programming background tend to use extensive use of control structures. Whenever
you find cascading if-else statements or switch statements checking for types or attributes of different types to take
actions, consider using inheritance by replacing the if-else or switch code with virtual method calls.
!37. Follow Liskov’s Substitution Principle (LSP).
!Follow Liskov’s Substitution Principle (LSP), which is one of the cardinal rules to follow in object oriented
programming.
!38. Avoid using reflection extensively.
!Reflection is a powerful feature, but using reflection exposes internal details of the classes. So, using reflection
should be strictly avoided for solving high-level programming problems; it should be used only when knowing
internal details of the classes at runtime is required.
!39. Prefer using higher-level language features instead of reflection.
!For most of the higher-level design problems, it is possible to provide acceptable solutions without using reflection.
So, prefer using virtual functions or some other language features to achieve the same functionality instead of using
reflection.
©2006-­‐2014.	
  All	
  rights	
  reserved.	
  Ganesh	
  Samarthyam.	
  
Based	
  on	
  "60	
  Tips	
  on	
  Object	
  Oriented	
  Programming",	
  Tata	
  McGraw-­‐Hill,	
  2006. 	
  
!40. Avoid using RTTI extensively.
!Other than safe uses such as performing downcasts and event-driven programming, RTTI should not be used for
solving higher-level programming problems. Extensive use of RTTI when not necessary indicates that the design is
bad and the implementation is brittle and un-maintainable.
!41. Prefer using virtual functions instead of RTTI.
!Extensive use of RTTI can potentially make maintaining the code difficult. Whenever you find cascading if-else
statements for matching particular types to take actions, consider redesigning it by using virtual functions instead.
!42. Use namespaces for avoiding name-clashes and organizing the software.
!The techniques and approaches used for programming in large are considerably different from programming in
small. One of the major objectives in programming in large is to support modular software and avoid name-clashes
and versioning problems. By using namespaces effectively, programmers can create logical modules (set of related
class) facilitating software distribution and maintenance.
!43. Hierarchically partition the namespace.
!One of the major benefits that object orientation provides is the ability to write reusable classes that can be used for
wide range of projects instead of just providing code that will solve the specific needs. One of the widely used ways
to organize such reusable software is in the form of class libraries that are organized (or partitioned) in a hierarchical
format. Namespaces can be effectively used to provide a hierarchical organization of the software.
!44. Selectively introduce only the specific namespace members you need to the code.
!In general, it is a good programming practice to selectively introduce the names from a namespace to the code. This
will avoid name-clashes that happen because of introducing all the members of namespace to the code. This will
also avoid breaking the programs because of independent additions in the original namespace/package.
!45. Selectively expose the types to the clients.
!Namespaces play an important role in organizing software. One of the important ways in which namespaces help in
organizing the software is to provide control to the programmers on the classes that are exposed to the clients. A
programmer should use namespaces for providing public access, limited access or hiding the names in the
application to the clients.
!46. Avoid hiding of names in different scopes.
!Hiding of names in different scopes is unintuitive to the readers of the code and using name hiding extensively can
affect the readability of the program. Though hiding of names is a convenient feature, it is recommended to avoid
name hiding as it can result in subtle defects and unexpected problems.
!47. Strive for eliminating all forms of code duplication.
!Code duplication is unnecessary and it can create major maintenance problems. There is no reason for any form of
duplication of code and alternative approaches and solutions should be used for eliminating code duplication.
!48. Use design patterns whenever appropriate.
!Many of the problems in large-scale programming happen because of tight coupling between various software
components. Design patterns reduce dependencies between the components; so, use them appropriately to create
more flexible and reusable software.
!49. Provide frameworks for effective reuse of architectural design.
!
©2006-­‐2014.	
  All	
  rights	
  reserved.	
  Ganesh	
  Samarthyam.	
  
Based	
  on	
  "60	
  Tips	
  on	
  Object	
  Oriented	
  Programming",	
  Tata	
  McGraw-­‐Hill,	
  2006. 	
  
Frameworks can be very useful in providing solutions in specific application domains. Since frameworks support
higher level architectural design reuse, it is preferable to provide frameworks or consider reusing existing
frameworks.
!50. Provide class libraries for any set of related reusable classes.
!Instead of providing specific solution to the given problem, consciously identify reusable components and provide it
as a class library.
!
©2006-­‐2014.	
  All	
  rights	
  reserved.	
  Ganesh	
  Samarthyam.	
  
Based	
  on	
  "60	
  Tips	
  on	
  Object	
  Oriented	
  Programming",	
  Tata	
  McGraw-­‐Hill,	
  2006. 	
  

Más contenido relacionado

Destacado

Writing an Abstract - Template (for research papers)
Writing an Abstract - Template (for research papers) Writing an Abstract - Template (for research papers)
Writing an Abstract - Template (for research papers) Ganesh Samarthyam
 
Refactoring guided by design principles driven by technical debt
Refactoring   guided by design principles driven by technical debtRefactoring   guided by design principles driven by technical debt
Refactoring guided by design principles driven by technical debtGanesh Samarthyam
 
MIDAS: A Design Quality Assessment Method for Industrial Software
MIDAS: A Design Quality Assessment Method for Industrial SoftwareMIDAS: A Design Quality Assessment Method for Industrial Software
MIDAS: A Design Quality Assessment Method for Industrial SoftwareGanesh Samarthyam
 
Introduction to Continuous Delivery (BBWorld/DevCon 2013)
Introduction to Continuous Delivery (BBWorld/DevCon 2013)Introduction to Continuous Delivery (BBWorld/DevCon 2013)
Introduction to Continuous Delivery (BBWorld/DevCon 2013)Mike McGarr
 
Let's Go: Introduction to Google's Go Programming Language
Let's Go: Introduction to Google's Go Programming LanguageLet's Go: Introduction to Google's Go Programming Language
Let's Go: Introduction to Google's Go Programming LanguageGanesh Samarthyam
 
Micro Anti-patterns in Java Code
Micro Anti-patterns in Java CodeMicro Anti-patterns in Java Code
Micro Anti-patterns in Java CodeGanesh Samarthyam
 
Refactoring for Software Architecture Smells - International Workshop on Refa...
Refactoring for Software Architecture Smells - International Workshop on Refa...Refactoring for Software Architecture Smells - International Workshop on Refa...
Refactoring for Software Architecture Smells - International Workshop on Refa...Ganesh Samarthyam
 
Continuous delivery applied (RJUG)
Continuous delivery applied (RJUG)Continuous delivery applied (RJUG)
Continuous delivery applied (RJUG)Mike McGarr
 
Continuous Delivery at Netflix, and beyond
Continuous Delivery at Netflix, and beyondContinuous Delivery at Netflix, and beyond
Continuous Delivery at Netflix, and beyondMike McGarr
 
Zero to the Cloud with @NetflixOSS
Zero to the Cloud with @NetflixOSSZero to the Cloud with @NetflixOSS
Zero to the Cloud with @NetflixOSSMike McGarr
 
Bangalore Container Conference 2017 - Poster
Bangalore Container Conference 2017 - PosterBangalore Container Conference 2017 - Poster
Bangalore Container Conference 2017 - PosterGanesh Samarthyam
 

Destacado (11)

Writing an Abstract - Template (for research papers)
Writing an Abstract - Template (for research papers) Writing an Abstract - Template (for research papers)
Writing an Abstract - Template (for research papers)
 
Refactoring guided by design principles driven by technical debt
Refactoring   guided by design principles driven by technical debtRefactoring   guided by design principles driven by technical debt
Refactoring guided by design principles driven by technical debt
 
MIDAS: A Design Quality Assessment Method for Industrial Software
MIDAS: A Design Quality Assessment Method for Industrial SoftwareMIDAS: A Design Quality Assessment Method for Industrial Software
MIDAS: A Design Quality Assessment Method for Industrial Software
 
Introduction to Continuous Delivery (BBWorld/DevCon 2013)
Introduction to Continuous Delivery (BBWorld/DevCon 2013)Introduction to Continuous Delivery (BBWorld/DevCon 2013)
Introduction to Continuous Delivery (BBWorld/DevCon 2013)
 
Let's Go: Introduction to Google's Go Programming Language
Let's Go: Introduction to Google's Go Programming LanguageLet's Go: Introduction to Google's Go Programming Language
Let's Go: Introduction to Google's Go Programming Language
 
Micro Anti-patterns in Java Code
Micro Anti-patterns in Java CodeMicro Anti-patterns in Java Code
Micro Anti-patterns in Java Code
 
Refactoring for Software Architecture Smells - International Workshop on Refa...
Refactoring for Software Architecture Smells - International Workshop on Refa...Refactoring for Software Architecture Smells - International Workshop on Refa...
Refactoring for Software Architecture Smells - International Workshop on Refa...
 
Continuous delivery applied (RJUG)
Continuous delivery applied (RJUG)Continuous delivery applied (RJUG)
Continuous delivery applied (RJUG)
 
Continuous Delivery at Netflix, and beyond
Continuous Delivery at Netflix, and beyondContinuous Delivery at Netflix, and beyond
Continuous Delivery at Netflix, and beyond
 
Zero to the Cloud with @NetflixOSS
Zero to the Cloud with @NetflixOSSZero to the Cloud with @NetflixOSS
Zero to the Cloud with @NetflixOSS
 
Bangalore Container Conference 2017 - Poster
Bangalore Container Conference 2017 - PosterBangalore Container Conference 2017 - Poster
Bangalore Container Conference 2017 - Poster
 

Similar a Object Oriented Best Practices - Summary

Android interview questions
Android interview questionsAndroid interview questions
Android interview questionssatish reddy
 
Android interview questions
Android interview questionsAndroid interview questions
Android interview questionssatish reddy
 
Class Members Access/Visibility Guide (Checklist)
Class Members Access/Visibility Guide (Checklist)Class Members Access/Visibility Guide (Checklist)
Class Members Access/Visibility Guide (Checklist)Jayasree Perilakkalam
 
20 most important java programming interview questions
20 most important java programming interview questions20 most important java programming interview questions
20 most important java programming interview questionsGradeup
 
Effective Java - Chapter 4: Classes and Interfaces
Effective Java - Chapter 4: Classes and InterfacesEffective Java - Chapter 4: Classes and Interfaces
Effective Java - Chapter 4: Classes and Interfacesİbrahim Kürce
 
When & Why: Interfaces, abstract classes, traits
When & Why: Interfaces, abstract classes, traitsWhen & Why: Interfaces, abstract classes, traits
When & Why: Interfaces, abstract classes, traitsAlena Holligan
 
Object-oriented programming 3.pptx
Object-oriented programming 3.pptxObject-oriented programming 3.pptx
Object-oriented programming 3.pptxAdikhan27
 
Solid principles, Design Patterns, and Domain Driven Design
Solid principles, Design Patterns, and Domain Driven DesignSolid principles, Design Patterns, and Domain Driven Design
Solid principles, Design Patterns, and Domain Driven DesignIrwansyah Irwansyah
 
what is differance between abstract class and interface ppt
what is differance between abstract class and interface pptwhat is differance between abstract class and interface ppt
what is differance between abstract class and interface pptmanojsharma469262
 
MC0078 SMU 2013 Fall session
MC0078 SMU 2013 Fall sessionMC0078 SMU 2013 Fall session
MC0078 SMU 2013 Fall sessionNarinder Kumar
 
Domain Driven Design Thoughts Mat Holroyd
Domain Driven Design Thoughts   Mat HolroydDomain Driven Design Thoughts   Mat Holroyd
Domain Driven Design Thoughts Mat Holroydmelbournepatterns
 
What is Interface in Java | How to implement Multiple Inheritance Using Inter...
What is Interface in Java | How to implement Multiple Inheritance Using Inter...What is Interface in Java | How to implement Multiple Inheritance Using Inter...
What is Interface in Java | How to implement Multiple Inheritance Using Inter...Edureka!
 

Similar a Object Oriented Best Practices - Summary (20)

Android interview questions
Android interview questionsAndroid interview questions
Android interview questions
 
Android interview questions
Android interview questionsAndroid interview questions
Android interview questions
 
Class Members Access/Visibility Guide (Checklist)
Class Members Access/Visibility Guide (Checklist)Class Members Access/Visibility Guide (Checklist)
Class Members Access/Visibility Guide (Checklist)
 
20 most important java programming interview questions
20 most important java programming interview questions20 most important java programming interview questions
20 most important java programming interview questions
 
C#
C#C#
C#
 
Template pattern
Template patternTemplate pattern
Template pattern
 
Effective Java - Chapter 4: Classes and Interfaces
Effective Java - Chapter 4: Classes and InterfacesEffective Java - Chapter 4: Classes and Interfaces
Effective Java - Chapter 4: Classes and Interfaces
 
When & Why: Interfaces, abstract classes, traits
When & Why: Interfaces, abstract classes, traitsWhen & Why: Interfaces, abstract classes, traits
When & Why: Interfaces, abstract classes, traits
 
Core java questions
Core java questionsCore java questions
Core java questions
 
Dtacs
DtacsDtacs
Dtacs
 
Object-oriented programming 3.pptx
Object-oriented programming 3.pptxObject-oriented programming 3.pptx
Object-oriented programming 3.pptx
 
Solid principles, Design Patterns, and Domain Driven Design
Solid principles, Design Patterns, and Domain Driven DesignSolid principles, Design Patterns, and Domain Driven Design
Solid principles, Design Patterns, and Domain Driven Design
 
what is differance between abstract class and interface ppt
what is differance between abstract class and interface pptwhat is differance between abstract class and interface ppt
what is differance between abstract class and interface ppt
 
C# interview quesions
C# interview quesionsC# interview quesions
C# interview quesions
 
MC0078 SMU 2013 Fall session
MC0078 SMU 2013 Fall sessionMC0078 SMU 2013 Fall session
MC0078 SMU 2013 Fall session
 
1
11
1
 
Domain Driven Design Thoughts Mat Holroyd
Domain Driven Design Thoughts   Mat HolroydDomain Driven Design Thoughts   Mat Holroyd
Domain Driven Design Thoughts Mat Holroyd
 
Core_Java_Interview.pdf
Core_Java_Interview.pdfCore_Java_Interview.pdf
Core_Java_Interview.pdf
 
What is Interface in Java | How to implement Multiple Inheritance Using Inter...
What is Interface in Java | How to implement Multiple Inheritance Using Inter...What is Interface in Java | How to implement Multiple Inheritance Using Inter...
What is Interface in Java | How to implement Multiple Inheritance Using Inter...
 
Designing Better API
Designing Better APIDesigning Better API
Designing Better API
 

Más de Ganesh Samarthyam

Applying Refactoring Tools in Practice
Applying Refactoring Tools in PracticeApplying Refactoring Tools in Practice
Applying Refactoring Tools in PracticeGanesh Samarthyam
 
CFP - 1st Workshop on “AI Meets Blockchain”
CFP - 1st Workshop on “AI Meets Blockchain”CFP - 1st Workshop on “AI Meets Blockchain”
CFP - 1st Workshop on “AI Meets Blockchain”Ganesh Samarthyam
 
Great Coding Skills Aren't Enough
Great Coding Skills Aren't EnoughGreat Coding Skills Aren't Enough
Great Coding Skills Aren't EnoughGanesh Samarthyam
 
College Project - Java Disassembler - Description
College Project - Java Disassembler - DescriptionCollege Project - Java Disassembler - Description
College Project - Java Disassembler - DescriptionGanesh Samarthyam
 
Coding Guidelines - Crafting Clean Code
Coding Guidelines - Crafting Clean CodeCoding Guidelines - Crafting Clean Code
Coding Guidelines - Crafting Clean CodeGanesh Samarthyam
 
Design Patterns - Compiler Case Study - Hands-on Examples
Design Patterns - Compiler Case Study - Hands-on ExamplesDesign Patterns - Compiler Case Study - Hands-on Examples
Design Patterns - Compiler Case Study - Hands-on ExamplesGanesh Samarthyam
 
Bangalore Container Conference 2017 - Brief Presentation
Bangalore Container Conference 2017 - Brief PresentationBangalore Container Conference 2017 - Brief Presentation
Bangalore Container Conference 2017 - Brief PresentationGanesh Samarthyam
 
Software Design in Practice (with Java examples)
Software Design in Practice (with Java examples)Software Design in Practice (with Java examples)
Software Design in Practice (with Java examples)Ganesh Samarthyam
 
OO Design and Design Patterns in C++
OO Design and Design Patterns in C++ OO Design and Design Patterns in C++
OO Design and Design Patterns in C++ Ganesh Samarthyam
 
Bangalore Container Conference 2017 - Sponsorship Deck
Bangalore Container Conference 2017 - Sponsorship DeckBangalore Container Conference 2017 - Sponsorship Deck
Bangalore Container Conference 2017 - Sponsorship DeckGanesh Samarthyam
 
Google's Go Programming Language - Introduction
Google's Go Programming Language - Introduction Google's Go Programming Language - Introduction
Google's Go Programming Language - Introduction Ganesh Samarthyam
 
Java Generics - Quiz Questions
Java Generics - Quiz QuestionsJava Generics - Quiz Questions
Java Generics - Quiz QuestionsGanesh Samarthyam
 
Software Architecture - Quiz Questions
Software Architecture - Quiz QuestionsSoftware Architecture - Quiz Questions
Software Architecture - Quiz QuestionsGanesh Samarthyam
 
Core Java: Best practices and bytecodes quiz
Core Java: Best practices and bytecodes quizCore Java: Best practices and bytecodes quiz
Core Java: Best practices and bytecodes quizGanesh Samarthyam
 
Advanced Debugging Using Java Bytecodes
Advanced Debugging Using Java BytecodesAdvanced Debugging Using Java Bytecodes
Advanced Debugging Using Java BytecodesGanesh Samarthyam
 

Más de Ganesh Samarthyam (20)

Wonders of the Sea
Wonders of the SeaWonders of the Sea
Wonders of the Sea
 
Animals - for kids
Animals - for kids Animals - for kids
Animals - for kids
 
Applying Refactoring Tools in Practice
Applying Refactoring Tools in PracticeApplying Refactoring Tools in Practice
Applying Refactoring Tools in Practice
 
CFP - 1st Workshop on “AI Meets Blockchain”
CFP - 1st Workshop on “AI Meets Blockchain”CFP - 1st Workshop on “AI Meets Blockchain”
CFP - 1st Workshop on “AI Meets Blockchain”
 
Great Coding Skills Aren't Enough
Great Coding Skills Aren't EnoughGreat Coding Skills Aren't Enough
Great Coding Skills Aren't Enough
 
College Project - Java Disassembler - Description
College Project - Java Disassembler - DescriptionCollege Project - Java Disassembler - Description
College Project - Java Disassembler - Description
 
Coding Guidelines - Crafting Clean Code
Coding Guidelines - Crafting Clean CodeCoding Guidelines - Crafting Clean Code
Coding Guidelines - Crafting Clean Code
 
Design Patterns - Compiler Case Study - Hands-on Examples
Design Patterns - Compiler Case Study - Hands-on ExamplesDesign Patterns - Compiler Case Study - Hands-on Examples
Design Patterns - Compiler Case Study - Hands-on Examples
 
Bangalore Container Conference 2017 - Brief Presentation
Bangalore Container Conference 2017 - Brief PresentationBangalore Container Conference 2017 - Brief Presentation
Bangalore Container Conference 2017 - Brief Presentation
 
Software Design in Practice (with Java examples)
Software Design in Practice (with Java examples)Software Design in Practice (with Java examples)
Software Design in Practice (with Java examples)
 
OO Design and Design Patterns in C++
OO Design and Design Patterns in C++ OO Design and Design Patterns in C++
OO Design and Design Patterns in C++
 
Bangalore Container Conference 2017 - Sponsorship Deck
Bangalore Container Conference 2017 - Sponsorship DeckBangalore Container Conference 2017 - Sponsorship Deck
Bangalore Container Conference 2017 - Sponsorship Deck
 
Google's Go Programming Language - Introduction
Google's Go Programming Language - Introduction Google's Go Programming Language - Introduction
Google's Go Programming Language - Introduction
 
Java Generics - Quiz Questions
Java Generics - Quiz QuestionsJava Generics - Quiz Questions
Java Generics - Quiz Questions
 
Java Generics - by Example
Java Generics - by ExampleJava Generics - by Example
Java Generics - by Example
 
Software Architecture - Quiz Questions
Software Architecture - Quiz QuestionsSoftware Architecture - Quiz Questions
Software Architecture - Quiz Questions
 
Docker by Example - Quiz
Docker by Example - QuizDocker by Example - Quiz
Docker by Example - Quiz
 
Core Java: Best practices and bytecodes quiz
Core Java: Best practices and bytecodes quizCore Java: Best practices and bytecodes quiz
Core Java: Best practices and bytecodes quiz
 
Advanced Debugging Using Java Bytecodes
Advanced Debugging Using Java BytecodesAdvanced Debugging Using Java Bytecodes
Advanced Debugging Using Java Bytecodes
 
Java Class Design
Java Class DesignJava Class Design
Java Class Design
 

Último

BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - InfographicHr365.us smith
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationkaushalgiri8080
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfkalichargn70th171
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptkotipi9215
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 

Último (20)

BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - Infographic
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Exploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the ProcessExploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the Process
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanation
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.ppt
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 

Object Oriented Best Practices - Summary

  • 1. Essential  OO  Best  Practices     1. Provide a consistent and intuitive class interface. !Clients of the class should only need to know how to use a class and should not be forced to know how it provides the functionality. To achieve this, a class should provide a consistent interface that there is no ambiguity in using the class interface; it should also be intuitive to understand to avoid mistakes by the clients. !2. Provide common properties of classes in a base class. !The common properties of various classes should be identified and should be provided in a common base class (which can be an abstract class, concrete class, or interface). Providing methods in the base class interface is the basis of runtime polymorphism; it also avoids code duplication and unnecessary downcasts. !3. Do not expose implementation details in the public interface of the class. !The public interface is meant for external users. If the class interface exposes only implementation details of the class, it violates the rules of class abstraction; also any changes to internal details of the class can affect the interface of the class, which is highly undesirable. !4. Consider providing helper classes while designing large classes. !Consider separating the logical parts of the implementation detail or functionality from larger class and provide them as smaller helper classes. Also, when a small helper class is meaningful only in a limited context, and is used by a single class, prefer making that class as an inner/nested class of the class that uses it. !5. Keep the data members private. !Every object has an identity, state and behaviour. For objects, name and type provides identity, data members provide state and methods provide behaviour. Data members (that is, the state of the object) are considered as an implementation detail; so it is better to keep them private and expose only methods (that is, behaviour) as methods are preferable to be part of the public interface of a class. !6. Provide lowest possible access to methods. !Providing minimal access to the members of a class promotes encapsulation by hiding implementation details. By providing lowest possible access to the class members, the class is more cohesive and more decoupled from other classes; the internal implementation can also be more easily changed if the limited access is provided to the members. !7. Strive for loose coupling between classes. !Classes should either be independent of other classes or should use only the public interface of other classes. Strive for such loose coupling between classes because such classes are easy to understand, use, maintain and modify. !8. Beware of order of initialization problems. !Many subtle problems can happen because of order of initialization issues. Avoid code that depends on particular order of implementation as provided by the compiler or the implementation. !9. Write unit tests for classes. !An application that is built using various classes can be expected to work fine only if it is ensured that the classes work fine in isolation. Write unit tests to ensure robust implementation of individual classes. !10. Avoid low-level code. ©2006-­‐2014.  All  rights  reserved.  Ganesh  Samarthyam.   Based  on  "60  Tips  on  Object  Oriented  Programming",  Tata  McGraw-­‐Hill,  2006.  
  • 2. !Using low-level code is convenient for many situations, such as making use of legacy code, accessing platform specific features, efficiency etc. However, low-level code compromises portability, hampers software maintenance and reuse, and makes the application unsafe and un-secure. So, avoid writing or using low-level code. !11. Avoid calling virtual functions of the same class from constructors. !Constructors do not support runtime polymorphism fully as the derived objects are not constructed yet when base class constructor executes. So, avoid calling virtual functions from base-class constructors, which might result in subtle bugs in the code. !12. Consider providing factory methods. !Factory methods have many advantages over constructors. Depending on the situation, consider providing factory methods instead of constructors or in addition to existing constructors. !13. Make constructor private if there are only static members in the class. !When there are only static members in the class, there is no need to instantiate it; enforce this by making the default constructor of that class private. !14. Avoid creating unnecessary temporary objects. !Programming in object oriented way tends to introduce lots of temporary objects. To a large extent, programmers can consciously avoid creating such temporary objects. The techniques that can be used to avoid such temporary object creation are mostly language specific as languages differ on the ways and situations in which temporary objects are created. !15. Prefer creating immutable objects. !Immutable objects help us in avoiding bugs because of modifying the state, when it shouldn’t be done. Immutable objects have many advantages over the usual mutable objects, so it is recommended to use immutable objects whenever possible. !16. Consider creating and using null objects. !Providing sanity checks to avoid null pointer access or special casing the code to check for null condition can be avoided for container/utility classes by creating and using null objects. !17. Provide special objects with read only access in the class itself. !Consider providing specific special objects that are often required by the application readily in the class itself as read-only static fields. There is no need to create new objects of such special objects provided in the class and those objects can be used all over in the application safely. !18. Overload functions only if it is intuitive and unambiguous to the users. !Function overloading should improve the readability of the code and make the programming convenient. If the users find the overloading ambiguous or unintuitive, then it is not recommended to overload the functions. !19. Overload functions only if the functions do semantically the same thing. The methods that are semantically same should have syntactically same name, so use overloading; methods that differ semantically should have syntactically different names, so provide descriptive names. !20. Overload operators only if it is natural and improves the readability of code. !Operator overloading is meant for convenience in using user defined types like primitive types and it is also meant for improving the readability of the code. Overload operators only if the meaning of the operator is evident, un- ©2006-­‐2014.  All  rights  reserved.  Ganesh  Samarthyam.   Based  on  "60  Tips  on  Object  Oriented  Programming",  Tata  McGraw-­‐Hill,  2006.  
  • 3. ambiguous, natural, and intuitive for a given user-defined type; otherwise it will adversely affect the readability of the code. !21. Overload symmetrical operators or family of operators together. !When overloading an operator, if there are any related operators, overload those operators also. !22. Do not use inheritance when structure and behaviour are same for related classes. !Often beginners make the mistake of using inheritance relationship to model relationship of classes that differ in some value, when the class structure and behaviour remain the same. The correct solution is to keep that value as a data member in a class. !23. Prefer object composition over inheritance. !In many cases, inheritance and composition can provide equivalent functionality. Class inheritance is white-box reuse whereas object composition is black-box reuse. Since object composition is a better reuse model than class inheritance, favour composition over inheritance. !24. Avoid inheriting from multiple classes. !Understanding, supporting and using multiple inheritance to solve design problems can be complicated. The situations in which multiple inheritance is required are less-common and it is possible to arrive at equivalent solutions with single class inheritance itself. It is better to avoid using multiple inheritance. !25. Prohibit inheriting from a standalone class if the class is not meant for inheritance. !It is a good practice to make stand-alone utility classes as non-inheritable if the classes are not designed with inheritance in mind. !26. Consider making concrete leaf classes non-inheritable. !Base classes (or root classes) are designed having inheritance in mind for providing a common interface to the inheritance hierarchy whereas concrete (or leaf classes) are written for providing implementation and hence not for inheritance or for providing an interface. It is a well-known practice to consider making base classes abstract, but it is also equally important to consider making leaf classes as non-inheritable. !27. Write code referring to generic interfaces of classes. !Keep generality in mind and write the code for interfaces, rather than specific implementations; in this way, the code becomes extensible. In specific, when there is an abstract class or an interface available in a class hierarchy, prefer writing code in terms of referring to that interface instead of using any of the concrete classes directly in the code. !28. Beware of versioning problems in evolving base classes. !Evolving base classes can introduce versioning problems: Later versions of a base class might end up inadvertently overriding a method in the derived class in the client, which will result in breaking the client code. Beware of possible versioning problems in evolving base classes and avoid introducing virtual methods in later versions of base classes. !29. Avoid deep inheritance hierarchies. !It might be sometimes convenient to develop deep inheritance hierarchies. However, it is very difficult to understand, use, maintain and test classes in such hierarchies, so creating such deep inheritance hierarchies should be avoided. !30. Consider making common base classes as abstract classes. ! ©2006-­‐2014.  All  rights  reserved.  Ganesh  Samarthyam.   Based  on  "60  Tips  on  Object  Oriented  Programming",  Tata  McGraw-­‐Hill,  2006.  
  • 4. The abstract classes and interfaces help us to think about design in conceptual terms rather than worrying about implementation details. Instead of thoughtlessly mapping the application requirements into concrete classes to get the work done, it is better to spend time in finding the common properties in the concrete classes and map them into a abstract base class design and create reusable, extensible and flexible software. !31. Use abstract classes for modelling abstract properties and sub-classing. !Sub-classing refers to reusing interface and the implementation of a class; use abstract classes for modelling sub- classing. For related classes, provide the common functionality in an abstract base class and provide different implementations in separate concrete classes inheriting from it. !32. Provide default implementation class(es) with abstract classes or interfaces. !Abstractions have their own place in object oriented analysis and design. But without concrete classes, abstract classes or interfaces are not of much use. If you’re providing abstract classes or interfaces, ensure that you are providing at least one default implementation to make use of the abstract class or interface. !33. Use interfaces for sub-typing and multiple inheritance. !Sub-typing refers to reusing only the interface of a class; use interfaces for modelling sub-typing. Use pure interface classes to bind unrelated classes for some common functionality. Also prefer interfaces for supporting multiple inheritance. !34. Consider providing adaptor classes for interfaces. !Concrete classes that implement interfaces should provide the implementation of 'all' the methods, which is inconvenient in many situations. Provide adaptor classes for such interfaces providing the default implementation for the methods; this is for convenience as those adaptor classes will be easier to use. !35. Keep in mind that interfaces are difficult to evolve. !Pure class interfaces are difficult to evolve: Once clients have started using a pure interface class by implementing it, it is not possible to add or delete method declarations or make any modifications to the old method signatures; any such changes will break the client code. So strive for designing interfaces with an emphasis on establishing stability in the contract it provides. !36. Use virtual functions instead of chained if-else or switch statements. !Programmers from structured programming background tend to use extensive use of control structures. Whenever you find cascading if-else statements or switch statements checking for types or attributes of different types to take actions, consider using inheritance by replacing the if-else or switch code with virtual method calls. !37. Follow Liskov’s Substitution Principle (LSP). !Follow Liskov’s Substitution Principle (LSP), which is one of the cardinal rules to follow in object oriented programming. !38. Avoid using reflection extensively. !Reflection is a powerful feature, but using reflection exposes internal details of the classes. So, using reflection should be strictly avoided for solving high-level programming problems; it should be used only when knowing internal details of the classes at runtime is required. !39. Prefer using higher-level language features instead of reflection. !For most of the higher-level design problems, it is possible to provide acceptable solutions without using reflection. So, prefer using virtual functions or some other language features to achieve the same functionality instead of using reflection. ©2006-­‐2014.  All  rights  reserved.  Ganesh  Samarthyam.   Based  on  "60  Tips  on  Object  Oriented  Programming",  Tata  McGraw-­‐Hill,  2006.  
  • 5. !40. Avoid using RTTI extensively. !Other than safe uses such as performing downcasts and event-driven programming, RTTI should not be used for solving higher-level programming problems. Extensive use of RTTI when not necessary indicates that the design is bad and the implementation is brittle and un-maintainable. !41. Prefer using virtual functions instead of RTTI. !Extensive use of RTTI can potentially make maintaining the code difficult. Whenever you find cascading if-else statements for matching particular types to take actions, consider redesigning it by using virtual functions instead. !42. Use namespaces for avoiding name-clashes and organizing the software. !The techniques and approaches used for programming in large are considerably different from programming in small. One of the major objectives in programming in large is to support modular software and avoid name-clashes and versioning problems. By using namespaces effectively, programmers can create logical modules (set of related class) facilitating software distribution and maintenance. !43. Hierarchically partition the namespace. !One of the major benefits that object orientation provides is the ability to write reusable classes that can be used for wide range of projects instead of just providing code that will solve the specific needs. One of the widely used ways to organize such reusable software is in the form of class libraries that are organized (or partitioned) in a hierarchical format. Namespaces can be effectively used to provide a hierarchical organization of the software. !44. Selectively introduce only the specific namespace members you need to the code. !In general, it is a good programming practice to selectively introduce the names from a namespace to the code. This will avoid name-clashes that happen because of introducing all the members of namespace to the code. This will also avoid breaking the programs because of independent additions in the original namespace/package. !45. Selectively expose the types to the clients. !Namespaces play an important role in organizing software. One of the important ways in which namespaces help in organizing the software is to provide control to the programmers on the classes that are exposed to the clients. A programmer should use namespaces for providing public access, limited access or hiding the names in the application to the clients. !46. Avoid hiding of names in different scopes. !Hiding of names in different scopes is unintuitive to the readers of the code and using name hiding extensively can affect the readability of the program. Though hiding of names is a convenient feature, it is recommended to avoid name hiding as it can result in subtle defects and unexpected problems. !47. Strive for eliminating all forms of code duplication. !Code duplication is unnecessary and it can create major maintenance problems. There is no reason for any form of duplication of code and alternative approaches and solutions should be used for eliminating code duplication. !48. Use design patterns whenever appropriate. !Many of the problems in large-scale programming happen because of tight coupling between various software components. Design patterns reduce dependencies between the components; so, use them appropriately to create more flexible and reusable software. !49. Provide frameworks for effective reuse of architectural design. ! ©2006-­‐2014.  All  rights  reserved.  Ganesh  Samarthyam.   Based  on  "60  Tips  on  Object  Oriented  Programming",  Tata  McGraw-­‐Hill,  2006.  
  • 6. Frameworks can be very useful in providing solutions in specific application domains. Since frameworks support higher level architectural design reuse, it is preferable to provide frameworks or consider reusing existing frameworks. !50. Provide class libraries for any set of related reusable classes. !Instead of providing specific solution to the given problem, consciously identify reusable components and provide it as a class library. ! ©2006-­‐2014.  All  rights  reserved.  Ganesh  Samarthyam.   Based  on  "60  Tips  on  Object  Oriented  Programming",  Tata  McGraw-­‐Hill,  2006.