1 / 21

Key design principles

Key design principles. Tutorial 7b. Why design?. The design of the system is one of the most crucial stages: “Low” enough to address implementation issues. “High” enough to avoid wasting time.

yuma
Télécharger la présentation

Key design principles

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Key design principles Tutorial 7b

  2. Why design? • The design of the system is one of the most crucial stages: • “Low” enough to address implementation issues. • “High” enough to avoid wasting time. • Problems solved at design time will save coding and debugging time; problems left open will become bugs. • "באג ב-design, זין ב-debug"

  3. Key design concepts • What are we trying to achieve? • Decomposition • Abstraction • Information hiding • (Correct) Modularity • Extensibility • “Virtual machines” • Hierarchy

  4. Decomposition • Large pieces of code are impossible to understand or maintain. • “Monster” classes • “Monster” methods • How to avoid? • Decompose the system to modules until each module has a single major role. • Distribute responsibility among modules equally • No “show runners”.

  5. Decomposition rules • The palm rule: a method containing logics should not be larger then your palm. • Usually a single editor screen • The kiss rule: methods without logics should be small enough to kiss. • Usually a couple of code lines. • The scroll rule: the entire class should be small enough to scroll through effortlessly. • Usually less then 1000 code lines, best less then 200.

  6. Decomposition - example • Consider the Simple Mail Transfer Protocol (SMTP) for sending and receiving e-mail. • Has a sending and receiving side. • The protocol has several stages (handshake, recipients list, the content itself…) • All communication is low-level (plain text) and has to be parsed • Each stage must be handled and acknowledged. • And so on…

  7. SMTP – phase 1 SMTP +send() +receive() HELP!!!Isn’t that the class that ate Cincinnati…

  8. SMTP – phase 2 SMTP Sender +send() Receiver +receive() Monsters, but small ones…

  9. SMTP – phase 3 Sender SMTP Receiver * * RcvStage* SendStage* SndRcpts SndText SndEnd RcvPcpts RcvText RcvEnd

  10. Abstraction • We want to focus less implementation details and more on functionality and characteristic. • Implementation can change • Functionality derives from need • Bad abstraction: • Exposing fields • Using drawing commands (point, line, fill…) • Good abstraction: • Getters/setters • Shapes (rectangle, circle…)

  11. Information hiding • We strive to hide as much details as possible, to avoid high coupling between modules. • Bad hiding: • Access to unneeded methods/fields • Better hiding • Using well-defined interfaces to match the needs. • Best hiding • Module is operated using a defined set of command (table logic, mini-language)

  12. Modularity (1) • High cohesion: when a change occurs, it is quickly reflected to relevant modules. • Bad cohesion: modules are packaged in an unrelated manner, so change is reflected slowly. • For example: “all utilities in the system” • Good cohesion: related modules are close, so it is easy to change them • Best: all modules responsible for carrying out a common functionality.

  13. Bad cohesion example Changed ImageReader ImageParser ImageRenderer ImageDisplay FileReader TextReader TextParser TextRenderer TextDisplay SoundReader SoundParser SoundRenderer SoundPlay Packaging by functionality (handle an aspect) is more appropriate then order of execution

  14. Modularity (2) • Low coupling: when a change occurs, it shouldn’t be reflected in unrelated modules. • Usually a result of exposing impl. details. • For example: exposing the internal structure of a data structure (list, tree…) to allow iteration. • Can be avoided by using an interface or an abstraction • For example: providing an itertor to allow iteration. • Even better: a foreach function (Perl, STL, C#)

  15. Extensibility • You will need to modify your system sometime • New version, requirements change, bug fixes… • The best way to do it is by extending its abilities. • But for that the right module needs to be extensible • Extension can be done by: • Inheritance • Adding elements / plug-ins • Mini-lang scripting, table logic…

  16. “Virtual Machines” • Allow reusability of systems by supplying a defined set of basic, simple operations. • The same basic layer can serve several applications. • Several systems can implement the operations set. • Some more examples: • OpenGL / DirectX • CORBA • SQL / ODBC/ JDBC • PostScript

  17. Using hierarchies • Hierarchies are a convenient way to view (and implement) a system • By inheritance, composition, usage layers… • Benefit of hierarchical design: • Use stubs instead of lower level for prototyping • Use drivers instead of higher level for testing • Well-defined architecture and dependencies • Common problem: allowing two hierarchies to vary independently, while keeping some relation.

  18. Common design trades (1) • Abstraction / interfaces • Using a well-defined interface to “protect” the impl. • At best, the interface is external: • Mini-language: Tcl/Tk, VB/COM, SQL, BeanShell… • External data: table logic, XML • Protocols: HTTP/HTML, FTP, SMTP, IRC, SOAP… • Virtual machines: JVM, OpenGL, Unix… • Another alternative: instead of an interface, provide command objects that operate the module.

  19. Common design trades (2) • Use objects instead of explicit control commands (if/else, switch/case) • Control is evil (!בקרה זה רע): • Causes monster methods and classes • Hard to change • High coupling • Instead of coding the entire behavior of the module in a single function, create objects that behave that way – that’s what OOP is for!

  20. Control  objects myState State* do() Client switch (state) { case INIT: … case OPEN: … case CLOSE: … } Init Open Close if (foo != null) { // do nothing } else { // do real job } Foo* RealFoo NullFoo Ban the control!

  21. Common design trades (3) • Avoid using variables: • NEVER expose a field • Avoid global variables as much as possible • Since fields are global in the class scope, avoid them too. • Best variables: • Method parameters • Temporary methods • Fight the variables!

More Related