Adding Comments

It's important source code to be readable. One way is to use whitespace. But another direct way is add comments to our source code. We add our note why we writing these statements. Well placed comment can make all different when programmer understanding source code. So we want source code to be well commented when we reading it, and that means we commented when we writing it.

One of the most common way to adding comment is just beginning a line with two slashes :

public class HelloWorld {

    public static void main(String[] args) {
        // Prints "Hello, World" to the terminal window.
        System.out.println("Hello, World");
    }

}

This work in some language like Java, Swift, C#, and several others. As you can see, after two slashes we write comment to explain what the next statement going to do. That comment will ignored by computer, so it will not affect any outcome. There are other variations :

// this is a comment (Java, C#, etc)
#  this is a comment (Ruby, Python)
'  this is a comment (visual basic)
REM this is a comment (BASIC)

Usually you write the comment above the statement you want to explain. If you want to write multiple lines of comment, you have to use different way. In some language, you can use slash asterisk (/*) as opening and asterisk slash (*/) as closing, Everything between that is comment.

public class HelloWorld {

    public static void main(String[] args) {

        /* I'm gonna print this hello world
        So world will know
        that I say hello to him */

        System.out.println("Hello, World");
    }

}

Remember, if you forget to close it, any statement below will be treated as comment, so be careful.

results matching ""

    No results matching ""