Sitemap
Jam

Jam

A developer who writes

What makes ‘Haunted’ Code?

4 min readOct 28, 2023

--

I feel an overwhelming sense of dread as Visual Studio begins to load the next solution. Project reference errors begin to trickle down the Error window like itemized drippings of blood. ‘Loading project 1 of 10…’ it scribes in the notification bar. ‘Its slow today, am I on the VPN?’, I wonder. As my machine begins to allocate more and more resources, I hear a louder and louder whine - stemming from my poor laptop fans. The neighbor’s dogs outside begin to howl as the coil whine amplifies. The projects begin to appear out from the dark-themed Solution explorer. Visual Studio slowly recovers from the list of over 200 errors as it finds the courage to load its project dependencies and restore all NuGet packages. The fog clears. Its quiet now — I’m ready to code.

Press enter or click to view image in full size

Files & Structure: Lost in the Labyrinth

Getting lost in a codebase can be like a haunted hedge maze. A poor file structure is often a heuristic for a lack of conventions. When conventions are missing, we tend to reinvent the wheel and create many alternative solutions to many of the same problems.

Lacking of effective file grouping quickly sprouts a disorganized and illogical mess in the filesystem. Developer productivity suffers as a result of this disorganization. Overall, poor structure of files creates a maintenance nightmare for yourself and others. Proper structure can serve as metadata for self-documentation. For larger projects, you should decide early on your grouping — do you want to create feature-sliced folders, or responsibility-sliced folders?

Feature-sliced folders promotes isolation, but encourages repetition. Responsibility-sliced folders promotes reusability, but encourages heterogeneous arrangement.

Create something familiar and stick with it. In the past I have seen folders like:

  • ‘App’ — for UI, Controllers, Message Handling,
  • ‘Business’ — for entities, logic and business rules,
  • ‘Services’ — for DB and API access, and
  • ‘Utils’ — for helper functions and extension methods.

You can’t go too wrong starting with that.

Multithreading: From Hell

The roots of all multithreading evils are race conditions or deadlocks.

Get Jam’s stories in your inbox

Join Medium for free to get updates from this writer.

Race conditions occur when two or more threads access shared resources concurrently and the final outcome depends on the timing and order of thread execution. This can lead to unpredictable and incorrect results in your program. Race conditions can usually manifest when values are caluclated incorrectly in CPU bound work. In IO bound work, you might uncover null reference exceptions as resources are required before they are retrieved.

A deadlock is a situation where two or more threads are unable to proceed because the threads are waiting for the others to release a resource. This can bring your program to a standstill. An apparition of this is when tasks seem to never complete, your program appears to be ‘stuck’ or your UI is frozen.

Unit Testing: Dreadful Suspense

A spectral avatar hands over a black box to you. ‘Business layer’, it whispers. ‘This is yours now’.

A black box without much to signal its purpose or function is a suspenseful nightmare. What will it do? What is it supposed to do? How do I use this thing? Its depth is unknown, a deep abyss. The spectral avatar was a metaphor for your ex-colleague who has now left the company — i.e. their display picture is greyed out.

Untested code is usually unchanged code because we are terrified of opening Pandora’s box.

A lack of unit testing is dreadful because you become suspicious of production code. Its hazy because we lose sight of what the happy-path was supposed to be and we lose documentation of what is expected to happen. We become paranoid and fear changing it. If code cannot be unit tested, consider falling back to commenting the function and purpose of the code is acceptable. Product requirements change all the time, but we need to know what the requirements were at the time it was written.

We have to write code and unit tests under the assumption that none of us, with all our business requirements and context, will still be around in 6 months to maintain it. Unit tests done right will allow us to validate every input, every branch, every output. This is for when you cross over, the undeparted will know what to do.

Code coverage is a good measure of the quality of our code. You don’t need any fancy automated tool suite to determine code coverage. If your tests consider the combinations of inputs, branches and outputs, you’ll be in a better place.

Conclusion

Code can be scary. This Halloween, sharpen your new projects with a cleaner file system, organising by feature-sliced or responsibility-sliced structures. When programming multi-threaded procedures, ensure that you aren’t introducing new synchronization issues through race conditions or deadlocks. Lastly, an inherited project can be daunted when it does not indicate how it should work. Unit tests act as documentation of the happy-path and what the intention of your app code is.

Happy Halloween.

--

--