Solarion
Honorary Master
- Joined
- Nov 14, 2012
- Messages
- 28,051
- Reaction score
- 17,804
[)roi(];19001352 said:Sure...it's YOLO scenario:
Btw, my comments (and that of my MyBB peers) aren't meant to criticise, rather to share experience or to encourage you to consider alternatives.
Put it this way, I've never felt discouraged and wanting to quit, rather the comments from every single one of you here always leave us newbies here feeling challenged and wanting to do better. So thank-you!
I referred earlier to a "either monad with bind"; which as a basis deals with outcomes, for example:
...depending on the outcome, you could either continue the process chain or terminate (i.e. for recourse at the point of initiation).
- Success
- ...or, Failure
Bind in short ties up the input and outputs i.e. 1 function feeds into another in a chain of events. If any link (function) fails; the link is terminated; however the cause is always captured and relayed back to initiating event.
For example:
PHP:var a = function1(arg1); var b = function2(a) .... var z = function27(y)
We call function1and store the result in "a"; and then call function2 and pass in the function 1 result: "a". "a" could be as example:
Either way if function2 is dependent on the result of function1; "a" should ideally be sufficient for function2 to determine is it can safely proceed or whether it needs to abort and relay the failure back to the originating event.
- a reference to an instance,
- value
- an error code.
Naturally all of this can be expressed i.t.o. if-elseif-else statements.
Bind FYI is a similar approach to the if-elseif-else one, except that it follows a more declarative and terse style. The Either type btw is just an approach to encapsulate two possible outcomes, e.g. Success or Failure. Success would be what the next function in the call chain would require, whereas Failure would be an instruction to that function to bypass the processing in order to return the failure up the call stack.
Similarly you'll find code that uses exceptions for this purpose; but in practice that can easily lead to more try {} catch {} wrappers that ultimately just ignore the underlying fragility. Worse you have an implicit result i.e. your function shows it returns nothing; void (an Action), however a re-throw is also a result, except it's anything but explicit.
Using a Either style monad for the result type would mean your functions are modelled more like this:
We start with function1, and bind into function2 with the result from function1. function2 then has internal code to explicitly deal with deal with Success or Failure; Failure is a simple bypass, simply returning the input (i.e. the 1st issue), ultimately leading to that Failure being returned up the call stack to initiating call: "a" would either be the result of a Success or Failure.PHP:private static Result<Success, Failure> function1(InputType input) { } private static Result<Success, Failure> function2(Result<Success, Failure> input) { } // this is called as follows var a = function1(inputValue) .bind(function2) .bind(function3) .bind(function4);
Let me know if you'd like to see how to implement this in C#. Not that much code is required.
This, is incredibly helpful. What is happening at the moment is the failure is simply making it's way back down the chain to the source and not being dealt with. And if I have multiple Inserts or Selects, each call is simply throwing an error at the source (DAL), going back down the chain ending up with something like a DataSet which is now equal to NULL over and over. Does that make sense?
Thanks for the advice here, I am going to need to rework error capturing right through.