effective.net ted neward neward & associates

15
Effective .NET Effective .NET Ted Neward Neward & Associates http://www.tedneward.com

Upload: bennett-gardner

Post on 21-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Effective.NET Ted Neward Neward & Associates

Effective .NETEffective .NET

Ted Neward

Neward & Associates

http://www.tedneward.com

Page 2: Effective.NET Ted Neward Neward & Associates

CredentialsCredentials

Who is this guy?– Independent consultant, architect, mentor– Instructor, Pluralsight (http://www.pluralsight.com)– BEA Technical Director, Microsoft MVP Architect– JSR 175, 250, 277 EG member– Founding Editor-in-Chief, TheServerSide.NET– Author

• Effective Enterprise Java (Addison-Wesley, 2004)• Effective .NET (Forthcoming)• Pragmatic XML Services (Forthcoming) • Server-Based Java Programming (Manning, 2000)• C# in a Nutshell (OReilly, 2003)• SSCLI Essentials (w/Stutz, Shilling; OReilly, 2003)

– Papers at http://www.tedneward.com – Weblog at http://blogs.tedneward.com

Page 3: Effective.NET Ted Neward Neward & Associates

AboutAbout

Effective .NET– part of the Scott Meyers “Effective” series– language-agnostic (not about C# or VB,

but .NET)– chapters include

BasicsDesign & DeclarationImplementationConcurrency

Page 4: Effective.NET Ted Neward Neward & Associates

BasicsBasics

Know the difference between references, objects, and value types– references are what we directly use– objects are on the other side of references– value types have different semantics from ref

types– these semantics can make a huge difference

identity, equality, assignment, and more

Page 5: Effective.NET Ted Neward Neward & Associates

BasicsBasics

Recognize the difference between equality and identity– equality: “are the objects that these references

point to semantically equivalent?”– identity: “are the objects that these references

point to the same object?”– C# allows for the blurring of these two

questions by overloading the comparison operator (==)

Page 6: Effective.NET Ted Neward Neward & Associates

BasicsBasics

Understand pass-by-value and pass-by-reference semantics– everything in .NET is passed by value

unless dictated otherwise (ref, out)– passing by ref means modification of the thing

passed is acceptable inside the methodthis is rarely what you want;

counterintuitive– but note that passing references by value can

achieve the same thing… sort ofthis is why understanding pbv and pbr

is important

Page 7: Effective.NET Ted Neward Neward & Associates

BasicsBasics

Avoid finalizers– finalizers are only for non-memory resource

cleanupfired after GC has determined the

object is reclaimable; this means GC must make at least two passes over this object to get rid of it

Page 8: Effective.NET Ted Neward Neward & Associates

Design & DeclarationDesign & Declaration

Obey the general contract for System.Object for domain object types– provide a ToString() implementation

return an object dump suitable for carbon-based lifeform consumption

– provide an Equals() implementationreturn an implementation that tests

for all possibilities: subtypes, – provide a GetHashCode() implementation

Equal objects must return identical hashes

Page 9: Effective.NET Ted Neward Neward & Associates

Design & DeclarationDesign & Declaration

Use interfaces for design, base classes for refactoring and implementation– interfaces

Page 10: Effective.NET Ted Neward Neward & Associates

Design & DeclarationDesign & Declaration

Prefer referencing objects by their interfaces– allows for substitution w/o modification– allows for decoration w/o modification

Page 11: Effective.NET Ted Neward Neward & Associates

Design & DeclarationDesign & Declaration

Consider providing static factory methods instead of constructors– encapsulation is

Page 12: Effective.NET Ted Neward Neward & Associates

Design & DeclarationDesign & Declaration

Minimize class & member accessibility– this means starting private and working

outwardsprotected data is a necessary evil, but

still evilprotected methods are less evil, but

still evil– NOTE: returning “handles” to private data is

dangerouseither make defensive copies …… or else return the data, not the

object

Page 13: Effective.NET Ted Neward Neward & Associates

Design & DeclarationDesign & Declaration

Minimize mutability– in other words, design immutable objects

objects which have ZERO mutable state– immutable objects have no concurrency issues

this means no worries about lock()…… and therefore no worries about thread-

sync– immutable objects are easy to understand

different shift in thinking…… but it’s not unmanageableafter all, anybody remember

System.String?– immutable objects are actually easy to write

but it does contradict property-set semantics

Page 14: Effective.NET Ted Neward Neward & Associates

SummarySummary

Effective .NET—a work in progress– offer feedback!

Page 15: Effective.NET Ted Neward Neward & Associates

QuestionsQuestions

?