{"id":246568,"date":"2016-10-21T06:08:18","date_gmt":"2016-10-21T13:08:18","guid":{"rendered":"http:\/\/css-tricks.com\/?p=246568"},"modified":"2016-10-22T16:28:00","modified_gmt":"2016-10-22T23:28:00","slug":"on-style-maintenance","status":"publish","type":"post","link":"https:\/\/css-tricks.com\/on-style-maintenance\/","title":{"rendered":"On Style Maintenance"},"content":{"rendered":"
I was talking to a brilliant engineer friend the other day who mentioned they never get to build anything from the ground up. Their entire career has consisted of maintaining other people’s (often quite poor) code.<\/p>\n
In a perfect world, we’d all get to write code from scratch, it would work perfectly, and we would put it into a bin in the sky, never to be looked at by anyone again.<\/p>\n
We all know that’s not how it works. Code need to be maintained<\/em>.<\/p>\n <\/p>\n Kyle Simpson often begins his talks explaining that we spend only 30% of our time writing code, and 70% of our time maintaining it. Being a good coworker and programmer is not just about being a skillful problem solver, but being legible. Great developers produce code with maintenance in mind. <\/p>\n I often joke that I don\u2019t want to hire a code ninja. Ninjas come in the middle of the night and leave a bloody mess.<\/p>\n I want a code janitor. Someone who walks the hallways of code, cleaning up pieces, dusting up neglected parts, shinning up others, tossing unnecessary bits. I prefer this gentler, more accurate analogy. This is the person you want on your team. This is a person you want in your code reviews. <\/p>\n Shoutout to JD Cantrell<\/a>, who is an awesome code janitor and code reviewer.<\/p>\n In programming there are many tools and procedures available to meet the same end. There is no right answer. Here is a succinct definition by Michael Feather of two very popular programming paradigms:<\/p>\n Object-Oriented Programming makes code understandable by encapsulating moving parts…<\/p>\n Functional Programming makes code understandable by minimizing moving parts.<\/p>\n<\/blockquote>\n Let’s consider both in terms of not just understandability but also maintenance.<\/p>\n I have seen for myself the value of using a functional approach in front-end JavaScript. When we write code, sometimes we want to assume that it will be used forever the way we intended, but most of us have been doing this long enough now to acknowledge that that isn\u2019t always the case. <\/p>\n Functional programming includes, but is not limited to:<\/p>\n I believe functional programming can be extremely useful in terms of maintenance because of the lack of side effects. This is huge. This keeps our code from becoming brittle. People sometimes think the biggest problem is errors in our code. I\u2019d argue that the worst code isn\u2019t code that errors out and fails. We can track down with methodical isolation. The worst code is code that behaves a way that you can\u2019t predict, quietly, all over the place.<\/strong> You run around the code base playing whack-a-mole and sometimes it\u2019s difficult to find the culprit. A functional programming style takes on this problem preemptively, because it\u2019s built from the ground up to keep this from happening (as much).<\/p>\n Here are some resources if you\u2019d like to dig deeper into functional programming:<\/p>\n As much as I’m in love with functional programming, please be aware that there can still be issues with maintenance. If a few things use the same pure function, and over time you adjust that function for one of its applications, you can get into a problem where you’re also altering other things that are hidden dependencies. Good documentation is really helpful here.<\/p>\n In contrast, Object-Oriented Programming is a little more like following the steps to a recipe. It uses objects, which may or may not contain data, and methods, which tends to be procedural. Typically objects have an idea of self (e.g. “this” in JavaScript). Object-Orientation doesn’t focus on purity, but rather, tends to use encapsulation to make sure nothing leaks into outer scope.<\/p>\n At it’s best, Object-Oriented approaches tend to think of the highest order of something, then pares down to each type of instance you can have, breaking out what to do in each case. If you’re thinking about it like the Linnean System of Classification for animals and how we’d make Morphology Trees (who wouldn’t? :)) You’d start by asking is it warm-blooded? Then, does it have fur? Then does it have a snout? etc. I’m really butchering biology here, but it’s an example.<\/p>\n At it’s worst, Object-Oriented programming gets a lot of pretty necessary criticism because sometimes you’re not clearly describing what you think you’re describing. You can think of this like: you think something\u2019s a banana but really it\u2019s a peach because the only thing that\u2019s described to you is that you have a fruit. We talked a little bit before about how that kind of code can be a nightmare to maintain, so while that’s not always the case, it can certainly come up.<\/p>\nLet’s think about code maintenance through the lens of two different programming paradigms<\/h3>\n
\n
Functional Programming<\/h4>\n
\n
\n
Object-Oriented Programming<\/h4>\n