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)  
  • refalo@programming.dev
    link
    fedilink
    arrow-up
    10
    ·
    edit-2
    17 days ago

    If status is modified in another thread you could have a race condition with method 1 because you’re checking it twice. The two methods are not exactly the same code due to that.

    • MousePotatoDoesStuff@piefed.socialOP
      link
      fedilink
      English
      arrow-up
      4
      ·
      17 days ago

      Not to worry, the variable is local and the function is single-threaded.

      The elif is a good idea, though - although I’ll likely go with a switch statement in the end.