Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
hardyp180
Active Contributor
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 FOUR!


JavaScript Book


To recap I have already written three 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/

https://blogs.sap.com/2023/02/06/how-to-avoid-huge-javascripts-part-03/

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

King Constant

When we last left JavaScript world the chapter was talking about using re-usable code blocks – what 99% of ABAP programmers in 2023 would consider FORM routines and function modules, what 1% of ABAP programmers in 2023 would consider methods of classes. I reckon I have circa 25 years until I die of old age and on my “bucket list” would be if the percentage of ABAP programmers using OO increased to at least 2% during that period.

Leaving that aside, going back to the JavaScript book I had noted that what makes my head spin (as an ABAP person) is that in JavaScript one tends to declare all the “variables” as constants. In the chapter at hand what I would call the “returning” parameter from a function call is always stored in a constant.

I suppose in the same way that in OO you are supposed to declare everything private in a class unless you 100% need it protected or public. I have often wondered why the code inspector does not say “you can make that variable private you know” in the same way it keeps trying to get you to make all the classes final. Anyway, in JavaScript my guess is you are supposed to declare everything as a constant unless you 100% need to change it during runtime.

In the same way in most countries everything is legal unless specifically not allowed, and in Germany everything is illegal unless specifically allowed. In Heidelberg my wife got chased down the riverbank by the police for feeding the ducks, for example (there were tons of them in the river at that time, ducks that is not police). That was in 2011. I think the logic is that if you feed ducks bread it hurts them. It was explained to me by a Ranger in Australia that feeding a duck some bread makes the eggs they lay thinner and thus more prone to damage i.e., that would mean less ducks born, reducing the duck population going forward.

I returned to Heidelberg in 2018 and there were no ducks at all then in the river. I suspect that Judge Death had decided since he could not stop people hurting the ducks by feeding them the optimal solution was to kill all the ducks thus making it literally impossible to hurt a duck by feeding it. Because there were no longer any ducks. That got rid of that crime. That is how things work in Mega-City one at any rate. Obviously in the real world that would never happen.


Judge Death


Return to Sender

Coming back to reality for a brief second, I would like to “return” so to speak to one of the violent differences between languages like ABAP and languages like JavaScript where a function is a “first class citizen”.

In any such language where this is the case a function can be a return value. I keep coming back to this point like a broken record, but this is a different concept to anything we have in ABAP, and it took me ages to get my head around it and I am not 100% sure I do even now.

In ABAP you can of course return an instance of a class, and that instance can perform calculations - but the difference here is that ABAP is a strongly typed language so the returning parameter must be typed to a class or an interface whereas JavaScript can return anything it feels like.

Given the lack of restrictions in JavaScript on how many parameters must be passed into a function and what type they must be, maybe you can start to see where this is going? More on this later.

Basil De-Fawlty

When you have an importing parameter in ABAP you can specify a default value for that parameter. It is 100% the same in JavaScript. The syntax varies depending on the JavaScript language version, in later versions it is obvious, in earlier versions it was a load of old gobbledegook that no-one in their right mind could ever hope to understand.

Clean Code / Secret Code / Machine Code

I have never made any secret that I like programming languages to be “verbose” as in they read as much like Plain English as possible (sorry for anyone for whom English is not a first language). This concept was called “Literate Programming” by Donald Knuth.

Thus, for example I prefer

ADD 1 TO MONSTER_COUNT (Comes from COBOL so must be bad, because it is old)

To

MONSTER_COUNT +=1. (Modern syntax so must be good, because it is new)

The former is much easier to read. No-one can tell me the latter is intuitive.

A quick look on the internet and here we have a moan that the following is not possible in ABAP.


Odd Code


To be honest I am quite happy ABAP cannot do this. I cannot even begin to guess what that code is trying to do.

To me, even if very concise, that code reads like machine code. The computer is fine with it, therefore it is not “clean code” as my definition of clean code is something that is easy to understand and thus maintain.

Now that is out of the way you may wonder why I am bringing this point up in the first place.

The reason is that in JavaScript you sometimes get secret codes and there is no way a casual reader could work out what the secret code means.

For example take this declaration

…whatAmI

