learn-gdscript icon indicating copy to clipboard operation
learn-gdscript copied to clipboard

Level 14 - need to improve code quality for 14.2 - Reducing damage at higher levels

Open Aakochar opened this issue 2 years ago • 1 comments

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:

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, 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.

Aakochar avatar Oct 08 '23 10:10 Aakochar

Thanks for taking the time to give feedback. In this case, I think a way to change this would be to have an if/else or something. This kind of "good practice" is going to go way over learners' heads.

If anyone wants to, PR welcome, and the solution code could be changed to something like this:

if level > 2:
	health -= amount * 0.5
else:
	health -= amount

NathanLovato avatar Oct 16 '23 17:10 NathanLovato