EMMA

A Catgirl Software product

Recipe Blogging: Setter and Getter Functions

CW: suicide, questionable mental health things

In programming (especially in object-oriented programming), the concept of getter and setter functions, sometimes called accessors and mutators, appears frequently. A getter function is simply a function that can be used to get a piece of data, like the value of a variable; likewise, a setter function can be used to set that data. The fundamental goal behind setter and getter functions, in my mind at least, is limitation and control of access. Most of the time, it is preferable that a section of code can access all of the information and power it needs to complete its function, and nothing else. Anything that can be accessed too easily can be accessed accidentally, or when it otherwise shouldn’t be; limiting access removes some of the potential for bugs, and can be used to enforce some base level of coding standards.

When I was fifteen or so, it fell to my year ten drama teacher to teach us something about mental health. This was presumably in conjunction with the PE teacher taking a week out to teach us about sex (and also alcohol, drug safety, how to have normal human relationships, and the fact that gay people existed). So we got split up into groups and given a topic to come up with a short scene about; me and another guy got paired up for Depression, one group had like… Schizophrenia I think? Neither my partner nor I were great, passionate actors, he was there for a bludge subject and I was there because I was interested in doing stage lighting, and neither of us actually had depression (that we knew of at the time; in hindsight, I'm less than confident in that statement), so we did our research and did a little bit about the impact of suicide on the friends and family of the victim. It was questionable, we were fifteen, go cancel me on the internet or something. One particular phrasing that came up in all of our looking at webpages was this idea of a chemical imbalance, this concept that depression isn't a consciousness thing, it's a your brain doesn't have the right brain juices so it functions poorly thing. Which we got conceptually but struggled to imagine or empathise deeply with, so that's what our scene was about - struggling to understand the experiences and choices of someone who, on some base level, thinks and sees the world in a different way.

Three years later, I found myself lying on my back on the concrete behind my house, and I thought about that concept again, and the idea that some people experience things differently because their weird brain chemicals aren't quite right. But, somehow, I still didn't get it, even though I had drunk alcohol by this point so I had at least that experience with similar concepts.

Getter Functions

Godot, the game engine I am working in at the moment, tragically lacks a concept called access modifiers. In other languages, like C# (the language I was using when working on Starwing in Unity), the data inside an object can be made inaccessible from outside. For example:

			class Sword {
				public string name  # this can be accessed freely
				private int value   # if your code tries to access this it will break
				
				public int get_value() { return value; } # but you can get the value with this function!
			}
		

Godot doesn’t let you make things private, so anyone can go around accessing the value of a sword and changing it however they like, without using the get_value() function at all. I really don’t like this, it’s my least favourite Godot feature.

The thing I use getter functions for most aggressively is error handling. Take the following example, fresh from Untitled Blacksmith Game (conceptually, at least); a Sword has some Parts that represent its components, and a Part has a Material that it’s made out of, and a Material has a Name, like “steel” or “wood” or “flesh”.

			func print_material_name(sword):
			# X.Y gets the property of Y from X
			# X.Y.Z gets property Z from property Y of property X
			var name = sword.blade.material.name
			print(name)
		

But the problem with this code is that a Sword object might exist without a blade Part, or without any Parts at all, or the blade Part might be created without having a Material set for it. If you try to get the blade, and that comes back with nothing, then your code tries to get the `material` property of nothing, and understandably isn’t able to do that. You're making a lot of assumptions and if any of them is incorrect in the wrong way, your code crashes.

So you use getter functions, and you add code that gives a sensible response if it's needed assumptions don't work. For instance:

			func Part::get_material_name():
			# If we have a material, give the name
			if material:
			return material.name
			# If we don't, the next layer up still wants a result
			else:
			return “none”
			# Could also just be “”, an empty string
			
			func Sword::get_material_name(part):
			# part is the name of the Part we are trying to check
			if child_exists(part):
			return child(part).get_material_name()
			# however, if we don’t have a Part by that name:
			else:
			return “none”
		

Now you can use the get_material_name function of a sword, and if you name a part that the sword has, and that part has a material, you’ll get the name of that material. But if either of those things is not correct, you’ll get the word “none”, which is much better than your code crashing.

The other thing that’s nice about getter functions is you don’t need to make them; if you have a private variable, and no function to get that variable, no one else is able to look at it1. So you get to define exactly what things people are allowed to ask your object for, or about, and exactly how you are going to answer those requests. The classic example is a Rectangle object, which has a width and a height, but you don’t have functions to get those, you only have a function to get the rectangle’s area instead.

Limiting the information that people are allowed to get feels right and comfortable to me. I have a weird habit of tracking and playing around with what knowledge I have made available to particular people; not as, like, a manipulation thing, I just enjoy weird logic puzzles and I guess it interests me to know when and how I am creating those logic puzzles for other people around my own life. Maybe that’s kind of fucked up? I don’t mean any harm by it, I just think it’s fun to follow. More fundamentally, though, I like having the ability to keep secrets, and to lie when I need to - the idea of someone being able to poke around my code’s inner workings and extract things they weren’t meant to be accessing feels wrong, in the same way it would feel wrong to have them do that to me personally. It feels like some combination of mind reading and some dude stabbing electrical probes directly into my brain, and it combines the worst of both options.

Setter Functions

Where getter functions are usually passive, setter functions are active. They do things. Mostly, they change a value, but they’re a good place for other things too.

			func set_name(new_name):
			name = newname
			
			func set_age(new_age):
			# setter functions can be used for validation - an age can’t be negative!
			if new_age < 0:
			print(“Error! Can’t have a negative age!”)
			else:
			age = new_age
			
			func set_text(new_text):
			# maybe we have other things to be checking
			if text_is_editable:
			text = new_text
			
			func set_temp_fahrenheit(new_temp):
			# maybe we want to change the data we’re getting set?
			# our code uses celsius because we’re cool
			temperature = fahrenheit_to_celsius(new_temp)
			
			func set_container_size(new_size):
			# what if there are other things that need to happen when this gets set?
			size = new_size
			send_alert(“size changed”)
			update_capacity()
		

Even more than getter functions, setter functions in my mind are about control. When someone tries to give you a piece of data, your setter function seizes it, and you can twist it and change it and look at it from every angle, and do whatever you like with it, because you own it and it can do nothing to stop you. You should probably set your own data using it eventually, because if your setter function doesn’t actually set things when it should other people will get annoyed and not use your code, and a lot of the time that setting is all you will ever do. But whatever happens, it happens on your terms, not theirs. And importantly, just like with getter functions, you don’t need to make a setter for anything; if outside code shouldn’t be allowed to change some of your data, just don’t give it a way to do so, and it can’t2.

Once again, please understand that Godot makes it practically impossible to enforce setter functions either, and this pains me deeply.

This is partially a comment on me as a person. I like to design my code such that it theoretically cannot be used incorrectly; obviously this doesn’t work in practice, but it’s about the principle. And the principle of limiting ways people can set data so that my objects can maintain their own correctness, even if someone gives them bad input, doesn’t really work as well if anyone can reach in and freely change things, avoiding the paths I made for checking that that change won’t break anything.

The Human Object Model

For a long time I had this idealised view of people and how they work. You have one object which is the person, the mind, the soul, the consciousness, and another object which is the body, and they’re mostly disconnected. The mind can use setter functions on the body (to move), and getter functions (to get sensory input), and sometimes the body can use setter functions on the brain (to say “I’m hungry!”). But the mind is the one that’s in control; you can ignore being hungry, you can notice it and suffer but avoid acting on it, but if your muscles start ignoring their setter functions then something is distinctly wrong.

This is a vaguely comforting model for me; I tend to struggle a lot with trust, and also with weird identity things, so the idea of my body being separate from me but still under my control is something I can work with and be happy in. I am me, my thoughts are my own, there are no influences I am not conscious of that are going to change the way I work. All of my brain’s setter functions are contained and isolated and I know where they are and they cannot control me.

Of course, I’ve come to learn that that’s complete bullshit.

I guess alcohol was the first thing that broke that model in my head. The first time I drank properly, I got kind of messed up by how completely it affected my mental faculties, but I was around (at the time) friends who were able to get me through it, and I learned not to worry or think about it too hard - until I learned that actually, I should have been worrying after all. THAT fucked me up for a while, in hindsight.

It hasn’t all been bad, but every now and then I come across something new that teaches me that my model of existence doesn’t actually work. In 2021 I started taking estrogen, and while it’s mostly around for the physical effects, it’s had a powerful, lasting effect on my mental state - emotions feel deeper, and fuller, and more flooding. A bit after that I started on cyproterone, which blocks the production of testosterone in the body, and that had its own effect. I genuinely feel less active anger without T; frustration, sure, but I don’t get as angry as I used to. Late last year I started on another hormone, progesterone, which has a wide variety of unofficial effects on the minds of transfem people (and presumably also cis males who start taking it I guess), but the thing it does to me specifically is it gives me really vivid, lifelike, memorable dreams.

“But Emma,” one might argue, “you should have known the hormones would affect your emotions, that’s why teenagers or other people expressing emotions get called hormonal!” But it’s not just those, it’s so many other things too. I’ve never had them myself, but I know people who have medication that increases their neurotransmitter levels, and that just works. I have friends who get prescribed lithium, and it helps them. Lithium! A fucking metal! And I am deeply thankful that they have medication that makes their lives better, but it kills me that something so simple actually does that. And it’s not even just external things, my own damn body has its fun little chemical rewards for spending time with my loved ones or going outside in the sun every now and then, and I’m not gonna complain about them because they’re pretty cool but it freaks me out to know that those things are there.

So… yeah, I get it now. Ten years after that drama class, I finally understand just how fundamentally those chemical imbalances can change things, and I hate it so damn much. I want to be in control, I need to be in control to feel safe, but I can’t escape the fact that I am a slave to the whim of a handful of chemicals, and not even particularly rare or interesting ones. I want the world to use the functions my mind has for it, but the world is set on reaching in and making its changes directly, and I am powerless to stop it.

How I wish I was written in C# instead.


  1. -ish 

  2. -ISH