A write function with three string inputs — subject, body, and a keyword to filter by. You want to refuse the send if any input is bad, before the API call ever fires. What order do the checks go in?
Cheapest checks first? Empty-string check is a single comparison — regex or length check after that? And each failed check bails with False?
Exactly. Early returns make the happy path obvious — the function reads top-to-bottom as a series of guards, then one write, then a return:
if not subject or not body:
return False
if len(body) < 10:
return FalseAnd only after every check passes does the real execute_action call happen? That way a bad input never hits the API at all?
Zero wasted API calls, zero broken writes. The validation is local Python; the send only fires when the inputs are trustworthy:
def validate_and_send(subject: str, body: str) -> bool:
if not subject or not body:
print("Validation failed: subject or body empty")
return False
if len(body) < 10:
print(f"Validation failed: body too short ({len(body)} chars)")
return False
toolset.execute_action(Action.GMAIL_SEND_EMAIL, {"to": "me", "subject": subject, "body": body})
print(f"Sent email with subject '{subject}'")
return TrueShould the function raise an error instead of returning False? What's the trade-off?
Raise when the caller must act; return a boolean when the caller might legitimately skip. For a send that is part of a batch, False is friendlier — the batch continues. For a send that is the critical step of a workflow, raise ValueError forces the caller to notice.
So every write I write from here on gets a validation preamble — a wall between bad inputs and real side effects?
Validation first, write second. The guards are local, cheap, and free of network cost. One pattern covers every send, create, and update you'll ever write.
TL;DR: Validate locally before any network write — fail cheap, save the API call.
if not subject or not bodyif len(body) < 10execute_action after every guard| Response | When |
|---|---|
return False | Batch pipelines; caller skips |
raise ValueError | Critical step; caller must notice |
| Return dict with reason | Caller needs per-check detail |
Boolean is the default — concise and easy to chain. Upgrade to a dict or an exception only when the caller asks for more context.
A write function with three string inputs — subject, body, and a keyword to filter by. You want to refuse the send if any input is bad, before the API call ever fires. What order do the checks go in?
Cheapest checks first? Empty-string check is a single comparison — regex or length check after that? And each failed check bails with False?
Exactly. Early returns make the happy path obvious — the function reads top-to-bottom as a series of guards, then one write, then a return:
if not subject or not body:
return False
if len(body) < 10:
return FalseAnd only after every check passes does the real execute_action call happen? That way a bad input never hits the API at all?
Zero wasted API calls, zero broken writes. The validation is local Python; the send only fires when the inputs are trustworthy:
def validate_and_send(subject: str, body: str) -> bool:
if not subject or not body:
print("Validation failed: subject or body empty")
return False
if len(body) < 10:
print(f"Validation failed: body too short ({len(body)} chars)")
return False
toolset.execute_action(Action.GMAIL_SEND_EMAIL, {"to": "me", "subject": subject, "body": body})
print(f"Sent email with subject '{subject}'")
return TrueShould the function raise an error instead of returning False? What's the trade-off?
Raise when the caller must act; return a boolean when the caller might legitimately skip. For a send that is part of a batch, False is friendlier — the batch continues. For a send that is the critical step of a workflow, raise ValueError forces the caller to notice.
So every write I write from here on gets a validation preamble — a wall between bad inputs and real side effects?
Validation first, write second. The guards are local, cheap, and free of network cost. One pattern covers every send, create, and update you'll ever write.
TL;DR: Validate locally before any network write — fail cheap, save the API call.
if not subject or not bodyif len(body) < 10execute_action after every guard| Response | When |
|---|---|
return False | Batch pipelines; caller skips |
raise ValueError | Critical step; caller must notice |
| Return dict with reason | Caller needs per-check detail |
Boolean is the default — concise and easy to chain. Upgrade to a dict or an exception only when the caller asks for more context.
Create a free account to get started. Paid plans unlock all tracks.