C# and FP – episode 2 – OOP today, (im)mutability.

Now it’s like nobody knows me…

Thomas – The Ogre of Athens (1956)
Image from clickatlife.gr

Introduction

An object’s state, describes its current condition. If object was a human, this could be their emotional state (e.g happy, sad, angry). To think a bit further, the height value in an object’s field is not a state, but the height’s value help us to define it’s height category (e.g tall, short, average), that is actually the state. And it is the state, based on the context we, as society, have set on defining when someone is considered tall or not.

An OO software system is an objects graph. Objects exchange messages, and based on the message content an object changes it’s state or not. Other objects do not directly read or write the state of another object. The state itself, must never leak out of the object to another. In short, objects do not share any references to state, but instead only report to each other with copies of stateful data. In this fashion, each object is the sole manager of its own state.

But this only half of the story. If we are too strict with the state management, we will not finally have a graph of objects, but rather a strict flow of composition (object B has no meaning or purpose in the system without A) or aggregation (B exists independently (conceptually) from A).

The state will be concentrated to a root object, itself composed by a number of stateful objects, which in turn may be composed of stateful objects. The objects of this hierarchy may pass messages to their immediate children, but not to their ancestors, siblings, or further descendants.

How fragile is the object’s state?

I am sure, that all of us, have worked with source code of a flexible or nonexistent hierarchical pattern at all. We need indeed to be (maybe more) flexible in our everyday work, and the source code is a part of it, but we might end up with objects leaking state.

But a wonderful clean hierarchy is needed when the specifications ask for it. While we organize our objects into a specific hierarchy, the state management is enforced to follow the same pattern. If our business logic is something trivial, probably we will have no trouble at all.

How do we handle use cases that involve multiple objects that are not directly related? One solution could be a common ancestor, other possible solutions is to use the Mediator pattern of the actor model. The larger the software, the more complexity there is, so we have to be really careful with the objects state management. Shallow hierarchies as possible, allow us to successfully manage state [2]. For more information please read here.

Types of immutability

Immutable

Object itself has no fields or properties that can be changed. This object will remain totally unchanged through its life cycle. For reference types the field must be read only and the type must also meet the immutable guidelines. Types that meet the immutable guidelines, are inherently thread safe. They are not subject to race conditions because they cannot be changed and thus viewed differently between threads [1].

Jared Parsons

Shallow Immutable

The direct contents of this object must be immutable in the sense that they cannot be changed to point to another object. However there is no guarantee that all of the fields are themselves immutable. All of the fields in the object must be read only. For primitive values this is enough to guarantee them meeting the Immutable guidelines and hence Shallow Immutable. For reference types this will ensure they will point to the same object thus doing all that is needed to meet the Shallow Immutable Guarantee. Types with this guarantee are thread safe to a degree. They are thread safe as long as you are accessing the fields with meet the Immutable guarantee. You can also access the references which are read only as long as you do not call any instance methods. For instance a Null check is thread safe [1].

Jared Parsons

Shallow Safe Immutable

Slightly stronger guarantee than Shallow Immutable. For all fields that are read only but not immutable, the type must be thread safe. Types which meet this guarantee are thread safe also to a degree. They are as thread safe as the fields which are guaranteed to be thread safe [1].

Jared Parsons

Benefits and a drawback of immutability

No matter if we decide to use immutability a lot, or enforce it, or use it partially, the goal has always to be the minimization of state usage. Immutability is not a binary decision. It depends on the requirements we have in hand. There is not a specific decision threshold on picking strict immutability or not. The benefits of immutable objects could be summarized to the following (probably incomplete list):

  • Immutability prevents null references
  • Immutable objects are thread safe
  • Immutable objects fight temporal coupling
  • Immutable objects have have failure atomicity*
  • Immutable objects always return new objects and not copies
  • Immutable objects and caching. The cached object is the same with the one in the execution flow. It doesn’t change.

* An immutable object throws an exception, it will never result with the object left in an indeterminate state. This term was coined by Joshua Bloch.

However, if a modified version of an immutable object is needed, we have to suffer the penalty of creating a new object. This must be handled with care in terms of Garbage Collection (GC).

Examples of immutability

Instead of writing numerous examples, I provide you Jon Skeet‘s excellent presentation about immutability. He explains, in details with source code examples, whatever is mentioned, more or less, in this article.

