views:

206

answers:

4

Here's an explanation of the rule that that I am trying to understand. Here's the simplified code that the Code Analyzer was complaining about:

Public Class CustomerSpeed

    Public Enum ProfitTypeEnum As Integer
        NotSpecified = 0
        FlatAmount = 1
        PercentOfProfit = 2
    End Enum

    Private _ProfitTypeEnum As ProfitTypeEnum

    Public Sub New(ByVal profitType As ProfitTypeEnum)

        _ProfitTypeEnum = profitType

    End Sub

End Class

If the enum pertains only to the class, why is it a bad thing to make it a contained type within the class? Seems neater to me...

Does anyone know what is meant by the following line?:

Nested types include the notion of member accessibility, which some programmers do not understand clearly

Using Namespaces to group the Class and Enum doesn't seem like a useful way to resolve this warning, since I would like both the enum to belong to the same parent level as the class name.

A: 

I guess it indicates that nested type can be confused with static variable because they both will appear in intellisense (auto complete) after the ".".

Even if you see BCL (Base Class Library) of .NET, all the enums, even if they are only used in one class, they are not nested, reason is, when you want to compare or instantiate, the class names + "." + enum names will be confusing as shown below, also Microsoft is right that it will confuse people.

-- Example

class RootClass
{
   enum NestedEnum
   {
      StaticItem = 0     
   }

   static string NestedString = "";
}


RootClass.NestedEnum <-- represents enum,
RootClass.NestedString <-- represents static variable

One is a type and other is variable, most MS Code Analyzer warnings are for better design, however it has an big exception for @George, if you dont like it, dont use it, just go ahead and disable the warnings. You can certainly use big class names instead of namespaces, its simply your choice. But good programming skills is about how others percieve your code rather then what you like !!!

And namespaces are there to organize and by organizing we type less, thats what all about doing more work with less efforts. But if you like big names to type, no one is stopping you.

Akash Kava
Bigger to type? Man, the .NET framework is a huge tree of namespaces. Why use name spaces at all when this causes one to type more? The answer is, I would think, "To organize them so you can find stuff!" Why are enums in a class any different? Their recommended solution is to use Namespaces to organize. This approach doesnt reduce the amt of typing required but for some reason it is "preferred." So, on the typing point your making, I don't buy it. As far as confusing the nested type with a static type--not sure i understand. By static ttype, do u mean,eg, a constant? The enum is a constant!
Velika
Yeah, I realize I can disable it and I am questioning this and using Code analyzer so that i can improve my programming skills. Did you see my screen cast? In VS2005 intellisense did the typing for you. I dont get you pt about enum vs. static var. As with any method, you have to look at the return type of the function, which is displayed in the tooltip. You can have a function the RootClass Class that returns yet anotehr object type, would this be confusing and we should warn against it? Heck no. MS just needs to make the intellisense work like it did in ,my posted screen cast. It's a useful..
Velika
..feature to embed. And their solution solution is to use namespaces, so your point about typing too much doesn't hold up. I suspect your probably right (just cause I am in the minority here) but I don't think you made a good case or I am misunderstanding.
Velika
@George, please do not take anything personally, we are only here to give opinons and help people understand, I am just trying to give little more detailed description of the warning, yes VS intellisense does work as it is supposed to, Warnings are not blocking errors, You can ignore warning and also in certain extreme cases it is not bad to ignore warning, when you do not have any other alternative. Lot of time when we look at someone else's code to solve bugs, that time these rules do help us in understanding and resolving issues faster.
Akash Kava
Personally? Heck no! I'm just guilty of enjoying a good debate a little too much and having fun in the friendly "confrontation" I realize this is probably the end of the debate but I have one short question. You said "intellisense is working properly." Is which version 2005 or 2010? See my video. I say 2005 is better (in this case): http://screencast.com/t/ODBiNDJl
Velika
+2  A: 

Enumerations are not usually placed in the class that uses them, so people are not used to having specify where the enumeration is:

Dim speed As New CustomerSpeed(CustomerSpeed.ProfitTypeEnum.FlatAmount)

Placing the enumeration outside the class makes it easier to use:

Dim speed As New CustomerSpeed(ProfitTypeEnum.FlatAmount)

The enumeration is still contained in the same namespace as the class. As the analysis explanation points out, you should use namespaces to group public members rather than nesting them in each other.

Guffa
Well, I like your answer better, but I guess I don't agree with you and MS that this embedding enums in a class is bad because it will confuse people that are not used to having enums embedded in classes. I think the solution is to make it obvious that the enum is embedded (if that is the case) rather than advsing against it. It looks to me as those intellisense for enums is not automatic anymore. For example, in the 1st line of code you typed where u instantiate a new CustomerSpeed obj, regardless of whether the enum is embedded or not, intellisense does not present a narrow pick list
Velika
..of only enum values. Although the intellisense shows you the qualified namespace heirarchy of the param expected, it could also help you by providing a pick list of only values of ProfitTypeEnum type. It looks to me as though Intellisense took a step backward with VS2010 (in this case, generally improved)
Velika
Good intellisense (in addition to the param tooltip info already provided) would solve the problem with the user not expecting to qualify the enum. HEY...Just try this is 2008. I am pretty sure it works in 2008 as I describe it should in 2010.
Velika
Yep. I was right: http://screencast.com/t/ODBiNDJl
Velika
Putting an enumeration in a class may seem like a good idea, but you are creating a pattern of isolating members that you can't keep consistent. An enumeration that is only used in one class can be placed in that class, but an enumeration that is only used by one method can not be placed in that method.
Guffa
A surprising twist. I would say that this is a limitation of MS's implementation of the language, not with the concept of whether it is a good or bad idea to allow for more localized declaration of a type. In theory, I would say that allowing a type to be local to a method would be rare but a good thing, since I would maintain that you would always want to narrow your scope as much as possible-hence my desire to make the enum more local.
Velika
Thanks for your thoughts. I enjoyed it.
Velika
Answer award for effort, I stubbornly remain unconvinced.
Velika
+2  A: 

MS screwed up with the Code Analyzer. You make perfect sense George.

And by the way, my reputation isn't really 28, it just rolled over a couple of times.

Velika2
+1  A: 

In addition to the useability and discoverability issues already covered in other responses, there is also a potential maintainability issue. What happens on the day that you discover that your enum is also potentially useful elsewhere? Moving it out of its parent class would be a breaking change, copying it would introduce its own maintainability problems, and requiring API consumers to use it with another class starts to get ugly. Why not avoid these potential problems by systematically avoiding nested enums?

Nicole Calinoiu
As you know if you read enough of what I wrote, I didn't buy the discover-ability or usability argument, because with Intellisense working as it does in VS2005 rather than 2010, I didn't think either was an issue. But your new pt, I absolutely agree. I did consider it briefly on my own but lost focus since it wasn't brought up by anyone. I must admit don't have a defense for that argument and don't expect to have one later, so... I am convinced on this basis alone. That seems to me to be enough reason not to embedd enums in classes. Thanks!
Velika