Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
November 22, 2021 09:52 pm GMT

Why is good naming so important?

I started to feel bad about torturing my fellow colleagues to have better function, variable and class names and also easy to understand error messages.

If you can think of good names for every piece of your code I believe that you also understand the domain of the problem, you can think in actions and you can deconstruct a long flow into a logical set of them.

So by spending time on naming your variables, functions and classes you also have to think on a higher level, mapping out what you are really doing by verbalising it. And that is a great detector of uncertainty and half-baked ideas.

Examples

Super generic name

import { hosts } from 'config';export function serverhost() {  return hosts[1];}

Is it an action to host a server? A variable that return a hostName of a server? Is the returned value host an object maybe which has IP address as well? Why do you return only the second one?

Does way too many things

import { server } from 'my-fav-server-lib';export function getHostNameAndLogErrorAndSetItAsWellIfItIsNotDefinedAlsoStartServer() {  const hosts = server.getHosts();  if (hosts.length === 0) {    hosts[0] = server.start().getDefaultHostName();    console.error('Hosts was empty! Starting a new server instance.');  }  return hosts[0];}

Why are we starting a server in a getter? Why are we getting its host name?

Summary

Exercise your mind by naming things well and do not be afraid to split and reorganize code if you struggle with a good name. Also your colleagues can help you finding the right words if you are stuck!


Original Link: https://dev.to/latobibor/why-is-good-naming-so-important-4k52

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To