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)  
a code screenshot (code in description)
you are viewing a single comment's thread
view the rest of the comments
[–] 4 points 5 months ago* (1 child)

Are 1 and 2 magic values in this case?
You could use consts or even use an enum to further improve readability:

const STATUS_ERROR   := 1
const STATUS_WARNING := 2
# or alternatively:
enum Status { FINE, ERROR, WARNING }

In the latter case, you'd change status to be the new enum type Status.

edit: Wait, nevermind. I think you're already using enums in the actual code (not screenshot).

  • source
  • hideshow 1 child comment