Comments on On Designing Good Libraries -- Part II
Some good comments
on my last post on guidelines… here is some thoughts on those
comments:
Roland asks:
Ø
Attributes: Never use
overloaded constructors
What's the reason/idea
behind this?
Custom
Attributes, unlike other classes, supports optional named arguments. So CA
support two ways of doing optional arguments: overloading and optional named
arguments. Whenever there is a
choice such as this it leads to inconsistencies so, we make a recommendation, in
this case the value of named arguments makes them a better
choice.
>Use a zeroed state for the default value
(such as: 0, 0.0, false, “”, etc.)
Isn't the zeroed
state for a string null, not ""?
It is a
grey zone.. you are right that technically string is a reference type and
whenever the runtime “zeros” a reference type its value becomes “null”. However from an API design angle we try
to avoid returning null for strings (and collections) as it requires extra
checking code before operating on the value. The recommendation is to return
String.Empty (“”) in most cases.
Frank has
some suggestions that are valid… I think CBrumme had a blog about MarshalByRef
didn’t he?
Also, to allow better inlining, don't
inherit from MarshalByRef, or any of its derived classes: Component, Control,
etc. Also Component is heavier than some people realize, and has a finalizer
which may never be needed.