3 min read

Using atom-beautify and uncrustify to lint C# files in the Atom IDE

A beautifier is a program that changes source code, making it more readable and “pleasing for the eye”. This way, the code becomes easier to understand. Think of things like consistent spacing, indentation, and code block sizing rules… a beautifier automatically enforces these things when you save a file. So, you get a better-looking file and you really don’t even need to remember a ton of styling guidelines, because the beautifier does that for you!

Nice and clean alignment happens automatically thanks to atom-beautify and uncrustify’s opinions!

Ok, why spend the time setting up a beautifier?

The benefit to implementing a beautifier is more developers will be able to understand and work on your code, and you yourself will have an easier time coming back to parts you may have to revisit. Basically, beautifying your code is a tool to pay down some of the tech debt that comes with the jangly unreadability of quick builds, tests, and solo development.

There’s also a pride-of-workmanship element that comes into play, at least for me.

Unfortunately, there is no hard and fast rule for what “well-formatted code” looks like. It’s not just a matter of having the correct indents and spaces: sometimes you need to break lines in order to make them easier to read. A beautifier needs to implement the rules of formatting that were decided by its developer. Hence, beautifiers can be configured based on pre-existing boilerplates to fit your needs.

Atom-beautify is a very popular, community-driven beautifier that’s installable as a package in the Atom text editor (my favorite at the moment). In this post, I’ll examine how to set up and configure atom-beautify, using the uncrustify config tool.

How to set up a C# beautifier with the Atom IDE

We’ll be using the atom-beautify library by Glavin001 for this example. It supports several other languages and lets you “beautify HTML, CSS, JavaScript, PHP, Python, Ruby, Java, C, C++, C#, Objective-C, CoffeeScript, TypeScript, Coldfusion, SQL, and more in Atom.”

I’m operating on MacOS but the setup process is similar for other systems. Also, my target use case is C# for a Unity3D project.

  1. Add the atom-beautify package. You can install it via the command line or the Atom package install interface (Atom > Preferences > Install… then search for atom-beautify and install via GUI). For more info on the package and specific install instructions, view the docs here.
  2. Choose your settings. In the atom-beautify settings, scroll to C# and configure the “Beautify On Save” option (I choose yes). Set the default beautifier to “Uncrustify.” Uncrustify is a source code beautifier with an easy-to-configure C# config file. More details at the repo here.
  3. Install uncrustify. Oh, we need to install uncrustify so we can take advantage of it! Simply use the terminal to `brew install uncrustify` on MacOS and you’re off to the races.
  4. Set the config path. In Atom, back inside the Beautify settings, set the config path to the desired uncrustify .cfg file, which is what the beautifier will use to format your code. I set my path to ~/.atom/packages/atom-beautify/src/beautifiers/uncrustify/default.cfg … simple enough.
  5. Configure your .cfg file. I copied mine from rindeal’s boilerplate, available on Github at this link. Make yours whatever you want!
  6. Test it. Modify a file and hit save. You should see the file is reformatted to fit your standards.

You should have things looking nice and pretty now.

The consistency that comes with a beautified, opinionated C# project is so … soothing? Plus, having a beautifier working makes me feel much more organized, letting me spend more brainpower on code and less on spaces and formatting. Hopefully, this makes you more productive too.

Next up… writing better commit messages, deployment infra, containers, telemetry, and much more.

Come back soon, and be sure to check out the Discord for my latest project, Spacefreighter, too! If you have any questions about beautifying your C# in Atom, I’d be happy to help you there.