You’ve probably seen medical dramas on TV where someone is lying on an operating table, and medical staff with gowns and masks are operating on them. You can’t see most of the person because they’re covered in green sheets, which are called surgical drapes. There’s a gap in the sheets big enough for the surgeon to operate on the relevant bit of the patient, but apart from that gap they’re mostly covered up.
Surgical drapes are a physical barrier to help control infection and are green to show blood (and to be a contrast to white thread used for stitches). The whole of the patient is still accessible, but not without a little bit of effort.
There is an analogue for programmers working on a body of code, and in this article I’ll describe what they are and why you might want to use them.
What do I mean by programming drapes?
Programming drapes are a way to temporarily hide irrelevant bits of code. They don’t change the underlying files, just how they are presented to you by your editor or IDE. There are two varieties that I’ve come across – one is very common, and one I have seen only in Emacs.
The common one is the standard show/hide controls to the side of the main editing area, that expand or contract bits of program. These could be just big things like classes, interfaces and methods, or could also include fine-grained things like individual large statements or statement blocks (like the body of an if statement).
This might seem like no big deal, and instead might be a statement of the bleeding obvious, but it’s an area where it pays to know your tools. Things like Visual Studio let you hide all details in a file with one keyboard shortcut, so that all you see is each method’s signature. (The shortcut in Visual Studio is CRTL-M CTRL-O.) Once you get the shortcut into your muscle memory it becomes trivial to hide all the detail in a file, so that you can then expand just the bits you’re working on.
If you don’t know the shortcut you have to find the relevant menu item or button or, worse still, manually close each method’s details by clicking on its control. Because of the faff involved, you’re much less likely to get the code’s appearance into a helpful state i.e. there will be no drapes on at all.
Emacs lets you go further by defining a region in your file, and then narrowing the file to that region. Until you undo this (i.e. you widen the file again), the parts of the file outside the region are completely invisible – it’s as if they have been deleted. Therefore, you can’t edit those regions and searches won’t find anything in the regions either.
Benefits of programming drapes
The main benefits of programming drapes are to limit these things to just the relevant bits of code:
- Your attention and short-term memory;
- Edits (in a soft way for most IDEs, and a hard way for Emacs style drapes)
- Searches (just the Emacs style).
Your short-term memory is like a level 1 cache for a CPU – the CPU runs faster if everything it needs is there. Because of the limited size of most people’s short-term memory, trying to remember lots means lots of cache misses, that trigger delays as you scroll up and down the code to refresh your memory. Hiding irrelevant bits of the code means it’s harder for the irrelevant code to compete for your attention and hence short-term memory space.
You might think that you don’t need to use drapes because your classes have high cohesion. High cohesion helps, because you know that if you open a class to do with e.g. appointments you won’t be confused or distracted by code relating to e.g. tax calculation. However, your appointments class could still be too long for it all to fit into your short-term memory at once.
Even if you have just the softer style of drapes, you are still less likely to make edits to the wrong bit of code if you use drapes. Simply put – you can’t edit a line you can’t see, unless you’re doing a global operation like find and replace. If the code for a method is hidden behind its signature then it is safe (at least, safer than otherwise – see below). If you have the Emacs style of drapes, then the code outside the region is entirely safe from accidental edits – it’s like a surgeon operating on a patient through a gap in a close-fitting wooden box around them.
Another benefit is that it’s simply quicker to move around the file between the bits you’re interested, because the other stuff is folded up into a smaller number of lines. There are therefore fewer wrong lines to scroll through to get to the right lines.
Drapes are no silver bullet
Just because you manage to edit the code in area A while avoiding making changes to area B by mistake, that doesn’t necessarily mean that B (or A) is now correct. The changes to A might have introduced an inconsistency between A and B.
It is like a surgeon operating on a patient’s shoulder who ties off all the blood vessels there, while the rest of the arm and the body are covered by drapes. Without a supply of blood, the arm under the drapes will die. The drapes haven’t stopped this bad interaction between the different parts of the body (the arm being disconnected from the circulatory system), even though they did help the surgeon limit infection and focus on the correct arm.
Programming drapes are just common sense that needs a one-off bit of effort to get a keyboard shortcut into your muscle memory. They won’t stop you from introducing all bugs ever, but they will make you a bit more productive, and make it a bit less likely that you’ll go wrong.