Skip to main content

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:

DECLARE
   l_play_date   DATE
      := qdb_quiz_mgr.date_for_question_usage (:p46_question_id);
BEGIN
   :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:

DECLARE
   l_play_date   DATE
      := qdb_quiz_mgr.date_for_question_usage (:p46_question_id);
BEGIN
   :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!
Awwwwwwwwwww......

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.

Comments

  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...

    ReplyDelete
    Replies
    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.....

      Delete
  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 ;-)

    ReplyDelete
  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 :)

    ReplyDelete

Post a Comment

Popular posts from this blog

Running out of PGA memory with MULTISET ops? Watch out for DISTINCT!

A PL/SQL team inside Oracle made excellent use of nested tables and MULTISET operators in SQL, blending data in tables with procedurally-generated datasets (nested tables).  All was going well when they hit the dreaded: ORA-04030: out of process memory when trying to allocate 2032 bytes  They asked for my help.  The error occurred on this SELECT: SELECT  *    FROM header_tab trx    WHERE (generated_ntab1 SUBMULTISET OF trx.column_ntab)       AND ((trx.column_ntab MULTISET             EXCEPT DISTINCT generated_ntab2) IS EMPTY) The problem is clearly related to the use of those nested tables. Now, there was clearly sufficient PGA for the nested tables themselves. So the problem was in executing the MULTISET-related functionality. We talked for a bit about dropping the use of nested tables and instead doing everything in SQL, to avoid the PGA error. That would, however require lots of wo...

How to Pick the Limit for BULK COLLECT

This question rolled into my In Box today: In the case of using the LIMIT clause of BULK COLLECT, how do we decide what value to use for the limit? First I give the quick answer, then I provide support for that answer Quick Answer Start with 100. That's the default (and only) setting for cursor FOR loop optimizations. It offers a sweet spot of improved performance over row-by-row and not-too-much PGA memory consumption. Test to see if that's fast enough (likely will be for many cases). If not, try higher values until you reach the performance level you need - and you are not consuming too much PGA memory.  Don't hard-code the limit value: make it a parameter to your subprogram or a constant in a package specification. Don't put anything in the collection you don't need. [from Giulio Dottorini] Remember: each session that runs this code will use that amount of memory. Background When you use BULK COLLECT, you retrieve more than row with each fetch, ...

PL/SQL 101: Three ways to get error message/stack in PL/SQL

The PL/SQL Challenge quiz for 10 September - 16 September 2016 explored the different ways you can obtain the error message / stack in PL/SQL. Note: an error stack is a sequence of multiple error messages that can occur when an exception is propagated and re-raised through several layers of nested blocks. The three ways are: SQLERRM - The original, traditional and (oddly enough) not currently recommended function to get the current error message. Not recommended because the next two options avoid a problem which you are unlikely  to run into: the error stack will be truncated at 512 bytes, and you might lose some error information. DBMS_UTILITY.FORMAT_ERROR_STACK - Returns the error message / stack, and will not truncate your string like SQLERRM will. UTL_CALL_STACK API - Added in Oracle Database 12c, the UTL_CALL_STACK package offers a comprehensive API into the execution call stack, the error stack and the error backtrace.  Note: check out this LiveSQL script if...