Framework Design Guidelines

66

description

Framework Design Guidelines

Transcript of Framework Design Guidelines

Page 1: Framework Design Guidelines
Page 2: Framework Design Guidelines

Communication Artifacts

Properties Methods Events Constructors Exceptions

Page 3: Framework Design Guidelines

Four Keys of Framework Design

Page 4: Framework Design Guidelines

Outline

Page 5: Framework Design Guidelines

Naming Conventions

Page 6: Framework Design Guidelines

Saying It Load

Page 7: Framework Design Guidelines

Hungarian Notation

Do not use Hungarian notation in publicly exposed APIs and parameter names

public class CMyClass {public class CMyClass { int CompareTo (object objValue) {..} int CompareTo (object objValue) {..} string lpstrName {get;} string lpstrName {get;} int iValue {get;} int iValue {get;}}}

Page 8: Framework Design Guidelines

On Abbreviations, acronym, initialism and the like…

Avoid them! They are a classic JLT (jargon loaded term) OK to use them once they become words

Html, Xaml, etc

Don’t just spell them out Use a meaningful name

Abbreviations of more than 2 letters are cased as words, otherwise ALLUPPER IO vs. Html

Page 9: Framework Design Guidelines

Member Design

Page 10: Framework Design Guidelines

10

Constructors are

Do minimal work in the constructor Be Lazy! Only capture the parameters

public class XmlFile {public class XmlFile { string filename; string filename; Stream data; Stream data; public XmlFile(string filename) { public XmlFile(string filename) { this.data = DownloadData(filename); this.data = DownloadData(filename); }}}}

lazylazy

Page 11: Framework Design Guidelines

Properties

Property getters should be simple and therefore unlikely to throw exceptions

Properties should not have dependencies on each other Setting one property should not affect other

properties Properties should be settable in any

order

public class ArrayList {public class ArrayList { public int Count {get;} public int Count {get;}}}

Page 12: Framework Design Guidelines

Properties versus Methods

Use a Property: If the member logical attribute of the

type Use a method:

If the operation is a conversion, such as ToString()

If the getter has an observable side effect

If order of execution is important If the method might not return

immediately If the member returns an array

Page 13: Framework Design Guidelines

EmployeeList l = FillList();EmployeeList l = FillList();for (int i = 0; i < l.Length; i++){for (int i = 0; i < l.Length; i++){ if (l.All[i] == x){...}if (l.All[i] == x){...}}}

public Employee[] All {get{}}public Employee[] All {get{}}

Moral: Use method if the operation is expensive Moral: Use method if the operation is expensive

Calling CodeCalling Code

Properties and returning arrays

Page 14: Framework Design Guidelines

Extension Methods

Page 15: Framework Design Guidelines

CONSIDER using extension methods to "add" methods to interfaces

Page 16: Framework Design Guidelines

CONSIDER using extension methods to manage dependencies

Uri uri = “ftp://some.ftp.uri”.ToUri();

// higher level assembly (not mscorlib)

namespace System.Net {

public static class StringExtensions{

public static Uri ToUri(this string s){ … }

}

}

Page 17: Framework Design Guidelines

AVOID frivolously defining extension methods, especially on types you don’t own

Might add clutter (Chaos) Choose namespaces for sponsor types

carefully Remember that not all languages

support extension methods Users will have to use static method call

syntax

Page 18: Framework Design Guidelines

AVOID defining extension methods on System.Object

Page 19: Framework Design Guidelines

Type Design

Page 20: Framework Design Guidelines

Start

Run

Page 21: Framework Design Guidelines
Page 22: Framework Design Guidelines

22

Page 23: Framework Design Guidelines
Page 24: Framework Design Guidelines

Abstract and Base classes Prefer broad, shallow hierarchies

Less than or equal to 2 additional levels

Contracts and responsibilities are difficult to maintain and explain in deep complex hierarchies

Use abstract classes Make it clear what the class is for Provide a protected constructor for

subclasses to call

Page 25: Framework Design Guidelines

Virtual Methods

Method call virtualizes at runtime The static type doesn’t matter This is the danger and power of

virtual methods Danger: Owner of base classes cannot

control what subclasses do Power: Base class does not have to

change as new subclasses are created

Page 26: Framework Design Guidelines

Overriding Methods

All Virtual members should define a contract Don’t change the semantics of member

Don’t require clients to have knowledge of your overriding

Should you call the base?

Page 27: Framework Design Guidelines

References to base types must work with derived types without knowing the difference

To Virtual Or Not Virtual … Use non-virtual members unless you have specifically designed for specialization Have a concrete scenario in mind Write the code! Follow the Liskov Substitution Principle

Must continue to call in the same order and frequency

Cannot increase or decrease range of inputs or output

Barbara Liskov

Page 28: Framework Design Guidelines

Interfaces

No common implementation Challenging to version over releases The smaller, more focused the interface

the better 1-2 members are best But interfaces can be defined in terms

