Code for the next person

So many people think that a program that works is a good program. It might be, but it’s not guaranteed to be.

Those people tend to write their programs for the compiler. The thing is that the compiler doesn’t care if your code is readable. It doesn’t care about wonky formatting. It doesn’t care about convention. It just translates from a human-readable language to something closer to the bare metal.

But that’s only a small part of why you write a program. You’re communicating with the compiler, but you’re also communicating with the next person who interacts with your code.

Just because the compile can work with your code doesn’t mean that other people can.

So, for example, in Java when you have an error, you throw an exception. It’s the style of the language and that’s what people expect.

Today I ran into some code where a function was returning an optional generic of some type. It would be empty if the function had an error. Does it work? Yes. Does it make immediate sense for the reader? No, no it does not.

A large part of writing good code is to minimize any surprise that the next person may have when looking at your code. Use the conventions of the language. Don’t just make things up at random, even if the compiler might let you get away with it.

Next
Next

Multipart AI