Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
hardyp180
Active Contributor
1,357
Most ABAP programmers are terrified of JavaScript or indeed any other programming language than ABAP. They are also scared of being run over by a huge ship. Can an SAP Press book on JavaScript help address this fear? PART THREE!


JavaScript Book


To recap I have already written two blogs regarding my notes whilst reading this book.

https://blogs.sap.com/2022/09/10/javascript-book-review-part-01/

https://blogs.sap.com/2022/11/22/javascript-how-to-avoid-huge-ships-part-02/

And just to be clear, this is the book we are talking about: -

https://www.sap-press.com/javascript_5554/

And this is how to avoid Huge Ships: -

https://www.amazon.com/Avoid-Huge-Ships-John-Trimmer/dp/0870334336

What’s it all about Alfie?

It has been said that JavaScript is not a programming language at all, which is not correct. I presume that conclusion was reached because JavaScript is used as a front-end web type of thing (view/controller) rather than back-end (model) logic, but as Matthew said the language is “Turing Complete” so it can do all the conditional logic and looping and what not you would expect in a proper programming language. Plus, probably some things other programming languages cannot do i.e., “functional programming”.

The recurring theme in these blogs is that in many senses JavaScript is not that different from ABAP. Many languages are not that different from ABAP, and yet ABAP programmers would often rather saw their own heads off than even look at another language. Conversely it has been suggested that the only way you can truly make yourself a better programmer is to learn a new programming language every year.

Just to muddy the waters - in the recent SAP recommended programming models e.g., “Fairy Programming Model for SAP” and “April REST Fool Programming Model for SAP” the view is in XML and the model is in ABAP and “only” the controller is in JavaScript.

That leads people to think, oh well the controller is nothing, I will just paste the code from somewhere else, no need to understand it. Which is quite sad really. Why not master everything i.e., HTML, XML, JavaScript, and ABAP? Then you will be a “full stack of pancakes” developer. That way when someone says to you “Pat-a-Cake, Pat-a-Cake, Pancake Man, Build me a UI5 Application as Fast As You Can” (many managers talk like that) you will be laughing.

Part of that is understanding the JavaScript language in depth, so that is what I am up to, and therefore blogging about.

Variable in a China Shop

When I left off last time, I had reached the bit about variables in Java. The thing that would surprise most ABAP people is that in a lot of the examples most “variables” are declared as constants. That seems to be a fundamental difference in approach. As I mentioned before Robert Martin wrote a Star Trek style space war game and explained how when you blew up a Klingon ship the previous universe remained intact as it was all constants, and a new universe was created with a new set of constants, this time with one less Klingon ship. That is the “multiverse” theory whereby whenever you change one tiny thing a new universe is created, but I am still struggling to see why this might be a good thing in programming.

After a while the book comes on to the subject of Boolean variables. The question is – should a programming language have true Boolean variables? Here opinion is divided – ABAP says NO and every other programming language ever invented ever says YES. ABAP gets round it by having fifteen million different data elements all with BOOL in their name somewhere most of which use X for TRUE and SPACE for false, sometimes the other way around, sometimes with THREE Boolean values, and sometimes Y and N, and sometimes J and N to reflect the German words for yes and no. Oh yes and sometimes T and F for TRUE and FALSE as in that function that compares two internal tables to see if they are the same.

Come On, Come On, Urry Up Array, We’re Going Down the Pub

People in ABAP world do not realise just how spoilt they have been compared to other languages. When programming in ABAP you don’t have to worry about manually connecting to a database, everything is GUI driven rather than command line driven etc. If an ABAP programmer does desire that experience, then now, we have the CAP and BTP and so on and so forth.

One area where ABAP programmers don’t know how good they have got it is when it comes to the concept of internal tables. They are such a basic building block of ABAP that the idea of doing without them seems unthinkable.

Other languages solve the same problem with “arrays”. Often there are different types of arrays for different use cases. Naturally they can do pretty much everything an internal table can do but sometimes there is more fluffing around involved. In essence an internal table is the “Swiss Army Knife” of the array world.

One other big difference seems to be that in an internal table the first entry is at position 1, whereas in most languages including JavaScript the first entry is at position zero. That is fine for a computer but is not the way humans think – we tend to think that the first entry in a list is the first entry, as opposed to entry number zero. Mind you humans are just as bad sometimes – in different countries the “first floor” is in different places, sometimes on the ground floor, sometimes one floor up.

The example given for a multi-dimensional array (table with deep structures in ABAP) is that of a record player. I wonder if anyone young knows what one of those is? It must seem like a cassette player or a telephone where you turn the dial with your finger to enter the phone number.

OO, OO, OO, THERE IS NO NEED TO LAUGH!

You can have objects in JavaScript, but it looks like there is not as clear cut a separation between definition and implementation, and some of the attributes can be defined as functions which is a concept you do not really have in ABAP. At the start I used to think that passing in an object instance to an ABAP method or declaring an ABAP class attribute as TYPE REF TO was the same as doing the same sort of thing with a JavaScript function but it turns out that is not the case at all. I am still not at a stage where I can coherently explain the difference but one day, I will be able to. I hope.

