DEV Community

Discussion on: "Do not comment on your code, it should be self-documented". Well... I don't agree.

Collapse
 
davinderpalrehal profile image
Davinderpal Singh Rehal

There are certainly scenarios where writing comments can be useful, though I would also argue that you should not use comments as a cruch. Anywhere your logic is complicated by all means write a comment, but if your logic is not complicated you should focus on giving the write name to your function. For example.

/**
* Applies filters in the following order if they are set
* Date -> Category -> Sub-Category
* @param filterObj {
*    date: asc | dsc,
*.    cat: asc | dsc,
*.    subCat: asc | dsc
*.    }
*. @return filteredList
*/
function applyFilters(filterObj = null)
Enter fullscreen mode Exit fullscreen mode

But comments don't make much sense in the following example

function filterListByDate(isAscending = true)
Enter fullscreen mode Exit fullscreen mode

First preference in my opinion should be to name things properly, use the single responsibility principle as much as possible and then if logic is still complicated then add comment.