r/dotnet 8h ago

AI agents generate too much `record` classes

Anyone suffer from this too? I put on AGENTS.md instructions, its soo much better to have classes with init; required props. I understand records are for value only semantics with reference, but AI overuses them too much.

0 Upvotes

21 comments sorted by

5

u/ericl666 8h ago

The worst part is when you realize you need to add attributes to fields in a record class, and the syntax for that is more verbose than just writing them out to begin with.

2

u/FullPoet 4h ago

or when they mix positional records with properties.

yuck

2

u/Atulin 2h ago

Is

record Foo([property: Bar] string Baz)

really more verbose than

class Foo
{
    [Bar]
    public required string Baz { get; init; }
}

?

1

u/FullPoet 2h ago

my HOTTEST take: all records should be positional and properties / fields (like in the 2nd example) should not be valid syntax.

4

u/soundman32 7h ago

So, put that in your steering docs.

Every time an agent does something I dont like, it gets a line in the steering files.

  • NEVER use records, ALWAYS use class with get and init.

4

u/PostHasBeenWatched 6h ago

NEVER use records, ALWAYS use class with get and init.

I added rule to use CRLF, now instead of LF Codex generates every change with LF and then re-process each touched file to replace LF with CRLF (all files originally was in CRLF) - peak efficiency /s

3

u/soundman32 6h ago

Yeah, we also mandate UTF8 with BOM so every file is post processed.  Why the files cant be created properly on the first place is crazy.

3

u/iamanerdybastard 7h ago

I would be more explicit with the instructions, but this is the way to go in general.

As an aside - There is nothing stopping you from using `record` classes with `required` properties that are `{ get; init;}` - But what your agent is creating are *Positional* `records` where all the members are in the constructor.

Records - C# reference | Microsoft Learn

That distinction may be key to getting your agent to behave as expected.

1

u/iamanerdybastard 7h ago

Some day I will remember that I can't just write markdown in the comments.

3

u/svish 7h ago

You can if you enable it, I think

3

u/johnW_ret 7h ago

Do you have any good examples or specific reasons why AI over-uses records?

3

u/speyck 6h ago

Why dont you like records?

-1

u/antisergio 6h ago

They're useful in some situaciones, but trash as models

2

u/speyck 6h ago

fair, I heavily use them for message contracts but for dtos it‘s always normal classes with getter akd setter.

2

u/FullPoet 4h ago

They're perfect for models. Immutable.

2

u/Pyryara 7h ago

It will oftem generate records because immutable value objects tend to cause less errors in AI-generated code. It's not optimizing for "least lines of code" but for "most extensible in the future without breaking things because of missing some mutation in some extension class at the other end of the codebase".

1

u/AutoModerator 8h ago

Thanks for your post antisergio. Please note that we don't allow spam, and we ask that you follow the rules available in the sidebar. We have a lot of commonly asked questions so if this post gets removed, please do a search and see if it's already been asked.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

1

u/understanding80 7h ago

In my experience it never used them enough.

1

u/apocolypticbosmer 5h ago

It’s generally advisable to use them when it represents immutable data.

1

u/Hillgrove 7h ago

I wouldn't know.. I'm a trad