TERRA NULLIS

There is another concept that ABAP really struggles with and yet databases and most other programming languages do not. That is the concept of a NULL value – in ABAP a new variable gets assigned its INITIAL value e.g., 0 for an integer and so there is no real way to know if a variable is zero because someone explicitly told it to have the value zero or it is just zero because it has not been allocated a value yet. In JavaScript it looks like UNDEFINED means a variable has not yet been given an initial value, and NULL seems to be like IS NOT BOUND. If I am getting any of this wrong, then maybe I am not understanding the book I am reading very well. I am sure any mistakes I make will be gleefully pointed out in the comments.

LOGICAL SONG

There is a large chunk on Boolean operators. Going back to my notes I notice I had scribbled THIS IS BONKERS in several places. These comments relate to the ability to apply Boolean operators such as AND / OR to variables which are not Boolean values as in (IF FISH AND CAKE ) where FISH and CAKE are object instances. The JavaScript can handle this OK and you get a result according to some crackpot illogical rules.

How about this one – ( FALSE ?? TRUE ) to which the result is FALSE by the way. Obvious really. In my notes I had said this was not clean code in my mind.

Or ( 5 ||= 7 ) which translates to 5. ( 5 ??=7 ) is 5 as well.

This sort of syntax reminds me of machine code. I used to program in that when I was 14 and it is not the end of the world, but I would say if you really want “clean code” then the more like English it reads the better because then it is easier to understand and thus maintain. I cannot help but think some programmers wants the code to be as difficult to follow as possible as that way the business analysts cannot understand it.

Sadly, as time goes by ABAP is moving from “ADD 1 TO MONSTER_COUNT” to “++===//??” and the push to change primary keys of tables from “ORDER_NUMBER = 1234” to “GUID = ABCEF65432AEEEE564321”

That is also what happened in the George Orwell novel “1984” with the advent of “Newspeak”.

ZX81


ZX81



ZX81 Program in BASIC


I always like going back to talking about the ZX81 as that was what I used to write my first programs when I was fourteen. The more things change the more they stay the same, and ABAP is quite like the BASIC used by the ZX81 and indeed has become more like it in recent years with the FOR NEXT loop concept. One thing that JavaScript and the ZX81 share is that FALSE evaluates to zero and TRUE evaluates to one.

There is a great diagram in the JavaScript book which explains how the FOR/LET loop works and that in turn helps ABAP programmers understand the equivalent in ABAP which was presumably copied from JavaScript as were a fair few “functional” constructs in newer ABAP releases.

While we are talking about the past and loops as you know in ABAP we have WHILE/ENDWHILE. I was always very fond of the equivalent construct in the BBC Microcomputer where the construct was called WHILE/WEND. In JavaScript is just WHILE. These are all where the condition is at the start and there may be no executions at all.

In regard to where the condition is at the end of the construct and thus there is always at list one loop executed, in ABAP this is DO/ENDDO, in JavaScript you have DO/WHILE and in the BBC Micro it was REPEAT/UNTIL which I think is the best way to escribe what is going on.

This brings us to GOTO statements as in “GOTO 10” which was a staple of BASIC programming – you can see it used in the ZX81 program above. You cannot do such a thing in ABAP which I tend to think is a Good Thing. You can do the equivalent in JavaScript using JUMP LABELS which were invented by Van Halen.

As far as I can see the explanation of how to use Jump Labels in the book is basically a list of their flaws and thus is an implicit recommendation never to use them. They do seem to be fraught with danger – in ABAP you have words like EXIT and CHECK which do different things depending on where they are in the code, and these jump labels seem even more fraught with danger.

Theory of Evolution

The book mentions various versions of JavaScript e.g., ES2021 and I get the feeling that JavaScript rather like Java and many other languages is evolving rapidly, whereas after several “growth spurts” the core of ABAP seems to have remained rather static in recent years, presumably because all development effort is now going into the RAP.

It does not matter how long it is – at least that is what I say to myself

A comment complaint about ABAP is that variable and method names are limited to 30 characters whereas in other languages they can be as long as you want. The latter leads to less abbreviations and hence more meaningful names in the code.

If you asked SAP about this, they would say “those names are constrained by the length of the underlying data elements, and it is impossible to increase the length of data elements between releases- that would be incompatible. You would be far better off not worrying about this and concentrate on checking your code for data element MATNR which has increased from 18 to 40 characters long in later releases”.

In any event in most example code – this book is one such example, standard SAP documentation is another, instead of any meaningful business-related examples the variables are all called A1, A2, A3 and so on. The human brain just cannot relate to such examples. You must use examples with some sort of context e.g., monsters or (grudgingly) sales orders and then the examples will be ten billion times easier to understand.

A Bit IFFY

Next, I start reading about control structures – remember this book is aimed in part at people who have never programmed in any language at all before, so it has to cover the basics, so we move on to IF statements.

I still say one of the best questions ever on the SCN ABAP forum was asking whether ABAP had the equivalent of an IF statement.

Old King COALESCE was a Merry Old Soul

