Comments on On Designing Good Libraries — Part II


style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Some good href="http://blogs.gotdotnet.com/BradA/commentview.aspx/08775625-f9d1-4a7f-bbc3-6f07ba4487ac">comments
on my last post on guidelines… here is some thoughts on those
comments: "urn:schemas-microsoft-com:office:office" />


style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"> 


href="http://www.roland-weigelt.de">Roland style="mso-spacerun: yes"> asks:


style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l0 level1 lfo1; tab-stops: list .5in"> class=commentbody> style="FONT-FAMILY: Wingdings; mso-fareast-font-family: Wingdings; mso-bidi-font-family: Wingdings"> style="mso-list: Ignore">Ø style="FONT: 7pt 'Times New Roman'">     
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: style="mso-spacerun: yes"> 
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.


style="BORDER-RIGHT: medium none; PADDING-RIGHT: 0in; BORDER-TOP: medium none; PADDING-LEFT: 0in; PADDING-BOTTOM: 1pt; BORDER-LEFT: medium none; PADDING-TOP: 0in; BORDER-BOTTOM: windowtext 1pt solid; mso-element: para-border-div; mso-border-bottom-alt: solid windowtext .75pt">

style="BORDER-RIGHT: medium none; PADDING-RIGHT: 0in; BORDER-TOP: medium none; PADDING-LEFT: 0in; PADDING-BOTTOM: 0in; MARGIN: 0in 0in 0pt; BORDER-LEFT: medium none; PADDING-TOP: 0in; BORDER-BOTTOM: medium none; mso-border-bottom-alt: solid windowtext .75pt; mso-padding-alt: 0in 0in 1.0pt 0in"> class=commentbody> 


class=commentbody> 


href="http://weblogs.asp.net/fbouma">Frans points out:


It must be the case that no one
tested that method we added late in the final milestone as an instance method on
a sealed class with no constructors. That is right…. I believe this to be a
pretty rare instance, but it true. style="mso-spacerun: yes"> We have put in a couple of safe guards to
avoid this in the future including more formal check-in process, FxCop rules,
and even C# language enhancements.


style="mso-spacerun: yes">  style="mso-bidi-font-style: normal">– Does ‘sealing’ of classes get you any
performance gains when using it on classes with solely static
methods?
No, we only seal classes in this case to make their usage more
clear.  


style="BORDER-RIGHT: medium none; PADDING-RIGHT: 0in; BORDER-TOP: medium none; PADDING-LEFT: 0in; PADDING-BOTTOM: 1pt; BORDER-LEFT: medium none; PADDING-TOP: 0in; BORDER-BOTTOM: windowtext 1pt solid; mso-element: para-border-div; mso-border-bottom-alt: solid windowtext .75pt">

style="BORDER-RIGHT: medium none; PADDING-RIGHT: 0in; BORDER-TOP: medium none; PADDING-LEFT: 0in; PADDING-BOTTOM: 0in; MARGIN: auto 0in; BORDER-LEFT: medium none; PADDING-TOP: 0in; BORDER-BOTTOM: medium none; mso-border-bottom-alt: solid windowtext .75pt; mso-padding-alt: 0in 0in 1.0pt 0in"> class=commentbody>– “Always explicitly
add a default constructor to avoid versioning issues”. This text is also in the
library design guidelines in the .NET reference manual, and I first interpreted
incorrectly: I thought I should add a default constructor ALWAYS, even if I
already had a constructor in place which accepted parameters. Of course that’s
not necessary.
Yup – you are totally right, I will look at cleaning up
the guideline here.


class=commentbody> 


href="http://www.msjogren.net">Mattias Sjögren suggestions:
class=commentbody>In the static classes
section, it would be useful to mention the VB.NET Module type. Modules
automatically gives you the behavior of a static class, except that it has no
constructor, not even a private one, which I’d argue is even better.
Yes,
you are right, in fact C# is adding support in a future version as
well…


style="mso-bidi-font-style: normal">>Use a zeroed state for the default value
(such as: 0, 0.0, false, “”, etc.)
style="mso-bidi-font-style: normal">
Isn’t the zeroed
state for a string null, not “”?
style="mso-bidi-font-style: normal"> style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">


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”. style="mso-spacerun: yes"> 
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. style="mso-spacerun: yes"> The recommendation is to return
String.Empty (“”) in most cases.


class=commentbody> 


Frank has
some suggestions that are valid… I think CBrumme had a blog about MarshalByRef
didn’t he?


style="mso-bidi-font-style: normal">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.


class=commentbody> 

Comments (2)