DEV Community


Posted on


Is One Source of Truth just a fancy name for Global Variables?

I've created very large scale and extremely robust applications using object oriented programming. OO is a good way to modularize code and insulate it from other code, so things stay inside their design.

Now I am working on a React Native app, and while the view system is really good, the global state thing puzzles me.

[Naive] opinions:

  • One source of truth philosophy is just global variables.

  • The problems that redux and mobx solve are ingenious ways of dealing with the limitations of web development. These problems do not exist in native OO programming and the solutions to them are therefore irrelevant.

These are statements to discuss - I'd particularly like to hear counterpoints. I want it to make sense! I am happy to change my opinions if more aligned ones come along. As Groucho Marx said "I have my principles, and if you don't like them I have others!"

One source of truth seems to be the discovery, by web programmers, of global variables. Global variables are known to make code error prone, brittle, hard to debug, hard to maintain, and generally make a mess of things. If you want to get fired on your first day of the job as an OO programmer, introduce a bunch of static globals into the code base.

I understand why this happened - web programming has its own set of constraints thanks to a runtime environment that was never designed for apps, yet is used for apps. So these things had to be created to deal with it, and to get anything done. It's an amazingly elaborate and innovative way to get around these constraints.

But now it's creeping back into native development in React Native. The vast majority of problems that are solved by redux and mobx don't exist in native apps. What remains is a global state management system.

In any OO program I've worked on, we could have done that. We could have created a God-State object, and run all state through it.

The reason nobody would ever do that is that it's a very poor design. It won't stand the test of time, it'll be buggy, it'll require every coder to know everything about every corner of the application, and all the other reasons we do not use global variables.

You do not want one massive, complex object where changes to it could potentially break any part of your app. It's the last thing you want and it's the reason why first modular and later object oriented programming were invented to begin with.

Top comments (0)