Clean Code Csc

21
CS2110: SW Development Methods Design of methods (functions) Class design CRC cards UML class and sequence diagrams Software Design

description

presentation

Transcript of Clean Code Csc

Page 1: Clean Code Csc

CS2110: SW Development Methods

• Design of methods (functions)• Class design

– CRC cards– UML class and sequence diagrams

Software Design

Page 2: Clean Code Csc
Page 3: Clean Code Csc

Old problems

• Before OO, many languages followed the procedural paradigm– A program was based on functions that worked like static

methods in Java– A “main” function started things

• functional decomposition– Break problem down– Function deals with subproblem– Hierarchical. Levels of abstraction

• Problems:– Control and coordination centered in main method– Required changes have big impacts– Logic may be distributed– Data changes affect may modules

Page 4: Clean Code Csc

Functions:

• What we call them: functions, methods, modules

• Functions are a core construct in OO and non-OO programs

• Principals of good functions apply in either language– But less of a problem in OO – can you think

why?

Page 5: Clean Code Csc

What makes a function good?

• Your answers:

Page 6: Clean Code Csc

Good qualities in functions:

• Lots written about this!

• Clean Code: A Handbook of Agile SW CraftsmanshipRobert C. Martin– Read Chapter 3 in UVa digital library version

Page 7: Clean Code Csc

Good Qualities in Functions

• Small– How small?

• Do One Thing: Strong Cohesion– Why?– One level of abstraction per function

• Thus, functions fall into levels of abstraction

– Name reflects the one task it does

Page 8: Clean Code Csc

Interface Qualities of Functions

• What about its interface?– Inputs, return value/arguments– Other sources of data or “output”

• Side effects

• Some rules?

Page 9: Clean Code Csc

Good Qualities in Functions (2)

• Number of arguments: small• Flag arguments

– What are they? What bad thing do they suggest is happening?

• Avoid side effects• Avoid “output” arguments

– Arguments that are changed after the function executes

– Java: possible for “mutable” classes

Page 10: Clean Code Csc

Good Qualities in Functions (3)

• Don’t Repeat Yourself (DRY)

• Command/Query Separation– Do something. Or answer something.

Don’t do both.

• Returning Errors– Return value? Output argument?

• Burden on caller?

– Exceptions: what advantages?

Page 11: Clean Code Csc
Page 12: Clean Code Csc

Classes

• So far we’ve just talked about objects• We note that many objects are a “type of” the

same kind of thing– The same “abstraction”– E.g. 1, 2, 7 and 10 – whole numbers– E.g. 1, 3.5, 25, pi – numbers– Bob, Sally, Joe – Students

• But, they are Persons too, aren’t they?– cs201, cs202, engr162 – Courses

Page 13: Clean Code Csc

Classes, Type• Classes define a set of objects with the same

properties (state, behavior)• A class definition serves as a “cookie cutter” for

creating new objects– Instantiation of an object of a certain class– In Java, we do this with new and a constructor is called– Creates an individual object, also called an instance

• Variables and data objects have a type– What the rules are for that object– An object’s class is one form of object-type

• Also any superclass it extends, and any interface it implements

Page 14: Clean Code Csc

Finding Classes from Specifications

• Study the textbook, Sect. 2.3.1• Next In-Lab exercises

– Several problems for groups to do in lab– Later you’ll get sample solutions for all these

problems

Page 15: Clean Code Csc

Sample Problem: Library Loans• Problem overview: Imagine you were going to write a set of classes that would be

part of a software system to support loans in a library. A description might be:

The library contains books and journals. It may have several copies of any given book. Books and journals have titles and a call-number. Each copy has a copy-number. For each book we record the author. Some of the books are on "reserve" and can only be checked-out for 3 hours, but all other books can be borrowed as a regular loan (for a three week period). Members of the library can normally borrow up to six items at a time, but staff members can borrow up to ten items. Members of staff can borrow journals, but not regular members. The system must keep track of when books and journals are borrowed and returned. Our system might want find out what items have been borrowed by a given member, and which copies of an item have been checked out and by whom.

Page 16: Clean Code Csc

Class Identification

• What are possible classes here? Why?• <your answers here>

• How do you recognize classes?• What makes a good class name?

Page 17: Clean Code Csc

Sample Problem: Library Loans• Problem overview: Imagine you were going to write a set of classes that

would be part of a software system to support loans in a library. A description might be:

The library contains books and journals. It may have several copies of any given book. Books and journals have titles and a call-number. Each copy has a copy-number. For each book we record the author. Some of the books are on "reserve" and can only be checked-out for 3 hours, but all other books can be borrowed as a regular loan (for a three week period). Members of the library can normally borrow up to six items at a time, but staff members can borrow up to ten items. Members of staff can borrow journals, but not regular members. The system must keep track of when books and journals are borrowed and returned. Our system might want find out what items have been borrowed by a given member, and which copies of an item have been checked out and by whom.

Page 18: Clean Code Csc

Methods and Fields

• Pick a class: name its methods and fields

Page 19: Clean Code Csc

Class Relationships

• Do any classes have “structural links” to other classes?– “link” might mean “part-of” relationship.– This is a static relationship– Are such “links” one-to-one, one-to-many?

• Which objects might call methods on objects of another class?– This is a dynamic relationship (while the program

is running)

Page 20: Clean Code Csc

Another View on Defining Classes

• Classes have– Responsibilities

• In words: what is its role? Data encapsulation and management? Organizer of other objects? Some level of control or task-execution?

• Methods implement these

– Relationships• Static connections to other objects• Dynamic interactions with other objects

Page 21: Clean Code Csc

The CRC Card Technique

• Read info on “CRC cards” here: http://www.agilemodeling.com/artifacts/crcModel.htm