Crap C# Set property

I haven’t wrote a nerdy blog with code for a while. So let’s discuss C# Properties. A basic example could look like this:

private int _amount = 0;

public int Amount
{
   get { return _amount; }
   set { _amount = value; }
}

Here we have a property called Amount and the value is a number (int = Integer). You can get the value to see what it is, and you can set a new value. You could test it out with code like this:

Amount = 50;
MessageBox.Show($"Amount: {Amount}");

We set the Amount to 50. Then display the value in a message box. So this will display “Amount: 50”.

I saw a poor implementation of this basic C# feature. When I have come across crap code, I’ve sent it to my Apprentice to see if he understands why it is bad. This one caught him out.

This example looks similar, but notice the set method. It has a hardcoded value of 0 rather than the C# keyword “value”.

private int _amount = 0;
public int Amount
{
   get { return _amount; }
   set { _amount = 0; }
}

So what happens when you do this?

Amount = 50;
MessageBox.Show($"Amount: {Amount}");

Instead of showing “Amount: 50”, it actually shows “Amount: 0“. This can easily lead to bugs, or simply confuse a developer for a few minutes. The only way the value can actually be set in that implementation is via the “backing field”, the variable called _amount. This code will show “Amount: 50”

_amount = 50;
MessageBox.Show($"Amount: {Amount}");

Bonus SQL

I saw some bad SQL which I also sent to my Apprentice. I sent him this, hoping he’d maybe test it out, or maybe read up on bit fields, but he couldn’t tell me what was wrong with it.

declare @VerificationStatus bit
set @VerificationStatus = 2

A “bit” stores a value 0 or 1. So how can it be set to 2?

When you set it to 2, it doesn’t give you an error, it just sets it to 1. Any non-zero number is set to 1. This wasn’t the intention of the developer, he should have used an int. If he had tested his code, he would have seen it insert the wrong value into the table.

Mentoring #3

I am mentoring an Apprentice who has never done C# before and this is his first programming job. So this is a diary of some-sort of his progression and my ability to mentor.

I found an example on the Code Review section of Stack Exchange by someone who was writing their very first C# program. I thought it was a good example to give to my Apprentice to test him out.

So I showed him the code, and asked him to explain to me what it was trying to do, if he could suggest improvements or spot any bugs. Basically doing a Code Review.

I thought he explained the code fairly well and struggled in places where the code wasn’t well-written. He did point out one bug and did hint at a few areas of improvement but didn’t seem sure on how exactly to improve it.

I then took him through some changes, and explained how the code could be improved. Most of the code was jammed inside one method so it was 300 lines long. I showed him how you can easily refactor the code into smaller methods using Visual Studio, and how to rename variables (with the advice of choosing descriptive names). The end result is code that is self-describing.

You can read the popular book Clean Code by Robert C Martin which discusses more ideas along these lines.

I told him it’s an important skill to read other developer’s code, since maybe you end up spending 90% of your coding time reading existing code (not sure of the exact statistic there, but it must be high). Also you will be doing it in the code review process.

He seemed to really enjoy it, and was grateful for my tips. I told him I will probably be testing him with more of these challenges, and I also expect to see him put my advice to use in his own code.

Mentoring #2

I think I’ll end up writing loads of blogs about mentoring my new Apprentice. It’s going to be an interesting journey for sure.

  1. He doesn’t know C# or SQL and that’s a prerequisite
  2. He has never used our software before
  3. It’s his first software job so knows nothing of the process

I explained to him that this team is really difficult, but historically, we have always thrown juniors into a team like this, much to the protest of many. This team deals with a wide range of bugs, and often requires good knowledge of requirements, and excellent debugging skills. We also get really tight deadlines, since we are given all the “Major Incidents” which can have 1-3 days deadlines. It’s unfair to put that much pressure on an inexperienced developer.

I told him it’s a “sink or swim” type approach, but strangely I can’t think of anyone that quit. When I was a Junior, we all complained about this, but yet everyone stuck around and became good developers.

I was telling him my long journey into development, and told him it takes years to get good, although maybe I was just slow because I was extremely casual. He reckons he can learn within a few months. I think he is ambitious, but I reckon he can learn like 10 times faster than I did. He can put more effort in, and I can guide him.

I was telling him how I haven’t been given guidance on how to be a mentor, but I can just freestyle it. I told him I’d like to be the mentor that he expected to have. I mean, he is an Apprentice and has been here a year without anyone actually being assigned to him. That is such a classic thing to do: hire people on the promise of training and good things, then they get here and they are never assigned any official training.

Mentoring Again

Background

A few years back, I was assigned an Apprentice to mentor. I said I would love to do it, but I questioned why the Senior Developers in the team didn’t have anyone to mentor when it is literally in their job descriptions.

At the time, my manager said that she thought I’d be the best mentor in the team, and if I do it successfully, then that is good evidence I can be promoted.

I think the mentoring went successfully, but I didn’t get a promotion.

Present Day

Fast-forward to the present day: My new manager said that an Apprentice is joining our team, and out of everyone, he reckons I would be the best mentor. If I do it successfully, then that is good evidence I can be promoted.

I said I’d love to do it. This time I didn’t complain about the Seniors not mentoring. I didn’t want to risk my manager reassigning the Apprentice. I have a good feeling my manager will actually promote me, but we will see.

The Future

I had a chat with my Apprentice. He has never done C# before; but it’s vital to our team. We already had a lack of developers – and a lack of skilled ones. Now we have someone who has never seen the program we are working on, and never used the language it is written in. He has come via a bootcamp that taught him basic Web Development, so that’s some wasted training.

This means that I’ll have to spend a lot of time training him, which means my productivity to bug fixes/enhancements will drop. Our team’s productivity was already low, and now we have another member which is actually going to decrease productivity. I hope managers realise this.

I think I’ll have to encourage him to learn as much as he can on his own. I’ve sent him a C# ebook which is pretty comprehensive. He has access to an online training platform to watch in his own time, plus all the rest of the free content on the internet.

This is another topic to write about on the blog. It can document me learning how to teach someone from scratch, and also document funny mistakes he makes.