Firstly, I’d like to say thank you for the fast paced and easy understandable tutorial with a hands on project. I enjoyed the entire “Speedy Saucer” section. Even though I am a Software Engineer myself, I am new to game development, so I found your guidance quite engaging.
Nevertheless, I would like to mention the significance of “Clean Code” and its principles, such as not repeating yourself (DRY), among others.
Why am I bringing this up?
-
what we have experienced when mentoring Junior to Mid-Level Developers at our company, we’ve found that it’s crucial to introduce best practices as early as possible. Given that this course is likely to be taken by individuals completely new to Software Development, it’s important they understand the at least some of the clean code and refactoring methods from the beginning.
-
Your tutorial is great, as I’ve mentioned before. Therefore, at this point of the lectures, it would be a fantastic opportunity to offer an additional 5-10 minutes specifically to the refactoring part where clean code principles are applied.
Why is this important?
- Refactoring code not only improves learning but also support new developers to understand common patterns, such as Factory Pattern, Singleton Pattern, etc.).
- It enhances
- code readability,
- maintainability,
- overall software quality
Example for the player script to reduce complexity and code duplication:
# variables can de be declared as const since they are not overwritten
# these can then be placed into a "utils-function.gd" File
const FORCE = 1000
# summarizing the moving commands into one object reduces code duplication and complexity
const PLAYER_DIRECTIONS = {
"move_right": Vector2(FORCE, 0),
"move_left": Vector2(-FORCE, 0),
"move_down": Vector2(0, FORCE),
"move_up": Vector2(0, -FORCE)
}
# defining an "apply_force" function utilizing the apply_force() Method for code reusability
func apply_force_in_direction(direction):
apply_force(PLAYER_DIRECTIONS[direction])
# apply the logic for players movement by reducing the amount of if-statements by utilizing the for in loop
func _physics_process(delta):
for direction in PLAYER_DIRECTIONS.keys():
if Input.is_action_pressed(direction):
apply_force_in_direction(direction)
Note this example is an easy & simple approach to implement some clean code principles and refactoring methods