Monday, November 7, 2016

On the importance of keeping algorithmic logic separate from display logic

On the PL/SQL Challenge, all times are shown in the UTC timezone. Weekly quizzes end on Friday, midnight UTC. So I recently decided that when I display the time that the quiz starts and ends, I should add the string "UTC".

Our quiz website is built in Oracle Application Express 5.0, so I opened up the process that gets the date and found this:

   l_play_date   DATE
      := qdb_quiz_mgr.date_for_question_usage (:p46_question_id);
   :p46_scheduled_to_play_on := TO_CHAR (l_play_date, 'YYYY-MM-DD HH24:MI');

"OK, then," says Steven the Fantastic Developer to himself. "I know exactly what to do."

And I did it:

   l_play_date   DATE
      := qdb_quiz_mgr.date_for_question_usage (:p46_question_id);
   :p46_scheduled_to_play_on := 
      TO_CHAR (l_play_date, 'YYYY-MM-DD HH24:MI') 
      || ' UTC';

Ah, PL/SQL and APEX - so easy to use! :-)

Now, there are lots of things you could say about the change I made above, but here's one thing that is undeniably true:
P46_SCHEDULED_TO_PLAY_ON will never by NULL.
Right? Right. Of course, right.

So that's fine, though. Because that's what I wanted: to have "UTC" always show up, and there's always going to be a date when the question is used in a quiz, right?

Well, no. In fact, this code is part of our Quiz Editor page, and on that page we offer a button that allows you to easily and quickly schedule a quiz for play.

But only if it hasn't already been scheduled. If it hasn't already been scheduled, then the date is, oh wait, um, NULL.

And that's why we have a condition on that button:

And that's why Eli Feuerstein, the fine fellow who does most of the work on the PL/SQL Challenge and it's cool new sister, Oracle Dev Gym, reported an issue with this page:
The Schedule button never appears on the page!

So two lessons learned (re-learned, and learned again, then forgotten, then re-learned, then learned again....):

1. When I am about to make a change, ask myself: "What impact might this have?" 

In the world of APEX, it's pretty easy: search for the string "P46_SCHEDULED_TO_PLAY_ON" and see how it is used in the application. 

2. Keep completely separate the data (in this case, APEX items) that is used for algorithmic logic and the data that is used for display purposes.

I could create a separate item for display purposes, or a different item to be used in conditions and other PL/SQL blocks. 

But I should not use the same item for both.


  1. Hi, Steven

    Absolutely, the "display" item and the "logic" item should be seperate, true.

    If not possible (or as a quickfix), you could keep the logic of the item being NULL for NULL dates by putting your constant text within the date format mask:

    :p46_scheduled_to_play_on :=
    TO_CHAR (l_play_date, 'YYYY-MM-DD HH24:MI "UTC"');

    Then the UTC only appears for non-NULL dates...

    1. I am pretty sure that the appropriate response by Steven Feuerstein to Kim's comment is:

      1. Sound of hand slapping forehead.

      2. Mouth emitting loud: "D'oh!"

      Yes, Kim, that will certainly do it. Sigh.....

  2. No need to sigh ;-)

    The main point is still valid - the logic deciding whether to show a button or not should not be based on a the value of what is basically a display function, it should be based on the base column value.

    Or perhaps better yet use a boolean "is_scheduled" function containing the logic - presently simply "return qdb_quiz_mgr.date_for_question_usage is not null", but that logic might change over time.

    Anyway, you know all that - this is just something that happens to every application that evolves, and every application that is used does evolve (otherwise it's a dead end ;-)

  3. Here is one for quiz: You have two apps in apex running on different NLS, for one first day of week is Monday for other its Sunday. Database is running in one of this mode. When some of this app request current day of week how will you determine what day of week it is? Changing of NLS in apex page is not allowed :)