Docsity

Object-Oriented Programming Best Practices, Summaries of Object Oriented Programming

Toggle sidebar
bg1
1
Coding rules
Nima Chitgar
1. Use singular nouns or noun phrases as names for classes. Use Pascalcase casing for their
spelling.
2. A class documentation comments start with an enumeration of all the characteristics ascribed
to the class itself, as well as all the characteristics ascribed to the individual objects of that
class.
3. Include at least a version number (@version) and a list of author names (@author) in the
class documentation comment.
4. User verbs or verbs phrases in active tense as name for methods. Use camelCase for their
spelling.
5. Choose method names in such a way that their invocations read as commands or questions
in a natural language.
6. Start the definition of a class with a definition of all its constructors.
7. Group all elements of a class related to a single property
1
. Start a group with its public
elements; finish with its private elements.
8. Introduce a getter and setter for each single-valued, mutable property. Introduce a getter for
each single-valued, immutable property.
9. Do not define a method that at the same time changes the state of some of the objects
involved in it, and returns a result.
10. Inspectors never change the observable state of any object.
11. Mutators never return a result.
12. In a group of elements related to a single property, inspectors precede
2
mutators with the
same access right.
13. Prefer instance methods
3
over static methods
4
.
14. Use a static method if only primitive
5
values are involved in it.
15. Use class methods if their behavior must at all times be the same for all the objects of a class.
Use class methods among other to manipulate class properties.
16. Use singular nouns or noun phrases as names for formal arguments and local variables. Use
camelCase for their spelling.
17. Start documentation comments attached to methods with a one line summary of the method’s
semantics
18. Clarify in a method’s documentation the role of each formal argument in a clause head by
the predefined tag @param.
19. Introduce basic inspectors to get the current value of each property ascribed to classes and
their objects, and add the annotation @Basic to their definition.
1
Field
2
Come before something in time
3
Object in memory
4
Method that belongs to a class rather than an instance of a cl ass
5
Undefined, null, boolean, string and number
pf3
pf4
pf5

Partial preview of the text

Download Object-Oriented Programming Best Practices and more Summaries Object Oriented Programming in PDF only on Docsity!

Coding rules

