# Create a codemod - Part 1 - Why?


So, you've got a nice big codebase full of code you're _reasonably_ happy with.

Great!

But, of course, it's _rotting_.

If you've been coding with JavaScript (or most other languages, to be honest)
for any amount of time, you'll have noticed that habits and best practices
_change_ over time. What was a cool way to do something six months ago isn't
quite so cool any more:

-   maybe there's some new syntax in JS or a new Babel plugin
-   maybe there's a new library that makes it easier
-   maybe one of your libraries or frameworks has introduced a cool new feature

What's stopping you adopting those new features or practices? Most likely it's your existing codebase.

Maybe you want to turn on a new eslint rule across your codebase, but you currently
have a lot of violations. You could mark them all as excluded so that you can
apply it to new code, but that old code simply isn't going to fix itself.

You _want_ to improve your codebase, but as you get more and more of it, it feels harder and harder to change it.  So it starts to fall behind.  That's the _rot_.

# Codemod it!

A codemod (_code_ _mod_-ification) is a tool that automatically applies a set of changes to an entire codebase.  So that little thing you'd like to add to your codebase that would make _everything better_ suddenly becomes something you could actually consider.

## Should I write a codemod or just make the changes by hand?

This is a great question, and the excellent XKCD comic has addressed this 
[on](https://xkcd.com/1205/)...
[several](https://xkcd.com/1319/)...
[occasions](https://xkcd.com/1445/).

It's a simple equation:

1.  How long will it take you to develop the codemod? (Let's call that time `D`.)

    Note that, like any code, getting something simple and basic working won't take very long, but making it cope with all the edge cases once it hits the reality of your codebase might increase this time.

2.  How long will it take to make the changes manually, without the codemod? (Let's call that time `M`.)

    Note that if this is an error-prone modification to make, the cost will be higher.

Is `M > D`? If so, the codemod will be worthwhile. If not, it's probably not going to be worth your time.

Writing a codemod might feel cool, but if you're only going to run it once, you either need to be able to write codemods quickly or your codebase had better be large enough - or you need to share your codemod with enough other people - to make it worthwhile.

So, as you can tell, the more efficiently you can write a codemod, the more value you'll get out of writing codemods.

That's _why_ I've written this series of articles.

# Summary

We've reviewed what a codemod is, why you might (or might not) want to write one. In [part 2](codemod-tutorial-part-2-how-ckd7v8uyi00ovses15sv4hkmc), we'll start to look into what they involve. Until next time!

