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 work, revamping algorithms, ensuring correctness, you know the score. Then my eyes snagge

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,

Quick Guide to User-Defined Types in Oracle PL/SQL

A Twitter follower recently asked for more information on user-defined types in the PL/SQL language, and I figured the best way to answer is to offer up this blog post. PL/SQL is a strongly-typed language . Before you can work with a variable or constant, it must be declared with a type (yes, PL/SQL also supports lots of implicit conversions from one type to another, but still, everything must be declared with a type). PL/SQL offers a wide array of pre-defined data types , both in the language natively (such as VARCHAR2, PLS_INTEGER, BOOLEAN, etc.) and in a variety of supplied packages (e.g., the NUMBER_TABLE collection type in the DBMS_SQL package). Data types in PL/SQL can be scalars, such as strings and numbers, or composite (consisting of one or more scalars), such as record types, collection types and object types. You can't really declare your own "user-defined" scalars, though you can define subtypes  from those scalars, which can be very helpful from the p