Specifically, I mean code structured in a way where storage is global. Bare with me, as my terminology is all over the place. If I want to create a variable, I "load" it into a global cache. Any time I want to refer to this variable, I use an index.
It feels like the opposite of OOP, where there's a ton of abstraction and most objects inherit from some other object, etc...
I think OpenGL employs something like what I'm trying to put a name to (i.e. one has to "bind" variables)?
I just can't seem to put the right words into Google to find what I'm looking for, so I thought I'd pop a simple question here. Thank you for your help!
-
5The name of this design pattern is: bad code.freakish– freakish01/31/2025 06:45:14Commented Jan 31 at 6:45
-
11There isn't one. There isnt a named pattern for every conceivable implementation of things.whatsisname– whatsisname01/31/2025 07:06:47Commented Jan 31 at 7:06
-
3It's not a singleton. This aspect of openGl isn't implementing any particular design pattern, it's a result of the somewhat unique quirks of the interface across the PCI bus to the independent graphics hardware. Its constraints and therefore resulting design really doesn't exist in "normal" software taking place entirely in the CPU.whatsisname– whatsisname01/31/2025 07:20:38Commented Jan 31 at 7:20
-
9Storing "variables" into "global storage" and accessed via "index" is called a database.Euphoric– Euphoric01/31/2025 07:53:12Commented Jan 31 at 7:53
-
3This is what the computer actually does. All our computer languages and frameworks do is hide that stark fact behind a representation that is easier to work with for humans.Kilian Foth– Kilian Foth01/31/2025 09:10:06Commented Jan 31 at 9:10
5 Answers 5
You don’t have to know the name of the pattern to write the code. Most patterns were written before they were named.
Some patterns were given names before we even had a book about patterns. I think that’s what’s happened here.
The point of naming a pattern is to make it easier to talk about. These names make me think about what you describe:
- Global array
- Offset + index = address
- Pointer
- Reference
All of which map into system memory which is global (and an array). What makes a variable global is everywhere knowing about it.
You won’t find these names in pattern books mostly because we understood this stuff before we understood patterns. So we just gave them names.
What your description doesn’t make me think of is indexing into a database which is a thing but I don’t think it’s your thing.
-
2+1 for "Most patterns were written before they were named."Kilian Foth– Kilian Foth01/31/2025 14:44:14Commented Jan 31 at 14:44
-
+1 Thank you for the helpful clarification! The list of names you give makes it nice to have a label to wishy washy concepts in my head.Scene– Scene01/31/2025 15:52:19Commented Jan 31 at 15:52
-
8"Most patterns were written before they were named." – I would go further: "ALL patterns were written before they were named." Patterns are discovered, not designed.Jörg W Mittag– Jörg W Mittag01/31/2025 16:28:51Commented Jan 31 at 16:28
-
Global arrays, offsets/indices, pointers and references are not patterns. They are abstractions in programming languages. A pattern would be a lazily allocated buffer, for instance, or a pointer linked list.cmaster - reinstate monica– cmaster - reinstate monica01/31/2025 21:07:25Commented Jan 31 at 21:07
-
@cmaster-reinstatemonica I can see why you might think that way. Just understand that there is more than one patterns book.candied_orange– candied_orange01/31/2025 21:21:33Commented Jan 31 at 21:21
I wouldn't really say this is a conventional "design pattern".
The issue here isn't just the global scope of access, but the fact that you're looking things up each time by "index", rather than using the compiler facilities (like named variables).
As others have said, this sounds somewhat like how a database works. But that is only from the perspective of its external API, where the whole point is to provide an abstraction of storage, including allowing permanent records to be referred to in a machine-independent and session-independent way (because those things will change over the life of the record, and the reference must stay valid).
It's not really how database engines work internally as executable programs.
As a technique for storing "variables" as we usually know them within running programs, this is either just extravagant complexity and reimplementation in bespoke software of the things that are adequately provided by hardware and standard compilers, or it is designed to overcome unusual memory constraints where perhaps there needs to be shared memory between two processes, a system for accessing it, and an inability to compile the two processes simultaneously so as to guarantee a specific memory layout.
Even if the technique is legitimate, there's the potential there won't be a specific name for it because it isn't used often enough by enough practitioners, but is instead used (and perhaps, autonomously invented by) by a very small number of those creating applications under highly specific circumstances, for whom the technique is just an unnamed facet of how their application works (and of which facet their understanding is entirely non-verbal).
It's called "using global variables". Usually there is no design and no pattern involved, and if you did call it a pattern, general consensus is that it is an anti-pattern. (Anti-pattern is just like a pattern, but causes problems instead of helping you).
-
Yes! And sometimes it's wrapped into a singleton for the good conscience ;-)Christophe– Christophe01/31/2025 13:42:56Commented Jan 31 at 13:42
To me this sounds similar to how file handles, window handles etc. are used. Particularly when writing applications for (older?) Windows OS.
https://en.wikipedia.org/wiki/Handle_(computing)
The life cycle, memory management etc. is handled by the "kernel" and the "user level" code passes a handle into "system calls" to read/write/render.
It doesn't necessarily make for a bad pattern at all. I think the key to keeping it manageable is to make sure the "handles" are opaque objects (indexes/pointers fit that bill) and only the "kernel" code actually reads/writes to the data. This is actually very object oriented, it just turns the "usual" syntax of object.method(...)
"inside out" (i.e. method(object, ...)
) so the biggest downside is likely to be name-spacing the method names.
The word "dictionary" comes to mind here. One might argue that a dictionary is a design pattern, but I'm inclined to just call it a data structure. In any case, this seems messy in the context that you're describing. Such a dictionary would have to have untyped values in it to be much use. That translates to much type checking and casting, which can ultimately impact application performance and become a maintenance nightmare in the long run. The perfect storm is where one part of a program places a value of Type A at a given index in the dictionary, and another part replaces that value with an instance of type B, and both are expected to work well. In the long run, this causes a mess.
If you're thinking about designing this way, DON'T.