Object Oriented Programming Style

Object Oriented Style
  • Good programs do more than simply statisfies their functional requirement
  • programs that follow proper design guidelines are more likely to be correct reusable, extensible, and quickly debugged.
  • More style guidelines that are are intended for or conventional program also supply to object oriented program
  • in addition facilities such has inheritance are used to object oriented languages and required new guideline
  • Following are the object oriented style guideline
Reusability
Extensibility
Robustness




Reusability
  • reusable software reduces design coding and testing cost by reusing serveral design
  • releasing the amount of code also simplified understanding which increases the correctness of code
  • Reuse is possible in conventional language ( pop languages ),but object oriented languages greatly improve the possibility of code reuse.

Kinds of Reusability
  • There are two kinds of reuse:
Sharing of newly writer cold within a project
Reuse of previously writing call only projects
  • Counselling can be done by finding a repeated code sequences in the program and by writing a procedure of function of their implementation
  • Sharing of cosmic the programs smaller and faster
Style Rules For Reusability
  • Keep Methods Coherent :- A method is coherent if it performs a single operation ( function ) or a group of closely related operations. If it does two or more unrelated operations then break it apart into smaller methods.
  • Keep methods small :- if a method is large break it in to smaller methods. A method that exceeds one or two page is probably too large. By breaking a mothod into smaller parts, you may be able to reuse some parts even when the entire method is not reusable.
  • Keep Method Consistent :- similar methods should use the same name; argument order data types, return value, and error conditions. For example, in the C library, there are two inconsistent functions o output strings, puts and fputs. The puts function writes a string to the standard output, followed by a newline character; fputs writes astring to the standard output , followed by a newline character; fputs writes a string to a specified file, wihout a newline character. Avoid this kind of inconsistency.
  • Separate Policy and Implementation :- Policy method makes decisions and changes arguments. policy methods switch control among implementation method. Policy method should check for status and errors; they should not directly perform claculations or implement complex algorithms. policy methods are often highly application depended, but they are simple to write abd easy to understand. policy methods are the ''mortar''.   implementation methods perform specific detailed operation, withot decidind whether or why to do them. implementation methods are ''bricks''.  for example , a method to scale a window by a factor of 2 is a plocy method it shpould set the target scale factor for the window and call on an implementation method to scale the window by ab arbitary acake factor latar if you decided to changes the default scale factor to9 another value, such as 1.5, you just have to modify the parameter in the policy method. without changing the implementation method actually does the work.
  • Provide Uniform Coverage :- If input condition can occur in various combinations, write methods for all combinations, not just the ones that you currently need. for example, if you write a method to get a last element of a list, also write the method to get the first element.
  • Broaden The Method As Possible :- try to generalize argument types, precondition and constraints assumption about how the method works and the situation in which method opertes. Often a method can be made morre general with a slight increase in code.
  • Avoid Global Information :-  Minimize external references.

Usinginheritance for Reusing code :-
There are several techniques of breaking up methods to inherit some code.
1. Subroutines: The simplest approach is to factoe out the common code in a single ,ethod that is called by each method as shown in following figure. The common method can be assigned to auper clas.





Figure. Code reuse Via subroutines
2. Factorimg :- In some cases the best way to increases code reuse between similar classes is to factor out the differences between the method of different classes, leaving the remainder of the code as a shared method




Figure. code reuse bia factoring
  This approach is effective when the differences between methods are small and the similarities are great. The common Portion of two methods is made4 into a new method as shown in figure.


Extensibility
  • Extensibility means increasing the computing power of the software.
  • Most software is extended in different ways.
  • The guidelines for reusability increases extensibility as well
  • In addition the following object oriented principles increases extensibility
* Encapsulate classes :- A classes in encapulated if its internal structure is hidden from other classes. only Methoda in the class should access its implementation
* Hide data structures :- Do not allow the variables of classes tobe access from outside methods.

* Distinguish Between public and private operations












Robustness
  • An application (software) is robust of ot does not fail in exceptional ( error ) conditions
  • a method is robust if it does not fail even if it receives improper parameters.
  • To achieve the robustness consider the following points while developing application

1). Protecr against errors: spftware should protect itself against incorrect user input.
2). Optimize after the progam runs: Don't optimize a program until you get it working.
3). Validate argument: Check the arguments of methods before using them.
4). Avoid predefined limits: possibly avoid fixed size arrays. when possible use dynamic memory allocation to create dara structures that don't have predefined limits.




















No comments

Theme images by enjoynz. Powered by Blogger.