Inversion of control (IoC) is a programming technique in which a general code structure is used to govern a number of unique and specific subroutines. This turns traditional programming methods, where specific code governs a number of reusable and general subroutines, upside down. Inversion of control is typically used in situations where the programmer knows he or she will not have to reuse a specific piece of code more than once, allowing a flexible design where a program's subroutines can be switched in and out without substantial changes to the overall program.
Traditional Versus IoC Programming
In traditional programming, the main body of the code will repeatedly call for general subroutines which perform individual functions. For example, in a program dealing with accounting, a subroutine designed to allow the end user to search for a specific order number will probably be called on multiple times in various sections of the program, allowing the user to perform that very general search algorithm from a number of different areas of the program. Reusing the code simplifies the programming process, but creates complexities if the programmer wishes to tweak the searching algorithm for one section of the program without affecting the other sections in which the code is used.
Using the same example under an inversion of control scenario, the single search subroutine would not be called multiple times across a number of areas of the program. Instead, each section of the program would contain its own completely self-contained searching subroutine. This increases the amount of time needed to initially code the program, but simplifies any specific tweaks which might need to be made later to individual subroutines in the design process. Changing one subroutine in one specific area will leave the remainder of the program completely unaffected.
Advantages of IoC
One major advantage to this technique is that it makes program design far easier when working in large-scale teams. As communication between team members will necessarily prove more and more difficult as the numbers of workers increases, inversion of control allows each team to program its own individual routines, allowing them to function independently of one another. It also simplifies the impact of bugs in the system, as any lingering errors in each team's subroutines will only affect their specific sections of the program. Due to this, when problems are discovered in one part of the system, the rest of the program should remain fully functional.
Disadvantages of IoC
While inversion of control can simplify program design, it does require prior knowledge of how to design objects. While each routine can be programmed individually, an IoC creator must know how to program each item in case changes need to be made, so it is not always easy for a beginning programmer to employ IoC. Also, because each routine functions independently, they are all made visible to the outside world, which may be frowned upon by some companies.