Comments | Godot GDScript Tutorial | Ep 04

Godot Tutorials
9 Apr 202007:10

Summary

TLDRThe video script explains the importance and types of comments in programming, specifically in GD Script. It describes two methods for writing comments: single-line using the pound sign and multi-line using triple double quotes. Four common types of comments are discussed: methodology, metadata, debugging, and code description. The script emphasizes using meaningful variable names and refactoring before resorting to comments for clarity. It advises against using comments for version control and suggests using platforms like GitHub or Bitbucket instead.

Takeaways

  • πŸ“ Comments are programmer-readable annotations in the source code that help explain the code but are ignored by the compiler.
  • πŸ”‘ There are two primary ways to write comments in GDScript: using the pound sign (#) for single-line comments and using three double quotation marks for multi-line comments.
  • πŸ’¬ Multi-line comments in GDScript are treated as multi-line strings, not actual comments, by the compiler.
  • πŸ” Four common types of comments are methodology description, metadata, debugging, and code description comments.
  • πŸ”‘ Methodology description comments explain the reasoning behind the choice of algorithms or methods in the code.
  • πŸ“œ Metadata comments often appear at the top of scripts and include information like the project name, file name, creation year, maintainers, and copyright.
  • πŸ› Debugging comments are used to temporarily disable code, such as print statements, during the debugging process.
  • πŸ›  Code description comments clarify the purpose of a line of code, but should be used sparingly and after considering meaningful variable names.
  • πŸ”„ It's better to use clear variable names and refactoring to make code self-explanatory before resorting to comments for intent.
  • πŸ€” Comments should explain the 'why' behind the code, not just the 'how', and should be used when code readability reaches its limit.
  • ❌ Avoid using comments as a form of version control or to track changes made by individuals, as tools like GitHub or Bitbucket are more appropriate for this purpose.

Q & A

  • What are comments in programming?

    -Comments are programmer-readable annotations in the source code of a program. They are used to explain the code, making it easier for others to understand the intent and methodology behind the code.

  • How many ways are there to write a comment in GDScript?

    -There are two ways to write a comment in GDScript: using the pound sign (#) for single-line comments and using three double quotation marks for multi-line comments.

  • What is considered when using the pound sign symbol in GDScript?

    -The pound sign symbol is used for single-line comments in GDScript. Anything written after the pound sign is considered a comment and is not part of the actual code that the compiler will read.

  • Why is a multi-line comment in GDScript treated as a multi-line string?

    -A multi-line comment in GDScript is treated as a multi-line string because the compiler reads it as such. It is not a real comment but a string that can be included in the code.

  • What are the four common types of comments mentioned in the script?

    -The four common types of comments mentioned are methodology description comments, metadata comments, debugging comments, and code description comments.

  • What is the purpose of methodology description comments?

    -Methodology description comments are used to explain the reasoning behind the choice of algorithms or methods in the code, rather than just the intent of the code itself.

  • Where are metadata comments typically found and what do they include?

    -Metadata comments are typically found at the top of a script and may include information such as the company name, file name, year of creation, names of maintainers, copyright, and more.

  • What is the purpose of debugging comments and how are they usually implemented?

    -Debugging comments are used during the process of finding and fixing errors in the code. They often involve commenting out print statements to temporarily disable them, with the intention of using them again later.

  • What is the difference between code description comments and meaningful variable names?

    -Code description comments are used to explain the intent of a line of code, whereas meaningful variable names are chosen to inherently convey the purpose of the variable, reducing the need for additional comments.

  • Why should comments not be used as a source of version control?

    -Comments should not be used as a source of version control because they are not designed to track changes made by individuals over time. Instead, version control systems like GitHub or Bitbucket should be used for this purpose.

  • What is the general rule of thumb for using comments in code?

    -The general rule of thumb is that code tells you 'how' something is done, while comments explain 'why' it is done that way. Comments should be used to provide rationale and intent when code alone is not self-explanatory.

Outlines

00:00

πŸ“ Understanding Comments in Programming

This paragraph explains the concept of comments in programming, which are programmer-readable annotations within the source code. It discusses two primary methods of writing comments: single-line comments using the pound sign symbol and multi-line comments using three double quotation marks. The paragraph also highlights that in GD script, multi-line comments are treated as strings rather than actual comments. Furthermore, it introduces four common types of comments: methodology description, metadata, debugging, and code description. Methodology description comments are used to explain the reasoning behind certain coding choices, such as why one sorting algorithm was chosen over another. Metadata comments typically appear at the top of a script and may include information like the company name, file name, creation year, maintainers, and copyright. Debugging comments are used to temporarily remove code, such as print statements, for troubleshooting purposes. Lastly, code description comments are used to clarify the intent of a line of code, but the paragraph suggests that meaningful variable names and code structure should be used to convey intent before resorting to comments.

05:02

πŸ” Best Practices for Using Comments in Code

The second paragraph delves into best practices for using comments in code. It emphasizes the importance of using meaningful variable names to convey the intent of the code, which can reduce the need for comments. The paragraph provides an example where instead of using a comment to explain that a variable represents the player's health, the variable is named in a way that clearly communicates its purpose. It also suggests that additional information, such as the type of variable expected, can be included in the code to further clarify its use. The paragraph advises that comments should be used to explain 'why' something is done, rather than 'how', as the code itself should be self-explanatory. It cautions against using comments as a form of version control or to track changes made by individuals, recommending the use of proper version control systems like GitHub or Bitbucket for such purposes.

Mindmap

Keywords

πŸ’‘Comments

Comments in programming are annotations that are not executed as part of the code but serve to explain the code to human readers. In the context of the video, comments are crucial for understanding the source code, especially for others who may read or maintain the code later. The script mentions two types of comments in GDScript: single-line comments using the pound sign and multi-line comments using triple double quotation marks.

πŸ’‘Single-line Comment

A single-line comment is a form of comment that applies only to the line on which it appears. In GDScript, as explained in the video, a single-line comment is created using the pound sign (#). Anything following the pound sign on that line is ignored by the compiler and is considered a comment, which helps in making notes or explanations directly beside the code without affecting its execution.

πŸ’‘Multi-line Comment

A multi-line comment extends over multiple lines and is used to describe larger sections of code or to temporarily disable blocks of code. In the video, it is mentioned that in GDScript, a multi-line comment is created using three double quotation marks. However, it's important to note that in GDScript, this is functionally a multi-line string, not a comment in the traditional sense.

πŸ’‘Methodology Description Comments

These types of comments are used to explain the reasoning behind the choice of algorithms or methodologies in the code. The video provides an example where a methodology description comment is used to explain why an insertion sort was chosen over a quicksort, illustrating the importance of these comments in conveying the developer's thought process.

πŸ’‘Metadata Comments

Metadata comments are typically found at the beginning of a script and contain information about the code such as the company name, file name, creation year, maintainers, and copyright. The video script describes an example of metadata comments from the Godot engine on GitHub, showing how they provide essential context about the project and its legal status.

πŸ’‘Debugging Comments

Debugging comments are used during the development process to help identify and fix issues in the code. The script mentions using comments to disable print statements temporarily, which is a common practice for debugging. Once the issue is resolved, these print statements are commented out, potentially to be revisited later.

πŸ’‘Code Description Comments

Code description comments are used to clarify the intent of a line or block of code. The video emphasizes the importance of meaningful variable names over comments for clarity, but acknowledges that comments can be used when necessary to explain the purpose of a particular piece of code, such as indicating that a variable represents the player's health.

πŸ’‘Meaningful Names

Meaningful names in programming refer to the practice of naming variables and functions in a way that clearly indicates their purpose or content. The video script suggests that using meaningful names can reduce the need for comments, as the names themselves can convey the intent of the code, such as a variable named 'playerHealth'.

πŸ’‘Refactoring

Refactoring is the process of restructuring existing code without changing its external behavior to improve nonfunctional attributes such as readability, maintainability, or performance. The video script implies that refactoring can make the code more readable, which in turn reduces the reliance on comments to explain the code's purpose.

πŸ’‘Source Control

Source control, as mentioned in the video, refers to the practice of tracking and managing changes to the source code in a project. The script advises against using comments for version control purposes, instead recommending the use of dedicated tools like GitHub or Bitbucket, which are designed to handle version tracking and collaboration more effectively.

πŸ’‘Compiler

A compiler is a program that translates source code written in a programming language into machine code that a computer can execute. In the context of the video, the compiler reads the source code and ignores comments, whether single-line or multi-line, allowing programmers to include explanatory notes without affecting the code's execution.

Highlights

Comments are programmer-readable annotations in source code.

There are two ways to write a comment in GDScript: single line with the pound sign and multi-line with three double quotation marks.

Single line comments start with a pound sign and are ignored by the compiler.

Multi-line comments in GDScript are treated as multi-line strings by the compiler.

Four common types of comments are methodology description, metadata, debugging, and code description comments.

Methodology description comments explain the reasoning behind code choices, such as why a specific sorting algorithm is used.

Metadata comments typically include information like the company name, file name, and copyright at the top of a script.

Debugging comments are used to temporarily disable code, like print statements, for troubleshooting purposes.

Code description comments help others understand the intent of a line of code, but should be used sparingly.

Meaningful variable names can often convey the intent of code better than comments.

Comments should explain 'why' the code is written a certain way, not 'how', as the code itself shows 'how'.

Use naming conventions and refactoring to make code readable before resorting to comments.

Avoid using comments as a form of version control or to track changes made by individuals.

For tracking changes, use version control systems like GitHub or Bitbucket instead of comments.

Comments are most effective when they clarify the rationale behind complex or non-obvious code decisions.

In open-source projects, metadata comments often outline the legal terms for using the project under its license.

Transcripts

play00:02

so what exactly are comments comments

play00:05

our programmer readable annotations in

play00:07

the source code of a program writing a

play00:11

comment is fairly straightforward ngd

play00:13

script there are two ways to write a

play00:15

comment the first way to write a comment

play00:17

is by using the pound sign symbol this

play00:20

is called a single line comment also

play00:23

keep in mind that anything written after

play00:25

the pound sign symbol will be considered

play00:27

a comment in not part of the actual code

play00:30

this means you can write both code and a

play00:33

comment on one single line basically

play00:35

anything before the pound sign symbol

play00:37

will be considered code for the compiler

play00:40

to read and everything after the pound

play00:43

sign symbol will be skipped by the

play00:44

compiler the second way to write a

play00:46

comment in GD script is by using three

play00:49

double quotation marks followed by your

play00:51

comments ending with three double

play00:54

quotation marks something to keep in

play00:56

mind is that a multi-line comment in GD

play00:59

script is a multi-line string this means

play01:02

that the compiler will read your

play01:04

multi-line comment as the multi-line

play01:06

comment is not a real comment but rather

play01:09

just a string you can write in your code

play01:11

there are many different types of

play01:13

comments out there however however these

play01:16

four types of comments you may find

play01:18

yourself using most of the time you may

play01:20

find yourself using the methodology

play01:22

description comments the metadata

play01:25

comments debugging comments and code

play01:27

description comments let's go ahead and

play01:29

take a look at each of these the

play01:31

methodology description comments can be

play01:34

used for the explanation of the

play01:36

methodology you use in your code you

play01:39

will find yourself using this type of

play01:41

comment to explain the code rather than

play01:44

a clarification of the codes intent for

play01:47

example a person may add a comment to

play01:50

explain why insertion sort was used

play01:53

instead of quicksort let's go ahead and

play01:56

take a look at an example of this so

play01:58

here we have used a multi-line comment

play02:00

with the following sentence as you can

play02:04

see here we are explaining through

play02:06

comments the reason behind the choice in

play02:09

our code rather than the intents of the

play02:10

code this type of comment is considered

play02:13

the methodology description

play02:15

comment next we have our metadata

play02:17

comments these comments you will find

play02:20

most of the time at the top of the

play02:22

script and may include the company name

play02:25

the file name the year the script was

play02:28

created the names of people who maintain

play02:30

it

play02:31

copyright and much much more

play02:34

let's go ahead and take a look at an

play02:36

example of metadata come for this

play02:39

example I took a portion of the metadata

play02:42

comments you may find yourself seeing if

play02:45

you look at the Godot engine at github

play02:48

this is what it may look like if they

play02:51

wrote this in GT script you can notice

play02:54

that we have the name of the Godot

play02:57

engine followed by the website followed

play02:59

by a copyright the year was created and

play03:02

who are maintaining it you will usually

play03:05

find metadata comments in open-source

play03:07

projects typically to write out in legal

play03:10

terms that you are free to use the

play03:12

open-source project however you see fit

play03:14

depending on the type of license they

play03:17

are giving away the third type of

play03:19

comments you may find yourself using a

play03:21

lot is the debugging comments when you

play03:24

are using comments for debugging you are

play03:26

most likely using a brute force

play03:28

debugging method an example would be

play03:30

commenting out print statements print

play03:34

statements is a common way for

play03:35

programmers to debug code when you are

play03:38

finish people tend to comment them out

play03:40

with the idea of using them again later

play03:42

let's go ahead and take a look at an

play03:45

example of a debugging comment as you

play03:47

can see here we have an if statement

play03:49

followed by a commented out print

play03:52

statement where we want to see the value

play03:55

inside of X printed out to the screen

play03:58

typically you use print statements to

play04:00

see if something's working and you come

play04:02

in 10th out so that way it doesn't run

play04:04

during compile time this is just a

play04:07

simple example however you may find

play04:09

yourself using this quite a lot when

play04:11

you're trying to debug issues with your

play04:13

code in the game the last type of

play04:16

comment is the code description comment

play04:18

generally used to make others understand

play04:21

the intent of the line of code this is

play04:24

vastly different than explaining the

play04:26

methodology or the reason you chose

play04:28

the code this type of comment should be

play04:30

used only when needed let's go ahead and

play04:33

take a look at a few examples this is an

play04:36

example I typically find beginners using

play04:39

a lot what they'd like to do is they

play04:42

tend to name their variables x and y

play04:44

because that's what they tend to read in

play04:46

articles however they use comments to

play04:48

explain the intent of the code in this

play04:51

case the programmer has decided to let

play04:54

you know that the variable X represents

play04:57

the player's health this is one way to

play04:59

use comments however there's a better

play05:02

way to explain this line of code then

play05:04

using a comment let's go ahead and take

play05:06

a look at what this would look like as

play05:08

you can see here instead of using a

play05:10

comment to explain the intent of our

play05:12

code we chose to instead name our

play05:15

variable something meaningful to explain

play05:17

the intents of the code in this case

play05:19

this is better as we get rid of one line

play05:22

of comment and yet the intention or

play05:24

rather the store we're trying to tell

play05:26

other programmers is this variable will

play05:28

represent the player's health

play05:30

now that we've named our variable

play05:32

something meaningful we can do even more

play05:34

to explain to other programmers the

play05:37

intent of our code let's go ahead and

play05:40

take a look at what this would look like

play05:42

as you can see here we have let other

play05:44

programmers know that we intend to use

play05:47

this variable for player's health and

play05:49

that this variable should only be an

play05:52

integer this is an even better way of

play05:54

describing to other programmers the

play05:57

intent of our code without using comment

play06:00

now I'm not saying that you shouldn't

play06:02

use comments to describe the intents of

play06:04

your code but rather you should first

play06:06

maximize the use of meaningful names and

play06:10

code intention first before using

play06:13

comments to describe the intention of

play06:15

your code when should you use comments

play06:18

there's a saying code tells you how and

play06:20

comments tell you why I generally like

play06:23

to attempt to use naming conventions and

play06:25

refactoring to make my code readable

play06:28

keep in mind that explaining yourself

play06:30

with code has its limits when you have

play06:33

reached that limit then you may use

play06:35

comments to explain why and the

play06:37

rationale behind your code when to not

play06:40

use comments you

play06:42

never use comments as a source of

play06:44

version or source control that is to say

play06:47

never ever ever use comments to explain

play06:50

which person did what what that person

play06:53

changed in the code and when that person

play06:55

made the change when working in a group

play06:58

and if information above is important

play07:01

use github bitbucket or get a Oh

Rate This
β˜…
β˜…
β˜…
β˜…
β˜…

5.0 / 5 (0 votes)

Related Tags
Code CommentsProgrammingGDScriptSource CodeAnnotationsDebugging TipsMethodologyMetadataBest PracticesCode ReadabilityDeveloper Insights