Tools

Why Resharper? Code Formatting

While Visual Studio is often made fun of for being bloated, and has more functionality than a single developer could hope to create in a career, it still has a few missing features. This is where Resharper from JetBrains steps in.

In particular today I want to talk about just one feature Resharper has, and many developers don’t use, to both their own and their team’s detriment. Consider this post both overview and entreaty to those who don’t use this feature.

That feature is automatic code formatting.

Visual Studio by default tries to give you a lot of automatic code completion and indention. One of the simplest things it won’t do however, it break your lines as you write large statements. LINQ practically becomes unreadable as variable names become larger and conditionals become more complicated. Methods and constructors that take a large number of arguments become unwieldy. While you can and should tackle these problems with factoring out new methods and variables to contain their incidental complexity, sometimes you just need to see how the brick is made inline.

You can apply automatic code formatting across a whole solution, a file, or as I often do in existing projects, just the hairiest parts of the code you are working on that day. In order to really get the most of your code formatting, you should configure the options to match your team’s coding guidelines, and then save those options in the repository so that they can be picked up automatically both other developer’s Resharper installations. You have these formatting changes be applied silently after you’ve set them up, removing the need to go thru the wizard on each run.

Some settings I particularly suggest you look at are those that handle those automatic breaks after opening a new block or method argument list. Having these being treated more consistently make reviewing changes much more palatable in a diff tool. I’m also a fan of creating automatic breaks in long chained method calls, ternary operators, or string concatenations, keeping operators at the beginning of lines, letting the human reader front load the meaning of any given line. These are just calls made from personal experience. One of my favorite programming books, Code Complete 2, backs many of these positions. While many development coding guidelines might be based on terminal interfaces, it turns out there are plenty of reasons based in science on why humans can’t read lines over 80 characters very well, especially when those lines are full of difficult mathematical logic.

I hope this has whet your appetite for nicely presented code in your projects. Luckily Resharper is the utensil you need to cut into this problem. If you’ve already installed Resharper, or this post has convinced you to try it out, please let me know how your experience with code formatting goes in the comments.

Leave a Reply

Your email address will not be published. Required fields are marked *