More on BCL type names


Jeff read href="http://blogs.gotdotnet.com/BradA/PermaLink.aspx/a1ac850b-e21d-4d77-8b93-8832d48af1a1">my
recent post and suggested I include ALL his reasons for using the BCL
name…  Here are they are…


style="MARGIN-LEFT: 0.75in; TEXT-INDENT: -0.5in; mso-list: l0 level1 lfo1; tab-stops: list .75in"> face=Arial color=navy size=2> style="FONT-SIZE: 10pt; COLOR: navy; FONT-FAMILY: Arial; mso-fareast-font-family: Arial"> style="mso-list: Ignore">1. style="FONT: 7pt 'Times New Roman'">                  
color=navy size=2> style="FONT-SIZE: 10pt; COLOR: navy; FONT-FAMILY: Arial">Devs face=Arial color=navy size=2> style="FONT-SIZE: 10pt; COLOR: navy; FONT-FAMILY: Arial"> think there is a
difference between String and string or int and Int32
and there isn’t. For example, I’ve run into some people who think that string
(lowercase ‘s’) is a string allocated on the stack
while a String (uppercase S) is a string allocated on the heap. All kinds of
crazy codes results from this incorrect assumption. = "urn:schemas-microsoft-com:office:office" />


style="MARGIN-LEFT: 0.75in; TEXT-INDENT: -0.5in; mso-list: l0 level1 lfo1; tab-stops: list .75in"> face=Arial color=navy size=2> style="FONT-SIZE: 10pt; COLOR: navy; FONT-FAMILY: Arial; mso-fareast-font-family: Arial"> style="mso-list: Ignore">2. style="FONT: 7pt 'Times New Roman'">                  
style="FONT-SIZE: 10pt; COLOR: navy; FONT-FAMILY: Arial">Code written using the
BCL names can more easily be interpreted/ported by
devs working in a different language. Since the
compiler primitive types exist, devs HAVE to know that
there is a mapping between long and Int32 for C# and long and Int64 for MC++
(for example). So, someone porting code (even just mentally) from one language
to another has to know BOTH compiler mappings.


style="MARGIN-LEFT: 0.75in; TEXT-INDENT: -0.5in; mso-list: l0 level1 lfo1; tab-stops: list .75in"> face=Arial color=navy size=2> style="FONT-SIZE: 10pt; COLOR: navy; FONT-FAMILY: Arial; mso-fareast-font-family: Arial"> style="mso-list: Ignore">3. style="FONT: 7pt 'Times New Roman'">                  
style="FONT-SIZE: 10pt; COLOR: navy; FONT-FAMILY: Arial">The CLR doesn’t know
about the primitive types. So, class=GramE>Type.GetType( class=SpellE>int”) will never work in any language but class=SpellE>Type.GetType(“System.Int32”) always works in all languages.
Many programmers don’t understand why the primitive type name works in one
context but not in another context.


style="MARGIN-LEFT: 0.75in; TEXT-INDENT: -0.5in; mso-list: l0 level1 lfo1; tab-stops: list .75in"> face=Arial color=navy size=2> style="FONT-SIZE: 10pt; COLOR: navy; FONT-FAMILY: Arial; mso-fareast-font-family: Arial"> style="mso-list: Ignore">4. style="FONT: 7pt 'Times New Roman'">                  
style="FONT-SIZE: 10pt; COLOR: navy; FONT-FAMILY: Arial">The BCL has many
methods with type names as part of the method name itself. If “ class=SpellE>br” is a BinaryReader then, in C#,
you can say float value = class=GramE>br.ReadSingle(); which is
technically correct but it feels very unnatural. I prefer Single value = class=SpellE>br.ReadSingle class=GramE>(); which looks and feels right. Again, this makes the code
easier to write, maintain, and understand. Using “float” is requiring the
programmer to know more and just makes things more
difficult.

Comments (3)

  1. Ali says:

    Isn’t there a flip side to the first point? Couldn’t Devs think that System.Int32 is allocated on the heap, while it’s actually allocated on the stack, and indirectly cause excessive boxing and unboxing? Couldn’t something like:

    Int32 i = new Int32()

    be just as unclear?

  2. Fumiaki Yoshimatsu says:

    Should this post be noticed, since Peter is now in the CLR team at Microsoft.

    http://www.razorsoft.net/weblog/2002/07/01.html

    I agree with him.