Trade Secrets Of A Developer / Entrepreneur

Code Folding Is A Design Error

Call it “code collapse”, “code folding”, “regions” or whatever your editor says it is called… just don’t call it a feature when it does little more than hide the monsters lurking in your code base.

You Shouldn’t Need Code Folding

The most common use case for code folding – well, more likely the reason it was created, in the first place – is to hide large chunks of code in your file.

Yes, that’s a single file with 747 lines of code, comments and formatting in it. I added a code fold just for this post, to illustrate the common use case: hiding monsters.

It’s a nightmare to work in this file, which is ultimately the problem that code folding allows.

The File Is Too Large

If you find yourself needing code collapse, your code is probably too large and needs to be split up into smaller chunks, potentially across more files.

The screenshot above is a perfect example of this.

In my haste to get things working, I didn’t bother to examine the reasons for the existence of certain pieces of code, early on. I didn’t realize that I was putting a lot of view-model formatting and similar code into this file. I was just putting things in there because it was easy and convenient to have everything in a single model / file.

But over time, this file became more and more difficult to work with. I found myself needing to create specific view-models for specific screens, but unable to do so. I had so much code baked into this one file, that I couldn’t see a way to split it up when I needed to.

I have dozens of view-specific models, multiple paths of code execution, features from different parts of the app and more, all crammed into this one file.

And this one file has become a nightmare; a garbage dump for anything related to this model.

You Won’t Fix Problems That You Don’t See

It’s in our nature to ignore problems that we don’t see, even when we know they are there.

Code folding allows us to do exactly that – to have files with hundreds upon hundreds of lines of code, and not think about it because we’re not seeing it.

But what problems are hiding in that massive file? What is in there that you don’t know about because you don’t want to open it up and look?

Stop Code Folding And Face Your Nightmare Head On

If you find yourself looking at a file like I’ve shown above, turn off your code folding regions. Just disable the feature entirely, in your IDE.

When you face the problem without hiding it behind a folded region, it becomes important; it becomes a problem to be solved, and it should be solved. It may not happen right away (probably won’t – we all have deadlines). But having the problem exposed will give you a constant reminder of the need for a better solution.

The hope of having the massive beast of code exposed is that it will eventually be cleaned up. One day, you’ll be so tired of looking at it and sifting through hundreds or thousands of lines of code, that you’ll actively seek ways to fix the problem.

Face the nightmare of code in broad daylight. Force yourself to find a better way forward.

About Derick

Derick Bailey is a developer, entrepreneur, author, speaker and technology leader in central Texas (north of Austin). He's been a professional developer since the late 90’s, and has been writing code since the late 80’s. Derick has built software for organizations of all shapes and sizes, including contributions to Microsoft's MDSN library, running several very highly regarded open source projects, creating software solutions for large financial organizations, healthcare orgnaizations, world-class airlines, the U.S. government, and more. These days, Derick spends most of his time working on content for his own entrepreneurial efforts at WatchMeCode.net, playing video games when he gets a chance, and writing code for for his few remaining clients. You can reach Derick at DerickBailey.com or on twitter, @derickbailey.