Course Content
Introduction to Scala
Introduction to Scala
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.
Main
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
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
object Main { def main(args: Array[String]): Unit = { println("Hello") println('Goodbye') } }
Thanks for your feedback!