How To Write Good Code

In this post, we will discuss on a high level about writing good code following basic approaches considering the scenario and usability of the code. We have demonstrated two basic approaches considering usability and maintainability to write good code as explained below.

Approach A - In this approach, the code has to be written once to achieve an immediate goal. We quickly write code without detailed planning, use it and forget it. This approach is good to be followed when we write smaller code for one time purpose and does not care about its readability and maintainability. We simply pick up an editor, do the coding, test it and use it for the immediate purpose.

It ends up in bad code which is not well formatted and documented but achieve the short-term goal. It can be considered a good approach when we have limited time, lesser features, need the code to try out something or to build a prototype.

Approach B - In this approach, we need to plan the code well to achieve a bigger and long-lasting goal which results in understandable and maintainable code. We can follow the software development lifecycle as explained below following the modern, iterative, incremental and agile development approaches. The below mentioned steps can be considered to develop the code in a controlled way following the best approach and methodologies suitable to achieve the end results.

Step 1 - Planning

We must plan the scope of software or code to be developed before we start coding. During this stage, we can generate high-level documents and diagrams to have a bigger picture of the code. We also need to identify the coding approach best matching with the scenario.

Step 2 - Designing

During this phase, we can generate several useful low-level assets to understand the requirements in details. We can generate graphical designs in case the final output needs an interface.

The low-level diagrams including Use Cases, State Charts and Class Diagrams can be generated to rectify most of the issues at an early stage. These documents also give a detailed idea about what has to be developed.

Step 3 - Development

The actual development will be done in this stage where we write a unit of code with the appropriate unit and functional test cases. We can also follow test-driven development where we write test cases covering the code unit and fail all the test cases. All the test cases must pass once coding is done.

There might be cases where time does not permit to write test cases and we directly write the code and test it manually. This approach seems fine in case the coding unit does not need maintenance. We need manual effort to test, every time we make changes to the code. Whereas if we write test cases, it reduces manual testing effort to a great extent.

Step 4 - Testing

The code must be thoroughly tested covering all the cases if time permits. The test cases can be updated to cover the additional cases identified while doing the testing. In the larger scenario, the development and testing teams might be different and the test cases can be updated based on the issues identified by the testing team.

Step 5 - Deployment

The deployment strategy depends on the type of software built. In the case of distributable and executable code, a proper installer should be made to hand over a working solution.

In the case of server environments, we can also setup continuous integration to automate the deployment process which reduces human errors which can cause accidental damage to live code.