Our life is like a code or the proper use of comments in the code

Whenever you are starting to learn a new programming language it is always definitely somewhere in Chapter 1-2-3 will be told about any special characters that would make a comment. According to Wikipedia: In computer programming, a comment is a programmer-readable annotation in the source code of a computer program. They are added with the purpose of making the source code easier to understand, and are generally ignored by compilers and interpreters. The syntax of comments in various programming languages varies considerably. But at the initial stage you never really understand why do we need comments.

So as soon as I clarified this for myself – I hasten to share my thoughts here!

So why do we use comments in the code:

  • for debugging
  • for making a plan of writing the code
  • to work together on code
  • for creating a documentation

Let’s consider all individually.

First of all, the very first use of a new programmer comments are used to hide the code. Hiding the code comes to find errors or to understand what works and what does not. Or, for example, when attaching javascript file to the html file we have learned to do checking. For this you write something like:

1

Other words if the file is connected successfully, the window appears with the word “Test”

2

Our whole life is always under the plan or scenario. Here for example what steps are you making to get from home to work?  Get out of the house, get to the stop, waiting for the bus, ride 10 stops, go down in subway, waiting again, now for a train, ride five more stops, get out, go into the building, go up in the elevator and here you are, at work! The steps are very simple, but important! And forgetting about one or more steps is very easy, and is like in life in coding, drafting a “scenario” of code step by step is very important. It’s just a reflection of the programmer that the code supposed to do step by step, but not too detailed. This will make it easier to create the code and do not forget what it is about.

3

When writing code for such a “scenario” code remains largely understandable only to the programmer who wrote code. Sample of code with a comment below:

4

But what if you work on a large project with a team?

In this case optimally to explain almost every line of code, except of course the easiest and most obvious. The example below

5

Well, now code now is more understandable. You can also plan writing the code in the comments as we did before, but for the team now. So that they have can implement the code based on the idea of senior programmer.

And last but not least! Comments for documentation.

In Visual C# you can create documentation for your code by including XML elements in special comment fields (indicated by triple slashes) in the source code directly before the code block to which the comments refer, for example:

6

When you compile with the /doc option, the compiler will search for all XML tags in the source code and create an XML documentation file.

XML-comments help solve many problems. Comments to the code, user manuals, test plans and a variety of other documents can be generated from a single source via the XML-tags. The possibility to store information about the code in the code itself – is very convenient. This solution makes it easier to change the documentation along with the source code.

Well, that was all of my thoughts on this subject. Thank you for reading this blog and don’t hesitate to ask questions if you have any. 🙂

© 2024 Helena Boitsova