Why does Visual Studio suggest simplifying names? - IDE0001 Name can be simplified

Asked

Viewed 1,482 times

13

Visual Studio 2013 suggests name simplifications, as you can see below:

SS

The suggestion is:

IDE0001 Name can be simplified Translation : IDE0001 Name can be simplicate

Example:

        var obj = new Object(); //Sugere a simplicação do Object para object
        var outroObj = new object(); //Tudo ok

        var teste = String.Empty; //Sugere a simplicação do nome String para string
        string teste2 = string.Empty; //Tudo ok
        String teste3 = string.Empty; //Também sugere a simplicação do nome String para string

I’ve always seen people recommend using:

string MinhaString = String.Empty;

Now Microsoft recommends:

string MinhaString = string.Empty;

Why this suggestion? What is the advantage?

  • I will let someone with more knowledge in the language answer, but take a look at this question of Soen, I believe it will help to clarify a little your doubt: http://stackoverflow.com/questions/7074/whats-the-difference-between-string-and-string

  • Did any help you more? You need something to be improved?

3 answers

12

String Empty.

First, String.Empty is the same thing as "". Who says it’s different or has advantages, does not know the implementation and is inventing things (has even been different in the past by internal implementation). What one can, is to say that it becomes more readable, more clear that you want a string empty be created there. Some will say it’s easy for you to look at "" and to " " and think it’s the same thing. This I can understand. Although I find exaggeration.

Selection of rules in Visual Studio

You should know that this information can be selectively turned off, right? If you don’t like some rule and don’t want to follow it, hang up. If you want one of your own, you can add it to the VS code analysis system.

Rule of names

There is no difference and it is only a decision that must be made to avoid each one doing his own way. Therefore the rule exists.

I could never find a reason to clearly choose one over the other. As the language preferred to have one alias for the BCL class, it is preferred to use it. Pity that even Microsoft does not follow it in its codes :)

Use of class names

There is a recommendation to use class names when some class or class member needs to take the type name. Example: it is better PrintInt32 and not Print_int or worse, Printint. This makes it easy for other languages that run on top of the CLR to understand what you are referring to since the int is something exclusive to C# and not the platform (but I don’t buy this much anymore). Int32 is the name of the guy all over CLS. That goes for all alias of language types.

In names of local or even private variables is no problem (may be problem in case of reflection, but it is rare to be) and it is preferable to use the name described by the language. In expressions, as in the question example, which is something that does not leak for use in other languages, the option is by the form of the language.

Some may think it would be more consistent to use only the class names since there are cases where it is better. It makes sense, but the option was made back there to make it easier for those coming from C/C++/Java.

  • I honestly think that the aliases get in the way more than help.

  • @Patrick can elaborate?

  • Learning issue, a person who has never seen it will not know the difference between Object and object; besides that, a lot of times, it doesn’t make anything clear what’s behind the alias.

  • That is, it disturbs so much anything else that the person does not know what it is. After she knows, it does not disturb anything. And in fact most people don’t know and program smoothly.

5

Since string is a alias (pseudonym) of String, there is only one advantage: consistency.

The standard is to use the aliases of C# (string, int, long, bool) instead of. NET types (String, Int32, Int64, Boolean)

5

To avoid using the "using System;"

  • 3

    It is an advantage but in general there is no useful code that does not use this.

  • @bigown, Poxa, has a lot of class implementation with get and set who doesn’t need using System;. Examples: Dtos and Entities.

  • @Dherik those are the few I talked about. Even so the gain is minimal. I guarantee that this is not the reason that Fxcop warns this.

  • @bigown, it wasn’t, but I found it interesting to mention that there are many useful classes that don’t need the using System;.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.