0

Say you have functions A, B, and C. A will be called and then call B then B calls C. C loops on a bunch of stuff. C determines there is a warning that the user should at least be made aware of but it doesnt block the process. How would you communicate these warnings to A?

If i pass a list to use for warnings from A to B, then its passed straight through to C as B has no use for it. Exceptions would halt C from continuing its loop.

Is this a normal problems? What are elegant ways to solve it?

EDIT: code example

def A():
    items = [
        [1,2,3],
        [4,5,6],
        [7,8,9]
    ]
    try:
        B(items)
    except Exception, e:
        print e

def B(items):
    for num_list in items:
        if len(num_list) == 0:
            raise ValueError('A numberlist cannot be emtpy')

        C(num_list)

def C(numbers):
    for num in numbers:
        if num > 9:
            ## -- Warning???
            print '%i is greater than 9, this might break things'

        print num
2
  • 1
    It is always helpful to provide an actual code example (even though it is broken, or incomplete) for others to fix it. Language does not matter. Commented Aug 25, 2014 at 14:43
  • What makes you think B "has no use for" a warning list? A calls B to get something done, including getting it set appropriately. B sets it appropriately by passing it when calling C. Just get clear in your mind what global state change and return value every function is to calculate. Commented Sep 3, 2014 at 8:07

1 Answer 1

1

I'd have A pass a function that can write to a log of some sort down to B and then C, which C could then call to make a note of the warnings. If you're using a language that supports parameters like Racket, then you could use those. Note that this doesn't refer to function arguments, in Racket parameters are a mechanism for handling state in a threadsafe and much more elegant style. Essentially a parameter is a variable that you can mutate only for a specific portion of code, and it automatically rolls back the changes after exiting, even with unexpected control flows such as exceptions or continuation jumps. I know of no languages other than Racket that offer this construct however.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.