Tag > estilo
Supported by
Supported by Inetum

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;

Thou shalt use command TABLES only when unavoidable

images/thumbnail.jpg - Thumbnail
Using SELECT-OPTIONS is one of the very few reasons for using TABLES. For all other cases, explicitly declare a local variable for the equivalent structure. TABLES basically creates obscure global variables which increase ambiguity in the code. And global variables should be avoided anyway.

Thou shalt use FIELD-SYMBOLs instead of working areas

images/thumbnail.jpg - Thumbnail
READ TABLE itbl ASSIGNING is always faster than READ TABLE itbl INTO wa. Besides, when making changes to internal tables, not only it doesn’t require the explicit MODIFY but it also does away with the auxiliary TABIX variable. The only situation in which a working area is better is when adding new lines to an internal table. Some people contend that working areas should still be used when no changes are to be made to the data.

Packages 2.0

images/thumbnail.jpg - Thumbnail
SAP R/3 repository is a wonderful thing. A vast warehouse of data elements, structures, tables and much more, readily available to one and all. As developers, it is extremely convenient to quickly pick these elements and pull them into our programs as necessity conveys, while our string of thought remains virtually uninterrupted. Well, not all is sunshine and roses. If you are not careful with the mushrooms you pick you might get a poisoned one.

Thou shalt group related parts together

images/thumbnail.jpg - Thumbnail
Sometimes you find an IMPORT in the code but you have no idea where the corresponding EXPORT is. When communication is needed between programs, it should be done using a pair of methods belonging to the same class. This way, when we run into one, we’ll always know where the other one is. Anyway, try to use EXPORT/IMPORT only when unavoidable. If possible, use static class variables instead.