14.01.2020 Views

ABAP_to_the_Future

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Design by Contract 7.3

future. If I give him fake money, then I’m at fault, the police arrest me, and hopefully

in the future I’ll stop printing bogus money. To put this into technical terms:

When the intended design of an interaction between routines in a program is violated,

the exact routine where the proble m occurred is identified and stopped

from happening again.

Here are some examples of program assumptions that, if violated, would cause an

exceptional situation that would cause the program to fail if there was no exception

handling to deal with the violation:

왘 The pension age in Australia has been 65 for over a hundred years, so it will

always be 65, right? Certainly, there is no need for programs that deal with

pensions to be able to handle an age of anything other than 65. Oh dear! The

current government is raising it to 70! Who would have thought?

왘 The assumption that a certain database read will always succeed. Surely there is

no need to test it with a SY-SUBRC = 0 ! That’s just a waste of time. Without

exception handling, if the result is 4 for fail, then the program will continue

on its merry way, unaware that the data it’s now using is most likely incorrect.

왘 When testing to see if an objectis locked by calling a standard ENQUEUE (locking)

function module and specifying in the code that the record is locked IF SY-

SUBRC <> 0, assuming that the only possible error could be 1 for FOREIGN_LOCK.

If this were the case, the system variable SY-MSGV1 would be filled with the user

name of the person locking the object and would send him a message. Unfortunately

for the person making this assumption, there are several possible

exceptions generated from an ENQUEUE module, all with different SY-SUBRC values,

and one of the possible return codes is 2 for SYSTEM_FAILURE. I learned this

the hard way: In my case, the production system kept failing, and the function

module came back with SYSTEM_FAILURE and SY-MSGV1 being blank. Meanwhile,

because SY-SUBRC was not zero, my code presumed there was a lock and

tried to send a message to the user in SY-MSGV1. It turns out that when you send

a message to a blank user name using the TH_POPUP function, every single user

in the system gets that message. As might be imagined, this did not make me

popular. My lunch breaks in Germany were halved because I made a failed

assumption.

The fact is, we all make assumptions all the time in our programs, similar to the

examples just mentioned. Th erefore, if eating is im portant to you, pay close

attention. In the rest of this section, you’ll look at how the design by contract

275

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!