Skip to Content
Personal Insights
Author's profile photo Paul Hardy

JavaScript Book Review – Part 05

Tomorrow, I fly off to New Orleans for the ASUG TECHCONNECT conference. But before I go, here is a very long rambling incoherent blog about JavaScript, masquerading as a book review, though it becomes more and more obvious with every blog that is not what I am doing at all.

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 FIVE!

JavaScript%20Book

JavaScript Book

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

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

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

Excursus

The other day I went to an SAP Inside Track event in Sydney where Gregor Wolf talked about “SAP and ChatGPT” and Mike Doyle talked about how TypeScript can clean up your JavaScript, using a “New Sheriff in Town” Wild West analogy.

I had heard of TypeScript, but I had no idea at all what it was.

If you have been reading any of my previous blogs on JavaScript or know anything about JavaScript at all you will have picked up that unlike ABAP where you must TYPE variables as primitive types like strings or integers or data elements or TYPE REF TO something or whatever, JavaScript has no equivalent concept. You do not specify a type for variables or parameters or anything at all, everything is 100% dynamic.

If you pass a string into a variable, then that variable is now a string type. In the next line you pass an integer into that same variable, the variable is now an integer type. In the same way you can pass a string, or an object, or anything at all, into a parameter that really was intended to receive an integer.

This is not an accident; it is not like building a house and then forgetting the gaps for the windows and doors. This design was 100% intentional, I am sure that presumably there is some enormous benefit in having an untyped language, even if I cannot see what it is. I am probably blinkered by having spent so long programming in strongly typed languages.

Some people do not think that way (untyped is good) however. A lot of people say that strongly typed languages like ABAP are far better because – for example – you cannot pass a string into a parameter that is expecting an integer. In FORM routines in ABAP, you could have untyped parameters, but you got a warning. It was worse in function modules, you would get a dump if you passed in the wrong type of value, but the static syntax check did not warn you. In methods you must type your parameters. Typing your parameters avoids dumps at runtime which cannot be detected statically.

