C# Sharpen Your Sword

Do you know C#? Yes. I do. How long have you been using it? 4-5 years. I am an experienced C# developer, sir.

While interviewing candidates for C# developer position, I came across many CV with saying kind of the same thing. Candidates claim that they are experienced C# developers. I am sure they are. Many of them are. But, unfortunately, there are some who misunderstood themselves.

Why was that? Regardless of C# evolution, they kept writing the same code as if it was C# 1.0. Many just use C# to write basic code such as class, if statement. They use basic data types not even care or know that there are better versions of data structures that can do a better job.

Many experienced developers have problems with writing code that access database, manipulate XML, read files, … The forever argument is they will google when they need it. Ok, that makes sense. Oh no! experienced developers ask google for basic operations?? Hmm that does not make any sense at all.

One day I realized that I am a C# developer, too (Just kidding I know I am a C# developer, writing C# code for a living). I started to ask myself

How much do I know about C#?

I know some. That’s all I can admit. With the thinking flow, I thought many C# developers have that problem; they are not aware of how much they know about C#.

I decided to compose a list of fundamental that every developer should know about C#. Not everyone uses all of them in their daily job.

There are 2 purposes when I composed the list

  1. Self-improvement: I want to improve my C# skill regardless of where I am now. I want to have a strong foundation.
  2. Help others: I have developers at my workplace. I want to help whoever accept my help. Remember that helping is growing, a perfect win-win situation.

 

Primitive Data Types and Keywords

What are differences between integer and float data types? between float and double? And when to use what?

The problem: many do not think twice before deciding a data type to use. What primitive data type do you use to hold a person’s age? Probably it is int, right?

What are checked and unchecked keywords used for?

String Manipulation and Regular Expression

Developers deal with string more often than they thought.

  1. Concatenate strings
  2. Format string with Join
  3. Format string with Format
  4. Find string with regular expression
  5. ToString(): when displaying a value (integer, date, currency, …), we convert it to a string.
  6. String and Culture

The regular expression is an exciting, sometimes headache, topic. How many ways are there to check if a string is an integer?

Protection Levels

Hey, do you remember these: Private, Protected, Internal, Protected Internal, and Public?

The problem: Do not know how to take advantages of the language to protect data correctly.

I have seen developers have a habit of using public for methods and properties, private for fields. The public modifier is used because it is the easiest, which allows a function can be consumed everywhere.

When was the last time you think of using Internal, or Protected Internal?

Collections and Concurrent Collections

How many types of collection do you know? And when to use what? List, Queue, Stack, Dictionary, Array, ArrayList, … Oh how about the readonly and concurrent versions?

The problem: Not use the right collection. Usually, developers take the easiest one instead of a proper one.

Using the right collection is very important. Because it will protect the data from unexpected access, unexpected modification.

The most common use is the List<T> class. Because it does not require any thinking process.

To use a proper collection type, at least a developer should consider these questions

  • Is it ordered?
  • Access pattern? FIFO, FILO, Random, by index, by key, …
  • Is it readonly?
  • Do we need to keep it after processing all items?
  • What are the most common operations (business operations) on that collection?

 

Threading, Task, Parallelism, Async and Await

This topic is not easy at all. It is not required that every developer has a deep understanding; however, a fundamental is crucial.

The problem: Use without a bit of understanding how it works. This causes many nasty issues in production.

XML

Sometimes, we forget how to process an XML document. That’s sad. A direct XML processing might not be popular these days. But a proper understanding is important.

File and I/O

The problem: We do not know what we are doing.

When there is a need to read a file, developers use the first API available File.Open with just enough required parameters. Many times, they fail to ask

  1. What is the file encoding?
  2. What if a file is being opened by another process?
  3. What if a file does not exist?
  4. What if a file is too big?
  5. Do we need to load its content at once? or should we process line by line?

Have you ever wondered those questions?

ADO.NET

The rise of ORM tools (EF, NHibernate, …) makes developers’ life easier when dealing with databases. That comes at a cost. Developers seem to forget how to interact with databases.

The problem: Do not know how to work with databases.

When doing interviews, I asked candidates about the SQL transaction in an EF application. They said EF does it all for us. Yes. It does. The next question, when does it commit a transaction? Answer: Oh, EF does it for us. Period.

Lacking the database fundamental is such a bad excuse.

LINQ

It is hard to say whether it is a fundamental thing. It came out with C# 3.0. Before, you can write code with for loop. Proper use of LINQ will improve the readability of your code.

Covariance and Contravariance

Same as LINQ. If you understand them, you are rock.

Dynamic

Not used often, but listed here for reference.

 

Having the list is a first step to sharpen my C# sword. The list works as an agenda, a guiding star. Some of the items I am good at, others I am a beginner.

Stay calm, sit down, put hands on the sword, move it slowly. That’s good! Let’s the game on.

Small Things Matter: Naming

Naming is hard. But we can improve and make a difference.

As developers, we write code for a living. Most of the time, we pay attention to solving problems, learning new technologies; kind of new cool stuff. We tend to ignore or look down on small things.

There are so many small things. Well, in reality, everything is small things. Big is just a sum of small things. Naming is one of them.

Dear developers, what is on your mind when you heard about “Naming”?

  1. Project name
  2. Class name
  3. Method name
  4. Interface name
  5. Property name
  6. Variable name
  7. Hardcoded value

They are all matter. Why? Because they help us__human being__can read the code. They help us understand the logic by just looking at the signatures.

In the technical jargon, we call readability.

Let’s take a look at “Hardcoded value” and “Variable name”. They are pretty much the same thing. I have seen many places that developers name a variable: b1, b2, … They have the same problem:

They give no meaning at all

Number Cannot Speak

Take a look at this code. I am sure all C# developer knows

    public class BadNaming
    {
        public void CanYouUnderstandTheLogic()
        {
            Thread.Sleep(180000);
        }
    }

The code says that: Should sleep (block and wait the current thread) for 180000 milliseconds.

What are the issues with that code?

  1. As a reader, I have to convert 180000 to a meaningful concept that my brain can process, Ex: Second, Minute, Hour. As a human, we do not usually deal with milliseconds.
  2. What is the logic behind the sleep? Usually, developers will write inline comments. (Hint: You should do a quick search and will know that inline comment is not a good approach)

The number itself cannot carry meaning. It cannot speak what it means.

Give It a Meaning

Now take a look at this code. What do you think?

public void GiveMeMeaning()
{
    var sleepThreeMinutes = TimeSpan.FromMinutes(3);
    Thread.Sleep(sleepThreeMinutes);
}

The Thread.Sleep method can take a TimeSpan. Usually, developers forget that option.

With a little change, I can assign meaning to a number. The code can self-explain its meaning.

No matter what languages you are using, there are primitive types that convey, naturally, no meaning. When you use them, think about the other person (maybe you in a year) will read that code. They have to spend time investigate the meaning.

I just pick a small corner of the problem with the hope that you get the idea and pay more attention with your code. Someone will thank you and call you a professional.

Professionalism is nothing than caring for small stuff.

Support serialization with Expando object

Recently I have a chance to work with project that need to store dynamic data, work with dynamic data. The data will be stored in RavenDB. After some considerations, I decided to use this implementation from Rick  Strahl. Everything works fine, except saving to database. It is store as empty object in RavenDB.

Just add these lines of code will solve the problem


public override IEnumerable<string> GetDynamicMemberNames()
{
return Properties.Keys;
}

The end result is nice

ExpandoRavenDB