Elvis Operator (aka Safe Navigation) in JavaScript and TypeScript

Posted Leave a commentPosted in Concepts of programming languages, Javascript, TypeScript

One of the first feature requests of TypeScript was the Elvis operator. More precisely, the null-safe navigation operator. That’s a variant of the “dot” operator allowing you to access attributes and methods even if the object preceding the dot is null or undefined. In most languages implementing the Elvis operator, the expression null.firstName simply returns null, relieving the developers of guarding the attribute accesses with countless if statements.

This article shows several approaches to deal with null-safe navigation in JavaScript and TypeScript.

Angular Components with Non-Standard Selectors

Posted Leave a commentPosted in Angular

When I teach Angular, I keep telling my students that Angular components are custom HTML elements, while Angular directives are custom attributes. Every tutorial I know teaches the same. Nonetheless, Angular is much more flexible. In reality, the selector of both components and directives can be any CSS selector. For instance, you are allowed to define a component like so:

  selector: '[framed-image]',
  templateUrl: './framed-image.component.html'
export class FramedImageComponent { ... }

This component is used like so:

<div framed-image="art-deco" src="somePainting.jpg"></div>

What to make of this? What’s the difference between such a component and a directive?

What Does the Keyword “new” do in JavaScript?

Posted Leave a commentPosted in Concepts of programming languages, Javascript

As an Angular teacher, I often say that JavaScript objects are functions. Most of the time, I get away with this bold claim. It’s not entirely wrong. But it leaves many things in the dark. For one, my short-hand explanation makes it hard to understand why Json objects are objects, too.

By the way, a few hours after publishing this article I remembered that every function is a first-class object in JavaScript. The only property distinguishing it from other objects is that a function can be called. However, this article investigates the other direction. Can every object be defined by a function? Are functions better compared to classes? Or are they something else?


Bang, Bang, You’re a Boolean!

Posted Leave a commentPosted in Concepts of programming languages, Javascript

Did you ever wonder why there are such strange operators like !!, === and !== in JavaScript?

Developers coming from other languages, such as Java, often have a hard time to accept they have to write a triple equals character instead of a double one. Actually, the first language I learned was Basic, followed by Pascal, so I still consider the double == weird. But that’s just one of the peculiarities you have to accept if you’re learning a language following the C tradition.

But of course, all these things have a reason. Hitting SHIFT+0 thrice, hundreds of times a day clearly gets on my nerves. It feels like stuttering. But the reason it had to be introduced was the attempt to save a few keystrokes.

Guiding NPM Through a Firewall

Posted Leave a commentPosted in Javascript, npm

npm doesn’t like to be fenced in by a corporate firewall. Too bad most npm installations live precisely there. The other day, I ran an npm install on the customer’s PC. The same command that finishes in 40 seconds on my machine took roughly a quarter of an hour. Plus several hours of troubleshooting. It was a Deja-Vue experience to me, having lived this nightmare several times before, so it’s high time to collect a short list of hints. BTW, if you want to contribute to this list of life-savers, please do so. Just leave a comment, thus giving me an opportunity to include it to the list.

Basic proxy settings

These two settings almost always do the trick:

npm config set https-proxy http://username:passwort@proxy.mycompany.com:8080
npm config set proxy http://username:passwort@proxy.mycompany.com:8080


What About the Performance of Java 8 Lambdas?

Posted Leave a commentPosted in Concepts of programming languages, functional programming, Java 8, java 9, JIT, Performance, Uncategorized

Isn’t it funny that I start writing a series of articles about Java 8 Lambdas in 2018? Mind you: my first article dates back to April 2012, six years ago. I fell in love with functional programming even earlier. I suppose it was roughly in 2008, ten years ago. From this perspective, functional programming is nothing new to me. I’m using it on a daily basis when I’m writing TypeScript code.

As things go, I work in a very conservative environment, so I know a lot of projects that have adopted Java 8 last year. And I’m bewildered what programmers make of it – and why. It’s high time for a reality check. Java’s Lambdas and method handles are great, no doubt about that. Streams are great, too. Sometimes.

More often than you might think, they are not.

Using Java 8 Lambdas Efficiently

Posted Leave a commentPosted in Concepts of programming languages, functional programming, Java 8

Functional programming is en vogue. There are many good reasons to adopt functional programming. Recently, I watch more and more Java programmers using the new programming style. As things go, they experiment and play with their new toy, pushing it to the limits. This article briefly shows why Lambdas are useful, what Java programmers make of it, and concludes with a few recommendations to do it right.

Escape Analysis in Java

Posted Leave a commentPosted in Concepts of programming languages, JIT, JVM, Optimization