You are a structure! More accurately the values from an array one after another. You can tell because of the three dots at the start. That is like using the prefix “S_” in ABAP i.e., Hungarian Notation, but much less readable and once again, needing a specialised knowledge of the language. At least “S” might give you a clue it is a structure given that S is the first letter of the word “Structure” but “…” has no inherent meaning to anyone.

DO SOMETHING WITH ABC

Amazing as it may seem I have often been attacked for using Monster examples in my books and blogs. I suppose they want me to use SFLIGHT or maybe something more realistic like sales orders. However, I am 100% sure no-one wants me to replace an example using concepts a human brain can relate to – be in Monsters, Aircraft or Sales Orders – with utterly abstract concepts e.g., change MONSTER_COUNT and all the other variables to A, B, C etc. and change method names like MAKE_NEW_MONSTER to DO_SOMETHING.

For example, I could say something like

IF MONSTER_COUNT LT MINIMUM_NEEDED_MONSTERS.

MAKE_NEW_MONSTER( ).

ENDIF.

As opposed to

IF A LT B.

DO_SOMETHING( ).

ENDIF.

You might think – and many people obviously do – that there is no difference at all between those two code examples, so just go with the latter. The point is that a human brain can absorb concepts far faster if there is even a tenuous link to the real world (as in Monsters) as opposed to generic meaningless concepts like A, B and C.

When trying to get a point across and using an analogy/parable does anyone giving a speech ever say (using these exact words) “One day A was walking down the road and met B and they DID_SOMETHING and the end result was Z”.

No - they replace the placeholder likes “A” and “B” with specific values. Yet in most textbooks and internet examples it is always the A / B / C thing….

Put another way when you want to demonstrate how to use an existing or new functionality the only way you are ever going to be able to explain it properly is to give a practical example of how this existing/new thing can make your life better. For such an example generic values like “A” and “B” for variables and “DO_SOMETHING” for method names are just not going to cut the mustard.

Far from the Shadow Now

As we know in ABAP a local variable is not visible outside it’s routine (which is why it is local) and if you really want to get yourself into trouble you can name a global variable, a local variable and a parameter coming into the routine, all with the exact same name. That is not a very sensible thing to do, but you can do it, and I have in fact seen it done with a FORM routine which declared a local variable called OK_CODE which overshadowed the incoming parameter OK_CODE and the global variable OK_CODE.A horrific situation which is the exact opposite of “Clean Code”. ABAP Methods give you a hard error when you try that trick.

In Java things seem to work a little differently. If I am in a function ten levels down the call stack and I try to use variable BANANA and that variable is not declared inside the function being executed, the interpreter goes looking up the call stack until it finds the highest calling level where such a variable is declared, which could be at the global level. That implies that any variable is visible to all routines below it in the call stack and presumably you could do the overshadowing trick in the lower levels.

There (in the book) are then some examples showing the bizarre behaviour of functions with the result varying depending upon where they are called from.

It’s not 100% relevant but I once again noted that in the examples in JavaScript an attribute of an object definition can be a function (because functions are first class citizens) and this is not the same as defining the attribute as TYPE REF TO SOMETHING. In ABAP you are saying the attribute is going to be filled with an object instance, in JavaScript is more like the attribute (function) could be the signature and implementation of any method of any class. That is probably not an exact analogy but it the only analogy I can come up with. This is one of the areas where the difference between the languages does make an ABAP persons head spin. One needs about a million examples before one can get one’s head around the concept.

Philosophical Point

I was puzzled to read that because JavaScript is an interpreted language syntax errors could not be detected until runtime – that did not match at all what I had been seeing in Eclipse or VSCode. It turns out it is the IDE that is doing static checks on the code, just like in SAP. Not that the developer cares at all where the check is done, only that it is done. In the same way due to the SAP Community – primarily Lars – it is possible to do static syntax checks on ABAP code totally outside of an SAP system.

CATCH in the Rye

In ABAP in a TRY-CATCH block you can specify several different types of exceptions in the CATCH block (via multiple CATCH statements) and apparently that is normal for programming languages. Not so for JavaScript however because it lacks the “strongly typed” nature of other languages so it cannot be sure what something is.

Plus – all exceptions in JavaScript are what I would call CX_NO_CHECK exceptions, which is the type I use pretty much all the time anyway.

Finally, we have a new Sheriff who is going to clean up this town

