C# Tutorial - 7. coding convention (concise code) in C#
Welcome to the seventh lesson of my Visual C# Tutorial. In this lesson, we are going to look at one of the C# coding conventions which is concise code. It is very important to learn how to make your code concise as readability and efficiency of your code depend on it. Therefore, when you find yourself write the same code over again and again, it might be better to take a close look at your code and see how you can make it compact.
Let's take a look at the following code example. Just in case you don't actually understand how to declare if statement, you can simply get back to the Last Lesson where I talked about syntax of if statement.
In this code example, each code block in the if statement has the same methods repeated, which are Console.WriteLine() and Console.ReadLine(). In order to make this code concise, we can simply take what is common and focus on what is different in each of these code blocks. These code blocks are only different in the literal string used in WriteLine() method. So let's re-factor the code to make it a bit more compact.
As you can see, I declared new string variable called message with empty literal string at the top. This empty literal string can be replaced with any other literal. Then, each code block set the literal string of variable message accordingly. It means that the literal string will be different depending which of the conditions is true. Then I call the WriteLine() method with the variable message passed through parameter at the bottom. it will display whatever the value of variable message is.
Tips. As you may notice, I got rid of those curly braces of each if statement. You can only take the braces away when only a single line of code is there in if statement. I covered it in the Sixth Lesson.
Now we have very concise code which works the exact same as the previous one, but I would like to talk about another way to make this code compact. Let me first comment those lines of code we wrote out and write the following code to see how it works.
Tips. You can easily comment out any number of lines of code by selecting the code and clicking comment out button in the toolbar at the top.
As you can see, I declared the string variable message with different code assigned, (userInput == "1") ? "a new car" : "nothing". Basically this code evaluates the value of the variable userInput, and if it is equal to "1", then the message is set to "a new car" while if it is anything else except "1", then variable message will have the literal string of "nothing".
Then, the next line of code is an optional version of WriteLine() method. It actually uses string replacement syntax. With this method, we can a number of different values that will be added into the literal string. Basically, the "{0}" in the literal string will be replaced with the value of the first variable after ",". It is actually zero based.
As you can see, the "{0}" has been replaced with the value of the variable message. Then let's see how to add different variables into this method.
Now this method will display two values from string variable message and variable userInput as following.
It can be illustrated as following. Notice that it is actually zero-based, So if you have declared like this "Console.WriteLine("{1}", variable 1, variable 2)", it will display the value of variable 2
Post a Comment