Spectre and Meltdown came as a shock. They showed that low-level CPU optimizations have an impact on our lives. Plus, they proved an illusion of most programmers wrong. Yes, you’re using a high-level language. But that doesn’t mean all those abstraction layers shield you completely from the CPU. I’ve demonstrated the effect of the CPU cache on a Java program some time ago.

Spectre and Meltdown took this to another level. If my sources are right (I still can hardly believe it!), it’s possible to observe the effect of speculative execution in JavaScript. Speculative execution is a very low-level CPU optimization. JavaScript is a very high-level language – it doesn’t even use bytecode, and the design of JavaScript makes compiling it a challenge. But that’s another day’s story.

The link between my click-bait introduction and the topic of the day is the RAM of your PC. CPUs have evolved much faster than main memory. Nowadays, CPUs spend most of their time waiting. A modern CPU can perform several instructions per CPU cycle. But if it has to access main memory, bypassing each of the three caches, it has to wait for roughly a hundred cycles. See this article on Extremetech.com for more details.

Short Introduction to Vue.js

Posted 1 CommentPosted in Javascript, Vue.js, web design, web framework

They announced Vue.js to me saying “It’s like Angular, only they did it right this time”. I don’t think there’s anything wrong with Angular, so my curiosity was piqued, and the stakes were high. What does Vue.js better than Angular? So I spend two afternoons playing around with Vue.js.

First impressions

Before starting to dig into the source code, let me share my first impressions with you. Vue.js is fast, it’s simple, and it’s easy to find info on the internet. I also felt that the error messages could be better, but then, that’s a typical beginner’s problem. In the early days, the error messages of Angular weren’t helpful at all. Vue.js is younger than Angular, so I guess they’ve done a good job here, too.

There were two pleasant surprises I didn’t expect. First, there’s a small but useful ecosystem. There’s tool support, there’s a small CLI to get started, and there’s even a browser plugin to help you debugging.

Second, there’s decent TypeScript support. At first glance, the source code seems to benefit a lot from the syntactical sugar of TypeScript. I always thought Vue.js was meant to be the successor of AngularJS, founded and supported by developers who didn’t accept the design choice of Angular 2. And one of the key choices was to embrace TypeScript. So TypeScript support comes as a surprise.

Scope of Vue.js

Before anybody starts a useless flame-war again, let’s have a look at the scope of Vue.js. It covers a rather limited scope. It supports the “V” of MVC. Everything else is up to you. Most people opt for third-party libraries. Some of these libraries (Vuex, the router, the CLI, and –
until recently – vue-resources) are maintained by the Vue.js team, so the scope of Vue.js is slightly larger than the scope of React.js, but even so, these libraries are optional.


Migrating to Bootstrap 4

Posted 2 CommentsPosted in responsive design, web design

The first application I migrated to Bootstrap 4 recently worked out of the box. Add the new CSS and JavaScript, find a replacement for the Glyphicons and you’re done. Awesome! Only… well, it took me a couple of days to realize that my application was broken. There’s a great migration guide for Bootstrap 4, and for a good reason. It’s a good idea to read it.

By the way, if you’re already familiar with the migration guide from an alpha version of Bootstrap 4, read it again. The Bootstrap team has made a couple of last-minute changes.

Material Design for Bootstrap

Posted 2 CommentsPosted in responsive design, web design

After a long search, I’ve finally found two good libraries bringing the design language of Material Design to Bootstrap. Actually, I’ve seen similar attempts before. Just think of the Paper theme bundled with BootsFaces. It’s not bad, but I wasn’t too excited when I saw it. I didn’t run a comprehensive test yet, but at first glance, the libraries I present today match my idea of Material Design better. Please take that with a grain of salt: beauty lies in the eye of the beholder, so that’s just my personal opinion.

Customizing Bootstrap 4 with Angular

Posted Leave a commentPosted in Angular, web design

Gone are the days of customizing Bootstrap with the sweat of your brow. At least if you’re using a current version of Angular and Bootstrap 4. Mind you: Bootstrap 4 is built with SASS. Angular supports SASS as a first-class citizen. So it’s hardly surprising that it’s a child’s game to customize Bootstrap with an Angular application.

Four easy steps

Basically, you have to follow these four steps:

  • Add Bootstrap to your package.json. The easiest way to do this is running the command npm i bootstrap@next.
    A short note on the “@next” bit: At the time of writing, Bootstrap 4 is still beta. Chances are you’re reading this article a lot later, so maybe the final version of Bootstrap 4 has already been released. If so, install Bootstrap via npm install bootstrap (omitting the “@next” bit).
  • Tell the Angular CLI to use SCSS by default. To do so, rename the styles.css to styles.scss and set the default extension in the .angular-cli.json to SCSS. There’s even an Angular CLI command for that: ng set defaults.styleExt scss.
  • Create an empty file customize-bootstrap.scss the in src folder, next to the styles.scss file. Later you’ll add your customizations in this file.
  • Add these two lines to the styles.scss file:
    @import 'customize-bootstrap.scss';
    @import '../node_modules/bootstrap/scss/bootstrap';

