This content is part of the Essential Guide: How to improve code quality, from teamwork to AI features
Get started Bring yourself up to speed with our introductory content.

How do I foster reusable code across dev projects?

Developers don't have a lot of free time. Code reuse helps dev teams focus on the most valuable aspects of a project. So ensure your team knows how to write, and test, reusable code.

The reuse of code dates back to the earliest days of computer programming, and it remains a focus in software development today. Developers must save time and effort by any means possible, including through the reuse of code, templates and other assets they or someone else previously created.

As enterprises incorporate more and more open source code into software projects, reusable code is of paramount importance. Efficiency is the principal code reuse benefit, as most applications contain mundane and repetitive functions, such as keyboard handling routines, display or dialog routines, networking and storage access functions. These simple functions rarely change from project to project, and there is no benefit to coding them from scratch. They won't sell the software.

As an example, an enterprise sets out to design an image processing application. Users will want -- or not want -- the application based on what it can do to their image files, meaning its processes and algorithms for brightness, contrast and sharpening controls. Yet, the application must also include a suite of supporting features and functions that move file data, handle user input, display the image in its various states of edit and save to disk. Developers can skip over building some of these functions when they reuse code from previous work or projects available under an appropriate license. Such shortcuts enable developers to get right to the pivotal processes and algorithms that make the application stand out.

How to write reusable code

The processes and practices developers use to create reusable code vary dramatically. These procedures can range from simple practices, recognizing casual targets of opportunity, to more sophisticated methods -- e.g., carefully planned software suites developers architect to maximize code reusability. In the latter case, code reusability is part of fundamental software design and business planning.

How code reuse helps app developers

To be reusable, the code must possess several characteristics. First, reusable code should be modular and task-specific. The code should do one clear thing or a narrow set of closely related things. A reusable code library of display routines, for example, generates dialog boxes for menus and dashboards. The larger and more comprehensive the portion of code, the higher the potential for features that get left unused, which adds bloat and to the software and eats up memory.

Second, the code should be as human-readable as possible. While some programming languages are more readable than others, any reusable code should follow well-established styles, use meaningful and unambiguous variable names, and include appropriate commenting or other documentation. These conventions will help other programmers read, follow and modify the code for reuse.

Third, the team should thoroughly test the code, as well as write it to handle errors in a graceful manner. For example, if a routine asks for a name, but the user enters numbers instead, will the resulting program crash the routine or recognize the error and recover?

Do due diligence

Remember that software can be subject to legal protections, including copyright, patent, trademark and other intellectual property issues. Verify that you can reuse the code before you work with it. These license matters are primarily a concern with open source code, but they can also crop up when code includes sensitive intellectual properties, such as an algorithm or process, that a business does not want propagated.

Also, keep in mind that there can easily be many other requirements and details that define your company's approach to reusable code. And, of course, one's approach can depend on the specific business needs and development standards in place.

Next Steps

What five common open source licenses say about code reuse

Are microservices the key to reusing code?

When to sink the time and resources into refactoring bad code

Dig Deeper on Code refactoring and management