C#: "using" everything too pedantic? - using Generic classes.



  • In my C# source, I try to avoid doing something like:

    <FONT face="Courier New">using System;</FONT>

     

    just to use Guids and a few other things.  Instead I write:

    <FONT face="Courier New">using Guid=System.Guid;</FONT>

    <FONT face="Courier New">using Console=System.Console;</FONT>

     

    I think it helps me understand what I'm doing a lot better and makes the code easier to read since everything I'm "using" is explicitly stated at the top of the source.  My only exception is that I don't know how to use generic classes, e.g. I've tried several variations of this without success:

    <FONT face="Courier New">//I don't know, maybe this exact syntax actually does work, but I gave up a while ago</FONT>

    <FONT face="Courier New">using Collection=System.Collections.ComponentModel.Collection;</FONT>

     

    So two requests, submitted for your approval:

    1. In your opinion, what are the advantages/disadvantages of explicitly using everything?
    2. Is there a way to explicitly "use" a generic class? (I really looked for this one, honest)


  • @Oscar L said:

    So two requests, submitted for your approval:

    1. In your opinion, what are the advantages/disadvantages of explicitly using everything?
    2. Is there a way to explicitly "use" a generic class? (I really looked for this one, honest)

    IMO this is a feature that is really missing in C#, compared to Java.

    1.: Disadvantages: you have to do more typing; code might be less readable.
    Advantages: It's always clear where the class comes from. (but then, the IDE would probably tell you if you asked...)


  • @Oscar L said:

    In my C# source, I try to avoid doing something like:

    <FONT face="Courier New">using System;</FONT>

    just to use Guids and a few other things.  Instead I write:

    <FONT face="Courier New">using Guid=System.Guid;</FONT>

    <FONT face="Courier New">using Console=System.Console;</FONT><FONT face="Times New Roman">

    </FONT>

    I guess my main question is just: Why?  'Using' just the class you want won't save any memory -- and it'll make your source code longer and less standard compared to most other people's C#.  

    -cw



  • The only time I can see where it might be advantageous not to go the "using" route is if your referencing a third-party or custom assembly whose classes are named identically to those in the standard framework that your also "using".  I've never come across this particular case, so I'm not sure how the compiler would handle ambiguous references.  Maybe someone here has and can enlighten the rest of us.

    Larry



  • @CodeWhisperer said:

    @Oscar L said:

    In my C# source, I try to avoid doing something like:

    <FONT face="Courier New">using System;</FONT>

    just to use Guids and a few other things.  Instead I write:

    <FONT face="Courier New">using Guid=System.Guid;</FONT>

    <FONT face="Courier New">using Console=System.Console;</FONT><FONT face="Times New Roman">

    </FONT>

    I guess my main question is just: Why?  'Using' just the class you want won't save any memory -- and it'll make your source code longer and less standard compared to most other people's C#.  

    -cw

    I definitely don't intend to save memory by being specific about which class I'm using.  I haven't really given the issue much seious thought until today - up until now it's just been an intuitive decision.

    Thinking about it now as someone who's learning the Framework bit by bit, I've found it

    1. helpful to have reminders in each file telling me where in the library the thing I was using is
    2. helpful in mentally categorizing all the stuff in System.Web.UI vs. System.Web.UI.WebControls vs. System.Web.UI.HtmlControls and the like
    3. difficult to understand my coworkers' code wherever they included a lot of unfamiliar namespaces in a file
    4. prompts fewer questions from coworkers trying to understand my code... admitedly, they end up having more problems whenever trying to change a codefile that doesn't have all the namespaces they typically put in their source files

    Perhaps what you're suggesting about the length of the resulting source code is that it makes a poor trade-off between space and readability.  Having explicit aliases(there's the term I was looking for) makes the file longer, but so do comments and judicious use of the ternary operator.  The latter two can certainly affect the readability of code drastically for me.

    As to the non-conformity issue, I definitely wonder if I'm the only one out there trying to code like this.  I think the documentation on "using" is the only place I've ever seen that convention outside my own code.

    My final reflection for now on the issue is that I think I started using "using" in this way in part as a reaction to what I saw trying to learn other things like the Java framework and win32 programming.  I vaguely recall looking at one of the win32 header files and experiencing some degree of dizziness at seeing the amount of stuff I had to sift through to find what I wanted to do.



  • @ammoQ said:

    @Oscar L said:

    So two requests, submitted for your approval:

    1. In your opinion, what are the advantages/disadvantages of explicitly using everything?
    2. Is there a way to explicitly "use" a generic class? (I really looked for this one, honest)


    IMO this is a feature that is really missing in C#, compared to Java.

    1.: Disadvantages: you have to do more typing; code might be less readable.
    Advantages: It's always clear where the class comes from. (but then, the IDE would probably tell you if you asked...)

    The amount of typing is definitely somewhat of a concern at times.  I've backed off on the keyboard a few times over the years when I started getting that tingle in my wrists.  That may have had something to do with all the Quake I was playing, too, though.

    That's where I wasn't sure if the advantage was really there.  It makes it really easy for someone new to the framework to find out what's going on, but I may be doing my successors too much of a (dis)courtesy by holding their hands through what they should probably already know by heart.



  • @Oscar L said:

    So two requests, submitted for your approval:

    1. In your opinion, what are the advantages/disadvantages of explicitly using everything?
    2. Is there a way to explicitly "use" a generic class? (I really looked for this one, honest)


    In the System namespace you will find many of the commonly used types like int16, 32, 64, Array, Enum and so on. So I prefer to "use" all the namespaces I need. But I also try to remove any unneeded Using statements so that my code is as readable as possible.
    To make my code readable I try to split my code into small classes with specialised functions. That way I won't need too many Usings and the code is more easily understood.

    What do you mean by explicitly using a generic class? The generic collections are found in the System.Collections.Generic namespace. But other generic classes are found everywhere in the .NET Framework. You can do the following:
    <font face="Courier New">Using intList = System.Collections.Generic.List<int>;

    private class blabla
    {
        private intList myList = new intList();
    }</font>

    Is that what you are looking for?


  • @runegri said:

    @Oscar L said:

    So two requests, submitted for your approval:

    1. In your opinion, what are the advantages/disadvantages of explicitly using everything?
    2. Is there a way to explicitly "use" a generic class? (I really looked for this one, honest)



    In the System namespace you will find many of the commonly used types like int16, 32, 64, Array, Enum and so on. So I prefer to "use" all the namespaces I need. But I also try to remove any unneeded Using statements so that my code is as readable as possible.
    To make my code readable I try to split my code into small classes with specialised functions. That way I won't need too many Usings and the code is more easily understood.

    What do you mean by explicitly using a generic class? The generic collections are found in the System.Collections.Generic namespace. But other generic classes are found everywhere in the .NET Framework. You can do the following:
    <FONT face="Courier New">Using intList = System.Collections.Generic.List<int>;

    private class blabla
    {
        private intList myList = new intList();
    }</FONT>

    Is that what you are looking for?

    That's exactly what I was looking for.  Thanks!

    I think I may settle for your approach to just minimizing the number of namespaces I use.  CW had a good point that using individual classes, whether or not it's a Good Thing™, will probably be confusing to people who expect an entire namespace to be included when they see one of the namespace's members in a piece of code.



  • @ammoQ said:

    @Oscar L said:

    So two requests, submitted for your approval:

    1. In your opinion, what are the advantages/disadvantages of explicitly using everything?
    2. Is there a way to explicitly "use" a generic class? (I really looked for this one, honest)

    IMO this is a feature that is really missing in C#, compared to Java.

    1.: Disadvantages: you have to do more typing; code might be less readable.
    Advantages: It's always clear where the class comes from. (but then, the IDE would probably tell you if you asked...)

    If I understood how it works, it also prevents the cluttering of the local namespace, which I always consider a good thing.



  • @masklinn said:

    @ammoQ said:
    @Oscar L said:

    So two requests, submitted for your approval:

    1. In your opinion, what are the advantages/disadvantages of explicitly using everything?
    2. Is there a way to explicitly "use" a generic class? (I really looked for this one, honest)

    IMO this is a feature that is really missing in C#, compared to Java.

    1.: Disadvantages: you have to do more typing; code might be less readable.
    Advantages: It's always clear where the class comes from. (but then, the IDE would probably tell you if you asked...)

    If I understood how it works, it also prevents the cluttering of the local namespace, which I always consider a good thing.

    I've been coding a class that has quite a few aliases at the top, and the list is growing to be a pretty substantial number compared to what I could have done using namespaces instead.  I was thinking, though - if the class starts to use "too many" other classes, whatever that means, wouldn't that be a good indicator that the class should be broken down further?


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.