The most effective code is sometimes writing no code at all.
There is only one reason to write effective code. That reason is to solve a business problem. If you are write code to showcase your coding skills, then you cannot write the most effective code. You might think, you are solving a business problem by your code. Yes, that is true. But is it the most effective way? Maybe. Here is a quick framework, that helps you to write the most effective code which solves the right business problem fast.
Can you avoid writing code and still solve the problem?
Business does not need your coding skills. The business needs you to solve its problem. If you can solve the problem without writing a single line of code, you have given the business infinite ROI. This can happen by training business about an existing feature in the existing product. Software products are complex. Business only uses the features that they most need. More often than not they would have forgotten about the features that are already there.
The benefit of not writing code is immense. No test cases, no deployment, no increase in tech debt, and immediate results for the business.
Can you solve by updating the configuration?
Ask this question as excellent products always have features that are configurable. Development team would have disabled the feature as business might not need or if it depends on other feature to become live. Development team would have disabled the feature as part of A/B testing or to allow the application to scale in a controlled fashion.
This has minimal changes and more often than not, it only requires a restart. It saves development time and any Yes, if you can solve the problem by adding a configuration, the business would be happy as it might only require a restart. You have solved a problem without touching a line of code.
Can you use an existing pattern to solve the problem?
If you have reached this question, you will write some code. But you want to deliver it fast with high quality. Find out if you can solve the problem with an existing pattern. There can be a requirement that your application need to listen to a stream of events and publish it to a table in DB.
The code would already have this pattern in one of the earlier features. You can piggyback your solution to this feature and you could implement the solution fast. The pattern would already be robust and you would not introduce any tech debt. The unit test cases will all be thorough and the business gets their feature fast with high quality.
Can you enhance an existing pattern?
Sometimes, you will not have all the pieces of a pattern in the code. An example can be, that you have patterns for writing to a database and publishing to a queue. But the requirement would be to publish to a queue and then listen to the queue to publish to a database.
In this scenario, you marry up two patterns to arrive at the solution. You are only writing the wiring logic to achieve our results. There is no new code from scratch yet. You only need to test the wiring logic. You have reused rest of the code.
Can you use an external library?
I have seen developers spend an enormous amount of time doing some string manipulations in Java for which the code is already available in the Apache commons library. The library code is robust as it has already undergone multiple iterations.
You can write your own code can satisfy your ego but it can introduce bugs, take more time, and affects business revenue. People have been writing code for decades now and most problems have come up in one form or another for most of your requirements.
Time to roll up your sleeves!
If none of the above works, maybe it is now time to write some fresh code. There is nothing wrong in doing this if you have gone through all the above steps. You got to do what you got to do.
But most beginner programmers use the last approach without going through any of the above steps. This is also what separates excellent programmers from talented programmers. If you think about it, you do not need to win coding wars to excel as a developer.
The next part of the post talks about the concepts you need to know to help you answer the questions.
Business Knowledge
You need to have domain knowledge. This is not something you can learn in boot camps. You need to know what’s good for the business, how it works, and how the business is making revenue. This comes with experience but most businesses give you an introduction when you join a new organization. This is the reason you need to pay attention to these sessions.
Current Code-base Knowledge
One of the first things that you got to do when you join a new organization is to read the code-base. It is the only source of truth that you can rely on. Get familiar with the patterns and features in the code. Find out your team have implemented features.
As an exercise, try coming up with alternative patterns and see what breaks. This gives you a very good understanding of the patterns and their usefulness. It will allow you to enhance patterns when you get new requirements.
SOLID Principles & Design Patterns
Existing code will have these principles and patterns. Knowing these patterns and when to use them will help you write robust code. Most new developers focus on learning algorithms than learning about the principles and patterns.
To get to write the most effective code, you need tons of practice and experience with different systems. You cannot get that experience in bootcamps or university. It needs a sustained desire for excellence and passion to solve business problems.
Conclusion
As new developers, the urge is to write code as soon as we get the requirements. This stems from either the desire to showcase their skills to the business or to solve the problem fast for the business. But this leads to buggy code, missed requirements, tech debt, and sometimes solving the wrong business problems. The most experienced developers know this fallacy from their experience.
I covered key questions to ask to write the most effective code:
- Can you avoid writing code and still solve the problem?
- Can you solve by updating the configuration?
- Can you use an existing pattern to solve the problem?
- Can you enhance an existing pattern?
- Can you use an external library?
I also covered key concepts you need to have to answer the above questions. This include:
- The business knowledge
- Current Code-base Knowledge
- Know SOLID principles and design patterns
Have I missed anything? Did it make sense? Chime away in the comments.