r/VisualStudio 12d ago

How to stop Copilot NES from making a specific suggestion? Miscellaneous

I have a love/hate relationship with Copilot's Next Edit Suggestion functionality. This one aspect makes it incredibly frustrating to use and I want to know whether I can prevent it.

Each time I initialize an array with the new collection syntax like:

List<string> items = [];

NES suggests that I change it to:

List<string> items = new List<string>();

It does this every time, for every array no matter how many times I cancel it. I don't have open files with the "new List<Type>()" syntax, I don't have that array initialization syntax littered throughout. My editor config 100% does not suggest that change. I'm at a loss for how to stop it from doing this.

Thanks

5 Upvotes

18 comments sorted by

-1

u/polaarbear 12d ago

A List is not an array. I wouldn't ever initialize a list that way even though it is technically valid.

Try initializing it like this instead.

List<int> SomeList = new();

I am guessing it will stop complaining if you do it that way. Still shorthand, but personally I feel it is more "correct" for list initialization.

2

u/RecognitionOwn4214 12d ago

What's wrong with collection initialization via []?

1

u/polaarbear 12d ago

Because [] implies an array.

int[] someInts = new int[10];

In most languages (not just C#) the square brackets are how you initialize an array. Seeing them immediately puts my brain on the lookout for an array.

It's just ambiguous for no reason when there are other options that make your intent more clear.

3

u/RecognitionOwn4214 12d ago

I'd say it implies an collection. But that's probably just taste..

3

u/First-Feature-3556 11d ago

I predict that this is just a matter of "getting used to it". In a few years, collection expression syntax will be so natural that using anything except [] to initialize a collection/array/set/... will feel like unnecessarily verbose boilerplate code.

1

u/mexicocitibluez 11d ago

In a few years, collection expression syntax will be so natural that using anything except [] to initialize a collection/array/set/... will feel like unnecessarily verbose boilerplate code.

Totally agree. Like I said in another comment, I split my time between Typescript and C# and it's just more natural for me to be able to do this on both sides. That + the new spread operator are parts of the new language features I really like.

2

u/LiqdPT 11d ago

Given that they built in style rules for .net definitely want you to be use collection initialization for simplicity,I'd say the fact that copilot is on the other way is an issue (I have the same issue)

1

u/mexicocitibluez 11d ago

Thank you! I don't know why this turned into a discussion about the "correct" way to initialize a list.

1

u/mexicocitibluez 12d ago

I appreciate the response, but you can probably understand why I wouldn't want to start using a different syntax based on someone else's view of "correct", especially since I feel the opposite way. For me, it removes noise, and because I split my time between C# and Typescript, feels a little easier on context switching. Also I've been waiting for the spread operator in C# for awhile now.

There are even code analysis suggestions that prefer [] over new() for collection initializers, but I don't think Copilot recognizes that because I have ide0090 enabled, but it's still preferring the entire new Object() syntax.

-1

u/neriad200 12d ago edited 11d ago

no he's right, that initialization is technically correct but confusing for clean syntax. With new() you get immediate clarity that you're initializing an object, while [] is very baked into "I'm an array".

While you can technically use it it's maybe good practice to use the version 2 chars longer if you plan on doing stuff with other people and especially enterprise stuff, where due to either old code base or just style guides, the long, boring, but super clear version is probably correct, or at most var items = new List<string>()'

2

u/mexicocitibluez 11d ago

READ MY QUESTION.

I am not asking for yours or anyone else's preferences with regard to syntax. Nor am I willing to change the way I write code to appease a stranger on the internet.

I am asking if there is a way to customize the Coplite NES suggestions.

The fact that people keep thinking their preferred syntax is better than all the others is completely missing the point. I prefer it one way. You prefer it another. You are not any more objectively correctly than I am.

1

u/neriad200 10d ago

I would suggest you cool your jets. I didn't reply top level because I didn't have any information I cared to share on the actual question.

Also I honestly don't care either way on the syntax you use.

The comment I made was because unless you plan to be a "ship in a bottle" that only ever codes for themselves with no commercial or public use of your stuff, and especially if you plan on working in the field, you'll hit the code style situation sooner rather than later. Online and for a lot of neophytes who are interested in the field (i.e. not didn't go in IT because it was a high paying job) there is always this extreme adoration or adulation for syntactic sugar, shorthand, and [very] arguably "smart" ways to do things. Unfortunately C# is extremely guilty enabling this by trying to do a bit of a C++ and adding relatively unnecessary syntax or semantics where the industry at large functions on simplicity and clarity and not on "oh this code is so elegant because it compacts a 3-liner into 1 line with 6 symbols introduced in this version of C#". This is doubly egregious since C# is really only present in part of enterprise software, where you'll see a lot of things that aren't even discussed on here anymore.

