Your Simple Coding Guide: Functions and Methods

What makes a good function or method? It's not just one thing, but rather a combination of things where each is significant. If something is flawed, it affects the whole function. So what are these common slip-ups, and how can you avoid them?

It Has a Meaningful Name

Functions should have names that describes their purpose or functionality. When a function has a meaningful name, it's easy to read and understand its purpose.

For example, if a function's purpose is to find a customer by ID, a good name could be findCustomerById(id: String). It could also just as well be findCustomer(id: String) because the function signature implies that the customer is found by their ID. The word find also implies that the customer might be found or it might not be found.

If the function's name would be changed to getCustomer(id: String), its meaning changes because now it implies that there's no fallback; the customer is either found or the function fails miserably and maybe throws an exception.

Both are valid names for a function but they have a different meaning and therefore their implementations should also be different.