TExcelTAD
Home › Julia › Julia Comments

Julia Comments

Comments are notes for humans. Julia ignores them when running the program. Use them to explain why code does something, to leave reminders, or to temporarily disable a line.

Single-line comments

A # starts a comment that runs to the end of the line. It can occupy a whole line or follow code:

julia · try it yourself
# Compute the area of a circle
radius = 2.0
area = π * radius^2   # π is a built-in constant
println(round(area, digits=3))
# println("this line is disabled")
12.566

Multi-line comments

Text between #= and =# is a block comment and may span many lines. Unlike in many languages, Julia's block comments can be nested, so you can comment out code that already contains a block comment:

julia · try it yourself
#=
This program prints a short countdown.
Written as an example of block comments.
=#
for i in 3:-1:1
    print(i, " ")
end
println("lift-off!")

#= Outer comment
   #= inner comment =#
   still inside the outer comment
=#
x = 5 #= an inline block comment =# + 1
println(x)
3 2 1 lift-off!
6

Docstrings

A string placed directly before a function, type or constant becomes its documentation. Triple quotes let it span several lines, and the text is Markdown. The REPL's help mode (?area_of_square) shows it, and the @doc macro returns it from code:

julia · try it yourself
"""
    area_of_square(side)

Return the area of a square with the given `side` length.
"""
area_of_square(side) = side^2

println(area_of_square(4))
doc = string(@doc area_of_square)
println(occursin("area_of_square(side)", doc))
println(occursin("Return the area", doc))
16
true
true

The convention is to start a docstring with the function signature indented by four spaces (rendered as code), followed by a one-line description and, optionally, # Examples and # Arguments sections.

julia
"""
    clamp_score(x, lo=0, hi=100)

Limit `x` to the range `lo:hi`.

# Examples

    julia> clamp_score(120)
    100
"""
clamp_score(x, lo=0, hi=100) = min(max(x, lo), hi)

Good commenting habits

Do Avoid
Explain intent and non-obvious decisions Repeating what the code obviously says (i += 1 # add one to i)
Write docstrings for public functions Leaving large blocks of dead, commented-out code
Keep comments up to date when code changes Comments that contradict the code
Use TODO: / FIXME: markers for unfinished work Very long comment lines; wrap at ~92 characters
julia · try it yourself
# Convert a price including VAT to the net price.
# The rate is stored as a fraction (0.2 means 20 %), not a percentage,
# because that avoids dividing by 100 in every calculation.
const VAT_RATE = 0.2

net_price(gross) = gross / (1 + VAT_RATE)

println(net_price(120.0))   # TODO: round to cents for display
100.0

Test yourself

Which syntax starts a multi-line comment in Julia?

  1. /*
  2. #=
  3. """
  4. --[[

Where must a docstring be placed?

  1. Inside the function body
  2. Directly before the function, type or constant it documents
  3. After the end of the function
  4. In a separate .md file

Can block comments be nested in Julia?

  1. Yes
  2. No, it is a syntax error
  3. Only two levels deep
  4. Only inside functions