Logical operators in Javascript do not always return boolean result

Given a variable myObj in Javascript, evaluating myObj.child.name || ‘Kid’ will cause an error if myObj.child does not exist. In comparison, in PHP, $myObj->child->name ?? ‘Kid’ would just yield “Kid” with no errors. One way would be to write the expression let result = (myObj && myObj.child && myObj.child.name) ? myObj.child.name : ‘Kid’;. Another way …

When an object is assigned to a variable, will modifying the variable modify the original object?

This is similar to the previous blog post except this is more direct. The answer is “Yes” for all tested languages except C and C++. This may seem trivial until you meet a situation where the same original object is shared across requests/sessions, like a public static variable in Java or an incorrectly scoped Javascript …

Does modifying objects passed in to functions modify original object?

As per title, ran this across various languages. Results are mixed, but IMHO, it seems unsafe to design your functions/methods this way. It would be better to pass in the object, compute the changes without modifying the object, return the computed changes and assign it back to the original variable. Below are sample code snippets …

Get actual app ID and version from Android library without Context

References: How to get package name from anywhere? Get Android application package without Context Source code for android.app.ActivityThread This addresses the scenario where you are writing an Android library/SDK and it’s used in an app. How to get the actual app ID (package name) and version from inside the SDK? BuildConfig.APPLICATION_ID and BuildConfig.VERSION_NAME would return …

Simple Java logging class with inferred TAG

Been using a lot of Log.d(TAG, “message”); when working on Android projects. Problem with TAG is that it needs to be explicitly declared, e.g. protected String TAG = MyClass.class.getSimpleName(); instead of being inferred. Unlike PHP, Java has no magic constants such as __CLASS__ or __FUNCTION__ that can resolve automagically to the class/method being used at …

Evaluating Javascript using WKWebView in iOS

Sample of page loaded in web view: <html> <body> <script> function getWindowInfo() { return { width: window.innerWidth || document.body.clientWidth, height: window.innerHeight || document.body.clientHeight } }; </script> </body> </html> In a UIView or UIViewController: import WebKit // Enable Javascript in web view let config = WKWebViewConfiguration() let prefs = WKPreferences() prefs.javaScriptEnabled = true config.preferences = prefs …

Lessons from writing Swift framework that works in Objective-C app

As mentioned in my previous post on Fix for Google Analytics pod not working in Swift framework a few months ago, I was helping to refactor a client’s outsourced mobile SDKs. Last month, one of their clients had issues using the iOS SDK in their Objective-C app. Turns out that the Swift framework, i.e. the …

Resize view to fit aspect ratio of video in Android ExoPlayer

Been playing with ExoPlayer in Android and trying to resize a view to fit the aspect ratio of the video being played, i.e. fill the view with the video. References: Change resize mode for ExoPlayer Change aspect ratio of SurfaceView for ExoPlayer video 5-part series on building video player using ExoPlayer import android.view.ViewGroup.LayoutParams; import com.google.android.exoplayer2.Player.EventListener; …