Blame
|
1 | <!-- Generated from applied-computing-au vic/unit3-4/sat/C07-2026/C07-Reference-Godot by port-reference-godot-to-wiki.py — do not hand-edit; re-run the port. --> |
||||||
| 2 | # Naming Conventions — GDScript |
|||||||
| 3 | ||||||||
| 4 | **Skill codes on this page** |
|||||||
| 5 | ||||||||
| 6 | | Code | Level | Skill | |
|||||||
| 7 | |---|---|---| |
|||||||
| 8 | | C711 | 1–2 | identifies naming conventions | |
|||||||
| 9 | | C721 | 3–4 | applies naming to variables | |
|||||||
| 10 | | C731 | 5–6 | applies naming to interface controls | |
|||||||
| 11 | | C741 | 7–8 | applies naming to code structures | |
|||||||
| 12 | | C751 | 9–10 | applies naming to ALL solution elements | |
|||||||
| 13 | ||||||||
| 14 | **Definition.** A *naming convention* is an agreed set of rules by which to name source code elements such as variables, functions, classes, methods and objects. (*Camel case:* each word after the first starts with a capital. *Snake case:* words joined with underscores. *Hungarian notation:* the name encodes purpose and type.) |
|||||||
| 15 | ||||||||
| 16 | C7-1 is one ladder: each level applies the convention to **more kinds of element**. In the validation Part B naming audit you state your convention, then fix non-compliant names live. |
|||||||
| 17 | ||||||||
| 18 | ## The Godot house convention |
|||||||
| 19 | ||||||||
| 20 | | Element | Convention | Example | |
|||||||
| 21 | |---|---|---| |
|||||||
| 22 | | Variables | `snake_case`, descriptive | `pairs_to_find`, `is_concession` | |
|||||||
| 23 | | Constants | `UPPER_SNAKE_CASE` | `DAILY_CAP` | |
|||||||
| 24 | | Functions | action-verb `snake_case` | `calculate_fare()` | |
|||||||
| 25 | | Classes (`class_name`) | `PascalCase` | `MykiCard` | |
|||||||
| 26 | | Nodes / interface controls | `PascalCase`, purpose + control type | `AddButton`, `BalanceLabel` | |
|||||||
| 27 | | Signals | `snake_case`, past tense | `fare_calculated` | |
|||||||
| 28 | | Files and scenes | `snake_case` | `team_row.gd`, `card.tscn` | |
|||||||
| 29 | ||||||||
| 30 | This is the convention every studied project uses — and the one Godot's own style guide recommends. |
|||||||
| 31 | ||||||||
| 32 | ## C711 — Identify your convention |
|||||||
| 33 | ||||||||
| 34 | Level 1–2 is **stating** the rules, in internal documentation, before applying them: |
|||||||
| 35 | ||||||||
| 36 | ```gdscript |
|||||||
| 37 | # C711 — naming conventions in this project: |
|||||||
| 38 | # variables and functions snake_case; constants UPPER_SNAKE_CASE; |
|||||||
| 39 | # classes and nodes PascalCase; signals snake_case, past tense |
|||||||
| 40 | ``` |
|||||||
| 41 | ||||||||
| 42 | **Earns the tick:** the convention written down where the marker can find it. In the naming audit, this is the sentence you open with. |
|||||||
| 43 | ||||||||
| 44 | ## C721 — Variables |
|||||||
| 45 | ||||||||
| 46 | Bad names hide meaning; the fix is descriptive `snake_case`: |
|||||||
| 47 | ||||||||
| 48 | ```gdscript |
|||||||
| 49 | # ✗ before — earns no C721: the names hide the meaning |
|||||||
| 50 | var b := 20.00 # what is b? |
|||||||
| 51 | var f := 5.30 # what is f? |
|||||||
| 52 | ``` |
|||||||
| 53 | ||||||||
| 54 | ```gdscript |
|||||||
| 55 | var balance: float = 20.00 # C721 — descriptive snake_case |
|||||||
| 56 | var fare: float = 5.30 # C721 |
|||||||
| 57 | var is_concession := false # C721 — booleans read as yes/no questions |
|||||||
| 58 | ``` |
|||||||
| 59 | ||||||||
| 60 | **In the studied projects:** `pairs_to_find`, `is_busy`, `flip_back_delay` (tile-matching-game); `points_for_win`, `team_name` (club-ladder). Every boolean starts `is_` — the name reads as the question the code asks. |
|||||||
| 61 | ||||||||
| 62 | **Magic numbers are a naming problem too.** A bare `0.5` says nothing; a named constant says everything: |
|||||||
| 63 | ||||||||
| 64 | ```gdscript |
|||||||
| 65 | # ✗ before — magic number, earns no C721 |
|||||||
| 66 | fare = FARE_TABLE[zone] * 0.5 # what is 0.5? |
|||||||
| 67 | ``` |
|||||||
| 68 | ||||||||
| 69 | ```gdscript |
|||||||
| 70 | const CONCESSION_DISCOUNT := 0.5 # C721 — the rule now has a name |
|||||||
| 71 | fare = FARE_TABLE[zone] * CONCESSION_DISCOUNT |
|||||||
| 72 | ``` |
|||||||
| 73 | ||||||||
| 74 | **Earns the tick:** no single-letter names (a loop `i` is fine), booleans as `is_`/`has_` questions, magic numbers replaced by named constants. |
|||||||
| 75 | ||||||||
| 76 | ## C731 — Interface controls |
|||||||
| 77 | ||||||||
| 78 | Name every Control node **purpose + control type**, in `PascalCase`: |
|||||||
| 79 | ||||||||
| 80 | ```text |
|||||||
| 81 | NameField (LineEdit) ← what it holds + what it is |
|||||||
| 82 | AddButton (Button) |
|||||||
| 83 | TitleLabel (Label) |
|||||||
| 84 | WarningLabel (Label) |
|||||||
| 85 | ``` |
|||||||
| 86 | ||||||||
| 87 | Those four are club-ladder's real scene — the name tells you what the control does before you click it. The anti-pattern is Godot's defaults left in place: `Button1`, `LineEdit`, `Label2`. |
|||||||
| 88 | ||||||||
| 89 | ```gdscript |
|||||||
| 90 | # C731 — controls named purpose + type, so the code reads in English |
|||||||
| 91 | %AddButton.pressed.connect(_on_add_pressed) |
|||||||
| 92 | name_field.text = "" |
|||||||
| 93 | ``` |
|||||||
| 94 | ||||||||
| 95 | **Earns the tick:** every control in the picked feature's scene named this way — the audit will open your scene tree and look. |
|||||||
| 96 | ||||||||
| 97 | ## C741 — Code structures |
|||||||
| 98 | ||||||||
| 99 | Functions get **action verbs**; classes get **PascalCase nouns**: |
|||||||
| 100 | ||||||||
| 101 | ```gdscript |
|||||||
| 102 | # ✗ before — earns no C741: the name says nothing |
|||||||
| 103 | func f(x): # what does f do? |
|||||||
| 104 | ``` |
|||||||
| 105 | ||||||||
| 106 | ```gdscript |
|||||||
| 107 | func calculate_fare(zone: int) -> float: # C741 — verb says what it does |
|||||||
| 108 | ``` |
|||||||
| 109 | ||||||||
| 110 | ```gdscript |
|||||||
| 111 | class_name MykiCard # C741 — PascalCase noun, a clear concept |
|||||||
| 112 | ``` |
|||||||
| 113 | ||||||||
| 114 | **In the studied projects:** `load_csv()`, `mark_matched()`, `start_new_game()` (tile-matching-game); `class_name Team`, `Season`, `Ladder` (club-ladder) — every function name is a verb phrase, every class a noun. |
|||||||
| 115 | ||||||||
| 116 | ## C751 — All solution elements |
|||||||
| 117 | ||||||||
| 118 | Level 9–10 extends the convention to **everything that has a name**: signals, scenes, files, autoloads — consistently, throughout. |
|||||||
| 119 | ||||||||
| 120 | ```gdscript |
|||||||
| 121 | # club-ladder — the convention on every element kind |
|||||||
| 122 | signal result_recorded(team: Team, result: String) # C751 — signal: past tense |
|||||||
| 123 | signal remove_requested(team: Team) # C751 |
|||||||
| 124 | # files: team_row.gd, season.gd — snake_case |
|||||||
| 125 | # scenes: main.tscn, team_row.tscn — snake_case |
|||||||
| 126 | ``` |
|||||||
| 127 | ||||||||
| 128 | **Earns the tick:** open any file in the project and the convention holds — variables, constants, functions, classes, nodes, signals, files. One inconsistent corner (`studentName` beside `student_id`) drops you back. |
|||||||
| 129 | ||||||||
| 130 | ## Check Your Understanding |
|||||||
| 131 | ||||||||
| 132 | 1. What convention does a Godot node (interface control) use, and what two things should its name say? |
|||||||
| 133 | ||||||||
| 134 | >| ### Answer |
|||||||
| 135 | >| `PascalCase`, saying purpose + control type — `AddButton`, `BalanceLabel`. |
|||||||
| 136 | ||||||||
| 137 | 2. Why is `fare * 0.5` a naming problem, and what is the fix? |
|||||||
| 138 | ||||||||
| 139 | >| ### Answer |
|||||||
| 140 | >| `0.5` is a magic number — its meaning is invisible. Replace it with a named constant: `CONCESSION_DISCOUNT := 0.5`. |
|||||||
| 141 | ||||||||
| 142 | 3. What separates C751 from C741? |
|||||||
| 143 | ||||||||
| 144 | >| ### Answer |
|||||||
| 145 | >| C741 covers code structures (functions and classes). C751 extends the convention to *every* named element — signals, scenes, files, autoloads — with no inconsistent corners anywhere. |
|||||||
