Hello.
In this video, we're going to talk about Question 30, what are immutable types and what's their purpose?
Immutability of a type means that once an object of this type is created, none of its fields or properties
can be updated.
Let's see a simple, immutable type.
The objects of this glass are immutable because the X and Y properties do not have stars.
Once we create an object using the constructor, it will not be possible to modify it.
As you can see, the concept of immutable thought is very simple it can extend to more complex types
than point, for example, collections.
Once we create an immutable collection, no element can be ordered, removed or altered.
The question is why should we bother creating immutable types?
First of all, immutable types let us create clear and simple gold.
Let's consider this.
What do you think will be printed from the fourth line?
Well, it's impossible to say because we don't know what happens in some mimetic and some other method.
Maybe they simply redid the values of the point, but maybe they alter it.
We won't be sure what the code does and how it behaves until we follow the flow of the code very carefully
checking what exactly every method does with the point object.
If the point was immutable, we wouldn't need to worry.
We would be sure that once it's created, its value remains the same.
And we would know right away what would be printed to the council.
Another thing that immutable types make simpler is writing pure functions.
Pure functions are functions whose results only depend on the input parameters, and they do not have
any side effects.
They don't alter the state of the application and they don't modify the input parameters.
We can call a pure function anytime we want with the same set of parameters, and it will always yield
the same result.
Because of that, we can cache the result, making the parameters the key of the cache.
Pure functions are simple to understand, as we don't need to be aware of the context in which they
are called.
Testing is extremely simple as we only track.
The result is as expected for testing purposes we don't need to set up and the context in which those
functions work, as they only depend on the input parameters and not.
For example, the state of the class they live in, using immutable types and creating pure functions
work very well together.
And actually, there are two tenets of functional programming, according to the IGN, that grows more
and more popular for its clarity, as well as working great in multi-threaded applications, which actually
leads us to the next point.
When working with multi-threaded applications, we must always be very cautious when it comes to making
assumptions about the state of an object because it can always be the case that another threat alters
the state without our knowledge.
Using immutable objects wipes this program out.
If an object cannot be altered, there is no need to worry that some other threat altered it, right?
This makes the creation of multi-threaded applications much simpler and less error prone, and you must
know that the finding box in multi-threaded applications can be extremely hard as they often happen
in our non deterministic manner that can be extremely hard to reproduce.
Another benefit of using immutable types is that they make it easier to avoid invalid objects.
To understand this, let's consider this class and its constructor.
Someone clearly put up a lot of work to make sure that when an object of the person cross is created,
it is valued its ideas correct.
The name is not empty and they are of birth is reasonable.
If objects of the person class could be mutated, it would mean that at any time of the application
execution, they can be rendered invalid.
If objects of the person class are mutable, we never know when someone changes some of its properties
to something invalid, for example, like in this line, if the person's class objects can become invalid
at any moment.
That would mean that our code would quickly become filled with lines like this.
This is not only a code duplication because we already define the districts in the persons class constructor,
but it also creates a lot of notes in the code, making it harder to understand, maintain and dust.
Because whatever we want to test code that uses this type will have to write tests for both valid and
invalid person objects.
The easier testing is another benefit of immutable objects.
But before we move on to respond, let's consider another aspect of making objects invalid, which is
identity mutation.
Imagine we want to use the person class object as a key in.
Additionally, we want to use the person's ID as the hash code that the dictionary will use.
This looks simple, but what if the idea is mutable, we can actually lose this object in the dictionary?
Let's see this in practice.
Let's see what happened here.
First, we used the person object as the key in the dictionary using its ideas hush code, then the
idea changed.
Because of that, this line throws an exception because in the dictionary there is no key, with the
hash code coming from the new ID string.
The only key release is the one build move.
The old idea.
As a rule of thumb, if an object is meant to be a key in the dictionary, it should be immutable or
at least those fields and properties that are used in the good hash called method.
The last benefit of immutable types that they wanted to mention is easier.
Testing immutable objects make it easier to understand, and they also give us a guarantee that once
a valid object had been created, it will remain valid forever.
This makes testing much simpler because we have fewer puffs of code to test us.
Handling of invalid objects is simply not needed.
As mentioned before, using immutable objects make it easier to create pure functions, and they are
extremely simple to test.
All right.
Seems like immutable objects can be really beneficial, but following this, nothing of a tenuous rule
can be demanding.
After all, we sometimes need to change something.
Let's consider the daytime type, which is immutable in c sharp.
Adding seven days to January the 1st won't make it a different date.
It will produce a no doubt.
It makes perfect sense.
After all, a date never changes tenders and worry.
The first 2022 will always be January the 1st 2022.
Such apparent modification of immutable objects is called a non-destructive mutation.
It is an operation of creating a new object based on another immutable object.
The immutable object won't be modified, and the result of these non-destructive mutation will be a
new object.
We'll learn more about it in the next lecture when we'll talk about records.
All right.
We learned what immutable types are and what are the most important benefits of using them.
But we must also be aware of the important disadvantage they have with the non-destructive mutation.
Each update of an object actually creates a new object allocating new memory.
The old object must be cleaned up by the garbage collector.
It is usually not an issue of small types, but remember, even collections can be immutable.
Imagine having a list of milyong elements and that adding a new item to this list means actually building
a whole new collection of size million and one.
It may sound scary, but don't be discouraged to use immutable types.
First of all, there are implementations of collections that actually make this quite efficient.
Second, not all applications suffer from the performance loss when using immutable types, and the
benefits are often bigger than the costs.
Garbage Collector is a smart tool, and most often you won't even notice the performance impact of introducing
immutable types.
Nevertheless, there are cases when performance is critical.
For example, I wouldn't recommend making every type immutable when developing video games, as it would
make the garbage collector kick off more often.
And remember that when garbage collector works, all other trends are frozen until it finishes.
In the case of games, it could lead to a performance decrease that would be noticed by the players.
During the interview, you can hear questions like What are pure functions, pure functions or functions
whose results only depend on the input parameters, and they don't have any side effects, like changing
the state of the class they belong to or modifying the object passed as an input.
What are the benefits of using immutable types?
The code using immutable types is simple to understand.
Immutable types make it easy to create new functions.
They also make it easier to work with multi-threaded applications, as there is no risk that one thread
will modify a value.
That's the other thread is using immutable objects retain their identity and validity.
Mutable objects make testing problematic.
This thing, called using immutable types, is much simpler.
What is the non-destructive mutation?
The non-destructive mutation is an operation of creating a new object based on another immutable object.
The immutable object won't be modified, but the result of this apparent modification will become a
new object.
The real life analogy could be adding seven days to a date of January, the first.
It will not turn out the date of January the first, but it will produce a new date of January eight.
All right, that's it in this lecture.
Thanks for watching and see you in the next one when we'll talk about records.