DEV Community

Cover image for How To Write Better and Quality Code

How To Write Better and Quality Code

Jenuel Oras Ganawed on November 06, 2023

Code quality is a critical aspect of software development, encompassing various practices and principles aimed at producing clean, maintainable, an...
Collapse
 
prasadchillara_55 profile image
Prasad Chillara

Nice article, good to see AI as well. I would add code linting tools as well, that helps to maintain readability and consistency.

Iā€™m kind of disagreeing with comments. They are optional and can be on top of everything else. We need to keep them up to code all the time, otherwise they are dangerous

Collapse
 
jenueldev profile image
Jenuel Oras Ganawed • Edited

agree, I don't like comments either but as I said, comments are like tour guides. This will help other new developers, especially the new Devs coming in. And it can also work like a note, so that if you come back to the code after a long time, at least you know what is for. Comments are really good, and it also gives linting for your IDE:

/**
     * get a list of inactive services and extensions on an account for a given threshold period
     *
     * @param AccountCustomerMap $account The account we want to interrogate
     * @param string $period the strtotime period for the inactive threshold
     * @return array an array containing either the phone number or extension reg name
     */
    public static function inactiveItemsOnAccount(AccountCustomerMap $account, string $period = '-6 months'): array
    {
            // long methods of codes
    }
Enter fullscreen mode Exit fullscreen mode

Just updated the comment section now for example.

Collapse
 
click2install profile image
click2install

That comment is proving the case for no or minimal commenting. If a getInactiveItemsOnAccount method, notice the prefix, did anything but what it says there are bigger issues at play.

Thread Thread
 
jenueldev profile image
Jenuel Oras Ganawed

Sorry for the context, here is a context:

"The function is quite old and is being used in different places. With old developers out, and with new developers on board, we felt it was important to add comments to make things clearer for everyone. šŸ˜Š"

Collapse
 
dsaga profile image
Dusan Petkovic

Good write up, just wouldn't entirely agree on comments part, it depends, but if you can make your code clear enough that it doesn't need comments, the better.

Collapse
 
iamwillpursell profile image
Will Pursell

I agree with writing super readable code so that others can easily understand it, but I still think writing good comments are helpful. I want to make sure that whoever is looking through my code can quickly understand what they are looking at and then intentions behind. Sometimes, over communication isn't a bad thing in my opinion.

Collapse
 
dsaga profile image
Dusan Petkovic

As long as the comment is maintained its fine, keep in mind that comments also need to be maintained when code changes, and nothing actually enforces the comment to be true, so it might be lying

Thread Thread
 
dsaga profile image
Dusan Petkovic

So I would recommend to use them sparingly, when something really needs an explanation, for other stuff tests can be documentation

Collapse
 
jenueldev profile image
Jenuel Oras Ganawed

Agree It depends. I don't like comments either but as I said, comments are like tour guides. This will help other new developers, especially the new Devs coming in. And it can also work like a note, so that if you come back to the code after a long time, at least you know what is for. Comments are really good, and it also gives linting for your IDE:

/**
     * get a list of inactive services and extensions on an account for a given threshold period
     *
     * @param AccountCustomerMap $account The account we want to interrogate
     * @param string $period the strtotime period for the inactive threshold
     * @return array an array containing either the phone number or extension reg name
     */
    public static function inactiveItemsOnAccount(AccountCustomerMap $account, string $period = '-6 months'): array
    {
            // long methods of codes
    }
Enter fullscreen mode Exit fullscreen mode

Just updated the comment section now for example.

Collapse
 
dsaga profile image
Dusan Petkovic

If you can properly type your methods (depends on the language), you shouldn't need those nasty @param @return comments, as they are already defined in the code, unless you're actually "typing" the code with comments :/

Thread Thread
 
jenueldev profile image
Jenuel Oras Ganawed • Edited

Yup, totally agree with you. It's true that well-typed code can reduce the need for comments in many cases. And, comments still play an essential role in programming for several reasons which can be for clarification, documentation, API library usage, maintainability, and collaboration.

Collapse
 
brense profile image
Rense Bakker

It's better to reserve code comments for cases where you need to explain specific business logic/rules. At function level you should almost never need a comment, because the name and parameters should be descriptive enough themselves. If it needs further comments, its often a sign that the function needs a better name, or is trying to do too many things and should be split into smaller functions.

Collapse
 
jenueldev profile image
Jenuel Oras Ganawed

agree, Im going to put this above

Collapse
 
dhengghuring profile image
Ridho

I agree with the comment, this really important for all developer

Collapse
 
iamspathan profile image
Sohail Pathan

practice, practice and practice. Build some good projects.