DEV Community

Discussion on: How would you rewrite this Python code?

Collapse
 
cathodion profile image
Dustin King

I don't think I'd heard of CQS before. I'll have to think more about it. It does make the code clear in this case.

On first examination, one issue I have with the general concept is that it seems that functions that act like constructors, such as file_or_stdout() in my example, or even just output = open(...) would violate it because they both do something (e.g. open the file, build a context manager) and return a reference to the thing they've opened or created.

CQS seems similar to a rule of thumb that parameters are for input and return values are for output, which I'm violating with do_something(), because it was a stand-in for a couple lines in the original code.

Maybe the larger takeaway of this exercise is that mutability has its pitfalls.