Law of conservation of complexity

Discussion of hardware and software development for Super NES and Super Famicom. See the SNESdev wiki for more information.

Moderator: Moderators

Forum rules
  • For making cartridges of your Super NES games, see Reproduction.
Post Reply
psycopathicteen
Posts: 3140
Joined: Wed May 19, 2010 6:12 pm

Law of conservation of complexity

Post by psycopathicteen »

In physics class you learn about the laws of conservation of energy and matter, where matter and energy cannot be created nor destroyed.

I feel like it's the same way with code complexity. To a certain degree, if code is downright spaghetti code then it can be simplified, but after a certain point, getting rid of spaghetti code in one place often results in spaghetti code elsewhere.
User avatar
tokumaru
Posts: 12427
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: Law of conservation of complexity

Post by tokumaru »

psycopathicteen wrote: Sun May 16, 2021 9:56 amgetting rid of spaghetti code in one place often results in spaghetti code elsewhere.
I don't agree. In high-level languages, you can avoid spaghetti code by breaking the logic into smaller functions that do less work. When coding in assembly for slow CPUs, having a lot of nested function calls can really slow things down, but you can use macros instead and achieve the save kind of organization without the performance penalty.
coto
Posts: 102
Joined: Wed Mar 06, 2019 6:00 pm
Location: Chile

Re: Law of conservation of complexity

Post by coto »

Such laws should apply to APIs so conservation should resolve complexity issues.
93143
Posts: 1717
Joined: Fri Jul 04, 2014 9:31 pm

Re: Law of conservation of complexity

Post by 93143 »

I think a better analogy would be the Second Law of Thermodynamics - code complexity can't be lower than the minimum required to do the job, but it can certainly be higher.
tokumaru wrote: Sun May 16, 2021 10:18 am
psycopathicteen wrote: Sun May 16, 2021 9:56 amgetting rid of spaghetti code in one place often results in spaghetti code elsewhere.
I don't agree. In high-level languages, you can avoid spaghetti code by breaking the logic into smaller functions that do less work.
Yeah, that's called ravioli code. It's basically spaghetti code but the spaghetti is virtual - instead of branches and jumps, you're dealing with subroutines, and now you have to hunt through dozens of files in multiple cryptically-named directories to find out where the action happens. It does tend to be more robust, if you're careful with conceptual encapsulation...
User avatar
tokumaru
Posts: 12427
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: Law of conservation of complexity

Post by tokumaru »

Yeah, if you're literally just cutting pieces of code from one place and spreading them around it's not much of an improvement, but if you can break complex tasks into self-contained small tasks, the result is much cleaner and pleasant to work with.

When coding in 6502 ASM, if I find myself using the "branch over a JMP so I can put conditional code hundreds of lines away" technique, that's usually a sign that those big blocks of code should be broken up into smaller parts.
Kaisha
Posts: 14
Joined: Mon Apr 26, 2021 2:58 am

Re: Law of conservation of complexity

Post by Kaisha »

I've always thought that the idea of orthonormalization can be applied to languages/programs as well. You want the minimal set of constructs that still covers the entire range of concepts and ideas that you want to model.

Once you have that, everything else builds up in a pretty straightforward manner.
Post Reply