And a bonus video regarding the fundamentals and how data structures affect the way we develop. They effect immutability as part of the big picture.

Conclusions

Immutability is like a big ship. Skills are needed to pilot it, but you have to be careful. A good solid knowledge of OOP is necessary. Try to keep the objects graphs as small as possible, and minimize the dependencies between the objects. We will discuss about the degree of dependency in a future article.

The next episode

In the next episode, we will discuss again about the object’s state and its (im)mutability and its relationship with Inversion of Control and especially with Dependency Injection.

References

[1]. https://blogs.msdn.microsoft.com/jaredpar/2007/12/04/types-of-immutability

[2]. https://wiki.c2.com/?ShallowHierarchies

Who really appreciates software developers?

Why can’t it be like before? Please don’t go. Stay with me tonight. Let me borrow you.

Bai Ling – 2046 (2004)
Image from patrasevents.gr

Introduction

Computer science is a relatively young science compared to others, like math, physics, chemistry, medicine etc. Especially computer science, as we know it today, started its development since Alan Turing‘s era and work. And till today a lot of people do not understand, nor appreciate the work we do and the results we deliver.

Think yourself, a software developer, sitting at a table with other STEM people. And suddenly someone comes at your table and ask what do you do for a living. Who do you think will have to provide the most information about their work? The doctor? The chemist? The architect? The mathematician? You got it! The software developer!

Oh you work with computers!

When I was younger, and sometimes even today, I had, and I have, to explain to older people what I do for a living. The concept of a computer device that does almost anything was too big for them. Especially when I told them I program them!

Unfortunately the same thing happens with some (much) younger people. It is uncanny to me that a young person doesn’t know how a computer and internet work, and how a software is developed. No, no, no I am not talking to know in depth the details of our work, but simple general things, like the fact that a programming language allows us to instruct the computer’s CPU to execute some calculations and save the results to the RAM or the hard drive! And no, I am not exaggerating! Unfortunately I don’t keep a diary with all the crazy things I have heard all those years.

The next big thing idea!

Another thing that I don’t like, is people who believe they have the next big thing idea and need a software developer to make it for them. And I feel really sad about the developer who will go down the rabbit hole listening to bullshit from people who think that software is developed with magic and want to underpay. And you end up explaining to them all the fallacies, the limitations, the cost and the human resources needed, they put themselves into the denial domain and return with counter arguments that make no sense! The worse case scenario is that managers and bosses do the same. And if you have to face them 9-5 every day…. oh boy!

Oh come on, you just press some keys

And we come the worse point of human interaction between a software developer and anybody else. The ones that try to humiliate you, to make you feel bad by degrading your profession. Well if our jobs was just to press some keys then everybody would it. And unfortunately this toxic behaviour is not only against software developers but almost any profession. I have seen people bossing and deriding doctors! There is no limit for those people!

Rate my setup!

I really enjoy when people upload their setups at 9gag and ask from the other users to rate their setups. I don’t care if setups are actually their or they do it for points and the comments, but I have seen great setups. And I have been asking myself “Why don’t we see similar setups at the offices?”. Yes, the cost, I know. You can’t buy really good chairs and desks for 20, 50, 100, 500 employees and I can’t accept working seating to a shitty chair and to a horrible desk. Our work, is mental, and this means our body must be relaxed. If it is impossible to seat properly, or after a time my body is pain, I will not be able to as productive as expected.

The open space hell

The original idea of open space offices goes back in the 50s by a team in Hamburg, who thought that this would help the communication among employees.I worked at an open space office, and no it didn’t help at all. 60 people in a huge room, all of us with headset on in order to survive the noise. Add the chit chat, the long conversations, I don’t know how this open space thing helps software developers to be productive. At least it didn’t help me. A prefer working in a small office, three to four people max.

We are invisible

Ask your non software related friends if they know five people. Artists, politicians, athletes and one the names let it be a computer science person. How many do you think they know the CS person? We “know” each other, through blogs, books, magazines, you tube, conferences but we are completely out of the mainstream media. Maybe in the States things are better, but related to what mainstream media show in the 195 countries around the globe, I don’t think we are a priority.

In the end who appreciates us?

