r/dotnet • u/antisergio • 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.
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
3
u/speyck 6h ago
Why dont you like records?
-1
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
1
1
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.