AUTHOR
Julien Delange, Founder and CEO
Julien is the CEO of Codiga. Before starting Codiga, Julien was a software engineer at Twitter and Amazon Web Services.
Julien has a PhD in computer science from Universite Pierre et Marie Curie in Paris, France.
You started learning to code the last few months and you are very proud of what you can build (and you should be!). But as you make progress and maintain your project, add features or collaborate with other persons, you struggle to maintain your software and even understand the code you wrote weeks ago.
This article reviews major mistakes from junior developers and how to avoid them. If you have more examples, I would love to hear from you: shoot me an email or join us on our Slack community
Mistake 1: long functions and files
As you write code, you try to perfect a given functionality and continue to pile new lines of code in existing functions, classes, or React components. After hours of iterations, you finally get your software working: everything is in few files, working with few functions.
Weeks later, you attempt to update functions that span over hundreds of lines of code. As you modify one variable, it impacts other parts of the program and you struggle to understand why your program no longer works.
Collaborators also start to update the code and each of them has pull requests to merge. However, as all the code is in just a few files, merge conflicts occur and it takes hours to merge code that was accepted by the team.
Yes, writing large units of code (function, file, classes) is not optimal.
Problem(s)
- It’s hard to follow the data flow: how variables are used between variables
- It makes collaboration way harder
- Large functions are harder to test
Solution
Write small units of code. If a function is more than 50 lines of code, you should probably refactor your code and decompose it with multiple sub-functions. Similarly, files should only contain few functions to improve readability and maintainability. Separate concern: one file should handle one clear particular aspect of your software.
Mistake 2: Not writing documentation
You finish your code and everything is perfect! Three weeks later, a user reports a bug and you reopen your code only to find that you have no idea how it works anymore. After hours of reading what you wrote weeks ago, you start to try to fix the bug, without full confidence your fix will address the reported bug.
Documentation is a love letter to your future self
— Damian Conway
Problem(s)
- You have a hard time understanding the code, taking more time to maintain
- Any new developer working on this code base spend hours trying to understand how it works
Solution(s)
- When the code is not obvious (e.g. if the function is not named
capitalizeFirstLetter
), document the function and/or the body of the function - Write some high-level documentation (in
README.md
,DEVELOPER.md
or any other file) to help developers get started and understand the overall architecture of your project.
Mistake 3: no testing
You write code and it works on your machine. Unfortunately, when other users start using your product, it crashes, without really knowing why. You try to reproduce the bug and spend hours manually reproducing the bug and fixing it.
Problem(s)
- Your project may work only in your particular development environment and does not represent the production environment
- You test your code manually and limit your tests to the success cases
- As you code manually, you do not test that your code is working at each change. A change (such as an updated dependency) may break your code without you knowing it.
Solutions
- Write unit tests for every function you have and include at least one test for the success case and one test for the failure/error case. If possible, try with different possible inputs to validate each failure case.
- Use integration testing that will test that your project works correctly and all functions correctly call each other.
- Automate your tests by running unit and integration tests at each commit and pull request.
Conclusion
Becoming a good developer takes practice, constancy, and hard work but the best advice is to always keep improving.
What do you think of these top five mistakes? Do you see more? Join our Slack community and join the conversation!