In my humble opinion, I believe that we still have a lot of milestones to achieve till people understand us better. TV series like The Big Bang Theory and IT crowd depict the truth more or less. The bottom line is that scientists in general are not appreciated enough, it’s not just us the source code riders. This article could last forever trying to analyze why this is happening, but I leave the rest to you!

The cover letter bullshit!

The animal that threatens us is a “cat”. The most dangerous animal there is. It eats meat, children’s flesh in particular. After lacerating its victim with its claws, it devours them with sharp teeth. The face and whole body of the victim.

The father – Dogtooth (2009)
Image from rp.news

Introduction

So, what is a cover letter? A letter with additional detailed information about you, on why you are qualified for the job. You should include specific information on why you’re a strong match for the employer’s job requirements. It actually is a sales pitch of yourself and skills.A cover letter usually, accompanies your resume you send out. Possible employers use them as a way to screen you, and determine if they would like to interview you. Cover letter is from the 50s, it’s an old idea that has to be retired.

So if I match or not will be determined by a piece of paper? Yes and no! If you are not included in the next steps of the process, it will be why you failed to impress them with your cover letter, even if they never met you! On the other hand, if you are successful to the next stage and meet you, they might use your cover letter against you, like in the movies: “Everything you say will be used against you….” Even a spelling error might be your failure!

What you should write in a cover letter, and why it’s pointless

A cover letter is to highlight the qualifications you have for the job: Then why is the CV for? Why should’t I add more details into my CV? And by qualifications we mean what? Technical qualifications? Personality qualifications (team player open minded etc?)

Explain why you want to work for the specific company/organization: The answer is simple. I want to work because I have to make a living, put food on the table and pay the bills. If I were a millionaire I would have other priorities. Yes, yes, I know… We have to strive for excellence, to become better professionals and try to work for the best employers like Google, Microsoft etc. Well true, but are you sure that you really fit into their world? But in order to work for them, you would lie for sure!

Add well formed enthusiasm as a selling point: This is true. I have done it. And is the biggest lie you have to write! To be enthusiastic about what? About their code base that I haven’t seen, the colleagues and the managers I never met, the company’s culture that is completely unknown to me? If I have to be enthusiastic about the job description, the big salary (if there is one), the company’s name or the size, I will pass! What is the point of working for a big company and suffer cause of toxic people around you?

Behind the curtains

Are we completely sure that an HR manager, HR team member, recruiter spends time reading a CV and a cover letter of dozens or hundreds of candidates? I believe not! But I am sure that they use the cover letter as leverage to push candidates more. They don’t want the best ones, but the most obedient ones!

One thing I have never understood is that how a HR person/recruiter knows if the candidate writes the truth or lies in the cover letter? How do they verify that? Also, which are their priorities? How do they evaluate their candidates? What is their evaluation process? Do they have one? And if the do, you as a candidate should be aware of it!

The best way to know a candidate, and a candidate knows the company, is to meet and talk! It doesn’t matter if it via Skype or in person, but there has to be a meeting! Cause in the end what you actually hire, is a personality that has to work well with other personalities.

Enough with the recruiters and the HR circus

I respect every profession, from the simplest one the most complicated one, but under one basic criterion. To offer something to our every day life. HR and recruitment, as it is applied today, is a completely abuse to everybody and leads nowhere.

  • What is the point a recruiter with no IT background at all, asking me IT questions?
  • Who writes the job description? A technical person how probably knows their shit, or a recruiter?
  • What is the point talking to a recruiter that works for a completely different company than the one I applied for? I will never see them again, but they decide my fate.
  • Why can’t I just talk with the people I am going to work with? If they don’t have the time to do it at that moment, how can I be so sure they will spend time later to train me, if I am finally hired?
  • How do I know the recruiter is really a professional who do know their job by qualifying the best (that they actually don’t know who they are) cover letters, and not the ones they like based on their personal taste?

Conclusions

Interviews in general suck today a lot! Cover letter is just part of the problem. Another problem is the online coding tests platforms. But this is for another day. In order the companies to find an efficient way screening the candidates, they ended up humiliating them by adding useless processes besides the interview itself.

I totally respect the interview and evaluation process when is done correctly, and by correctly I mean with respect to the candidate. I want to work for a company, that primary respects me as a person, and then as professional. I am not a soldier at a military campus training and being evaluated daily, and I don’t want to be scared at the same time, that if in the end of the day my results are poor, I am going to be kicked out.