Tag > estilo

Thou shalt modularize, modularize, modularize

images/thumbnail.jpg - Thumbnail

Historically ABAP programs tend to grow very loooong. All programming best practices teach us there is not a single advantage in this approach. If any routine, be it a program, a method, a function or anything else, becomes longer than 200-300 lines, question it and seriously consider refactoring it into several sub-routines. This has the added advantage of potentially increasing code reuse. But the greatest advantage is encapsulation, isolating variables in their local context, instead of having all of them together, resulting in safer and more readable code.

Thou shalt reuse, thou shalt not rewrite

images/thumbnail.jpg - Thumbnail

If the same piece of code is repeated at least once, question yourself why and try to avoid it by creating a reusable routine. If there is more than one SELECT for the same table in a program, make sure you can’t merge them into a single one. Sometimes a smart use of RANGEs to unify parameters can avoid the need for multiple SELECTs. If the same code is used in 2 different programs, don’t repeat the code.

Thou shalt avoid global variables

images/thumbnail.jpg - Thumbnail

The more global variables a program has, the most obscure it becomes. Please avoid them. This is a basic rule of good programming and should always be followed. Even if several variables have to be passed by parameter, it takes slightly more effort but yields a much more readable and safer code. Exceptions can be made for simple reports which run around a single internal table, which can be declared globally without compromising clarity.

How to ask if the line exists without seeming fashioned

images/thumbnail.jpg - Thumbnail

Long ago, you used the expression “groovy, man”. Later came “great, man”. Then there was “cool”. Today you say “awesome”. It’s important not to get confused and not make a fool of yourself.

And how do you ask an internal table if a line exists exists?

Thou shalt use TRANSPORTING NO FIELDS

images/thumbnail.jpg - Thumbnail

Many times we do READ TABLE itbl or LOOP AT itbl just to do a CHECK SY-SUBRC = 0. In these cases, the actual data read is not needed. For these cases always use TRANSPORTING NO FIELDS. This way is faster and avoids having to declare a target structure.

SELECT-OPTIONS default behavior

images/thumbnail.jpg - Thumbnail

Abapinho received a letter.

Mr. Abapinho,

Everybody knows how to set default values in select options using the DEFAULT keyword. What some people may not know is that one can also set the default option, sign and even if allows for intervals or just fixed values.

Chained exceptions

images/thumbnail.jpg - Thumbnail

Today I will teach you how to chain exceptions. It’s a very practical solution to a complicated, not so obvious problem. Let’s start by describing the problem. Imagine you are in the application BANANA. The application is quite complex. It has three modules: BANANA1, BANANA2 and BANANA3. Each one has its exception class ZCX_BANANA1, ZCX_BANANA2 and ZCX_BANANA3. Since the application is in fact well designed, all the exception classes inherit from the same ZCX_BANANA.

Ignore function module exceptions

images/thumbnail.jpg - Thumbnail

When calling a function module which returns exceptions you normally give them sequential numbers like this:

CALL FUNCTION 'GO_BUT_PLEASE_COME_BACK'
  EXPORTING
    ali = 'To the moon'
  EXCEPTIONS
    NOT_FOUND = 1
    GOT_LOST  = 2
    OTHERS    = 3.

But Code Inspector may be configured to report a warning if afterwards you are not careful to add an IF or a CASE to look at SY-SUBRC,

Let's concatenate

images/thumbnail.jpg - Thumbnail

We start with two variables:

DATA word1 TYPE string.
DATA word2 TYPE string.
DATA: phrase TYPE string.

word1 = this.
word2 = that.

And we want to concatenate them adding the word “plus” between them and, of course, separate them by space.

Where is the boolean?

images/thumbnail.jpg - Thumbnail

It’s not.

But they - the people who make and remake the ABAP itself - are trying to mend this unfortunate situation.

Look at this new functionality:

LOOP AT tbl ASSIGNING <line> CASTING

images/thumbnail.jpg - Thumbnail

Did you know that you can do a LOOP on an internal table of one type into a structure of a different type?

Thou shalt not use CHECKs directly in user-exits

images/thumbnail.jpg - Thumbnail

User-exits (and enhancements) are usually crowded with CHECKs. The tragic consequence is that, if the check fails, nothing else after it will run. Not even the standard code. Always try to correct these. Encapsulating the code inside a routine (ideally a method) is enough to render it harmless.

Thou shalt use LIKE LINE OF itbl

images/thumbnail.jpg - Thumbnail

When declaring structures which will receive data from an internal table, instead of declaring its type directly, use LIKE LINE OF. This way not only it becomes clear that they are related but also, if you happen to change the type of the internal table, you won’t have to worry about updating the structure’s type.

Thou shalt use a constants table

images/thumbnail.jpg - Thumbnail

Whenever you feel a constant value can change and you can’t add it as a user parameter, store it in ZCONSTS. This table should never be used directly. Instead, a class like ZCL_CONSTS should be created to properly access it, like shown in this article: (portuguese only) Resist the temptation of using T900 or similar tables for this purpose. It’s true that a lot of people do it. But it’s ugly, durty and besides these tables don’t even have an adequate key because they were not designed with this in mind.

Thou shalt build up and adopt toolkits with common tools

images/thumbnail.jpg - Thumbnail

Code which is used very often should be made available centrally, if possible under a single package (ex: ZTOOLS) so that it’s easily identified. There is a lot of code already available on the Internet to accomplish several common functions (ex: ABAP2XLSX). Adopt it; For your most common tasks, develop your own tools which you can reuse and add them to the central library; Advertise the existence of that library among your colleagues to avoid that they waste time come up with duplicate code wasting;