Showing posts with label Comments. Show all posts
Showing posts with label Comments. Show all posts

Tuesday, 5 November 2024

The Importance of Comments in JavaScript

 

What is a Comment in Code?

A comment in code is a piece of text that is not executed by the programming language. It's used to explain what the code does, clarify complex logic, or leave notes for other developers (or yourself). Comments help make the code more readable and maintainable.

 

Why Use Comments?

1.   Clarity: Comments make your code easier to understand by explaining what the code is supposed to do. This is especially useful in complex sections of code.

2.   Maintenance: Comments help future developers (or yourself) to understand the purpose and functionality of the code when making updates or debugging.

3.   Documentation: Comments can serve as inline documentation, helping to describe the behaviour of functions, classes, and algorithms directly within the code.

4.   Collaboration: In a team setting, comments help others quickly grasp your code, making collaboration smoother.

 

What Happens if We Do Not Comment the Code?

1.   Decreased Readability: Code without comments can be difficult to understand, especially if the logic is complex or not self-explanatory.

2.   Harder Maintenance: Maintaining or updating uncommented code can be challenging, as developers may need to spend extra time figuring out what the code does.

3.   Increased Bugs: Without comments, developers might misinterpret the purpose of the code and introduce bugs when making changes.

4.   Longer Onboarding: New team members may take longer to understand the codebase if it lacks comments, slowing down their productivity.

 

Types of Comments in JavaScript

JavaScript supports two main types of comments: single-line and multi-line comments.

 

1. Single-Line Comments

Single-line comments are used to comment on a single line of code. They are preceded by //.

 

Example

// This is a single-line comment explaining the variable below
let sum = 0; // Initializing sum to 0

 

2. Multi-Line Comments

Multi-line comments can span multiple lines and are enclosed between /* and */. They are useful for commenting on blocks of code or adding detailed explanations.

 

Example

 

/*
  This is a multi-line comment.
  It can be used to explain complex logic,
  describe function behaviour, or provide documentation.
*/
function sum(a, b) {
    return a + b; // Returns the sum of a and b
}

 

Best Practices for Commenting in JavaScript

1. Keep Comments Up-to-Date: Always update comments when you modify the corresponding code. Outdated comments can be misleading.

 

2. Avoid Redundant Comments: Don’t write comments that simply restate what the code is doing in a trivial manner.

 

Example

let x = 5; // Set x to 5  // Redundant comment

 

3. Use Comments to Explain Why, Not What: Comments should explain why the code is doing something rather than what it is doing.

 

Example

// Checking if the user is logged in before allowing access
if (user.isLoggedIn()) {
    // Allow access
}

 

4. Comment Complex Logic: Use comments to explain non-obvious logic or decisions in your code.

 

5. Use JSDoc for Documentation: When documenting functions, classes, or complex code, consider using JSDoc comments, which are a standardized format for code documentation in JavaScript.

/**
 * Calculates the sum of two numbers.
 * @param {number} a - The first number.
 * @param {number} b - The second number.
 * @returns {number} The sum of a and b.
 */
function calculateSum(a, b) {
    return a + b;
}

  

In summary, Comments are a crucial part of writing maintainable and understandable code. They act as a guide for developers, explaining the purpose, logic, and decisions behind the code. Using comments wisely can significantly enhance the quality of your code and make it easier for others to work with.

 

Previous                                                    Next                                                    Home

Wednesday, 4 January 2023

PlantUML: Add comments to the diagram

Any line that starts with single quote (') is treated as comment.

 

comments.txt

@startuml

'-----------------------------------------------------
' Author : Krishna
' Date : 22-12-2022
' Client : Component is responsible to send
'		   the messages to server
' LoginServer : Server receive the messages
'				from client and process
' ADServer : Responsible to validate User credentials
'-----------------------------------------------------

Client -> LoginServer: Enter user name
Client -> LoginServer: Enter password
Client -> LoginServer: Submit the form 
LoginServer -> ADServer: Check the credentials
ADServer -> LoginServer: Login Successful
LoginServer -> Client: Login Successful

@enduml

 

Above snippet generate below diagram.



 

Previous                                                    Next                                                    Home

Monday, 19 September 2022

Add comments to TOML document

Any line that starts with # symbol is treated as comment.

 

comments.toml

# Following details are related application
appName = "Chat Server"
appVersion = 1.23

# Application owner details
[[Owner]]
firstName = "Ram"
lastName = "Gurram"

[[Owner]]
firstName = "Sailu"
lastName = "PTR

 


 

 

Previous                                                 Next                                                 Home

Thursday, 11 March 2021

Php: comments: Documenting the code

Comments are used to document your code. Comments start with // and any information after // is treated as comment and not processed by php.

 

Syntax

// This is an example of comment

 

Example

// Following statement prints data to console

echo "Hello World";

        

// Following statements define variable

$age = 23;

$name = "Krishna";

 

comments_demo.php

#!/usr/bin/php

<?php 

	// Following statement prints data to console
	echo "Hello World\n";
	
	// Following statements define variable
	$age = 23;
	$name = "Krishna";

	echo "Hello Mr." . $name . ", you are " . $age . " years old";
?>

 

Output

$./comment_demo.php 

Hello World
Hello Mr.Krishna, you are 23 years old

Php support two types of comments.

a.   Single line comments: Starts with // or # symbol

b.   Multiline comments: Starts with /* and ends with */.

 

comments_demo_1.php

#!/usr/bin/php

<?php 

	// This is single line comment
	# This is also single line comment

	/*
		This 
			is 
				a 
					multiline comment
	*/

?>




 

 

 

 

Previous                                                    Next                                                    Home

Thursday, 17 December 2020

Python: Comments

Comments are used to document your code. In python, any line that starts with # (hash symbol) is treated as comment.

 

comments.py


##############################
# Author: Krishna
# Date: 17-12-2020
# Description: Simple demo on usage of comments
##############################

# Person information
first_name = "Krishna"
last_name = "D"
age = 32

# Print person information
message1 = "Hi {} {}, you are {} years old".format(first_name, last_name, age)
print(message1)

 

Output

 

 

$python3 comments.py 
Hi Krishna D, you are 32 years old




Previous                                                    Next                                                    Home

Friday, 24 January 2020

Cassandra: Comments


Comments are used to document your code. Cassandra supports both single line and multi-line comments.

Single line comments
Single line comments begin with either double dashes (--) or double slash (//).

Multi line comments
Multi line comments place in between  /* and */

Example
-- This is a comment
// This is a comment too
/* This is
   a multi-line comment */

CREATE TABLE IF NOT EXISTS cassandratutorial.employee (
  id INT, -- Id of the employee
  firstName VARCHAR, // First name of the employee
  PRIMARY KEY((id, firstName)) /*
          Primary key of the employee
          */
) WITH comment = 'Table to store Employee information';


Previous                                                    Next                                                    Home