Discussions around software architecture mainly in the context of FW/1 and MVC
People
Repo info
Activity
Matthew J. Clemente
@mjclemente
I think this is the appropriate forum for this question.
I’m trying to establish guidelines for when to manually throw errors within my app.
For example, I have a function within a bean that should only run when it's a "draft". I could include logic within the function to account for this (check if draft, if not, create and return it), or, I could throw an error with a message about the restriction.
Sean Corfield
@seancorfield
The guiding question around exceptions is: is this an expected condition that I should be able to handle?
If calling that function without a draft is "expected" and the function could reasonably auto-create and return the new draft, then do that.
Matthew J. Clemente
@mjclemente
I guess, in this case, I’m trying to guide the application. I could account for whether it is a draft, but I think the application will run better if I don’t need to account for that.
So I’m making a business rule that it needs to be.
Sean Corfield
@seancorfield
An exception says: "Nope, sorry, I don’t know how to deal with this request" (as opposed to "Nope, I know about that but it’s an error").
Matthew J. Clemente
@mjclemente
Is there a preferred way to handle the latter?
Sean Corfield
@seancorfield
Depends on the language context and what the function does / is expected to return.
For example, functions that "do something" (and return nothing), can return a success / failure indicator instead.
Functions that return something specific on success, can return nothing on failure (null in Java, for example), or perhaps a struct with a success / failure indicator and a result key when success and a codes key when failure.
It really depends on what you’re trying to do. There’s no one size fits all here.
I have my validation functions return arrays of error descriptions, for example. So they return an empty array for success / no errors.
Lookup functions generally return a "thing" (success) or null (failure).
I’ll use a boolean result if all I care about is generically "Yes, I did it!" or "Nope, sorry, no can do!".
_
Matthew J. Clemente
@mjclemente
In this case, in broad strokes, an value is being saved, and then assigned to a “page”. So the value gets validated/saved. Then I pass it into the page bean, which has a function to save the value to a linking table for the page.
Sean Corfield
@seancorfield
(but, again, throwing an exception for "Halp! I haz no cluez!" is still reasonable)
Matthew J. Clemente
@mjclemente
So, right now it returns void… but that’s mostly becuase it’s just doing an insert that I’m assuming is successful.
The issue is that the page it is being saved to needs to be a “draft”. The page knows whether it is a draft or not.
But I’d rather insist that my controller (or other calling functions) always make sure they have a draft page called, before they try to save the value.
(hope I’m explaining this clearly)
Sean Corfield
@seancorfield
Sounds like you’d prefer an exception then, rather than silent auto-creation of the draft.
Matthew J. Clemente
@mjclemente
yeah, only because the silent auto-creation can be expensive
If there are a number of values that need to be saved (which there can be in some cases)
Is that lazy? Falling back on the exception?
Sean Corfield
@seancorfield
No, that fits the rule perfectly: you should always create any necessary drafts before calling this function (else it will throw an exception).
Matthew J. Clemente
@mjclemente
Thanks. I guess, returning full circle, it fits with the guideline you provided (is this an expected condition that I should be able to handle?)
Sean Corfield
@seancorfield
Yup. And you’re deciding, from a business p.o.v., that "no, this function should not handle that task".
Matthew J. Clemente
@mjclemente
If the rule (drafts only) makes the condition unexpected, it’s acceptable to throw the exception
Sean Corfield
@seancorfield
Yup!
Matthew J. Clemente
@mjclemente
Thanks! Really appreciate the insight @seancorfield
Sean Corfield
@seancorfield
Any time!
Matthew J. Clemente
@mjclemente
I will be adding that principle to my list of application design guidelines
Have a great day!
Sean Corfield
@seancorfield
"Halp! I haz no cluez!" :)
Matthew J. Clemente
@mjclemente
hahahaha. I might have to include that in the message
Sean Corfield
@seancorfield
if ( !page.isDraft() ) throw "Halp! I haz no cluez!";
Matthew J. Clemente
@mjclemente
exactly
Lampei
@Lampei
@seancorfield Just wondering if you might be able to answer a question on code structure for unit testing in a Java app (hopefully OK to ask as not a FW1 question)
I've done unit testing in python and CF and have used the "tests" directory, but I'm seeing a couple of ways to do it in Java...
projectRoot/src and projectRoot/tests vs. projectRoot/src/main and projectRoot/src/tests