Getting to know some pragmatic programming language features

Issue #270 As you know, in the Pragmatic Programmer, section Your Knowledge Portfolio, it is said that Learn at least one new language every year. Different languages solve the same problems in different ways. By learning several different approaches, you can help broaden your thinking and avoid getting stuck in a rut. Additionally, learning many languages is far easier now, thanks to the wealth of freely available software on the Internet...

May 23, 2019 路 8 min 路 Khoa Pham

Using camelCase for abbreviations

Issue #147 Each language and platform has its own coding style guide. This goes true when it comes to abbreviations. I鈥檝e had some debates about whether to use JSON or Json, URL or Url, HTTP or Http. I personally prefer camelCase, so I鈥檓 very happy to see that Kotlin is on my side. See Kotlin Style guide, I think this guide should be applied in other languages, such as Swift 馃槢...

January 26, 2018 路 2 min 路 Khoa Pham

How to use Function Literals with Receiver in Kotlin

Issue #139 From class HTML { fun body() { ... } } fun html(init: HTML.() -> Unit): HTML { val html = HTML() // create the receiver object html.init() // pass the receiver object to the lambda return html } html { // lambda with receiver begins here body() // calling a method on the receiver object } From @kotlin.internal.InlineOnly public inline fun <T> T.apply(block: T....

January 5, 2018 路 1 min 路 Khoa Pham

Learning from Open Source Making Deferred in Kotlin

Issue #135 From override fun adapt(call: Call<T>): Deferred<T> { val deferred = CompletableDeferred<T>() deferred.invokeOnCompletion { if (deferred.isCancelled) { call.cancel() } } call.enqueue(object : Callback<T> { override fun onFailure(call: Call<T>, t: Throwable) { deferred.completeExceptionally(t) } override fun onResponse(call: Call<T>, response: Response<T>) { if (response.isSuccessful) { deferred.complete(response.body()!!) } else { deferred.completeExceptionally(HttpException(response)) } } }) return deferred }

January 3, 2018 路 1 min 路 Khoa Pham

Understanding suspend function in Kotlin Coroutine in Android

Issue #123 Getting to know Coroutine From To continue the analogy, await() can be a suspending function (hence also callable from within an async {} block) that suspends a coroutine until some computation is done and returns its result: From We are using the delay() function that鈥檚 like Thread.sleep(), but better: it doesn鈥檛 block a thread, but only suspends the coroutine itself. The thread is returned to the pool while the coroutine is waiting, and when the waiting is done, the coroutine resumes on a free thread in the pool....

December 18, 2017 路 5 min 路 Khoa Pham

Understanding let, apply, with, run in Kotlin

Issue #114 Picture worths thousand words. Code worths thousand pictures. I don鈥檛 understand much until I take a look at Standard.kt in Kotlin standard library. /** * Calls the specified function [block] with `this` value as its receiver and returns `this` value. */ @kotlin.internal.InlineOnly public inline fun <T> T.apply(block: T.() -> Unit): T { contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) } block() return this } /** * Calls the specified function [block] with `this` value as its argument and returns its result....

December 5, 2017 路 1 min 路 Khoa Pham

Communication between Fragment and Activity

Issue #108 There鈥檚 always need for communication, right 馃槈 Suppose we have OnboardingActivity that has several OnboardingFragment. Each Fragment has a startButton telling that the onboarding flow has finished, and only the last Fragment shows this button. Here are several ways you can do that 1. EventBus 馃檮 Nearly all articles I found propose this, but I personally don鈥檛 like this idea because components are loosely coupled, every component and broadcast can listen to event from a singleton, which makes it very hard to reason when the project scales...

November 21, 2017 路 3 min 路 Khoa Pham