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

Conteúdo do Curso

Introduction to Scala

Introduction to Scala

1. Getting Started
2. Variables and Data Types
3. Conditional Statements and Loops
4. Arrays
5. Strings

Writing Comments

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.
java

Main

copy
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.
java

Main

copy
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.

java

Main

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

Tudo estava claro?

Seção 1. Capítulo 5
We're sorry to hear that something went wrong. What happened?
some-alt