Structures vs Classes

While structures & classes may seem to be very similar in most respects there aren’t many cases when I would suggest you use a structure instead of a class.

A class with public member properties isnt that different from a structure in terms of how your code uses it.

The biggest difference is that for a class you must use new to get a new instance. With a structure you just declare it and use it.

Structures are handy for some things. If you have to read or write a specific binary file format you might use a structure. If you’re going to call some OS API via a declare then structures are useful. And if you’re reading or writing binary data to binary streams via sockets or other communication channels they are useful.

But there are downsides. Since structures are JUST data any methods to manipulate them have to go in a module. Unlike a class you cannot add methods to the structure itself. And so you end up using a very non-OOP style – maybe a module with the structure defined in there along with all the code that manipulates the structure.

Classes allow you to just bundle up the data and code into a single item and all the code that manipulates the class is part of the class – along with any hidden methods it might need that can be completely hidden from the outside world.

If your still using VB style syntax and creating a lot of structures I’d suggest you give using classes a try.

Once you get the hang of it you will appreciate how classes let you organize your code and data in a nice single item.

4 Replies to “Structures vs Classes”

  1. As I said on the forum, you can define a structure in a class, but you can’t define another class in a class.

    I sometimes use structures defined in a class as properties of the class so everything is encapsulated within the class. Sometimes that is to logically group a bunch of properties so i can use dot notation to autocompleted items of the groups, and sometime it’s because you can get or set the whole structure as a string or both.

    1. True.
      It is a shortcoming that Xojo cant create private classes in classes which would be really handy for this sort of purpose.
      It would let us all use OOP principles a lot more cleanly and would let us have the best of both worlds (but without Joe or someone like him around I doubt it will happen any time soon)

      I often create a module and then put the class I want exposed in the module as global and other helper classes in the module as private.

  2. One aspect is missing in this article about structures, namely that a structure is a possibility to directly access and manipulate its data like in a memory block. I use structures for this purpose, for example. I also use a structure for example to store cache values. Many code profiler calls have shown me that this is sometimes much faster and can be processed much more efficiently than declaring the individual properties in the class.

    1. I prefer a class to wrap functionality into a single entity since structires do not have any capability in that regard.
      As for speed there are many aspects to speed beyond the speed of accessing a property on an instance. Choice of algorithm is likely to have a much bigger influence – but once you HAVE the optimal algorithm there are tweaks like you suggest that can squeeze every last ounce of speed out of your code.
      But they have a cost ; like a lack of encapsulation & information hiding that can have long term effects in terms of code maintainability, bugs caused by side effects etc.

Comments are closed.