Nima Chitgar

  1. Use singular nouns or noun phrases as names for classes. Use Pascalcase casing for their spelling.
  2. A class documentation comments start with an enumeration of all the characteristics ascribed to the class itself, as well as all the characteristics ascribed to the individual objects of that class.
  3. Include at least a version number (@version) and a list of author names (@author) in the class documentation comment.
  4. User verbs or verbs phrases in active tense as name for methods. Use camelCase for their spelling.
  5. Choose method names in such a way that their invocations read as commands or questions in a natural language.
  6. Start the definition of a class with a definition of all its constructors.
  7. Group all elements of a class related to a single property^1. Start a group with its public elements; finish with its private elements.
  8. Introduce a getter and setter for each single-valued, mutable property. Introduce a getter for each single-valued, immutable property.
  9. Do not define a method that at the same time changes the state of some of the objects involved in it, and returns a result.
  10. Inspectors never change the observable state of any object.
  11. Mutators never return a result.
  12. In a group of elements related to a single property, inspectors precede^2 mutators with the same access right.
  13. Prefer instance methods^3 over static methods^4.
  14. Use a static method if only primitive^5 values are involved in it.
  15. Use class methods if their behavior must at all times be the same for all the objects of a class. Use class methods among other to manipulate class properties.
  16. Use singular nouns or noun phrases as names for formal arguments and local variables. Use camelCase for their spelling.
  17. Start documentation comments attached to methods with a one line summary of the method’s semantics
  18. Clarify in a method’s documentation the role of each formal argument in a clause head by the predefined tag @param.
  19. Introduce basic inspectors to get the current value of each property ascribed to classes and their objects, and add the annotation @Basic to their definition. (^1) Field (^2) Come before something in time (^3) Object in memory (^4) Method that belongs to a class rather than an instance of a class (^5) Undefined, null, boolean, string and number
  1. Add the annotation @Immutable to inspectors without formal arguments, whose result does not change as long as the application is executing.
  2. Specify the result of derived^6 inspectors in one or more clauses head by the predefined tag @return.
  3. If we choose to handle an exceptional case in a total way, deal with it in a return-clause in a case of a derived inspector, and in a postcondition in case of a mutator or a constructor.
  4. Deal with all exceptional cases in the definition of a method, such that clients always know exactly how it behaves under all possible circumstances.
  5. Specify changes to the state of objects or classes involved in mutators and constructors in one or more clauses headed by the self-defined tag @post.
  6. The setter set® for a property ® must deal with illegal values in a nominal way, in a total way or in a defensive way.
  7. Specify the effect of mutators and constructors in terms of other mutators, respectively constructors, if their e®ect must at all times be equivalent with the combined e®ect of the latter methods. Use one or more documentation clauses headed by the self-defined tag @effect for that purpose.
  8. Invoke class methods against their classes. Do not invoke class methods against objects of their class.
  9. In a group of elements related to a single property, definitions of methods precede declarations of variables.
  10. Choose names of variables in such a way that they reflect as well as possible the information they store. Use camelCase for their spelling.
  11. Encapsulate all technical details concerning the internal working of a class in its definition.
  12. Use private variables to encapsulate all aspects concerning the representation of classes and their objects.
  13. Explicitly initialize an instance variable or a class variable, if a proper initial value exists, even if that value occurs at the same time with the default value of its type.
  14. Add a final qualification to the declaration of all instance variables and class variables that serve to register immutable properties.
  15. Use all upper case in spelling identifiers naming symbolic constants. Separate successive words in such names by underscore.
  16. Use symbolic constants for timeless properties. Use immutable properties for values that are constant during a running application, but that may change over time.
  17. Use the conditional operators && and || to combine Boolean subexpressions instead of the unconditional operators & and ||.
  18. Prefer qualified selections of instance variables of objects over unqualified selections.
  19. Access non-final instance variables and class variables for storing single-valued properties only in the body of getters and setters. Initialize final instance variables in the body of constructors.
  20. Invoke instance methods against the prime object in an unqualified way, unless you want to emphasize^7 that they are invoked against this.
  21. Do not access static variables or invoke static methods in a qualified way in the body of their class, unless their names conflict with names of other entities in nested scopes. (^6) Their result can be described in terms of basic inspectors. (^7) Benadrukken
  1. Use while statements^12 and do-while statements^13 for dynamically bounded iterations^14. Use do-while statements in case at least one iteration is needed; use while statements in all other cases.
  2. Use for statements and enhanced for statements for statically bounded iterations^15. Use enhanced for statements to encode iterations over all elements of an iterable collection; use ordinary for statements in all other cases.
  3. Complement the implementation of complex iterative constructs with loop invariants.
  4. Introduce each self-defined exception class as a direct or indirect subclass of the predefined class of runtime exceptions.
  5. Terminate names of self-defined exception classes with the suffix exception.
  6. Specify conditions under which methods must throw exceptions in clauses headed by the predefined tag @throws or @exception.
  7. Enumerate in the throws clause of a method all exceptions that can result from the execution of the method, and that directly or indirectly belong to the predefined exception class Exception, including unchecked exceptions of time RuntimeException.
  8. The set of exceptions enumerated in the throws clause of a method must be identical to the set of exceptions specified in its documentation.
  9. Isolate common aspects in conditions imposed on dependent properties ® 1 ® 2 …®n. Invoke that method in the checker for each property involving the value to be investigated and the current value of all other dependent properties.
  10. Facultatief
  11. Facultatief
  12. Facultatief
  13. Facultatief
  14. Facultatief
  15. Facultatief
  16. Facultatief
  17. Make sure that each object is an instance of the same class for its entire lifetime.
  18. For a uni-directional association with restricted multiplicity from class A to class B, class A introduces a single instance variable x. x stores a reference to an object of class B.
  19. Complement a uni-directionional association ®with restricted multiplicity in the definition ® the referring class with:
    1. a basic inspector get® returning the associated object
    2. a mutator set® registering a given object as the newly referred object
    3. an instance checker canHaveAs® or a static checker isValid® imposing restrictions on the referenced object
  20. Complement a bi-directional association with restricted multiplicity in the definition of both participating classes with a private instance variable referencing the associated object from the other class. (^12) A while loop will check the condition first before executing the content. (^13) A do while loop executes the content of the loop once before checking the condition of the while. (^14) Actual number of iterations cannot be known in advance. (^15) Iterations where simple code inspection makes it possible to predict the actual number of iterations
  1. Complement a bi-directional association ® with restricted multiplicity in the definition of the controlling class with:
    1. a basic inspector get® returning the associated object
    2. a mutator set® registering mutual references between its prime object and a given object
    3. an instance checker canHaveAs® or a static checker isValid® imposing restrictions on the referenced object
  2. Complement a bidirectional association ® with restricted multiplicity in the definition of the non-controlling class with:
    1. a basic inspector get® returning the associated object
    2. a mutator set® registering a reference between its prime object and a given object
  3. Encapsulate the class invariant for a bi-directional association with restricted multiplicity in a boolean inspector hasProper®, where ® is the role of the other class in the association.
  4. Isolate all restrictions on bi-directional associations except for consistency of mutual references in a boolean inspector canHaveAs®, where ® is the role of the other class in the association.
  5. If an association ® with unrestricted multiplicity is presented as a list, introduce a basic inspector getNb®s returning the number of associated objects and a hidden setter getNb®s to change that number. Only introduce a checker if an upper bound must be imposed on the number of associated objects.
  6. If an association ® with unrestricted multiplicity is presented as a list, introduce
    1. a basic inspector get®At returning the associated object at a given index
    2. an instance checker canHaveAs®At or a static checker isValid®At imposing restrictions on the associated object registered at a given index
    3. an instance checker hasProper®s encapsulating the class invariant
  7. If an association ® with unrestricted multiplicity is presented as a list, introduce mutators add®At and remove®At to add and remove elements. Often, simpler methods such as addAs® and removeAs® are sufficient.
  8. If a class is involved in an association with unrestricted multiplicity, introduce at least a constructor initializing new objects that are associated with the lowest possible number of objects of the other class.
  9. If a class is involved in an association with unrestricted multiplicity, introduce at least a destructor that disconnects the object to be terminated from all objects it is still associated with.
  10. If an association ® with unrestricted multiplicity is presented as a set, introduce a basic inspector hasAs® checking whether the given object can be included in the set of associated objects.
  11. If an association ® with unrestricted multiplicity is presented as a set introduce
    1. an instance checker canHaveAs® or a static checker isValid® imposing restrictions on associated objects
    2. an instance method hasProper®s encapsulating the class invariant.
  12. If an association with unrestricted multiplicity ® is presented as a set, introduce mutators addAs® and removeAs® to add and remove elements.
  1. Never extend the list of (unchecked) exceptions in overriding a method. Remove from the list all exceptions that can no longer be thrown, if the conditions under which they must be thrown at the level of the overriding method have reduced to false.
  2. If a subclass overrides an instance method, it must honor conditions under which that method must throw exceptions. A subclass is not allowed to extend nor to reduce conditions under which an inherited method must throw exceptions.
  3. Specifiy conditions under which methods throw exceptions in clauses headed by the predefined tag @throws. The formal specification starts with an expression under which the stated exception must be thrown, optionally followed by an expression under which the state exception can be thrown.
  4. If a subclass overrides an inspector, it must honor the return type as specified at the level of its superclass. A subclass my strengthen inherited return types to subtypes.
  5. If a subclass overrides an instance method, it must be honor access rights imposed on that method at the level its superclass. A subclass may widen access rights at the level of the overriding method.
  6. If a subclass overrides an instance method, it must honor raw annotations imposed on that method at the level of its superclass. A subclass may not leave out a raw annotation in overriding method, nor may it add that annotation to the method itself or to one of its arguments.
  7. If a subclass overrides an instance inspector, it must honor immutable annotations imposed on that method at the level of its superclass. A subclass may not leave out that annotation in overriding a method. Itmay, however, impose that annotation to the overriding method.