# Preferring code over comments

DevFeed: [Preferring code over comments](<https://devfeed.tech/articles/preferring-code-over-comments-52532.md>)

Original publisher: [Read original article](<http://www.jackfranklin.co.uk/blog/prefer-code-over-comments/>)

Published: 2018-02-15T00:00:00Z

Content type: opinion

Language: en

Sources: [Jack Franklin](<https://devfeed.tech/sources/jack-franklin.md>)

Topics: [code comments](<https://devfeed.tech/topics/code-comments.md>), [JavaScript](<https://devfeed.tech/topics/javascript.md>)

Tags: [api](<https://devfeed.tech/tags/api.md>), [code](<https://devfeed.tech/tags/code.md>), [code-comments](<https://devfeed.tech/tags/code-comments.md>), [debugging](<https://devfeed.tech/tags/debugging.md>), [javascript](<https://devfeed.tech/tags/javascript.md>), [knowledge](<https://devfeed.tech/tags/knowledge.md>), [module](<https://devfeed.tech/tags/module.md>), [object](<https://devfeed.tech/tags/object.md>), [query](<https://devfeed.tech/tags/query.md>), [responses](<https://devfeed.tech/tags/responses.md>), [results](<https://devfeed.tech/tags/results.md>), [status](<https://devfeed.tech/tags/status.md>)

## AI overview

Comments can drift out of sync with code and make debugging harder. In the JavaScript API wrapper example, numeric response codes initially have explanatory comments; when a code changes or a new one is added, those comments may become misleading. The article replaces the numbers with named constants in an `API_RESPONSES` object (such as `SUCCESS`, `NO_RESULTS`, and `INVALID_KEY`), so the mapping is defined once and the conditions read more clearly without duplicate comments. It recommends considering clearer, self documenting code before adding a comment, while recognizing that comments can still help when code alone is hard to follow.

## Source excerpt

I think we'd all agree that code comments are a good way to document code that is otherwise hard to follow. Sometimes there's just no way to make the code as clear as you'd like and a comment is a good solution. That said, comments have one large issue: they can get out of date. An outdated comment that is incorrect could cause you to lose a lot of time to debugging. You may set out with the best intentions of keeping the code and comment in sync but realistically over time it won't happen. Whenever possible it's best to remove comments if you can make the code more explicit. I came across a nice example of this recently that shows this in action. The problem with comments in action I was working with an API that would respond with a custom code property on each response. This API was taking a query and returning search results, and the code in the response would signify if the response was successful, or if no results were found, or if there was an API error. I wrote a first pass at a small JavaScript module to wrap this API and ended up with code that looked like so: makeRequestToLibrary().then(({ code }) => { if (code === 2000) { // 2000 is the success code return { success: true, ... } } else if (code === 4040) { // 4040 = our request returned no results return { success: false ... } } else if (code === 4020 || code === 4021) { // 4020 and 4021 are API issues - invalid key, invalid request, etc return { success: false, ... } } }) This works well, and is reasonably clear, but is leaving itself wide open to the outdated comments problem. It would be very easy for a developer to add in a new code we need to deal with and not update the comments, or the API to change its codes, or a combination of both of them. You'd be in danger of ending up with something that looked like this: } else if (code === 4030) { // 4020 and 4021 are API issues - invalid key, invalid request, etc return { success: false, ... } } Here the comment bears no relation to the error - is it the