Code Review: Name file after exposed function
Andrey Bodoev
Posted on April 10, 2020
In code reviews which we conduct in our team, I might find some examples of code, which is can be misleading or with no clear intent. This post is about to share reasoning and hear critique on some of these examples.
Name file after exposed function
One of the cases is having few functions in one file, related or maybe not, or can be completely not related, but somehow group in one file.
get/
index.ts
export getSomething :: Int -> Int
export getSomethingElse :: String -> String
Few things we can tell immediately,
- please don't use
index.js
, https://www.youtube.com/watch?v=M3BM9TB-8yA&vl=en - both
get
directory andindex.ts
gave no context whatsoever.
Instead, try to name files after exposed function, and move each of them into it's own file, like so
getSomething.ts
export getSomething :: Int -> Int
getSomethingElse.ts
export getSomethingElse :: String -> String
It shows intent by looking at filename.
Few side effects which you may discover later,
Functions are enclosed in file to ensure there's no shared variables; which leads to proper unit tests and maintainability.
And after this change, there's powerful way to quickly go through project structure; by opening files which named after function and not searching through functions in search output of your text editor.
Takeaways
- Name file after exposed function;
- Breaking up into small modules for unit testing and maintainability;
- Browsing code within file vs. browsing code within project.
One of the great examples in wild, https://github.com/lodash/lodash/tree/master/
Posted on April 10, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.