UPDATE: The consensus seems to be overwhelmingly in favour of the match variant. And not to worry, I have replaced the magic numbers with an enum. Will try to remember to merge the branch tomorrow

Does an if-statement block or a switch statement fit better here? For context (and advertisement), this is part of my all-purpose utility plugin ( Codeberg link)

The code:

		# Method 1 (Yandere Dev Technique)  
		if self.throw_errors and status==MpupTest.TESTSTATUS.ERROR:  
			push_error(result)  
		if self.throw_warnings and status==MpupTest.TESTSTATUS.WARNING:  
			push_warning(result)  
		
		# Method 2 (Pirate Software Technique)  
		match status:  
			MpupTest.TESTSTATUS.ERROR:  
				if self.throw_errors:  
					push_error(result)  
			MpupTest.TESTSTATUS.WARNING:  
				if self.throw_warnings:  
					push_warning(result)  
  • SteleTrovilo@beehaw.org
    link
    fedilink
    arrow-up
    14
    ·
    4 days ago

    The match statement is cleaner.

    With the sequential if statements, it might seem on a casual read that both if statements could pass - you’d catch both warnings and errors. Only by looking at the conditions do you realize that only up-to-1 of them will run.

    With the match statement, it’s very immediately obvious that only one of the commands will run.