Dec 9 - Part 2
The dates on the posts have got into a weird state.
It reflects my staying up to epic hours of late - usually working. And writing some shit in the wee hours of the morning.
Winter has finally arrived. A sharp toothed cold grips the city, and even at the peak of day, a sheet of ice sits on the car. It took me 5 minutes at 2pm to clean the ice off. Yikes.
On such a cold day, out with the lovely Athena, she of course thinks this is an excellent time for a dip in the river. Repeatedly. Wading up til its up to her chest.
She seems utterly oblivious to how bloody cold it is, and trots out of the water, tail up, a bounce in her step, water dripping from her belly. What ? Water. Good.
Is that not cold you idiot ?
A passerby smiled and called her brave.
Athena was unphased.
She was on good form today, having a bit of a run, trotting a lot, and being very investigative in her explorations. A bit like the old days. Where she would disappear out of sight 500m away.
The cold weather always suited my mutts. The hot weather they never liked. The cold weather - aces. Despite being short haired. Well. Athena is technically short haired. In practice. Some of her hair is long enough you can run your fingers through.
Hazel was back from her Dads. And accompanied us on our walk with Poppy.
Poppy opted sensibly not to go in the freezing water. She watched Athena. Was not having any of that cold nonsense.
Hazel asked how I was doing. Ok. Quiet. Uncharitable.
She listened for a bit. Did not seem surprised at the exhausting of my patience with Humanity at large.
Uh huh.
I pondered out loud to her whether I had finally become a cynic.
She didn't offer an answer.
Other than that, haven't managed to achieve shit today. Noodled around with some code. Pointless, stupid code. An easter egg in the charity app. Just because. It has dudes. And high performance timers. And animations. Heh.
I came up against some limitations in .NET itself again today. In very technical terms, the inability to overload an operator for a struct already defined in the framework. Which. Is pretty shit. Also fairly niche problem I admit. But. No actual way of giving an operator to a struct, beyond defining a new struct yourself. Which. Sorta defeats the point.
What, you are probably not asking yourself, is operator overloading.
In computer terms. An operator is.. an operator. Very informative. An operator is a "character that represents a mathematical or logical action or process". Which is a fancy way of saying, its what things like a plus, minus, divide by and multiply symbols are. So. 5 + 4. The operator is a +. The operation you are doing is addition. So. See a plus symbol. You do addition. It's a short hand.
As a non computer type person, you have been taught that when you see a plus symbol you need to add two things together. In simplistic terms. 5 + 4. You know you need sum those two numbers together. This is so ingrained within you, you probably don't even think about it. But. That plus symbol could be anything. You've just always been taught that + means addition. But. We'll get back to that.
For the moment. 5 + 4. You know that means add two numbers together.
Great.
In computing the same happens. But. What if it's not just numbers you are talking about. What if you had something like
Red + Blue
?
You might infer, oh, well, that means combining colours. So the answer would be... Purple ?
But very quickly you start to get out of traditional mathematical terms into... well... arbitrary undefined spaces.
Croissant + Jam = ?
Who knows ? Jammy Croissant ? Breakfast ? Jam dipped pastry ?
This is where an operator overload comes in.
What does overload mean ?
In terms of computing overload means, give more than one definition of, or more specifically, give more than one interface of.
Take a simple method.
Add(a, b) {
return a + b
}
This adds two numbers together.
So when called, Add(5 , 4), returns 9.
We could overload the method of Add with :
Add(a, b, c)
{
return a + b + c
}
If we call Add(5,4) it calls the first one, if we call Add(5,4,3) it calls the second one.
Two different bits of code called the same thing. Add.
This is overloading. Add has more than one definition ( interface ).
So operator overloading. To.. have multiple definitions... of ... something like a plus symbol.
In other words, we can tell something what it should do when it sees a plus symbol.
If you wanted to be very contrary, and something thats not a good idea, you could overload plus to be minus.
So anytime something like 5 + 4 is stated, the code asks, what does "+" mean, and you could return and say, it means you subtract.
5 + 4 ? Well. That equals 1. Because it's really doing 5 - 4.
Because you overloaded the operator in some horrific unintuitive way.
Operator overloading is a very powerful bit of effectively syntactic sugar ( syntactic sugar meaning a nice way of saying the same thing in a different way ) for handling variables. It means you can do something like
apples + fruit
and instead of the program saying, WTF I have no clue what you are talking about, it returns a result of
fruit salad.
Which makes coding sooooo much easier.
Let's take a real world example. Let's say we have a coordinate system, x and y. And we have that nicely in a stucture.
Location {
int x;
int y;
}
Cool. It describes a point in 2d space.
Imagine we have two Locations. The first is 5,1. The second is 9,4. How do we get the difference between them ? You would need to do something like
LocationDiff.x = LocationA.x - LocationB.x;
LocationDiff.y = LocationA.y - LocationY.y;
Which whilst not the end of the world, is a faff.
It would be nicer if you could just do.
LocationA - LocationB
Which is where operator overloading comes in. You can get a whole bunch of crap done, just by specifying minus.
Intutively it makes more sense ( if you've done it right ). It's quicker to code. Less maintenance. Less bugs. Reads nicer.
Big Win.
Of course. Abused, or over used, it becomes an absolute horrific nightmare of never knowing quite what is going on when you see something as innocent as a plus symbol.
The horrors of this are down to something like this :
5 + 4 = ?
Given sneaky operation overloading, the answer could literally be anything. 5 + 4 = 9. 5 - 4 = 1. 5 * 4 = 20. 5^4 = 625. And so on.
Skimming a piece of code you might assume that 5+4 of course = 9. And wonder why the hell your system keeps outputting 625.
Because some ass did something naughty with an overloaded operator.
But nevertheless, in the right hands, for the right reasons, it's super powerful.
There. Now you know what operator overloading is.
Comments
Post a Comment