We often hear the term "production quality code", some times in an interview where the interviewee asked to write production-quality code for a particular function, sometimes discussion between developers and programmers and sometimes during code review. I know most of you familiar with the term but for those who wonder what is production quality code in simple terms its code which can bear the test of time in a production environment. We all develop code in development, then test in QA and finally, it gets released in production.
But there is a lot of difference between test and prod and if you have not coded taking prod environment in mind most likely code will fail in prod or result in exceptions.
But there is a lot of difference between test and prod and if you have not coded taking prod environment in mind most likely code will fail in prod or result in exceptions.
So what makes the production environment different than development environment?
Here are a few things which I noted:
1) Production is all about the load/traffic/amount which will expose concurrency issues, load issues, memory, and CPU issues.
2) You will get a lot many scenarios in production that you might have thought of in development. I think through the process is not applied than most likely those scenario has not handled in production.
3) Different data input or incorrect data, one of the classic problems in production is the data that gets input to your program, be it from the upstream system or any other place you will get all sort of data and if your program doesn't handle those very likely it will suffer.
4) Boundary conditions, this is somewhat related to above point data and scenarios but most of the boundary condition e.g. null, empty, NA, incorrect default value etc exposed in production.
So if a code is written all these things and potentially domain-specific things and can sustain the test of production than it called a production-quality code.
And, believe me it takes a lot of experience, skill and think through process to write production-quality code not just in first time even after two or three iterations.
Though, as a developer, we should always strive for writing production-quality code in the first attempt.
6 Tips to Write Production Quality Code
Now let's see how we can do that, what are the things we should keep in mind:
1) Get your requirements right, understand the problem, talk to the user or business person as much as possible this will help you to find different scenarios as early as possible.
Many times business analysts (BA) or user does not tell you everything it's not their fault but it doesn't come in mind right through.
So if you are frequently speaking, discussing, and presenting a solution to the most likely they will ask questions, give feedback which eventually exposes more scenarios and subtle details.
So if you are frequently speaking, discussing, and presenting a solution to the most likely they will ask questions, give feedback which eventually exposes more scenarios and subtle details.
Here experience plays an important role.
More experience or domain knowledge you, much better code you will write.
2) Think through, Think through, and Think through. T
here is no substitute for this, it is more of a skill and art than science but you will get hold of this when you get more experience.
For example, if a user says that you need replay capability in your program.
You should be able to think of all possible scenarios where you need to replay and what could be required for that, what would be a side effect of that.
You should be able to think of all possible scenarios where you need to replay and what could be required for that, what would be a side effect of that.
Does replay would be requested, what if the requesting system went down again, will you be able to re replay, etc.
3) Boundary condition, always think that you will get bad input, you will get null or empty, small or very large numbers, the function may get called at the wrong time, etc. you can get rid of this by writing the unit test for each boundary condition.
4) Concurrency, this is is the major culprit and big problem which exposes itself in production when due to load multiple threads get triggered and access your program concurrently.
With the high-speed low latency electronic trading system and with many other java systems where its requirement to have a concurrent application this can only be addressed by proper design.
If you get the design right you will safe otherwise you will need to bear the pain of redesigning or rewriting code.
You can also expose concurrency issues by doing load testing in early QA cycles.
5) Error and Exception handling, this is the by far most important characteristic of production quality code, it must be able to handle an exceptional scenario in a clear cut predefined way.
Your whole program should never be crashed due to one single bad input or scenario.
6) Recoverable, code should be able to recover itself in the case of premature closing or crash.
These were few things I know from my own experience. If you want to share your own learning and experience, feel free to suggest in comments.
You can also see these 5 Best online Courses to learn Clean Code in Java to learn about coding and refactoring.
Here is also a nice diagram to remember these tips:
Please share how you guys write production-quality code, what are things you guys keep in mind, what questions you guys ask to yourself and to a user?
Thank you.
Related Java Tutorials
You missed some important things:
ReplyDeleteIt must be simple.
It must be short.
It must document itself.
Code should be expandable. Once in production, enhancement and new requirements r bound to come. The abstract design should allow for rapid development. Developer should keep on mind when coding
ReplyDeleteI had once encountered a problem due to the "System Locale". On Linux systems, the default locale is UTF_18. I was working on a code that used to read a bufferedinputstream from a file and the previous coder had chosen a method that was dependent on the locale. Later, when I changed the locale in production to "en_us", that same method worked correctly. The method was a part of the project library, hence I couldn't rewrite that part of the code.
ReplyDeleteGood point Kamalkishor, writing locale independent code is also one of quality for production code. These kind of thing should be part of Resources which allows you flexibility to change the language or resource based on locale.
ReplyDeleteNice article. I would also like to second the thought on "boundary conditions" as that is where most of the developers tend to miss during their unit testing.
ReplyDeleteAlso to consider that a well formatted and documented code helps maintainability of production code.