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)  
  • darthelmet@lemmy.world
    link
    fedilink
    arrow-up
    2
    ·
    2 days ago

    Naively: Isn’t the second one slightly better because the first one will always check both variables twice but the second one only checks one of them once then the other variable once when inside?

    • MousePotatoDoesStuff@piefed.socialOP
      link
      fedilink
      English
      arrow-up
      3
      arrow-down
      1
      ·
      1 day ago

      You’re right, it is slightly better. Not really a concern here since this code runs only once per run for each unit test (and only for debugging, it’s not meant to be in the game release) so it shouldn’t be a performance issue, but still.