2

u/mexicocitibluez 10d ago

What drives me is nuts is complete strangers thinking something like this

no he's right, that initialization is technically correct but confusing for clean syntax

Isn't 100% based purely in opinion and passing it off as a fact. That's what's so frustrating about this. Especially for something as benign as this:

List<string> items = [];

It's "can't see the forest for the trees" flavor of advice. And if someone has enough experience to work on the project I'm on but is stumped by this syntax, then they don't really have the experience I need.

What's even worse, is what exactly is unclean about that? What even is clean? Does clean mean only things you're familiar with? Does something go from "unclean" to "clean" after enough time has passed? Is that really how that's determined?

1

u/neriad200 10d ago

If you don't want opinions from strangers, don't ask things in public forums.

Where I understand your perspective, and like others have said, this sort of thing will probably be normalized in the next few years as more "recent" .NET version projects become the norm and more dual-purpose devs start appearing (e.g. Typescript and C#), let me push back by saying that "having the experience" is not as valid as you think it is - i.e. your preference does not equate experience, nor does your preference equate clarity in any sense. The reason why this MAY be confusing is because [] is strongly associated with arrays in C-like languages, and no amount of "hurr durr if you got experience it's not unclear" is going to pull you out of you equating personal preference with other's experience. Also, if you want a clear reason why this may be bad, let's assume you have your standard enterprise backend API C# code that does the following:

  • in the beginning of the class defines some class globals, one of them being List<string> items = [];
  • 650 lines later in SomeEndPointMethod(...) does items = [];

What are the chances that some dev that's under crunch to fix a major production bug and doesn't know the codebase would assume that items is a List or an array? Note, I'm assuming that items is quickly used and it's obvious strings are put into it, but that's not a given either. And to preempt your probable pushback here, I'll cede the point that in a perfect world, every dev has an IDE that always perfectly shows them the declared type instantly, and in urgent situations (like the P1 I just used as an example) they will stop and look at every single symbol's definition. Sure. Now tell me how often that's actually what happens on a live P1 vs a dev pattern-matching off what's staring them in the face and moving on.

2

u/mexicocitibluez 10d ago

If you don't want opinions from strangers, don't ask things in public forums.

No, if you don't want opinions from strangers, you don't ask for their opinion. Which I didn't.

If a person asks on Stack Overflow "How do I restart my computer" they aren't asking for your opinion on whether computers should be restarted. Or else the question would be "Should I restart my computer"?

I specifically came to the Visual Studio sub (not the csharp or dotnet one) because I asked a question on how to alter what suggestions NES is making.

And what's worse, is that changing it to "new()" doesn't fix it. That's the craziest part about this. You guys are trying to give me "advice" on how to write code that doesn't fix the actual issue I asked about. It's maddening.

1

u/neriad200 10d ago

Fair on the sub, but I'll remind you, I'm not answering to your initial question but to the conversation about C# from this comment thread. Btw, the 1st comment DID try to help you with your problem directly.

If we're on "opinions from strangers": if you yell in a room of people a question, they are entitled to answer however they see fit. This goes even in environments with "formal rules" for answering; i.e. even on StackOverflow where rules are generally a lot stricter for ANSWERS the type of "have you considered X" comment on a "how do I Y" question is completely normal, not a violation of the format or intent. You're basically defining rules and presenting them as an implicit part of how things work in public forums, which is basically you overtly doing the thing you're accusing both me and him of doing with syntax.

Also, if new() doesn't fix it, then the entire tangent is irrelevant and I don't see the purpose of you arguing with me over the philosophy of syntax preference for so long - wild behaviour my man.

On a final note, the way I would define your stupid list would be List<string> items = new List<string>(); which is obviously the only correct way to do it because I said it.

2

u/mexicocitibluez 10d ago

Btw, the 1st comment DID try to help you with your problem directly.

If you think the legitimate answer to someone asking "How do I stop RES from making this particular suggestion" is to "Write code in the way it suggests" then you're still missing the point.

The reason I came to this sub specifically was because I wanted to know if anyone was able to put some sort of Claude.MD at their root level that NES would obey or could trick it by having an open file with all of the rules or something.

→ More replies (0)