How to Make Your C# Code Prettier
If you’ve stumbled upon this article, chances are you’re seeking a solution to untangle those never ending lines of C# or to iron out inconsistencies in your code base’s formatting. Allow me to introduce you to the world of CSharpier! Think of it as the C# counterpart to the immensely popular Prettier package in the JavaScript realm.
What Is CSharpier?
As described directly on CSharpier’s website:
CSharpier is an opinionated code formatter for c#. It uses Roslyn to parse your code and re-prints it using its own rules. The printing process was ported from prettier but has evolved over time.
CSharpier provides a few basic options that affect formatting and has no plans to add more. It follows the Option Philosophy of prettier.
Why I Use CSharpier
When I landed my first job, I found myself thrust into the world of C# for the very first time. Despite my solid foundation in JavaScript from my college years and initial internship, I had grown accustomed to the sleek aesthetics of Prettier-styled code. Consequently, I found myself yearning for the predictability of Prettier’s styling.
Then I began to wonder: Does CSharp offer a tool similar to Prettier?
One day, as I navigated through a highly inconsistent codebase, I stumbled upon the elegant solution that is CSharpier. It was like a dream come true; not only did it ensure consistency in code readability between my Angular frontend and ASP.NET backend, but it also facilitated smoother navigation. While prettiness may not directly impact performance, I firmly believe that working with visually appealing code enhances the overall development experience. Additionally, consistent formatting reduces the difficulty of switching between files and repositories, making it easier for new team members to dive into the codebase.
Since then, I have enabled it to format on save in all of my IDEs (Visual Studio Professional 2022, JetBrains Rider, and Visual Studio Code).
Here is an example of how CSharpier makes my ugly code prettier:
Why You Should Use CSharpier
You should use CSharpier because it removes the guesswork from code formatting, handling the opinions for you and ensuring everyone adheres to them. You can even integrate it into your CI/CD pipelines to ensure code compliance with CSharpier. Additionally, it resolves whitespace inconsistencies, preventing Git from constantly encountering annoying spacing changes depending on who is merging their changes. By adopting CSharpier, you streamline your team’s workflow, enhance code readability, and promote a more consistent and professional codebase.
Conclusion
In conclusion, leveraging tools like CSharpier not only enhances the aesthetic appeal of your C# code but also promotes consistency, collaboration, and overall developer satisfaction. By adopting automated code formatting solutions, you streamline your workflow, reduce errors, and create a more enjoyable coding experience for yourself and your team.
If you found this article useful, feel free to read my other articles or give me a follow. All feedback and support is appreciated. You can find more information about CSharpier on their official GitHub:
Connect with me on LinkedIn for more updates and discussions!