That’s it! Now you can customize Bootstrap by editing the customize-bootstrap.scss file.

Angular Animations API

Posted Leave a commentPosted in Uncategorized

The Angular Animations API adds a shallow abstraction layer to the animations offered by CSS. It’s a bit like so many other Angular libraries: it adds enough syntactic sugar to make it useful, but it adds little value to the underlying technology.

At least, that was my first impression. During my research for this article, I’ve learned that such a shallow abstraction layer make a lot of sense. For instance, the same API could be used with AngularNative. As far as I know (correct me if I’m wrong!), that’s not the case today, but who knows what the future has in store for us.

Example animation

’nuff said. Let’s get our feet wet with real source code. Have you ever seen at the back-side of your application? This demo doesn’t work with Internet Explorer because I wanted to keep the source code of the demo simple, so please use another browser to see the animation:


Lazy Loading in a Angular Carousel

Posted 1 CommentPosted in Angular

Today’s tech tip is almost too primitive to mention. However, on second glance, it shows how to use Angular efficiently. I reckon there’s a general pattern there, even if I haven’t been able to distill it yet to make it a textbook lesson.

Setting the stage

Consider a carousel, such as the first demo at our BootsFaces showcase. It displays three images. At every point in time, only one of the three images is shown, unless you click to show the next image. In that case, the next image starts to slide into view, replacing the old image.

Things get ugly if we’ve got hundreds of images. They also get ugly if the carousel is used to implement a wizard. Chances are that the content of the second page of the wizard depends on the input provided on the first page of the wizard. In other words, the second page can’t be rendered at load time. As you can imagine, our use case was a combination of these two options.

Is React.js Just a Library?

Posted 1 CommentPosted in Angular, React

The other day, I was asked to compare frameworks like Angular and React.js. It was an interview, so I didn’t notice the tiny slip. Developers reading the interview didn’t hesitate to point out that React is a library, not a framework. A few months earlier other developers reminded me you can’t compare Angular with React because the former is a framework, while the latter is not.

The reactions of the community where so emotional that I gather many people consider it an important difference. That, in turn, means it’s fun to play devil’s advocate. It’s the kind of games generating new insights.


Java Classpath Tales – or: The Mystery of the Empty Beans.xml

Posted 5 CommentsPosted in Concepts of programming languages

BeyondJava on the empty beans.xml and how to retrieve files from the class path. Image source: https://pixabay.com/de/kaffee-tasse-kaffebohnen-171653/  published under a CC0 license
Image source: Pixabay
The age of a programming language shows in the clever tricks programmers use. One such trick is used by CDI. Did you ever wonder why you have to put an empty beans.xml into the META-INF directory?

By the way, you may not even aware that the beans.xml is allowed to be empty. Many developers aren’t. You can use the beans.xml to configure a couple of things, leading developers to believe they have to do that. Plus, the IDEs tend to complain about an empty XML file because they consider it ill-formed. As far as I know, Netbeans generates a non-empty beans.xml by default. But still, even if you haven’t seen it yet, it’s allowed to put an empty beans.xml into the META-INF folder of your jar (or the WEB-INF folder of your *.war file). Empty meaning really empty: it’s a file of zero bytes length.

What good is such a weird file?

We’ll answer this question in a minute. Plus, this article shows you how to read arbitrary files hidden in a jar file, or – more generally speaking – somewhere in the classpath. By the way, this is one of the few situations when the Junit test succeeds but the real application fails. That is interesting enough in itself.


BeyondJava @Conference: Angular vs. React – When to Choose Which?

Posted Leave a commentPosted in Angular, architecture, React, UI survey, web design

A couple of days ago, Marius Hofmeister and I held a talk about “Angular vs. React” at the International JavaScript conference iJS at Munich. Which one is better, Angular or React?

Truth to tell, we can’t answer the question. Actually, it’s the wrong question, anyway. The real question is “Which one is better for my project?”

It depends!

Because that’s what we found out during our investigation: During the last couple of years, Angular and React have learned a lot from each other. Each team is watching the progress of the other team. Good ideas tend to be adopted by the other team after a while, provided they match the general philosophy of the framework.

So we came to believe you can do anything with either framework. We’ve collected a couple of technical criteria helping you choose the right tool for the job. But you should base your decision on technical considerations alone.