6 ms·
A dumber but related habit I've gotten into is that if I want to use AI to do some sort of refactoring on a C# codebase, instead of asking it to edit the code d
by contextfree 2mo ago
A dumber but related habit I've gotten into is that if I want to use AI to do some sort of refactoring on a C# codebase, instead of asking it to edit the code directly I ask it to write a code transformation using the Roslyn compiler API, then run that on the code. The result is less likely to have subtle bugs if it appears to work and gets through a light code review on the transformation (i.e., attempts to cheat with weird special-casing are more likely to stand out amongst the Roslyn API code, and if there isn't such weird special-casing but the code is wrong, the result is more likely to be completely broken rather than subtly broken)
- twosdai 2mo agoThis sounds interesting, I am really naive. I don't code in C#, is there an analogy for other programming languages, like GO, or Python or Typescript? Like are you prompting like: --- I need code that does X,Y, and Z. Write it so that the Roslyn compiler on this machine can compile and the code passes the repo's styling and formatting requirements. --- Or something else.
- bob1029 2mo agoHere's the official docs if it helps https://learn.microsoft.com/en-us/dotnet/csharp/roslyn-sdk/tutorials/how-to-write-csharp-analyzer-code-fix https://learn.microsoft.com/en-us/dotnet/csharp/roslyn-sdk/t...
- kaashif 2mo agoNo, they are talking about refactoring, not adding new functionality to code. So it would be something like: Rewrite this Python code to use match/case instead of if/elif/else chains, write a script using the ast module to rewrite the code, do not edit it yourself, also write some tests with clear inputs and outputs I can inspect. Or something.
- xenophonf 2mo ago> Rewrite this Python code to use match/case instead of if/elif/else chains Is this a real example of something people use AI to do? If so, I don't understand why that's difficult, because prompting the AI to do stuff with ASTs etc. seems a bit over the top.
- kaashif 2mo agoWhy? Are people actually using AI to do programmatic refactors over million line codebases directly? That is far more insane. Using AI to write ruff rules or clang-tidy rules with fixes is literally the same thing and obviously best practice over running AI in a pre-commit hook to do those checks and refactors...
- contextfree 2mo agoRoslyn API basically lets you plug in to the C# compiler or use parts of it so that you can get the same view of the abstract syntax tree, typechecking, flow analysis, etc. that the compiler does. So it's a way to work with C# code at that level rather than just via textual manipulation. I'm less familiar with other languages but I think many have compilers with analogous capabilities, I know Haskell has the GHC API and I think the LLVM-based Clang (C++) and Rust compilers let you do similar things
- rubenvanwyk 2mo agoIs it open source, can you share? Have been pondering using source generators to achieve a similar effect and have the LLM build the source generator ‘framework’ that code would derive from rather than just generating the code itself, you create the code generation framework.