DEV Community

Cover image for Concrete Self-Documenting Code Strategies
Winston Puckett
Winston Puckett Subscriber

Posted on

2 2

Concrete Self-Documenting Code Strategies

Step 1: name things well.

I have three posts on Variables, Functions, and Classes if it interests you. In these posts, I talk about using the context and content of the code to create solid names. Going through the exercise of naming and re-naming code as it evolves is the foundation of self documenting code.

Step 2: Know what to document

Docunent anything that you would need to retain if you were to attempt a rewrite. That is, if the structure of the code changes, what would need to stay the same for the application to be successful?

For a CRUD application, this means documenting individual validation and transformation rules. For a game, this might mean the list of actions a player can make.

Let namespacing take care of the fact that a module is a sub-module of a parent. Dependencies should not have to be explicitly called out to achieve self documenting code.

Use comments when necessary

Complex operations are better documented and reimagined than not documented and rediscovered. Deadlines and incomplete domain understanding sometimes means that it's better to write a comment about what the code is supposed to represent, rather than leaving the comment off and letting the code misrepresent itself.

Don't be scared of writing comments. Be aware that when you run across a comment, it may be a candidate for refactoring.

In Summary

  • Name code appropriately
  • Document the right stuff
  • Use comments when the circumstance calls for it

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →