DEV Community

Juha-Matti Santala
Juha-Matti Santala

Posted on

5

Is my code self-documenting?

So you know how some people say that "Good code documents itself"?

Is this what they meant?

def self_documenting_sum(a, b):
    with open(__file__, 'r') as source:
        data = source.read().split('\n')

    with open(__file__, 'w') as source:
        if not data[1].startswith('    "'):
            documentation = '    """Sums up two integers"""'
            new_source = data[:]
            new_source.insert(1, documentation)
            source.write('\n'.join(new_source))
        else:
            source.write('\n'.join(data))
    return a + b


print(self_documenting_sum(2,5))
Enter fullscreen mode Exit fullscreen mode

(It's a Python script that adds docstring to its only function on the first run.)

edit: I expanded on the idea a bit in hamatti/self-documenting-code

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)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay