How to know when code is complex
In my past posts I discussed better ways to assess a program’s complexity than counting lines of code. There are good metrics like the Halstead Metrics that help you in comparing programs on a high level, but they really don’t get to the inside the program. In other words, you can’t see that one part of the program is more complex than another. For that we have to go down a level into the program — to the paragraphs, Procedures or Perform Groups.
Two programs could have the exact same number of lines, operators, and operands, yet be totally different to maintain. Why? It has to do with how they are coded, what they are doing and how the developer chose to implement the logic. The core of complexity is made up of decisions. Think about your own life. Decisions can be difficult, but if they’re made one at a time then they aren’t so bad. But what if they are linked — if a decision leads down one path, which calls for other decisions, which lead to other decisions, and so on and so forth? The overall complexity of the situation grows, and so does the importance of making the right decision at each point. This is the problem we face in code and therefore we need ways to manage the resulting complexity.
One way to manage this would be to just code all of these decisions into one area, be it paragraph, Procedure or module. Grouping them together is easy and it’s the way things are often done. But if you group decisions together, understanding the logic becomes very difficult because you will have to comb through a large section of code to get to the area in question.
[fusion_builder_container hundred_percent=”yes” overflow=”visible”][fusion_builder_row][fusion_builder_column type=”1_1″ background_position=”left top” background_color=”” border_size=”” border_color=”” border_style=”solid” spacing=”yes” background_image=”” background_repeat=”no-repeat” padding=”” margin_top=”0px” margin_bottom=”0px” class=”” id=”” animation_type=”” animation_speed=”0.3″ animation_direction=”left” hide_on_mobile=”no” center_content=”no” min_height=”none”] prof_showman: Approach decisions with balance in mind. This of a Calder Mobile.
It would be easier if you instead attempted to isolate important pieces of logic into separate areas. So, for example, after a main decision, you would branch to different areas and continue this process. Each section should be similar in size and provide balance. Think of it like a Calder Mobile. At each point there is a split with others balancing below it. But if any part gets too large, the balance is gone. This process should make it easier — assuming you have good naming and comments — to quickly locate areas you need to examine. You will have far fewer decisions to understand within those areas.
So, it’s better to break down the decisions into the smallest blocks possible and isolate them. In the end you still have the same number of decisions, but maintenance is far easier. But how to judge the size of these blocks? I’ll introduce a helpful Metric in my next post.[/fusion_builder_column][/fusion_builder_row][/fusion_builder_container]
Latest posts by Mark Schettenhelm (see all)
- Mainframe and Distributed: Uniting an IT House Divided - February 13, 2018
- Waterfall to Agile SCM Conversion: Why Not Use the Best? - February 6, 2018
- I Don’t Do New Year’s Resolutions—That’s Waterfall - January 2, 2018