6 ways to code faster and with less bugs
It can take ages to code what looks like a simple feature at first glance. Times flows while you have the impression that you made no measurable progress on that new feature and that everything is hopeless.
Outsiders will tell you that you’re only adding a checkbox or a text field and wiring stuff together, so it should not take this long. Why are you coding so slowly?
Thinking about the time it took to create a new feature as the number of lines of code you need to produce is a mistake, because the part that takes the most time is not typing code in the computer. Your keyboard shortcut skills are not going to help you with all the other things you must do to create the feature like:
- Understanding how it needs to behave when it’s done,
- Looking at the existing code to understand where it fits,
- Choosing the best controls and API methods for the job,
- Naming and documenting the variables and methods in your code,
- Testing the feature.
Indeed, I’m not going to give you tips to type code faster or technical shortcuts since those can be found in any technical book about your language or framework of choice. Likewise, there are many great posts about keywords shortcuts and tools for your IDE of choice.
So here are some ways to optimize all the other stuff so you can deliver faster:
- Make sure you understand what the feature should do properly before you start coding, including those hairy corner cases that you must handle. Things can change, but you’re going to lose a lot more time if you code something and have to start over: aim to do things right the first time.
- Plan out the big picture of how you are going to implement the feature and what are going to be the names of the most important classes and methods. This is the hardest tip in this article to stick to, but you’re going to be a better coder if you make it an habit to read the existing code to get a better idea of what to do and plan for the most probable pitfalls before writing the first line of code.
- Break down what you need to do in tiny wins instead of trying to take it in all at once. Doing just one small method at the time and incrementally adding more code as you go on is easier to manage than trying to do everything you’ll ever need in one shot. Ideally, you should always be able to run the application even if the feature is not complete. Don’t be afraid to leave TODOs in the parts of the code that are not done yet so you’ll remember what you wanted to do when you come back to finish it.
- Make sure that each tiny part is well-formatted, with good names and properly documented. It’s easier to do while you’re writing the method instead of waiting to do it all in the end, and chances are that once you’ve moved on you’re not really going to fix it. This will make it easier for you to use the method and change it as needed since it’s already nice and clean.
- Keep a piece of paper or a notepad beside you as you code to jolt down things that comes to mind as you work. While you code, you’re going to think about things like test cases, that bit of CSS you need to adjust and how to implement other parts of the feature. You can take a quick note without breaking the flow of what you’re doing and go back to the current task. That way, you don’t need to keep all those things in your head (think GTD) and you’re not going to forget them all if you get interrupted.
- Don’t hesitate to look for examples of code when you don’t know how to do, but avoid copy-pasting. Rewrite those examples using your own variable names and standards, and take ownership of it by understanding it as well as code you wrote yourself so you can fix it if something goes wrong. Also, if you have bugs and don’t understand what’s going on at first glance, use all the tools at your disposal like reading the docs for the API methods you’re using, using the debugger and inspecting the code of your page to make sure it’s behaving as you expected. There’s no need to try to be tough, examples and tools are there for a reason.
All those techniques will also help you with writing code that has less bugs since you’ll know where you are going. Coding more deliberately will help you make the best of your coding time and deliver better features.
Reference: | 6 ways to code faster and with less bugs from our WCG partner Cindy Potvin at the Web, Mobile and Android Programming blog. |