GDQuest / GDQuest/learn-gdscript
Level 14 - need to improve code quality for 14.2 - Reducing damage at higher levels
- Dominant language
- GDScript
- Stars
- 2.8k
- Forks
- 235
- PR merge metrics
- No merged PRs in 30d
Description
**Problem:**
The solution implies that we need to change function input parameter, **amount**, which is known as a not very good coding practice.
```
var level = 3
var health = 100
var max_health = 100
func take_damage(amount):
if level > 2:
amount *= 0.5
health -= amount
if health < 0:
health = 0
```
for example, there is explanation on [refactoring guru](https://refactoring.guru/remove-assignments-to-parameters):
> first, if a parameter is passed via reference, then after the parameter value is changed inside the method, this value is passed to the argument that requested calling this method. Very often, this occurs accidentally and leads to unfortunate effects. Even if parameters are usually passed by value (and not by reference) in your programming language, this coding quirk may alienate those who are unaccustomed to it.
> Second, multiple assignments of different values to a single parameter make it difficult for you to know what data should be contained in the parameter at any particular point in time. The problem worsens if your parameter and its contents are documented but the actual value is capable of differing from what’s expected inside the method.
[Martin Fowler also mentions that](https://refactoring.com/catalog/splitVariable.html), but could not find clear explanation by him in open sources.
**Proposal:**
1. Explicitly mention good coding practices in tutorial texts to that practice
2. IMHO, rewrite example so that function parameter would be unchanged, OR - explicitly mention that here is the exception needed for testing purposes.
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.