3 ms·
I actually think you can do a lot better than the code he posted, for the problem he posed. The business logic there is not explained, and drowns in a sea of r
by kragen 18y ago
I actually think you can do a lot better than the code he posted, for the problem he posed. The business logic there is not explained, and drowns in a sea of repetitive method names and excessive punctuation. Here's a version of the code that includes a lot less "noise" and includes references to the applicable regulations and regulatory agencies, which I made up for the purpose of this exercise.
I've also removed the original comments, because they simply restated verbatim what the code said, as a sort of acknowledgment that the code wasn't as readable as it should be (although I didn't think it was so bad as to require comments).
# [`supplemental_documents` probably isn’t the only business rule
# that depends on whether something is a corporation or not, so
# I’m factoring that out here to make the code more readable than
# it was in the DailyWTF exercise. -kjs]
def is_corporation(self):
return self.orgStatusCode == 'CORP'
def supplemental_documents(self):
"""Yield the form numbers of the supplemental docs §801.33 requires.
The NNDDA imposes some special legal requirements because we
are not a DDTL under §701.2.
"""
# §801.33(b)(2)(ii)
if self.stateCode in ('AZ', 'TX'):
yield 'SR008-04X'
yield 'SR008-04XI'
# §801.33(b)(4) (as amended 1998)
if self.ledgerAmount >= 500000:
yield 'AUTHDLG-1A'
# §801.33(b)(8)(i)
if self.coInsuredCount >= 5 and not self.is_corporation():
yield 'AUTHCNS-1A'
def attach_supplemental_documents(self):
"""Attach supplemental docs a customer filing requires under §801.33."
for doc in self.supplemental_documents():
self.attachDocument(doc)
I don't think the "self." is an improvement here, but despite that, I think the code that actually contains the business logic is a lot more readable this way.
Really I think that if your notation for these business rules imposes much more overhead than this, it will be easy to overlook ways that they don't say what they're supposed to say. You don't need to have reams of repetitive, highly-compressible code just because you're doing business applications.
The DailyWTF author scoffs at the idea that "end-users" should write business logic, somehow confusing business-logic DSLs with expert systems. I think that at the very least, you should write your rules in a notation that enables the end-users to understand what they say. I don't think it's at all unreasonable to build a business-logic DSL for things like this, but in this case I think Python might work just as well.
(In fact, the DailyWTF author seems to assert that writing a DSL parser is of equivalent difficulty to compressing random data. For what it's worth, a parser is a lot easier to build than an expert system, but I got news for you, Alex. There are actually working expert systems in the field, there have been for nearly 30 years, and people are building more of them all the time. They don't require any pixie dust or unicorns.)