Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Writing Comments | Getting Started
Introduction to Scala

Writing Comments

Scorri per mostrare il menu

Comments Basics

Before we dive into more complex topics, let’s focus on a fundamental aspect of writing clean and maintainable code: writing comments.

Such lines are integral to any programming language. They allow developers to do the following:

  • leave notes explaining what exactly a certain part of the code does or how it works;
  • temporarily exclude certain parts of code from compilation to identify whether a certain part of the code causes errors;
  • write concise documentation for the code.

Types of Comments in Scala

Scala supports two main types of comments:

  • Single-Line Comments: As the name suggests, these comments span only a single line. They are marked by two forward slashes (//). Anything following // on the same line will be treated as a comment.
Main.java

Main.java

12345678
object Main { def main(args: Array[String]): Unit = { // Printing a greeting println("Hello!") println("1") // Printing a number // println("Goodbye") } }
  • Multi-Line Comments: These are used for longer comments that span multiple lines. They start with /* and end with */. Everything between these markers is considered a comment.
Main.java

Main.java

12345678
object Main { def main(args: Array[String]): Unit = { println("Hello!") /* An example of a multi-line comment println("1") println("Goodbye") */ } }

Best Practices for Writing Comments

While writing comments, keep the following best practices in mind:

  • Clarity: Ensure your comments are clear and easy to understand.
  • Relevance: Comment on parts of the code that are complex or not immediately obvious. Avoid stating the obvious.
  • Maintenance: Keep your comments updated as you update your code.
  • Brevity: Be concise. A comment should not be longer than necessary.

Task

The code below contains an error. Your task is to comment the line which causes this error.

Main.java

Main.java

123456
object Main { def main(args: Array[String]): Unit = { println("Hello") println('Goodbye') } }

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 1. Capitolo 5

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

Sezione 1. Capitolo 5
some-alt