comments in kotlin, add comments to code, what are comments, android programming, kotlin,How to add comments in kotlin android

How to add comments in kotlin android

Comments are words or statements that are added either at the beginning or middle of codes to explain the meaning of a particular piece of code. Comments are supported by all programming languages since they allow readers who are not authors of the code to get the meaning of the code.

Comments form part of the documentation of a particular program since when they make a piece of code relevant and don’t lose meaning if time goes by.

Comments are also used as a faster reference to a particular function inside a code without having to go one by one to find what function does what.

In this article, we shall discuss how to add comments in kotlin while developing android applications and also how to use comments in kotlin since it is always recommended to follow this programming architecture.

A code that has comments in any programming language is always easier to learn, use and work with since it highlights the major functions in a file than the one without comments

While developing android applications using kotlin as the preferred language of programming, there are various ways that you can add comments and use comments.

How to add comments in kotlin while developing android applications

There are several ways of adding comments that are supported by kotlin language

  • Single line comments

This is a way of adding comments in a single line

To add single line comments we either use // or /* */

The double forward slash is added before a statement or words that you want to use as comments, for example, to convert a statement like this is a comment to a comment in kotlin, we add the double forward slash at the beginning of the text as below

// this is a comment

Unless you break to the next line, every word or statement following the double forward slash in the same line is treated as a comment

An example in android code showing how to add a comment using // (forward double slashes) is as follows

   /* variable declaration */

        var username="";

        var password="";

 

        // set on-click listener

        loginbutton.setOnClickListener {

             username = usernametext.text.toString();

             password = passwordtext.text.toString();

The above single-line comments are used to show what each function is doing, for example, the set on click listener

If the double forward slashes are used in different lines for different statements, they are still single-line comments

//proceed to the next function

 //username and password combination are correct

Another way to use single-line comments in android is using a single forward slash followed by an asterisk then the statement that you want to use as a comment and then an asterisk followed by a single forward slash /* */

Please note that for /* */ to be referred to as single-line comments, all the symbols and statements must be on the same line

For example,

/* this is an example of a single line comment */

  • Double line comments

In a case where there are comments that exceed one line or the comments are explaining a long statement that is displayed in more than one line, we use double line comments

The double line comments are represented by the following symbols

/*

*/

The symbols must be on different lines to mean double line comments, for example, the statement below shows usage of double line comments

/*check if variables have data

if username or password is empty do not proceed

  */

While adding comments, you can either decide to add a space after the symbols that represent comments or add the statement without a space after the symbol

The code below is an example of an android code that has comments as supported by kotlin language

package com.example.kotlinexample

 

//add imports

import android.annotation.SuppressLint

import android.os.AsyncTask

import android.os.Bundle

import android.text.TextUtils.isEmpty

import android.util.Log

import android.view.View

import android.widget.Button

import android.widget.EditText

import android.widget.ProgressBar

import android.widget.Toast

import androidx.appcompat.app.AppCompatActivity

import androidx.core.content.ContextCompat.startActivity

import java.io.*

import java.net.HttpURLConnection

import java.net.URL

 

class MainActivity : AppCompatActivity() {

    private val tag: String = "MainActivity"

    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)

        setContentView(R.layout.activity_main)

       /* initialize variables from activity_main.xml

         assign variables to textview, button and edittext */

        var usernametext = findViewById(R.id.username) as EditText

        var passwordtext = findViewById(R.id.password) as EditText

        var loginbutton = findViewById(R.id.loginbutton) as Button

        var progressBar=findViewById(R.id.progressBar) as ProgressBar

 

        /* variable declaration */

        var username="";

        var password="";

 

        // set on-click listener

        loginbutton.setOnClickListener {

             username = usernametext.text.toString();

             password = passwordtext.text.toString();

 

         /*check if variables has data

         if username or password is empty do not proceed

          */

            if(isEmpty(username) || isEmpty(password)){

                Toast.makeText([email protected], "Please fill all fields", Toast.LENGTH_LONG).show()

            }else{

                if(username=="username1" && password=="password1") {

                    //proceed to the next function

                    //username and password combination are correct

                }else{

                    Toast.makeText([email protected], "Wrong username or password, please try again", Toast.LENGTH_LONG).show()

                }

 

            }

        }

    }

 

}

 

Conclusion

Comments in any programming language remain important and they should be used every time you are developing any product. They help the reader to understand what the code is all about without the need to test and run the product

We have discussed how to add comments in kotlin while developing android applications and shown various examples of how to use single-line comments and double-line comments. Hope you have learned how kotlin uses comments.