DEV Community

Cover image for Your code should tell a story: Tips for writing code for others to read

Your code should tell a story: Tips for writing code for others to read

Tom Collins on February 24, 2020

When we write code, we are also writing a story for other developers. Here are a few tips on how we can write code for our future selves and other...
Collapse
 
endavid profile image
David Gavilan

I think userPassedCourse should be called userFailedCourse. Or at least, having a low mark suggests or low attendance suggests failure. But then, you switch passing false to passing true from one example to the next, so even if you are trying to refactor it to make it more readable, I'm getting really confused by the example.

Collapse
 
collinstommy profile image
Tom Collins

Changed to:
user.result > 40 && user.attendance > 50

Collapse
 
sonicoder profile image
Gábor Soós

Nice examples for promoting Clean Code!

Intention revealing code is prior to code length. There is a different tool for code length and it's called minifier.

Collapse
 
thekarel profile image
Charles Szilagyi

Nice one, should get more ❤️s!

By the way, I guess you meant

const entity = {
  UNDERGRAD: 'underGrad',
  POSTGRAD: 'postGrad',
}

getResultsFromDB({ entity: entity.UNDERGRAD, courseId: 'SCIENCE_101'})

Collapse
 
collinstommy profile image
Tom Collins

haha yes. That would very much negate the advantage here!

Nice spot. Article updated.

Collapse
 
lexlohr profile image
Alex Lohr

If you can, use TypeScript to document the API. The reading flow is better than with plain JSDoc. The IDE integration will make working with your code a pleasure. Pro-Tip: use JSDoc comments to comment more complex types - this documentation will then be shown on hover in modern IDEs.

Collapse
 
collinstommy profile image
Tom Collins

I used Typescript a couple of years ago. I definitely want to pick it back up again.

Collapse
 
pavermakov profile image
Pavel Ermakov

I really like these tips. Thank you!

Collapse
 
collinstommy profile image
Tom Collins

Fixed. Thanks for letting me know!

Collapse
 
georgewl profile image
George WL • Edited

I also feel like courseID could be an enum, if there's a set number of course IDs

Collapse
 
ghost profile image
Ghost

I've read some Lovecraftian code.

Collapse
 
djpandab profile image
Stephen Smith

Thanks for the read. Very informative.

Collapse
 
collinstommy profile image
Tom Collins

Amen. The frameworks are the kings of the jungle right now.

I do love React but the building blocks of clean code are the key to having a nice codebase.