of other simpler interfaces

public interface IComparable { int CompareTo(object obj);}

Page 29: Framework Design Guidelines
Page 30: Framework Design Guidelines
Page 31: Framework Design Guidelines

Libraries , Primitives, Abstractions

Page 32: Framework Design Guidelines

CONSIDER placing library types higher on the dependency stack Definition:

Library types are types that are not passed between components

Examples EventLog, Debug,

Easy to Evolve Leave old in, add new one Beware of duplication!

Page 33: Framework Design Guidelines

DO keep primitives policy free (i.e. simple) Definition:

Primitive types are types that are passed between components and have very restricted extensibility (i.e. no subtype can override any members)

Examples Int32, String, Uri.

Hard to Evolve Little need to Evolve Typically in lower layers

Page 34: Framework Design Guidelines

Definition: Abstractions are interfaces or classes with

unsealed members that are passed between

components. Examples

Stream, IComponent Hard to Evolve Unfortunately, pressure to evolve

Page 35: Framework Design Guidelines

Trends

Page 36: Framework Design Guidelines

Test Driven Development

Write tests first, design later Requires reusable APIs to be

testable:

Page 37: Framework Design Guidelines

Heavy Dependencies & Testability

Page 38: Framework Design Guidelines

Inversion of Control// your better API

public abstract class TraceListener {

public abstract void Trace(string message);

}

public class Tracer {

TraceListener listener;

public Tracer(TraceListener listener){

this.listener = listener;

}

public void Trace(string message){

listener.Trace(message);

}

}

Page 39: Framework Design Guidelines

Dependency Injection

Check Containers like:Spring.NET, Castle Windsor ,

Structuremap, , Unity …

MEF session tomorrow will talk about it (isA)

Page 40: Framework Design Guidelines

The Power of Sameness

Influence of expectations Naming conventions and common suffixes\

prefixes Habits win out over the special cases

Common Exception pattern With great power comes great

responsibility Method overloading

Meet developers where they are constructors and properties pattern teaches

us to meet developers’ expectations

Page 41: Framework Design Guidelines

41

The perilous summit of complexity by example

Page 42: Framework Design Guidelines

42

How do I read How do I read all the lines all the lines from a file?from a file?

How do I read How do I read all the lines all the lines from a file?from a file?

Page 43: Framework Design Guidelines

43

VS7 Era “IO” seems like a reasonable place to start

“IO” seems like a reasonable place to start

Page 44: Framework Design Guidelines

44

Why did they make it inaccessible

Backup, and try again…

Why did they make it inaccessible

Backup, and try again…

Page 45: Framework Design Guidelines

45

Open.. Looks like a good first step…

Open.. Looks like a good first step…

Page 46: Framework Design Guidelines

46

Hmm… OK, what do I do with a FileStream?

Hmm… OK, what do I do with a FileStream?

Page 47: Framework Design Guidelines

47

Ok good, synchronous and asynchronous operations.. What the heck is that?

Ok good, synchronous and asynchronous operations.. What the heck is that?

Page 48: Framework Design Guidelines

48

I think I am in the wrong place..

I think I am in the wrong place..

Page 49: Framework Design Guidelines

49

Back up, let’s try a different type

Page 50: Framework Design Guidelines

50

Ahh, ReadLine(), this looks more promising..

Ahh, ReadLine(), this looks more promising..

Page 51: Framework Design Guidelines

51

OK, How do you find the end?

OK, How do you find the end?

Page 52: Framework Design Guidelines

52

Thanks goodness there was a sample

Thanks goodness there was a sample

Page 53: Framework Design Guidelines

53

The pit of success waydevelopers fall

into doing things the right way

Page 54: Framework Design Guidelines

54

Just what I need…

Just what I need…

Page 55: Framework Design Guidelines

55

Ah, a string[] I know just what to do with that…

Ah, a string[] I know just what to do with that…

Page 56: Framework Design Guidelines

56

How simple!How simple!

Page 57: Framework Design Guidelines

Tools

Page 58: Framework Design Guidelines

Framework Design Tools

Page 59: Framework Design Guidelines
Page 60: Framework Design Guidelines
Page 61: Framework Design Guidelines
Page 62: Framework Design Guidelines
Page 63: Framework Design Guidelines
Page 64: Framework Design Guidelines
Page 65: Framework Design Guidelines

65

Make the Make the simple simple things things simple simple and the and the

hard hard things things

possiblepossible

Page 66: Framework Design Guidelines

References http://blogs.msdn.com/brada (Thanks)

http://channel9.msdn.com/pdc2008/PC58 http://tinyurl.com/brad-guidelines

http://blogs.msdn.com/kcwalina Krzysztof Cwalina

http://tinyurl.com/msdn-guidelines Design Guidelines for Developing Class Libraries

[Off-topic] http://tinyurl.com/patterns-enterprise

Patterns of Enterprise Application Architecture [SOLID] http://derickbailey.com

Derick Bailey