So, in UI5 world (and some modern aspects of SAP like the CAP (Cloud Application Programming Model) JavaScript is all over the place like my breakfast. Therefore, you need to use JavaScript but what if you consider the untyped nature of the language makes it so flaky and unstable that it is, to all intents and purposes, unusable?

This is where TypeScript comes in. You might consider it a “wrapper” over JavaScript. That is the language is 100% identical with the sole difference is that all the variables/constants/parameters must be typed. Plus, the file names end in TS as opposed to JS.

At runtime the TS file is compiled (transpiled is the exact word but it means the same thing to me) into JavaScript which is then compiled into whatever JavaScript is compiled into, so the running application thinks it is dealing with JavaScript as per usual.

I like this idea very much. It is like the OO concept of inheritance only in this case you are inheriting an entire language and then adding something extra. Presumably you could take this further and have further “subclass languages” which add extra things, maybe native support for “Design by Contract” or something, and then mix and match and choose what extra features you want. As it turns out even ABAP can be transpiled into JavaScript.

Another Excursus

Because I am bonkers, I am always doing ten things at once. That is not the most efficient way to do anything, but as I said, I am bonkers. I promised myself I would not start reading the “Clean UI5” book until I had finished the basic JavaScript book, but I went ahead and started reading it anyway. To be honest it is far more enjoyable. It is a bit on the chaotic side, but then so am I so any complaints in that area would be “Dear Kettle, Love Pot”.

It seems to be full of translation errors (German to English) but that does not matter in the grand scheme of things, and I report any I find to SAP Press.

One thing in the Clean UI5 book, right at the start, cleared up something that had been bothering me. That is, the keyword CONST. I had presumed this to be equivalent to the ABAP keyword CONSTANT.

It turns out that if you declare a variable as CONST in JavaScript then it supposed to be used as a constant, but it is not in fact a constant because the value can be changed. CONST means “constant assignment” which means the variable (and it is a variable) cannot be “reassigned” whatever that means. I am sure I will find out later.

OO Programming

JavaScript likes functions more than anything else, but it also allows OO programming. Guess what the first example is? The good old bookstore! Same as SAP always uses. As much as it pains me to say so, maybe people relate more to buying books than they do to monsters hiding under the bed and then coming out when you are asleep and eating you. I suppose the former happens more often than the latter. Either is better than using A / B / C to name examples and calling methods DO_SOMETHING in the example.

I may have this wrong but looking at the example code methods seem to be attributes of the object which I suppose makes sense given the nature of JavaScript where functions are the same as elementary variables in that they can be passed as parameters and so on.

In the film “The Greatest Showman” about P.T.Barnum there is a really good song called “This is Me”. In programming terms this is literally true – THIS in languages like JavaScript and Java is ME in ABAP.

OO in JavaScript is all based on “prototypes”. There is a design pattern of that name, I am not sure yet whether these two things are one and the same.

One thing I am sure of is that the IS_INSTANCE_OF keyword in ABAP came from JavaScript (or similar). This is where you have an interface or some such and you want to know what concrete class is currently inhabiting the reference variable. I imagine that is much more important in JavaScript where variables have no fixed type and can keep changing into whatever they feel like, like Odo on Deep Space Nine. In both cases this means due to the lack of strong typing every so often everything loses structure and dissolves and has to be stored in a bucket under the desk.

In ABAP the IS_INSTANCE_OF construct is a bit naughty as it is a violation of one of the SOLID principles which says the caller should not need to know or care about the concrete class of the object being processed, so long as it can do the job at hand.

It looks like you can make an object property immutable i.e. give it a value at construction which can then no longer be changed. Indeed, that is the default value, you have to say WRITEABLE = TURE to make them modifiable thereafter. You cannot do that in ABAP, a constant has a fixed value, defined in the class definition, not at runtime. Mind you, I am dubious how “constant” the constants in JavaScript are.

You know how in ABAP you say something like MONSTER_GANG->GREEN_MONSTER->TENTACLE to describe objects relating to other objects. In languages like JavaScript you say MonsterGang.GreenMonster.Tentacle. It is the same in CDS View (Entities) in ABAP. In both cases the -> or the dot between the objects is called a “member operator” because all methods and attributes of an object are known as huge “members”.

One thing that keeps coming up – in the normal JavaScript book and the Clean JavaScript book is that due to the lack of strong typing you have to use naming conventions. One example is to put underscores in front of object attributes just you can be sure they are not methods. Another example (not from either book) is that some years ago – and things may have changed now – all the example JavaScriopt code I saw in UI5 applications had Hungarian notation to say what type a variable should be e.g., the letter O at the start of what is supposed to be an object and so on.

One interesting thing is that if you have GET/SET methods for object attributes then I think (and this is a guess even after reading two descriptions of this from two books) every time you read/write to/from the variable in the code then the getter/setter is called implicitly.

Both books make much of the fact that until very recently you cannot have private attributes in JavaScript objects, and even now it is not as easy as one might like. That sort of ruins the whole OO thing in my mind, which is all about hiding things that the caller does not need to know or worry about.

Here is the next big difference from ABAP – at runtime you can add new attributes or methods to an object definition, or indeed remove existing ones. This is veering towards LISP where code is the same as a variable, that is the definition of the code can change as the program runs. That was supposed to make AI possible – that is the way you think can change over time, when faced with new data, so why not make computer programs work the same way? That was the idea, it does not seem to have worked too well until now, when it could be argued it is working far too well, with things like ChatGPT telling big fat lies because that is what humans do.

Because of this dynamic behaviour it is very difficult to do a syntax check – that is in one line you delete an attribute from an object, in the next line you try to read that attribute, there is no syntax error you just get an “undefined” value.

FOR goodness’ sake, I’ve got the Hippy Hippy Shake

As you probably know a lot of the “new” constructs in the ABAP language have been lifted right out of JavaScript. Hopefully they all do the same thing in ABAP as the equivalent keyword in JavaScript. That is good news for JavaScript programmers if they ever want to try their hand at ABAP but sometimes, I wonder if ABAP really needed all of the transplanted keyboards, or if was just done for the hell of it.

One such construct is the good old FOR/NEXT loop just like I was used to with the Sinclair ZX81 back in 1981. I really cannot see what advantages it has compared to looping over an internal table, if anything it tends to obscure the meaning of the code. I can see why you needs such things in JavaScript as other languages do not have internal tables. Internal tables are a killer feature of ABAP in my humble opinion.

I hit the jackpot the other day. In train stations you have a book exchange type of thing, and whilst waiting for the train to work I found a computer magazine from about 1981 which had a review of both the ZX81 and the VIC-20. The former got a thumbs up, the latter a thumbs down.

Moving back to internal tables, in other languages you have arrays. HIP HIP ARRAY! In ABAP we APPEND new records to the end of an internal table in JavaScript you PUSH( ) them to the end. You know how in ABAP when you execute certain commands the SY-SUBRC (or possibly other SYST variables) are set to store the result e.g., if I do a BINARY SEARCH on an unsorted table, I will probably get Sy-SUBRC = 8 to tell me the search failed. In JavaScript a lot of methods have RETURN parameters which of course you do not have to use but perform a similar function to the SY-SUBRC just in a more obvious manner. For example the PUSH() has a RETURN value which is the new length of the array after the new records have been added. In both languages it is an example of a command doing two things at once in this case adding a record and then returning some related information.

I don’t find UNSHIFT() a very intuitive keyword – it means add records at the start of an array. I will say the examples in this book are very good because they are not generic – instead of manipulating values like A, B and C, the array example is a to-do list with tasks like mowing the lawn, which makes things a billion times easier to understand.

On the Good Ship Lolly POP()

Have you ever tried to debug ME21N? It is agony because most of the code are calls to PUSH() and POP() methods with no code that seems to do anything. That seemed to be to be trying to replicate how things work in other languages just for the sake of it. SAPMV45A is much easier to debug, even if the technology is archaic. In the end, a program needs to be easy to debug and that simulated PUSH/POP thing in ABAP just does not cut the mustard.

I notice there are methods in JavaScript to get a list of all the attributes, to get a list of all the values, or both at once. This is all nonsense to ABAP people – the names of the attributes are in the definition, and you get the values by using the names of the attributes. I am sure you can do the latter in JavaScript as well. I cannot really see a use case for getting a table of attribute names, maybe it is because the language is weakly typed, and this is the equivalent of the ABAP RTTI.

Hurry up ARRAY

I had written “Oh Dear!” next to a box which said that if you call an ARRAY() constructor with one argument then that becomes the length of the constructor, but if you call it with two arguments, than the length becomes two and the contents are the two arguments. I am starting to see why the clean code people do not like optional parameters in methods.

There are a whole bunch of methods that can be used to manipulate and read arrays, far, far, more than the internal table equivalents in ABAP. Some of these have made their way into the ABAP language in recent years like REDUCE or to be able to loop through a table backwards or just read every fifth record of an internal table (never found a use case for that one either).

Out of SORTS

One aspect of JavaScript I keep coming back to is passing functions in as a parameter. This is a very difficult concept for an ABAP person to get their head around, but a wonderful example is given using the SORT function. You can optionally define another function whereby you sort the values using whatever logic you require. Then you pass that function into the SORT function and the default logic is over-ridden.

It is claimed no other language can do anything remotely similar, but I suppose the nearest equivalent in ABAP is when you define a custom ASSERTION via an interface and then during a unit test and pass that in.to CL_ABAP_UNIT_ASSERT=>ASSERT_THAT as a “custom constraint”.

STACK Overflow

The concept of a “stack” is very simple – an array where the last value in is the first value out, but as I have said when I have seen this implemented in ABAP (as in ME21N) it is somewhat horrific. You just do not need it in ABAP so jumping through hoops trying to replicate this and therefore making the code impossible to understand/debug seems a not very value-adding task.

In the JS book there was a generic example with A, B, C, E etc. as the values and then a practical example where you have a series of actions, which if the transaction is cancelled have to be reversed in order. Guess which example I liked best?

Here is something that will make an ABAP persons head spin. The reverse of a stack is a QUEUE (First in, first out). You add an element using the keyword ENQUEUE() and remove an element using DEQUEUE(). Apparently, this is normal in all non-ABAP languages, but as we know in ABAP those terms have a very different meaning.

There is an INCLUDES() function which is rather like the LINE_EXISTS in ABAP. Again, maybe the latter was copied from the former. This being JavaScript there is also a FIND() function which takes in another function as an argument and thus can search the array using all sorts of complicated logic e.g. return the first value in the array which is a Green Monster with six legs and rabies. That was not the actual example used.

I am wondering if any of these array types have the equivalent of the ABAP SORTED or HASHED concept to improve performance. They might have, but it is not mentioned.

MAP the Knife

Now we move onto a sort of array which is a KEY/VA:LUE pair and is called a MAP in other languages. There are lots of ABAP structures and table types like this, one is used in Web Dynpro quite a lot by Fred Flintstone. He loves Web Dynpro. In SAP configuration tables often do this sort of thing, as do “constant interfaces” so you have a meaningful name like BANANA against a value like 5.

Wrong Plaice makes things Fishy

There was a huge section on the destructing of arrays and especially objects, and the more I read the more confused I got about why you would want to do such a thing, that is, create a bunch of variables to mirror the object attributes. Right at the end of this huge section was a box saying do not do any of the above, it is far better just to access the object attribute. That is correct, but I think the warning should be at the start of the section as in – I am going to explain how to do something but DO NOT DO IT. That is what I do in my book regarding TEST_SEAMS in ABAP which are an abomination. The warning should not be in a box as often people do not read boxes, and it should be at the start because people might stop reading 90% of the way through, move onto the next chapter and not realise they had just read a giant explanation of what NOT to do.

Then we have a suggestion to make the code more compact by shortening the variables down to one letter e.g. “l” instead of “lastName” by which I wrote “No, just NO!”. I am not a huge fan of one letter variables name, no matter what the context.

The book moves on to string handling and in JavaScript you use SLICE( ) to get bits of strings. So monsterNameSLICE(0, 3) is like MONSTER_NAME+0(3).

I made the comment that in both ABAP and JavaScript the code to do this is a bit obscure, and the clearest syntax to do this can be found in Excel.

Then you have two almost identical named functions SUBSTRING and SUBSTR which sometimes behave the same as each other and sometimes behave differently depending on what you pass in. That must confuse newcomers to the language.

I will say that “toLowerCase()” is a better name than “TO_LOWER”. People have a go at ABAP for being too verbose but in my mind the whole point of fourth generation languages is to read as much like English as possible as opposed to machine code, and if you suddenly start truncating things or move from ADD 1 TO COUNT to COUNT =+1 then that is a huge backwards step, and it always reminds me of “Newspeak” in the novel “1984”.

No SETS please, we are British

A SET is liked a hashed table in ABAP – that is, the array entries have a unique key and there cannot be duplicates.

Now, the funny thing is that in JavaScript – and in ABAP – you do not get any sort of runtime error if you try and pass in duplicate values. I would have expected a short dump in ABAP like what you get when doing an SQL statement and trying to read duplicate entries into a HASHED table, or the JavaScript equivalent, but in both cases when you programmatically try to insert a duplicate entry into the table/array nothing happens, and you get no notification the operation failed.

We all live in a Yellow SY-SUBRC

All us ABAP types know that when it comes to SY-SUBRC the value of zero means TRUE (success) and the value of 4 means failure (FALSE). In fact, usually any value other than zero means FALSE. All well and good, but that is not very intuitive, especially as in many languages 0 means FALSE and 1 means TRUE, and every language apart from ABAP has a true Boolean data type. Why it is so difficult to add such a thing to ABAP if every other language has managed it? SAP would argue the 54 million different data elements with BOOL in their name can do the job, but it is not the same.

In JavaScript if you say DELETE( ‘GREEN MONSTER’ ) from an array and that record exists and is deleted, the result is TRUE. If it not deleted, then the result is FALSE. I like that. Not a lot, but I like it, as Paul Daniels would have said to the lovely Debbie.

As a final point in the book at the end of the huge section of arrays there is an example where six constants are declared, each one the name of a singer or group, and then the first five are added to the array (WEAK SET) and not the sixth value, and then when you try to see if the sixth value is in the array it is not there, and of course it is not there, because it was not added. I struggle to see what this example is trying to tell me.

Measles, Scarlet Fever, and MONTHS

I may have said I find the USA date system crazy i.e., MM/DD/YYYY which is a random series of values as opposed to ascending DD/MM/YYYY or descending YYYY/MM/DD. But at least in the USA system January has the value 1, and December has the value 12, which is what I hope anyone would reasonably expect.

Let us say it is the first of January. In JavaScript if I call GETDATE then back comes the answer 1 which is what I would expect, as it is the first of the month. But if I call GETMONTH the result is 0, as January has the value of zero and December has the value of 11.

Two things about that

  1. Even if that is how lists are stored in arrays, that is, first value has the index 0 rather than 1, that is not how humans think about the months of the year.
  2. But even if you are going to do that, why not be consistent and do it for the dates as well, that is have the first day of the month as zero, if the first month of the year is zero?

With minutes and seconds that makes sense, as a human would never say the time is 14:60:60, they would say it is 15:00:00 even if the two values represent the exact same point in time. So GETMINUTES and GETSECONDS start at 0 and end at 59 that is fine by me.

Naturally when creating a date object in JavaScript the USA pattern is expected as a string – that is “02 08 2021” for the 8th of February.

I now have a hundred pages of the “Clean UI5” book I have made notes on, but it is probably best not to mention any of that here, as that would muddy the waters.

End of Part Five

I am trying to stop myself writing blogs with over 3000 words, I did not do that so well here, anyway in due course I will write another JavaScript blog. However, I now have to write another “ABAP to the Future” version, so the pause will be over a year.

New Orleans, here I come!

Paddleboat%20Steamer

Paddleboat Steamer

Cheersy Cheers

Paul

Assigned Tags

      4 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Matthew Billingham
      Matthew Billingham

      Just one thing. You've not answered the question: Most ABAP programmers are... scared of being run over by a huge ship. Can an SAP Press book on JavaScript help address this fear?

      Well can it?!

      The benefit of untyped languages are that they're generally easier to learn, and are more suited to rapid development and short development cycles. Benefits of typed languages are that they are usually far more performant (to steal a German word) and robust, since typing errors are typically picked up statically - before runtime.

      I'm not sure describing Java as 100% dynamic concerning typing is correct. ABAP has dynamic typing of course.

      I agree that Internal Tables are ABAP's killer feature.

      One aspect of JavaScript I keep coming back to is passing functions in as a parameter. This is a very difficult concept for an ABAP person to get their head around, but a wonderful example is given using the SORT function. You can optionally define another function whereby you sort the values using whatever logic you require. Then you pass that function into the SORT function and the default logic is over-ridden.

      It is claimed no other language can do anything remotely similar,

      Hmm. Java lambda expressions do this, I'm fairly sure. It's the essence of functional programming. Although I'm sure I'll be correct if I'm wrong.

      Doubleplus good blog. I'm still convinced that JavaScript is doubleplus ungood, but could concede that TypeScript (which I thought was related to PostScript...) might not be ungood.

      Oh - a couple of typos:

      It looks like you can make an object property immutable i.e. give it a value at construction which can then no longer be changed. Indeed, that is the default value, you have to say WRITEABLE = TURE

      Now we move onto a sort of array which is a KEY/VA:LUE pair

       

       

      Author's profile photo Paul Hardy
      Paul Hardy
      Blog Post Author

      Going back to the most important bit, which is how to avoid huge ships.

      There are about a million reviews of that book.

      My favourite was a review which said "As a U-Boat Commander in the South Atlantic I have doubled my kill rate by simply doing the exact opposite of what is recommended in this book"

      I also felt sorry for the various people living hundreds of miles from the sea who got run over by a huge ship whilst walking to the bookshop to buy the book on how to avoid them.

      This past week in New Orleans I did a two hour hands-on session about "Build Code" (everything is called Build now) which is all about JavaScript, and the presenters/moderators did not mention about how to avoid huge ships EVEN ONCE.

      Author's profile photo Matthew Billingham
      Matthew Billingham

      I like this gem.

       

      This book could save your life!
      Reviewed in the United States on May 10, 2013
      Verified Purchase
      I'm very much scared of ships. I live over a hundred miles inland and at 6,000 feet elevation but one can never be too careful!

      Author's profile photo Uwe Fetzer
      Uwe Fetzer

      "How to avoid Huge Ships"

      Confirmed. Best book ever.