ALIAS fieldname
In a project I had the problem that we had to use dynamic structures. Of course not all of the structure was dynamically – there was a set of fields which are constant -, but the whole structure has been created dynmically.
We then had to aggregate this dynamic internal table on some constant fields. We used MOVE-CORRESPONDING <dynamic_structure> TO constant_structure to overtake all of the fields we knew to be in the dynamic structure.
I will show you here how you can set up an alias name for a fieldname.
I will not set up the example using dynamic structures and demonstrating the MOVE-CORRESPONDING, because this distracts us from the main solution I would like to show.
Database Structure
This is the origin structure:
The field SALE_FLAG results from incorrect spelling, lazy implementation of specifications or whatever and should be named SALES_FLAG.
As this table is in production you would need to do a lots of effort to rename this field to SALES_FLAG. To avoid loss of data, you must add a new field with the correct name (SALES_FLAG) and write a migration report to move the data from SALE_FLAG to SALES_FLAG. After that you will have to adapt all reports to use SALES_FLAG instead of SALE_FLAG.
The following solution is not that neat but helps getting along with a wrong fieldname.
.Include
You simply can replace the field SALE_FLAG by a new include that you can name whatever you want to. The include must contain the exact field definition of the origin table. If you do so you have exactly the same database structure than before.
so why should you do that? Because of named includes. You can name an include (group name) and give it a name where all the fields in this include can be addressed by.
You simply have to name that include SALES_FLAG and now you can use the field by its origin name and also by the alias name.
Using The Alias
The following example shows how you can use the alias fieldname in a program:
SELECT * FROM ztt1 INTO TABLE @data(lt_tt1) WHERE sale_flag = ‘X’.
LOOP AT lt_tt1 INTO data(ls_tt1).
ls_tt1–sales_flag = ‘Y’.
WRITE: / ls_tt1–matnr, ls_tt1–sale_flag.
ENDLOOP.
There is one restriction where this method does not work: You can not select data using the alias name:
SELECT * FROM ztt1 INTO TABLE @data(lt_tt1) WHERE sales_flag = ‘X’.
The compiler sends this message
In database operations, the flat view of the type structure is always
relevant. This means that alias components cannot be used. –
Advantages
The two advantages of this solution are:
- You have access to a field with two names. As discussed before this might be very important especially when using a dynamic structure and MOVE-CORRESPONDIG.
- The database structure does not change. You can change the database table as described above and all data will remain because the origin fieldname dows not change.
<offtopic>
Why is the command named MOVE? The origin field value is only copied to the target field, not moved. So shouldn’t the command be called COPY and COPY-CORRESPONDING?? π (According to the disussion of readable or not by Timo John Horst Keller, Suhas Saha, Paul Hardy and others)
The COBOL roots of ABAP ...
Talking of which (roots I mean) and sorry to hijack a thread but Horst - any chance of an 'IDES' copy of R/2 ? I have a machine that it'll run on and I want to provide those who have never seen a 3270 terminal a glimpse of the past (and what they are missing....)
Rich
(I keep asking....occasionally.)
I already guessed so. I just wanted to have a reason to link to the Masters Of ABAP Universe to get more attention for my blog post... π
It is like moths to the flame.
I spent two hours this afternoon renaming and renaming and changing my mind again and again to try and make about eight lines of code read as much like English as I could.
As to Masters of the ABAP Universe I don't know if that makes me He-Man, the way I look I think I am more like Skeletor, but with a beer gut, Horst can be He-Man if he wants.
Anyway, this was MRP related code and I started off with variables named LS_MARC-MMSTA and LS_MARC-DISMM and ended up with code like
IF we_do_not_have_any = abap_true AND we_want_some_more = abap_true.
etc etc
If had a 7.5 system in my day to day job I could do away with the ABAP_TRUE and have methods instead returning ABAP_BOOL or some such. That would be more convoluted, but I am prepared to go to almost any lengths to make code look readable.
I had two comments today at work which made me happy. A new programmer said he had never seen code like mine before because (a) it read like English and (b) it was peppered with jokes (if it did not read like English I could not add the jokes, as they would make no sense) so it was both obvious what was going on and funny to read. I hope my books are like that as well.
Generally, if I have a comment above the line of code I try to see if I can make the line of code read like the comment, so I can ditch the comment.
More importantly and end user was shown my code for some reason and she understood it as well despite never having programmed ever in her life. That's what I call victory.
Cheersy Cheers
Paul
LOL
If I had only you in mind I'd rather speak of Of Monsters And Man... π
Humor is really one of the most neglected things when discussing programming styles or methods. I wanted to write my ABAP Kochbuch a bit more Paul-Hardy-Style but I wasn't allowed to... π So it turned out to be a great but very stiff and formal book. I love your ABAP To The Future because of your funny style without being childish or foolish. But that's a different story... Maybe we should start a new discussion about humorous code?
I think one should not take too much time to make things readable as there is a very big chance that no one else will ever read it...
Therefor I blogged about this trick to simplify code (in this specific case). And you will have less code to write and to comment.
But maybe exactly the opposite, because of using an unusal technique?
However...
I always recall the writings of "Uncle" Bob Martin in the book "Clean Code" where one of this main points is "we are authors".
His position is that programmers spend 95% of their time looking at code, and only 5% writing it. He recorded a screen-cam of himself programming and found it mostly consisted of paging up and down and going on safari through the code he was changing, trying to work out what it did.
Given that, making code readable takes on a whole new meaning. Even if no-one else ever reads it, which I consider unlikely, as you could be on holiday or just too busy and someone else has to fix/enhance a program you have written, You yourself will have to read it at some point in the future. It is all too easy to forget whatever it was you were trying to achieve at the time.
We also have a peer review process in place. None of my code can make it to test unless one of my colleagues has looked at it first and signed off on it (we use RevTrac for this).
So my code will always be looked at by at least one other person. I can usually tell when this is happening as can hear them laughing and/or singing any song references I have put into the code.
Cheersy Cheers
Paul
I can usually tell when this is happening as can hear them laughing and/or singing any song references I have put into the code.
It reminds me to this one: π
<chuckles>
Could should almost read like a book, and I agree entirely with what Paul is saying, including the jokes!
Rich
Enno,
Thanks for sharing this.
If SALE_FLAG is used in any screen that will still show up as SALE_FLAG only but not as SALES_FLAG.Right ?
K.Kiran.
Hi Kiran,
it seems as if the ALIAS can be used (no syntax error) but without any context (no f4-values or f1-help).