Tuesday, April 20, 2010

Reduce PRPC Activity complexity: Use the question mark operator

One of the most overlooked operators available to PRPC developers is the '?', or ternary, operation. You can easily remove one activity step whenever it applies.

The following description can be found in Java tutorial section found here:

Another conditional operator is ?:, which can be thought of as shorthand for an if-then-else statement. This operator is also known as the ternary operator because it uses three operands. In the following example, this operator should be read as: "If someCondition is true, assign the value of value1 to result. Otherwise, assign the value of value2 to result."

Here is a simple example. We have a class "Test-Work" with two properties: BenefitCode and PayableBenefit. When the BenefitCode equals ENHANCED then the PayableBenefit is assigned $100, else PayableBenefit is assigned $50. (For this exercise we are assuming BenefitCode must be calculated progmatically and NOT declaritivly)

In out first activity, StandardActivity, we check whether or not BenefitCode is ENHANCED. If it is then PayableBenefit is assigned $100 and the activity exit. This requires two steps. The first one contains a Precondition, a Property-Set, and finally a Transition. (Not shown) The second Property-Set assigns the exception.



The second activity, QuestionActivity, accomplishes ALL of the above with a single Property-Set, and it does it without any Preconditions or Transisitions.



The ternary operartor is frowned upon by some programmers since it tends to compress a lot of code into a little space, making the program harder to understand. In PRPC removing Preconditions and Transitions tends to clarify the process, and this is exactly what the operator does.