Wednesday, February 4, 2026

CodeMaid, a Visual Studio extension for cleaning up & simplifing code

on today's visual studio toolbox we're
going to take a look at code made a
visual studio extension that cleans and
simplifies your code you are not gonna
want to miss this one hi welcome to
visual studio toolbox I'm your host
Robert Greene and joining me today on
skype from Indianapolis is Steve
Cadwallader hey Steve how are you doing
great how are you excellent steve is the
author of code made which is a visual
studio extension that he's going to show
us it has more than a million downloads
and it's very well received and Steve
you're going to show us all about this
extension and all the wonderful things
it does tell us a little bit about why
you wrote it and when did you write it
sure yeah I started writing it in 2006
and first published it in 2007 I wrote
it because I was very particular about
the way the code is organized and
cleaning up whitespace and little things
and like most developers got tired of
doing things automatically and I kind of
got into it from a friend Eric Potter
who was starting to work on Visual
Studio macros I saw what he was doing
there and I started with an add-on and
eventually moved up to an extension cool
and it's available in the marketplace
people can get it we'll have a link for
it in the show notes all right show us
what it can do great
so yeah to get started with code made I
recommend going to our website code made
dotnet from there you can see a
high-level overview of the features I
mean go down into a deep mail detailed
documentation as well as a YouTube video
demo walk-through we also have a blog
and you can see some information about
getting started this is an open source
project so I love contributions over 25
contributors so far and that's been
great you can also of course go to the
visual studio marketplace like Robert
mentioned download it look at reviews
and we do all of our work in give-up
that's we can file issues or grab the
source code no excellent so if somebody
wanted to know what it takes to write an
extension like that they can just go to
github and poke around in the code yeah
absolutely they never put a few other
extension authors mm-hmm excellent great
so I can start diving into some of the
features hop over
visual city a solution so the
bread-and-butter or code made where it
started out is code cleaning so the idea
is taking them code that's kind of just
on the page and making on a bit more
cleaned up automatically this is really
just saving you from having to do these
things yourself
so for example if we look at this file
we can see we have unused using
statements we have the using statements
out of order we have classes and methods
that don't have an access modifier on
them so you don't know if they're
internal or private by default we have
regions that don't actually have any
content we've got extra white space at
the front of the method extra white
space between lines extra white space
between arguments at the end of the
lines and so on and so forth and so what
I like to do show you everything through
a context menu you can use in Visual
Studio so just right-click on the
document there's code made where you
have all of our different commands
available as an aside everything has a
keyboard shortcut but all of course do
everything to the menu so you can see
what I'm doing
so forget we'll just clean up back to
the document and you can see well I'll
go ahead and clean up everything for you
so you can see the using statements are
in order the access modifiers been added
a bunch of white spaces and been removed
and standardized we do this by
leveraging some of the existing
functionality that's inside Visual
Studio so for example are moving and
sorting using statements is now natively
baked in we started back in Visual
Studio 2005 was our first integration
before that ever existed obviously
leveraging and gladly using anything the
Visual Studio starts to build in
natively so does that just mean less
code that you have to maintain yes
absolutely which I'm all for yeah I
won't mind if some day everything is
just baked in natively
likewise for formatting document a lot
of the argument level whitespace and
there's something that's already built
in to do the visual state videos format
document below there's stuff around
whitespace and things like that and so
you can do that mainly in one file at a
time a lot more powerful though as you
can turn on automatic cleanup on the
safe so every time you save your file
will automatically step in and will
clean up the file for you before it gets
written out to disk that's a real nice
way to just kind of turn it on and
forget it
yeah that is all I default as people
don't get too surprised but that's we
did one setting I recommend flipping
once you get comfortable with the tool
you can also do code up clap cleanup
across a whole bunch of awesome one so
for example you can clean up all of your
open documents you clean up your entire
solution or you can also pick any level
inside the solution Explorer right-click
and clean up the selected codes so for
example if you just want to clean up
that specific project if your project
inside a bigger solution you can do it
that way too
so all does the entire solution and if
you want to do one project at a time you
do it at the project level yeah you can
do it
folder levels or anything that's
appropriate okay cool that's the part of
the cleaning fix t-shirt next big
features we call the digging feature so
the idea is you have this file and you
want to kind of get an overview of what
you're looking at so we've got a tool
window I'll call it code mate Spade
because it digs through code that I'll
show you kind of a visual overview of
the file so for example we can see our
constructors our properties methods so
on and so forth you can click on
anything to navigate to it for example I
click on this event a my editor window
they'll jump to that file sorry jump to
that event you can double click on
something to highlight all the codes so
I'm gonna put on the clipboard
do something like that you can middle
click on regions and will synchronize
back and forth between the two whether
they're open there collapse so those are
all kind of useful mm-hmm one other
thing I like to do is you come into a
file you're not real familiar with and
perhaps things are kind of scattered
across first you just kind of want to
look at them in a standard order so you
can actually even though we sort by file
order by default you can switch the way
you view so you can view all the types
grouped together without affecting the
file so you can see all of our methods
have been grouped together and put in a
logical order mm-hmm or you can just do
a straightforward medical sort too if
you just want to look at it that way if
you know exactly what you're looking for
one other way that's handy to search is
by just using the search window so if I
know I'm looking for something a bird
path in it I can just type that in it's
just like solution Explorer is using the
same API they want to find the thing
looking for jump speak to it mm-hmm
so those are all useful and something
else that it's really helpful is you can
do drag and drop to reorder of your code
so for example I've got this is
initialized probably probably filled at
the bottom of the file style cough
convention
that's at the top of the file so you can
click and drag to move that up to the
top of your file and one of those code
numbers around for you that is cool
yep and one step further than that is
you can do multi select so for example
if I want to take everything in this
region I can either shift click or
control-click just like the Windows
Explorer and drag those items are around
so say I one of those methods to both be
at the top of the file for some reason
you can see that I can do that real
quick through there as well couple other
items like finding references and our
routing regions things like that that
are hooked in as well and then you can
delete methods from there as well or
member excuse me yep-yep regions as
popular one people don't like regions a
lot of stuff that's there as well
does that get rid of everything in the
region or just the region identifier it
gets rid of everything that's in the
region cool um so you can do that
manually through the digging window next
step forwards from there is you may want
to kind of just set that up on auto run
right you don't want to mainly drag
those items around so we have a another
feature called reorganizing so this will
basically do that automatically for you
so for example you can see we have this
file where the fields are scattered
across the properties are scattered
across methods scattered across so we
just want to kind of start the freshly
you cannot go in right click and come in
to reorganize active document just you
know see that we've moved all the fields
to the top of the file followed that by
the Constructors delegates events
properties so on and so forth so that's
a nice way to kind of clean up a file
that you're going to start over with or
just been looking into already and you
have the ability to set a custom order
for the Oregon is absolutely yep so yeah
I'll get into options a little bit later
go ahead and show that but it defaults
to the style cop guidelines right cool
and one other feature I didn't show in
the other window with is the ability to
do we have these tooltips to show up so
for example if you mouse over something
will show you any XML comments
associated with it will do a make a
complexity calculation for you so you
can see if something this kind of
complex that's what will give you like a
little
Fyers is a static tree dried things like
that this little queues for you to know
just kinda working through the files
yeah and that shows what those icons
were over to the right yeah again so
yeah these are just trade out individual
studios icons so the same things you'd
see like an outline window or in that
Explorer one runs right the 15 the one
the two yeah sorry so that 15 is a
mccabe complexity score okay my own
rough calculation that's not perfect but
close pretty close so you for example
you see this method has a whole bunch of
or conditionals that's triggering a high
mccabe complexity score within that
method and i kind of highlight those
you can configure what the thresholds
are I think Aven 10 is kind of like a
warning level and 15s in alert levels so
you kind of see where's the meat of your
file and he kind of look at something
right away or if something's getting
kind of unwieldy okay cool
cool so one other hello feature we have
is comic formatting so for example we
turn off word wraps you can see this
comet just kind of goes on and on and on
it's really long the parameters are just
kind of in place there's like space
amongst them things like that you do
have a common formatting feature to kind
of standardize that for you you can
specify you know what com you want to
wrap at which tags you want to auto wrap
things like that yep this little handy
feature needs and then below that a lot
of features are kind of more little
small utilities some simple we have a
join feature so if we look at this
property accessor and we want to join
those lines together can do that very
simply obviously it's very basic thing
to do
yeah you have the ability to sort code
that's natively sooner or later but um
it's basic alphabetical sort on the code
so you can see that when these
attributes were out of order before we
can help the ties it owes
into a certain order if you would like
to and then um hop over to another
session for a couple other features one
other thing we have is when you're
looking in a document you can hop to
that same document inside the solution
Explorer this has been built in
through the sink active documents and
later versions ranked you still have our
shortcut but we actually just turn
around and evoke the native command for
people who have the muscle memory so you
see it'll hop directly to that file
within the solution Explorer and we also
have the ability to collapse at
different levels so if we can't look out
a whole bunch of folders is it a real
thing it's gonna collapse them at a
certain level you can do that you can
also at the top level again that's one
the visual studio is now built in
natively so you can do that as well and
then one more I want to show off is when
you have a large solution waiting for
your build times there isn't a lot of
feedback I think and how far you are in
your builds what's being built right now
there's a building well as a build here
so I have a little tool window I'll show
you your build so example if I go for
you build my solution see your little
build window comes up and show me which
project it's building as it goes usually
as much faster so are you basing it on
the Amana code or how long it takes to
build things how do you make that
calculation so yeah it's coming from the
visual studio does have api's around
built progress events you can know an
individual projects completes or example
that could meet projects built now our
unit testing integration tests can kick
off and build on top of that okay and we
also show the Soto show a green status
or red status obviously as those builds
are running through they also do a
little update inside the windows taskbar
so you can see it down there as well so
it's it's it's based on the number of
projects yes see I have three projects
for code made right so there's a primary
solution integration test and unit tests
so the integration test and unit tests
depend on the primary project before it
can be built so you can see that just
that one project is building right now
and as soon as that other one fires
finishes the other two can start off
concurrently right so you don't have the
ability to calculate it based on the
amount of work the build has to do right
no it's really just project level but
I've had some larger solutions I had
like 120 projects in them it's really
useful for those yeah cool
so then one last area want to show is
kind of the options within code making
and so when you install code made
there'll be a top-level menu right up
here mm-hmm
shows all those different commands we
can also I didn't show removing all
regions that's another popular one you
just don't like any regions in your
files or if you remove selected regions
things like that to go down in the
options window we have a we've really
tried to focus on having the defaults be
pretty good so you don't generally have
to mess with these I'm personally of the
opinion it doesn't really matter what
your coding style is you just want your
code base to be consistent people can
argue a lot about little details but
honestly as long as it's the same that's
usually been my goal so I tried to make
the settings as general for most people
keep them happy but obviously you know
people have a lot of requests a lot of
very specific features that they want to
have so I think we have 170 options or
so trying to keep somewhat managed
within here and we'll go through all
those by any stretch of imagination I
just want to go through a couple of
these pains
at a high level so first one is we have
the ability to have both user level
settings which would be on your machines
stored in your user profile as well as
solution specific settings an example of
this is if you have specific files you
want to exclude from operations so for
example I don't want any of my
integration test data even though it
looks like c-sharp code I don't want it
to be cleaned because of course that's
what the test was supposed to be doing
for me right so you can specify you know
files you want to exclude based on
regular expressions but that's something
that's a good example of a solution
specific setting versus a user level
setting and you can store that just a
code need a config value store right
alongside your solution file and so we
support a whole bunch of languages as
you can see up here I won't listen to
them all c-sharp and VB definitely get
the best support because those have the
most integration with the visual studios
api's scaevola
c++ would be behind that we allow you to
turn on operations even for text files
but it's pretty basic just in the line
white space
duplicate white lines things like that
okay um another high-level feature that
has recently been added from
our community contributors is ability to
turn on or off any one of our features
so if you want to get rid of some
feature you don't want to see it in any
of the context menus you don't want to
hook into the event handlers you can
just turn those off altogether and it
will disappear from the menus pretty
straightforward just registries in most
operations and code made are I don't
until invoked so it's usually not too
harmful I'll leave everything on but if
you just want to clean up your context
menus you're welcome to do that what was
the general impetus for that the this
person only wanted to see the things
that they were going to use regularly
yeah so they thought this menu was too
long they want to be small right and
seem to go for this one where's does
show everything so Sheridan yeah it's a
nice featured app yeah anyway what's
trending sauce so we talked before about
the reorganizing types so yes you can
customize those so I've come down to
reorganizing types you'll see the
default order so if the elds
constructors so on so forth okay kind of
down the bottom did you can do a lot of
customization here so for example if we
want to merge our properties index to
get properties indexers together in this
drag and drop those onto each other mhm
we can rename those to something else we
wanted some support so you can customize
that however you want
that will affect both what you see in
the digging tool window as well as what
the reorganizing functionality invokes
and cleans up and you could have that at
the solution level or the user level
right so if there's a particular project
you're working on where you're using a
different a non-default
or non-standard order you could do that
yes absolutely
okay and then you can always reset to
default very important button right
there right oh yes I like that one
that's a super useful for debugging yeah
you know one last thing I wanted to show
off in here is this we have a little bit
of integration with third-party tools
I'm so JetBrains resharper telluric and
zamel styler mm-hmm these are
deactivated cuz I'm not using any of
them anymore but you can turn those on
and well basically chain or cleanup
alongside there's just like retaining
alongside visual studios cleanup okay
and you can even customize that further
if you have other commands right awesome
so have you
assume you've found over the years that
more and more of the features actually
get baked into Visual Studio um which
then gives you the ability to write less
code and just hook into what Visual
Studio is doing yeah that's been really
useful I think some of the big ones were
of course removing and sorting using
statements yes fault as well as
collapsing the solution Explorer cool so
how difficult was it to write this
extension what advice would you have for
people who were thinking of doing
something similar or potentially are
thinking of looking at your code and
either leveraging it or helping to
contribute how hard is it to figure out
the model for Visual Studio and then
also how hard is it to write an
extension like this yeah that's a great
question and it's very different today
than a decade ago when I wrote Visual
Studio 2005 it was mostly add-ons at
that point I was leaning a lot on Carlos
Quintero had a blog with Visual Basic
examples that was about the main source
of data had mm-hm
back then everybody's on SourceForge
things like that so it wasn't a ton of
available code to refer to nowadays
there's been a lot of push from that
team to really improve the documentation
so there's stuff out there directly on
MSDN I mean there's a ton of example
repos on github okay Matz Kristensen has
a ton of extensions who's done a great
resource snow' leaned on him as well as
he's got some examples of automation
event through that lawyer so you can
have a alpha channel basically a CI
Channel
really easy extension so I definitely
recommend checking out other extensions
there's a good starting point okay cool
so code made awesome tool highly
recommend people take a look at it
download it join the ranks of the
million plus people who have downloaded
it and they're currently using it that's
tremendous
thank you thanks a lot for coming on the
show thank you very much for having me
all right hope you enjoyed that go take
a look at the tool and we will see you
next time on visual studio toolbox
[Music]

No comments:

Post a Comment