Programming Style

Download Report

Transcript Programming Style

Programming Style

Chapter 14 Part 3: Implementation Object-Oriented Modeling and Design Byung-Hyun Ha [email protected]

Lecture Outline

 

Introduction Object-Oriented Style

 Reusability    Extensibility Robustness Programming-in-the-large

Introduction

The experienced programmer follows principles to make readable programs that live beyond the immediate need

Good style is important in all programming, but it is even more important in OO design and programming because much of the benefit of the OO approach is predicated on producing reusable, extensible, understandable programs

Object-Oriented Style

   

Reusability Extensibility Robustness Programming-in-the-large

Reusability

Kind of reusability

  Sharing of newly-written code within a project Reuse of previously-written code on new projects 

Style rules for reusability

        Keep methods coherent Keep methods small Keep methods consistent Separate policy and implementation Provide uniform coverage Broaden the method as much as possible Avoid global information Avoid modes

Reusability

Using inheritance

    Subroutines Factoring Delegation Encapsulate external code

Extensibility

OO principles for extensibility

    Encapsulate classes Hide data structures Avoid traversing multiple links or methods Avoid cast statements on object type  Distinguish public and private operations

Robustness

    

Guidelines for robustness

 Protect against errors • • User errors and low-level system errors Programming bugs Optimize after the program runs Validate arguments Avoid predefined limits Instrument the program for debugging and performance monitoring

Programming-in-the-large

     

Guidelines

    Do not prematurely begin programming Keep method understandable Make methods readable Use exactly the same names as in the object model Choose name carefully Use programming guidelines Package into modules Document classes and methods Publish the specification

Appendix: OO and Programming

We already discussed…

   Farm toString() and priority queue Window programming 

They cannot be possible without inheritance and polymorphism

 If you want to prepare those functionalities in the examples, you should consider the use of OO concepts  Otherwise, your program will never be understandable (even for yourself), extensible, and reusable

Appendix: OO and Programming

Inventory example

  Inventory simulation Assumptions • • • • Two types of suppliers Three types of demands Two types of policies Two ways of displaying results   If you want to carry out simulation for every possible combination of settings, you have to write 24 (= 2x3x2x2) programs • Too complex to write and manage Let’s make them using one program (Inv_manage1.java) • How about it?

• • Could you understand?

Do you think you can easily extend the program?

Appendix: OO and Programming

Inventory example (cont’)

   Let’s use OO concepts (Inv_manage2.java, …) First of all, easy to understand Coherent, small, clear, consistent, extendible, reusable, … Inv_manage

Supplier

put_order today_deliever

Demand

today_demand

Policy

today_order

Display

show

Appendix: OO and Programming

Tree example

  Tree traversal with different purposes Structural way (Tree1.java) • We should implement traversal algorithm every time we need  OO way (Tree2.java) • Don’t care about how to traverse, only need to write what to do 1 2 2 5 3 7 4 2 5 1 6 3