S.O.F.A. (SOFA) is a pneumonic acronym that describes four principles of object-oriented programming (OOP) design that apply to functions. These principles, simple as they are, help assure reusable, extensible, and performant software.
The principles outlined by SOFA are guidelines only—meant to advise not force. The presence of code smell often signifies a violation of one or more principles outlined in SOFA or similar OOP frameworks such as SOLID. Below is a brief discussion of each principle of SOFA.
‘S’ is for Short
Functions should be as short as feasible without taking away from clarity. The main concept being addresses should be clear and extraneous operations should be factored out into additional helper functions. A general guideline is that functions should take up no more than one screen unit such that minimal scrolling up/down is required to inspect.
‘O’ is for One Thing
Functions should address a single goal and rely on other functions for additional functionality. This principle is closely related to the Short principle but focuses more on input/output rather than syntax. By keeping the scope of functions limited to a single purpose one ensures that testing can be done as completely, efficiently, and easily as possible.
‘F’ is for Few Arguments
Functions should have as few arguments in number as is possible for them to address required functionality. By limiting the number of arguments, all permutations of parameters can be more easily tested for. This specifically allows developers to test multiple combinations of parameters required to measure essential functionality without more possible conflict than strictly necessary.
‘A’ is for Abstraction Level Consistency
Functions should limit the level of abstraction they use to be consistent across all sub-parts. For example, a function should not combine a high-level API call to a webserver with a binary data conversion operation to process response data. Delegation to helper methods should be used in such cases where the combination of these actions is necessary.
Final Thoughts
SOFA is only a conceptual framework to be used as guidelines when developing object-oriented software. There is no concrete rule saying your software won’t work unless carefully audited to comply with the principles of SOFA.
While such encompassing conceptual guidelines may be overkill for scripting or small hobbyist projects the principles of SOFA make larger projects more easily maintained, interoperable, and extensible. Having enough familiarity with SOFA to keep it rattling around in the back of your brain while coding can help avoid code smell, crushing code reviews, and the shudder one feels when inspecting one’s old code.