I recall many years back reading a “Functional Programming explained for ABAP people” type article which explained that one of the wonderful benefits of functional programming was the idea of COALESCE operator, which is a ternary operator. In essence it is a shortened version of an IF statement. This example (from the book) makes it obvious what is going on:

IS_AT_LEAST_I8 = AGE =>18 ? TRUE ;FALSE;

The article I read got all philosophical about how this was exactly suited to the way the human brain worked, and thus it was much more obvious to the reader than IF AGE > 18 THEN TRUE ELSE FALSE. I am not 100% convinced. In any event we do have the COALESECE statement in ABAP now, at least in SQL queries and CDS views, but you have to use a keyword rather than gobbledegook characters (I prefer words to gobbledegook).

Constant in a China Shop

Apropos of nothing I probably have mentioned several times before that most of the “variables” in the examples are defined as constants. This is a choice but seems to be something of a convention in JavaScript, that is you define something as a constant unless you are 100% sure it needs to change, whereas in ABAP we tend to define everything as a variable even if it does not actually change during runtime.

For example, if I took a copy of an internal table for the sole purpose of hanging onto its current state e.g., the state of the table just before user command processing then obviously I would never want to change the contents. So really, I should declare it as a constant. In ABAP I have never declared an internal table as a constant in my life and I am fairly sure you cannot fill a constant in ABAP with any sort of dynamic content at the point of declaration.

In JavaScript you can have a constant definition inside a loop and give it a dynamic value based on (say) the current index of that loop. This is possible because in JavaScript (as in most languages) if you do a data declaration inside a loop then it is only valid for that loop pass, and on the next pass the variable/constant is completely wiped out as if it had never existed, and so you can define it differently on the next pass. In ABAP no matter where you declare the variable/constant it is valid throughout the rest of the routine, even if you put the data declaration inside a loop.

Fun, Fun, Function, till her daddy takes the T-Bird away

Now is the time to get back to functional programming which takes some getting used to by an ABAPer.

In functional languages a function is a so called “first class citizen”. JavaScript is one such language that supports this concept. I looked up Wikipedia for the programming language definition of “first class citizen”

  • All items can be the actual parameters of functions

  • All items can be returned as results of functions

  • All items can be the subject of assignment statements

  • All items can be tested for equality.


As I said initially, I thought this was doable in ABAP as you can have a parameter (incoming or a result) typed as TYPE REF TO a class. However, after a while it becomes clear that an instance of a class and a function are two very different beasts. On the way to the pub just now it occurred to me that maybe that is why JavaScript is not a typed language – if you don’t have to declare a returning (result) parameter as TYPE REF TO ZIF_MONSTER but instead the returning parameters becomes whatever is passed into it then you can return all sorts of different functions.

The first difference is that methods must have names. Functions do not have to – they can have no name (like Clint Eastwood gunslingers) and become anonymous functions.

So, if you have something like this

CONST EXPLODE_MONSTER = FUNCTION( MONSTER_NUMBER) { CODE TO EXPLODE THE GIVEN MONSTER }

Then you have a constant which can then be accessed like a method call i.e. coding EXPLODE_MONSTER( 1 ) will explode Monster Number 1.

This leads to me suspecting two things

  • If the data declaration was a variable as opposed to a constant presumably you could pass a different function into the same variable mid-way through the program, maybe inside a loop.

  • Regardless of whether the definition is a constant or a variable EXPLODE_MONSTER could be passed as an IMPORTING or RETURNING parameter. Put another way the result of a function call could be a function which could do all sorts of different things when called thereafter because you don’t know the exact function you are getting back.


Mind Over Parameter

As we have seen you do not have to declare a type for parameters in JavaScript which is a big difference from ABAP. In Java you can “overload” a method i.e., create two implementations one with two incoming parameters and one with only one parameter and depending on how many parameters are supplied the appropriate method is called. In ABAP you can have optional parameters and use IS SUPPLIED to see what was passed in, though the clean code guidelines say that is naughty because if you have optional parameters then the method is no longer doing “one thing” anymore – it does different things depending on how many parameters the caller supplies. All parameters in JavaScript are optional always.

So, if you define a JavaScript function with three incoming parameters you can call that function with all three parameters, or just one parameter, or no parameters or – and here comes the kicker – with five parameters, and the compiler will be just fine with that.

There is a JavaScript equivalent of IS SUPPLIED (you can check if an incoming parameter is UNDEFINED) so that concept should not freak out too many ABAP programmers. It is the case where the caller supplies more parameters than possible that will make people start howling at the moon. As it turns out there are assorted way that the code within a function can query whether more parameters than expected have been passed in and evaluate the values. I cannot say that the concept has “clean code” written all over it though.

End of Part Three

I am trying to stop myself writing blogs with over 3000 words, so in due course I will write another JavaScript blog. By now though you will have got the general point which is that JavaScript is nothing to be frightened of. It has some concepts which are really strange to ABAP people, but ABAP has strange concepts also, and – this is the whole point – being conversant in several programming languages at once is not only not going to kill you but also increase your market value dramatically. And – at least to me – learning new programming languages is fun.

Cheersy Cheers

Paul
2 Comments
Labels in this area