How to write good code by creating good names, functions, objects & classes?

How to write good code by creating good names, functions, objects & classes?

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.

Write Good Code Using Proper 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 toread your code. 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. Whilecode quality 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 duplicationof 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 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.

Write Good Code 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 writegood code:

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 likeCodeGrip 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 orsmelly in your code. CodeGrip takes a minute to all that, saving you time, money and effort.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Cookie settingsACCEPT

Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are as essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.

This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are as essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.

Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.

Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.

We are launching on ProductHunt tomorrow and we need your supportShow Support