Comments
Mix.install([
{:jason, "~> 1.4"},
{:kino, "~> 0.8.0", override: true},
{:youtube, github: "brooklinjazz/youtube"},
{:hidden_cell, github: "brooklinjazz/hidden_cell"}
])
Navigation
Setup
Ensure you type the ea keyboard shortcut to evaluate all Elixir cells before starting. Alternatively, you can evaluate the Elixir cells as you read.
Review Questions
Upon completing this lesson, a student should be able to answer the following questions.
- How do you write a comment in Elixir?
- What keyboard command can you use in both Livebook and Visual Studio Code to comment one or more lines of code in Elixir?
Comments
You can create inline comments in your code in Elixir using #. Anything after the # on the same line will be part of the comment
# You can use comments to leave messages in your code.
It’s sometimes handy for documenting what your code does. However, it’s often better to use comments sparingly. Leaving a comment often means that there’s something in your code that didn’t explain itself.
In these moments you might find it useful to consider how you can improve your code to be self documenting rather than leaving a comment in unclear code. There are better forms of documentation that you’ll learn about in future lessons.
Sometimes it’s handy to use comments to temporarily remove pieces of code and save them for later. However, comments can be misused. You’re also likely to encounter codebases that leave thousands of TODO comments everywhere. TODO comments can be useful, but when abused they can be worse than no comment at all!
# Use comments to leave notes on your code.
Depending on your code editor, You can comment out many lines of code at once. Highlight the code you want to comment
then press the correct keybinding. The binding will be different depending on your editor and if you are using GNU/Linux, macOS, or Windows.
In Livebook, it’s likely CTRL-/ or COMMAND-/ but you can double check in the keyboard shortcuts tab.
Click on the keyboard icon in the left sidebar of this page to see all of the keyboard shortcuts.
Your Turn
In the Elixir cell below, create a comment # hello world
In the Elixir cell below, comment all of the code out using the Toggle lines comment keyboard shortcut.
1 + 1
2 + 3
3 + 1 + 2
123 + 12 + 256
Further Reading
Throughout this course we will often recommend trusted resources for further study.
Some of our favourite platforms to recommend include:
- HexDocs A hosting platform for documentation
- Elixir School An education platform for learning Elixir.
- Exercism An education platform with a great number resources and exercises. You can signup for an account if you would like further practice with a concept beyond the exercises in this course.
- Elixir-lang The official Elixir website which includes guides, documentation, and other Elixir resources.
Mark As Completed
file_name = Path.basename(Regex.replace(~r/#.+/, __ENV__.file, ""), ".livemd")
save_name =
case Path.basename(__DIR__) do
"reading" -> "comments_reading"
"exercises" -> "comments_exercise"
end
progress_path = __DIR__ <> "/../progress.json"
existing_progress = File.read!(progress_path) |> Jason.decode!()
default = Map.get(existing_progress, save_name, false)
form =
Kino.Control.form(
[
completed: input = Kino.Input.checkbox("Mark As Completed", default: default)
],
report_changes: true
)
Task.async(fn ->
for %{data: %{completed: completed}} <- Kino.Control.stream(form) do
File.write!(
progress_path,
Jason.encode!(Map.put(existing_progress, save_name, completed), pretty: true)
)
end
end)
form
Commit Your Progress
Run the following in your command line from the curriculum folder to track and save your progress in a Git commit.
Ensure that you do not already have undesired or unrelated changes by running git status or by checking the source control tab in Visual Studio Code.
$ git checkout -b comments-reading
$ git add .
$ git commit -m "finish comments reading"
$ git push origin comments-reading
Create a pull request from your comments-reading branch to your solutions branch.
Please do not create a pull request to the DockYard Academy repository as this will spam our PR tracker.
DockYard Academy Students Only:
Notify your teacher by including @BrooklinJazz in your PR description to get feedback.
You (or your teacher) may merge your PR into your solutions branch after review.
If you are interested in joining the next academy cohort, sign up here to receive more news when it is available.
Up Next
| Previous | Next |
|---|---|
| GitHub Collab | Arithmetic |