effective.net ted neward neward & associates
TRANSCRIPT
Effective .NETEffective .NET
Ted Neward
Neward & Associates
http://www.tedneward.com
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
AboutAbout
Effective .NET– part of the Scott Meyers “Effective” series– language-agnostic (not about C# or VB,
but .NET)– chapters include
BasicsDesign & DeclarationImplementationConcurrency
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
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 (==)
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
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
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
Design & DeclarationDesign & Declaration
Use interfaces for design, base classes for refactoring and implementation– interfaces
Design & DeclarationDesign & Declaration
Prefer referencing objects by their interfaces– allows for substitution w/o modification– allows for decoration w/o modification
Design & DeclarationDesign & Declaration
Consider providing static factory methods instead of constructors– encapsulation is
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
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
SummarySummary
Effective .NET—a work in progress– offer feedback!
QuestionsQuestions
?