Skip to main content

Never open up access to code protected by ACCESSIBLE BY?

In my Oracle Magazine article, When Packages Need to Lose Weight, I step through the process of breaking up a large package body into "sub" packages whose access is restricted through use of the new-to- Oracle Database 12c ACCESSIBLE BY feature.

The idea, to sum it all up, is that once I move code from my original too-large-to-manage package body to another package, the header moves to the spec of that package. This means that formerly-private functionality is now accessible to anyone with execute authority on that package.

For that reason, I stated:
The body of em_central shrinks to a fraction of its former self, because the body of each procedure is simply a redirect into the em_central_a and em_central_b packages. The subprograms in these packages should be invoked only by em_central.
A reader contacted me with this question:
For me this means that newly written subprograms should not invoke the new packages em_central_a and em_central_b directly. So the ACCESSIBLE BY clause could be applied to these packages as well.  For a colleague of mine, this is carrying things too far. He thinks you see no problem if newly written code accesses the packages em_central_a and em_central_b directly. If you agree to the opinion of my colleague, the cited excerpt from the article should be revised. 
Revise what I wrote? Accept that I was wrong? What is Arne thinking?

He is thinking that I am arational human being. And I deeply appreciate that. In fact, I have no (minimal, anyway) problem admitting I am wrong and fixing what I have written. I do that a lot. :-)

But I think in this case I will fight the good fight and provide a more thorough explanation.

First, Arne, regarding use of ACCESSIBLE BY with "these packages as well." I think that you must be referring to the packages containing the new subprograms that want to use em_central_a or _b? If so, yes, you can certainly expand the list of program units in ACCESSIBLE BY, which brings me to my main point:

The default position you should take regarding previously private code that was made "public" solely to help re-org your chubby packages is:

Don't let anyone/any other program access that code.

Which is what I am saying in that quote. Why? Well, because it wasn't designed for use anywhere else. It wasn't tested for use outside of the current execution path. I do think it is extremely important that the original intention of the original developer be respected until you find a good reason to change it.

Otherwise you are asking for trouble - unless that code is so transparent, so well-written and comes with an automated unit testing script that it can understood and used in a variety of ways with confidence.

Please think about this: how many packages have you written or seen from others for which this is true?

Which brings me to my final point:

Everything changes, and code reuse is critical to overall maintainability of code.

By which I mean: when you first break up the package and create pseudo-private packages for the previously private code, you should tightly restrict usage. But suppose a developer comes along, sees those inaccessible subprograms, investigates and decides: "Wow, I could really use that functionality!"

Am I suggesting you tell them to get lost? No, of course not! At that point, you:

  • do some analysis, verify that there really is a good fit;
  • sort out what would need to change in the existing restricted subprogram to be used elsewhere;
  • ensure that these changes would not affect the original use 
  • make the changes and test them
  • add the new package "user" to the ACCESSIBLE BY clause
So, sure, "no problem" - expand access to that now-possibly-usable code, but do with it intention and careful decision-making. 

Should I revise my article? After all, it's not so "black and white", is it? Well....I don't think I need to do that. After all, I don't say:

NEVER EVER USE THAT CODE ELSEWHERE OR YOU ARE AN IDIOT. [I do NOT say this!]

I say: "The subprograms in these packages should be invoked only by em_central."


For any and every programming feature you ever encounter, there will always be nuances. I'd rather use my articles to make developers aware of what is possible with new features, and the major motivations behind these features.

Good, clever, practical programmers will discover exceptions, interesting new ways to apply a feature, all the time. It's what we do. And my statement includes an implicit admission of that reality.

Comments

Popular posts from this blog

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

Three tips for getting started right with Oracle Database development

By "Oracle Database development", I mean, more or less, writing SQL and PL/SQL. I assume in this post that you have access to Oracle Database (which you can get via Cloud services, Docker , GitHub and OTN ). A. Use a powerful IDE, designed with database programming in mind. There are lots of editors out there, and many IDEs that work with Oracle Database. Sure, you could use Notepad, but OMG the productivity loss. You could also use a popular editor like Sublime, and then it get it working with Oracle. I suggest, however, that you  download  and install Oracle's own own, free, powerful IDE: SQL Developer . If you like to complement your graphical IDE with a command line tool (or OMG if you actually prefer  a command line tool to a graphical interface), you should also check out the relatively new and generating-lots-of-excitement SQLcl . B. Enable compile-time warnings and PL/Scope. The database has tons of useful functionality burned right into it, ready for...

Why DBMS_OUTPUT.PUT_LINE should not be in your application code

A database developer recently came across my  Bulletproof PL/SQL  presentation, which includes this slide. That first item in the list caught his attention: Never put calls to DBMS_OUTPUT.PUT_LINE in your application code. So he sent me an email asking why I would say that. Well, I suppose that is the problem with publishing slide decks. All the explanatory verbiage is missing. I suppose maybe I should do a video. :-) But in the meantime, allow me to explain. First, what does DBMS_OUTPUT.PUT_LINE do? It writes text out to a buffer, and when your current PL/SQL block terminates, the buffer is displayed on your screen. [Note: there can be more to it than that. For example, you could in your own code call DBMS_OUTPUT.GET_LINE(S) to get the contents of the buffer and do something with it, but I will keep things simple right now.] Second, if I am telling you not to use this built-in, how could text from your program be displayed on your screen? Not without a lot o...