Professional Documents
Culture Documents
CS130 Lecture 7
Where we Are
Started with requirements
Progressively digging deeper
How?
Design Patterns
Design Heuristics
Find a real-world analogy for objects
Encapsulate details, hide information
Makes programs easier to modify
Pattern example?
Working Classes
Class = Cohesive collection of data and
routines that share a responsibility
And let you
ignore the rest of the program when
looking at class implementation
Ignore class implementation when looking
at use
Good Abstractions
Consistent level of abstraction
Eg dont mix several responsibilities
Routines
Aka methods, functions, subroutines
Routines reduce complexity
Introduce understandable abstraction
Avoid duplicate code
Simple routines are good
Dont be shy of writing small ones
CS130 Prof Majumdar Lecture 7
Routine Cohesion
How closely its operations are related
Functional cohesion (best)
Routine does one thing only
Sequential cohesion
Operations share data and must be done in
sequence
Bad design:
Procedural cohesion
Routine exists only to sequence operations
Naming Conventions
[short digression: Naming conventions in McConnells book is
spread over several sections [7.3: routines, 10: variables, 12.7:
constants, 12.9: types]. I dont like that.]
Naming Conventions
Example
General Advice:
Name should accurately describe the named entity
E.g.: bpp bytes_per_page
Consistency is important!!
Bottom Line: Favor read-time
convenience over write-time convenience
CS130 Prof Majumdar Lecture 7
Bad Names
Visual puns: si10 vs silo
Inside jokes: revenge_on_joe
similar sounding: cownt, count
Similar names, different work:
aBufLength, a BufferLength
Defensive Programming
Write code so that bugs are easier to
catch and fix
Not a substitute for good design
principles!
Basic Idea: Dont trust the external
world
CS130 Prof Majumdar Lecture 7
Whats Wrong?
x = (struct foo
*)malloc();
x->a = 0;
Better:
x = (struct foo
*)malloc();
If(x == NULL) {
handle error
}
x->a = 0;
CS130 Prof Majumdar Lecture 7
Whats Wrong?
s = inputString();
Printf(s);
Whats Wrong?
S = SELECT pwd
FROM Table WHERE
name = ;
Name =
inputString();
Query = S ^ Name;
Execute(Query);
Whats Wrong?
Lock();
If(error condition) {
return;
}
Unlock();
Return;
On error condition,
lock is not released
Lead to deadlock
Assertions
Contracts (pre- and post-conditions)
Static Analysis
CS130 Prof Majumdar Lecture 7
Refactoring
Myth: Coding can proceed linearly, ie
written once, tested, and forgotten.
Reality: Various upstream issues
change, code evolves substantially
during initial development
Refactoring
Change internal structure, not
observable behavior, in order to
make code easier to understand and
modify
Motivated by projects that evolve
during construction
Agile/XP development
CS130 Prof Majumdar Lecture 7
When to Refactor?
Duplicate Code
Unused code
Awkward API
Poorly encapsulated classes
God class
Class that does little
Routines grouped together that
shouldnt be
Too long routine, too many parameters
Often called smells [Martin Fowler]
CS130 Prof Majumdar Lecture 7
Refactoring Techniques
General principles
DRY: Dont repeat yourself
(Copy and paste is a design error)
Techniques
Replace code with routine
Encapsulate data (eg introduce class)
Rework routine to avoid duplication
Refactor Gradually
As you go rather than all at once
Code Tuning
Performance often conflicts with
other design goals
Example of design pattern that can
reduce performance?
Code Tuning
Question to ask: Are you sure you want to
sacrifice design for performance?
Answer is no most of the time
Other things you can do:
Change platform (HW, OS, language)
Algorithmic improvements
Internationalization and
Localization
I18n and L10n
Adapt software to different languages and
regional differences
Why? Want access to different regional markets
without major changes in code
I18n, L10n
What to change:
Alphabets/scripts: Most systems use
Unicode standard to resolve character
encoding issues
Language translation: Normal and error
messages, menu titles,
Currency, weights and measures, ID
numbers, telephone numbers,
Number formats (decimal point vs comma,
positioning)
CS130 Prof Majumdar Lecture 7
Whats Done?
Place text in resource strings which are
loaded during program execution as needed
Resource strings translated into various
languages
No code change required, only point to a
different locale to load appropriate strings
See e.g. GNU gettext
Difficulties:
Have to keep parallel versions of the strings
consistent through project lifetime
Some other details (direction of writing) not so
easy, and may be solved using compilation time
switches
CS130 Prof Majumdar Lecture 7
More Info
IBM globalization (g11n) web site:
http://www-01.ibm.com/software/globalization/in
dex.jsp
Java:
http://java.sun.com/developer/technicalArticles/Intl/IntlIntro/
CS130 Prof Majumdar Lecture 7
Administrative Items
Project 3 (design review) due Monday
HW 2 is up (due Wednesday)
Tell me what each group is up to?