Better way to do if statements

Ive noticed that the instructors of the courses like to use nested if statements. while there is in theory nothing wrong with that. If u make a habit out of it. it will become a nightmare to debug later on if the game grows.

While u can do

	if isTransition == false:
	    if "Goal" in body.get_groups():
		    complete_level(body.nextLevel)
		
	    if "Hazard" in body.get_groups():
		    crash_sequence()

U can reverse the starting if and just pass. So it quits the function
like so:

	if isTransition:
		return
		
	if "Goal" in body.get_groups():
		complete_level(body.nextLevel)
		
	if "Hazard" in body.get_groups():
		crash_sequence()

This will make things very easy later on when the file sizes grow.

2 Likes

Privacy & Terms