A lot of ABAP programmers do not use exception classes and even if they do, they are probably unaware there is such a thing as a CLEANUP statement. It is just as well people do not know about this statement as it does something totally different than what you would logically expect it to do. So many people - including me - say never use CLEANUP as it is (ironically) the opposite of “Clean Code”. Clean Code meaning when you read the code you can easily work out what it does.

In JavaScript in the TRY/CATCH construct we have a code block called FINALLY which executes if no error occurs, and if an error occurs after he error handling finishes. An ABAP example I can think of is that you need to unlock a document after trying to change it regardless of whether the operation to change the document failed or succeeded.

Now you might say you can do that in ABAP by just having the code to be executed after the end of the TRY/CATCH block – but what if the CATCH decides that processing further code in the routine is not a good idea and does a RETURN? Then you would have to do two unlock statements, one in the error handling, one in the normal code.

The example given in the book has a 100% relation to ABAP. In other languages reading database tables is agony, you must manually connect and disconnect from the database. This seems like madness to ABAP programmer who have been spoiled with an environment where you can just access the database with a SELECT statement any time you feel like it, but I warn if you – if you dare to suggest that is better than what every other programming language does, you will be shot at dawn. Just you try it and see what happens.

In any event in ABAP if you have a global temporary table (which we use as a replacement for FOR ALL ENTRIES) at the start of the routine you insert entries into that database table, and at the end of the routine you must then delete all the entries from the database else the world ends – hence the word “temporary”.

In this case you must delete all the entries whether there is any sort of error or not. I do that by having the error handling not terminate processing of the routine, but something like FINALLY would make it more explicit what is going on.

In essence in such an example the FINALLY processing block does what you might expect the CLEANUP block in ABAP to do.

Writing someone a Reference

Now we move on to reference variables. Just like in ABAP that is where the variable points to an area of memory so if you say OBJECT1 = OBJECT2 they are “quantum entangled” and changes to one will result in changes to the other (because both variables point to the same area of memory). In real life I wonder if that quantum entanglement thing – where you change one atom, and the linked atom instantly has the same change no matter how far away it is – can really enable instant (i.e., faster than light) communication between Earth and Uranus. We shall see.

As I have mentioned loads of times most of the examples in this book declare constants as opposed to variables. I note an example that declares an object as a constant and sets two attributes. Then it changes one of the attributes. So, to me that is not a constant, to me a constant is something that maintains an immutable state once created. Obviously in JavaScript they keep using that word CONST – I do not think it means what they think it means.

Then comes a very good description of pass by reference as opposed to pass by value. A lot of people in ABAP world still cannot get their heads around this even after all these years.

I will give an ABAP example.

In an ABAP function module the default is pass by reference. That is if I pass in an IMPORTING parameter with one variable and want an EXPORTING parameter back then the values of the two variables are not passed to the function module but rather a reference to the memory areas of those two variables.

However, in a RFC call the remote system cannot possibly have access to those areas of memory inside SAP, thus all parameters have to be “pass by value” i.e. the values of the variables are sent/received as opposed to a link to the memory area of the variables.

In ABAP this is a lot cleaner in methods than functions calls, as RETURNING parameters must be “pass by value” and you cannot change importing parameters.

TYPE-TOE Through the Tulips

Once again it has often been noted than in JavaScript a variable does not have a TYPE and so becomes whatever value is passed into it, like a Zombie taking on the personality of the last person who’s brain it ate. So, in JavaScript there is a way of seeing what a variable is at any given point in time e.g., Boolean, or a number or whatever. I would stress this is not needed in ABAP – you can see the type of the variable in the debugger and the compiler does all sorts of checks based on the static type of the variable.

In any event one really interesting point is that the operator in JavaScript to determine the current type of the variable sometimes comes back with the wrong result, and the committee in charge of the JavaScript language developments says that bug cannot be fixed for reasons of backward compatibility. SAP has the exact same view on fixing bugs in ABAP. The FOR ALL ENTRIES bug where an empty table does a full table scan was always an obvious bug but SAP always said they could not fix that as that might break old code where the developer was relying on the bug to make the program work correctly.

That always sounded like a nonsense argument to me, and SAP had a once in a lifetime opportunity to address things like this with S/4HANA and ABAP in the Cloud where things did not have to be downward compatible anymore. But I bet nothing changed.

End of Part Four

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 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
3 Comments
Labels in this area