How to create good names, good functions, good objects, and good classes?
Tags: qualitycode, codegrip, testing, oop
Published: 2020-02-20 09:07:14

Coding practices that are done right results into good code, which is infact, a combination of various parts of the code being individually excellent. These parts are none other than the names, functions, objects, and classes. We tried to figure out how one can write good code by creating good names, good functions, good objects, and good classes.
 
**Naming Conventions**

Naming classes and methods seem like an easier job so you neglect it, comes back to haunt you and your code in the future. If you are building software for someone else, you approach to code in such a way that customer does not come back with an issue in future. The worst-case issue is that customer comes back to ask you what do you mean by the name of a function. That is the technical debt you added into the system by doing a small but yet heavy mistake.

An average coder spends 75% of the time understanding code, 20% of the time modifying existing code and only 5% writing new. It is important to follow a naming convention that makes it easy and meaningful to [read your code](https://www.codegrip.tech/productivity/how-to-format-code-for-maximum-code-readability/?utm_source=dev&utm_medium=pravin-dev-blog&utm_campaign=how_to_create_good_names_functions_objects_%26_classes). The names should be clear, without ambiguity ad should define what the function or class does. A self-explanatory name will help us to avoid looking into the code or comments for greater detail and know what the developer aimed to do with that function. Follow the steps below to write good names:

● Follow lower camel case: Example: httpsServer, removeItem(), todayDate()
It helps to differentiate between words without introducing any symbol and is thus easy to understand. Some people follow the camel case, which is RemoveItem() or every word is capitalized but as the code is written in lower case in continuity, it’s advised to start name by lower case as well.

● When you are naming a function, remember to add a verb to it as every function is an action. Like getTime() stands for getting time of the event, resetInputs() stands for resetting the input fields.

● Clarity is more important than shortness. Short names are easy to read but compromising it with what they mean is not acceptable. But this also doesn’t mean that you can write names that are irritatingly long. Rather you should use a shorter replacement.

● Avoid names that don’t have meanings like func1(), work(). We understand that the machine does not really care whatever name we put as it does not know the meaning, but it still doesn’t mean we can put gibberish. It may not be a problem for you but if any developer in the future tries to understand, it would be near impossible.

**Effective Functions**

Besides learning the language, developers need to learn how to write good functions. While [code quality](https://www.codegrip.tech/productivity/what-is-code-quality-how-to-measure-and-improve-it/?utm_source=dev&utm_medium=pravin-dev-blog&utm_campaign=how_to_create_good_names_functions_objects_%26_classes) depends on various factors of functions, the one we talk about here is writing effective functions. The points needed to be taken care while writing a good function are:

● Avoid [duplication](https://www.codegrip.tech/productivity/what-is-duplicate-code/?utm_source=dev&utm_medium=pravin-dev-blog&utm_campaign=how_to_create_good_names_functions_objects_%26_classes) of lines. If a line seems to appear more than once, it's better to make it a function and then calling it whenever you require.

● Always follow the single responsibility principle while creating functions. Good functions should do only one and a definite job. Each action and job should have separate functions.

● If it feels like your function is becoming too long and lengthy to read or having too many elements that make it complicated and difficult to understand, it is better to split them. Create sub-functions under a larger function so that editing and reading become easier.

● Always have a well-defined set of inputs and outputs. Your function input must be recognizable and differentiable by the code and output should be sorted.

**Using OOP Principles**

OOP or Object-Oriented programming is easy to follow. The principles are important because it helps you write good objects and good classes. Also, almost 90% of programming is done using this practice. We won’t tell you what your textbooks do but rather mention some important pointers to write [good code](https://www.codegrip.tech/productivity/difference-between-good-and-bad-code/?utm_source=dev&utm_medium=pravin-dev-blog&utm_campaign=how_to_create_good_names_functions_objects_%26_classes):

● Law of Demeter i.e. one never calls a method on an object you got from another call nor on a global object. Will save lots of time while refactoring.

● Methods that have many arguments are just hard to read. Rather use an object that represents the set of arguments required.

● Use the design principle, Inversion of Control (IoC) that is reversing different controls in object-oriented programming with the main aim to achieve loose coupling.
● Use recursion for components that have the same environment. Good code is a compact and good object is when work can be done without breaking a code.

**Write Clean Classes**

Clean classes don’t just mean functionality but also the manageability of code. Creating good classes take efforts that are mentioned below:

● Use proper order for classes: Always begin with a variable list and keep a public variable (unless you need it to be protected).

● Write smaller classes: Smaller is good, but we don’t really object to the number of lines. What we object to is a class having multiple dependencies and responsibilities. Always try not to have any dependencies and also only one responsibility.

● Always write reversible classes so that you can bring easy changes to it after testing.

**Conclusion**
You can use the above pointers to write good code or you can always use an automated code quality tool like [CodeGrip](https://www.codegrip.tech/?utm_source=dev&utm_medium=pravin-dev-blog&utm_campaign=how_to_create_good_names_functions_objects_%26_classes) for this task. The only difference is that CodeGrip is easier, quicker and more efficient to implement. All you need to do is just connect your repositories from GitHub or BitBucket and let CodeGrip do all the magic.                                            
CodeGrip will analyze your code and bring out key metrics like issues, duplications, and technical debt. You can refine your functions and classes using duplication detection. It also suggests you solutions if you write something wrong or [smelly](https://www.codegrip.tech/productivity/everything-you-need-to-know-about-code-smells/?utm_source=dev&utm_medium=pravin-dev-blog&utm_campaign=how_to_create_good_names_functions_objects_%26_classes) in your code. CodeGrip takes a minute to all that, saving you time, money and effort.