Digging into Coroutines

With Kotlin comes one of its most famous features: coroutines.

Javanese is my first language. No, not the Java programming language. Javanese. So whenever I hear a foreign word like coroutines, nothing shows up in my mind. It has no meaning. This post is an attempt to dig into the word to give my brain some understanding of it.

Coroutines, Threads, Processes

First, here’s what the official documentation says about it (emphasis mine):

One can think of a coroutine as a light-weight thread. Like threads, coroutines can run in parallel, wait for each other and communicate. The biggest difference is that coroutines are very cheap, almost free: we can create thousands of them, and pay very little in terms of performance. True threads, on the other hand, are expensive to start and keep around. A thousand threads can be a serious challenge for a modern machine.

“Your first coroutine with Kotlin”

I know that coroutines is a way to do asynchronous programming, and from the information above, it seems to have these three major points:

  1. Analogous to a light-weight thread,
  2. Multiple coroutines can run together at the same time, they can wait for each other, and also communicate to each other,
  3. Having a lot of them does not hurt performance.

I’m currently using Kotlin for programming Android apps, so the next step seems to be to figure out what exactly a thread is in this context. Here’s what the documentation says (emphasis mine):

When an application component starts and the application does not have any other components running, the Android system starts a new Linux process for the application with a single thread of execution. By default, all components of the same application run in the same process and thread (called the “main” thread). If an application component starts and there already exists a process for that application (because another component from the application exists), then the component is started within that process and uses the same thread of execution.

Android Developers — Processes and Threads

So from here, it seems like within Linux there is a concept of “process”, and also “thread of execution”. According to Wikipedia:

In computing, a process is the instance of a computer program that is being executed by one or many threads. It contains the program code and its activity. Depending on the operating system (OS), a process may be made up of multiple threads of execution that execute instructions concurrently

Wikipedia — Process (computing)

This helps understanding what a “process” is. One of the definition from Merriam-Webster is: “something going on”. So in this context, a process is the idea of an app that’s currently running within Android. That’s simple enough.

Also, it just blew my mind right now that this is why a processor is called that —again, English is not my first language—it’s basically the thing that runs the apps.

Let’s have a few seconds of visual break:

Photo by Tran Mau Tri Tam on Unsplash

Wikipedia has this to say about thread of executions:

In computer science, a thread of execution is the smallest sequence of programmed instructions that can be managed independently by a scheduler, which is typically a part of the operating system.[1] The implementation of threads and processes differs between operating systems, but in most cases a thread is a component of a process. Multiple threads can exist within one process, executing concurrently and sharing resources such as memory, while different processes do not share these resources.

Wikipedia — Thread (computing)

Thread, according to Merriam-Webster:

3 : something continuous or drawn out: such as

a : a line of reasoning or train of thought that connects the parts in a sequence (as of ideas or events)

So from the two explanations above, a process seems to be the whole existence of a running app, while a thread is the smaller element within the process.

For example in Instagram, a thread might be the start from finish when someone uploads a new picture (tap plus icon, find the image, add filter, add description, click upload, wait for it to be sent, have the image displayed). I don’t know if that’s technically accurate, but probably a good approximation.

The Wikipedia about thread also has this useful section about the difference between processes and threads:

Threads differ from traditional multitasking operating-system processes in several ways:

  • processes are typically independent, while threads exist as subsets of a process
  • processes carry considerably more state information than threads, whereas multiple threads within a process share process state as well as memory and other resources
  • processes have separate address spaces, whereas threads share their address space
  • processes interact only through system-provided inter-process communication mechanisms
  • context switching between threads in the same process typically occurs faster than context switching between processes

It also has this useful image:

A process with two threads of execution, running on one processor

So far we’ve learned about what a process and a thread is. And, specifically within Android, we have learned that a running app is given one process and one main thread.

So a coroutine is like a thread, and has similar behaviors, except that it’s light-weight and can perform better.

That’s a good start. Now let’s see if we can figure out what it means with multiple coroutines running in parallel, communicating and coordinating with each other.

Photo by Gaelle Marcel on Unsplash

Here is another explanation of what coroutine is:

coroutine A program component that allows structuring of a program in an unusual way. A coroutine resembles a subroutine, with one important difference. A subroutine has a subordinate position relative to the main routine: it is called and then returns. Coroutines, however, have a symmetric relation: each can call the other. Thus a coroutine is resumed at a point immediately following its call of another coroutine; it never returns, but terminates its operation by calling (resuming) another coroutine. — coroutine

What even is a subroutine?

In computer programming, a subroutine is a sequence of program instructions that performs a specific task, packaged as a unit. This unit can then be used in programs wherever that particular task should be performed.

Subroutines may be defined within programs, or separately in libraries that can be used by many programs. In different programming languages, a subroutine may be called a procedure, a function, a routine, a method, or a subprogram. The generic term callable unit is sometimes used.

Wikipedia — Subroutine
Photo by Mousssss Liu on Unsplash

So subroutine is basically a function. I know functions! I don’t think it’s practically possible to learn programming without learning about functions anyway.


“co-” prefix, from Wiktionary:

  1. together; mutually; jointly
  2. partner or subordinate in an activity
  3. to the same degree

So linguistically, perhaps coroutines mean functions that can run and work together as an equal.

I’m happy with that conclusion. It’s something I can picture in my mind. It does not seem to explain technically all coroutines can do in the context of Kotlin programming, in fact it sounds a little too bland to me. However, it is concise and memorable. And that perhaps is all a good name needs to do.


Simple Way to Add a Global Constants File with Kotlin

One of the most common things I’m adding when making an Android app is a file that contains various constant values to be used in various places. Here’s how I’d set it up.

First, create the .kt file (usually I’d name it Constants.kt). Add the package name at the top, then add the constant variables without creating any class for them. In this example I assume that the file is on the root folder of the app. For example:

package studio.oldblack.wpflightcontrol
const val WPFC_SHARED_PREFS_FILENAME = "wpfc_prefs"
view raw constants.kt hosted with ❤ by GitHub
Gist containing the Constants.kt file example

Finally, to use the constants themselves, we can just write the name without any fuss (on line 2 on the example below):

val sharedPreferences = EncryptedSharedPreferences.create(
Example of how to use a constant variable


Unboxing The Fujinon XF90mm F2 Lens

I got a new lens recently, the FUJINON XF90mmF2 R LM WR, for Fujifilm X-mount cameras. It has been quite an experience working with it as the focal range is not something I’ve used before. So far I find it to be really great for making portraits and somewhat-macro pictures (as it’s not a real macro lens, but it can sort of do the job from about two feet away).

I might write a longer review later on, or at least post some example pictures. But in the meanwhile, here’s an unboxing video I made:


What Exactly is a “Repository”?

The Repository pattern is one of the concepts frequently used and mentioned within Android Architecture Components. As English is not my first language, I’m drawing a blank in my head whenever I read or hear the term “repository” itself. I have some abstract idea about what it is, mostly from the concept of Github repositories: it seems to be a central place for something.

In my experience, not understanding an English term is a common barrier to having a better intuition of a programming concept. I would read about something new and would have a hard time connecting a term that drew a blank in my mind, to the larger understanding of the concept. So today I figured I’ll geek out a little bit and see what the “repository” word really means.

According to Merriam-Webster, the word means:

: a place, room, or container where something is deposited or stored

This definition matches my understanding of a Git repository: it’s a place where the code is stored.

What about when the term is used in Android Architecture Components? From this Codelab:

A repository class abstracts access to multiple data sources. The repository is not part of the Architecture Components libraries, but is a suggested best practice for code separation and architecture. A Repository class provides a clean API for data access to the rest of the application.


A Repository manages queries and allows you to use multiple backends. In the most common example, the Repository implements the logic for deciding whether to fetch data from a network or use results cached in a local database.

Uh, what? That sounds like a lot of things it’s doing other than just being a storage.

The Codelab also provides this graphic:

Repository concept in Android Architecture Components
Repository concept in Android Architecture Components

I found a clearer explanation from an article about the Repository pattern:

[The repository] will persist your objects sans the need of having to know how those objects would be actually persisted in the underlying database, i.e., without having to be bothered about how the data persistence happens underneath. The knowledge of this persistence, i.e., the persistence logic, is encapsulated inside the Repository.

In essence, the Repository design pattern facilitates de-coupling of the business logic and the data access layers in your application with the former not having to have any knowledge on how data persistence would actually take place.

In using the Repository design pattern, you can hide the details of how the data is eventually stored or retrieved to and from the data store. This data store can be a database, an xml file, etc. You can apply this design pattern to even hide how data that is exposed by a web service or an ORM is accessed. Martin Fowler states: “Mediates between the domain and data mapping layers using a collection-like interface for accessing domain objects.”

So from the above, it sounds like the Repository pattern is more of a way to allow access to various data sources, without the accessor having to know about how the data sources are created. It’s not just a dumb place to store things, instead it’s a smart storage that allows someone to add or remove things as needed.

That mental model still makes sense for a Git repository as well: I add and remove code to my repository without really knowing how Github stores it: is it in a database? If yes, what database? I don’t need to know.

Picture of a library

From now on I figure I’d imagine the Repository pattern as a sort of librarian who I can talk to to help me find a book: I mention a title and they’d go ahead and fetch it for me. From my perspective it’s not important where the book is stored within the library (shelves? Fridge? Vault?), as long as I can borrow and return it properly. After all, a library is indeed a repository of books, yeah?

Bahasa Indonesia Programming

Dagger 2 untuk Dummies Menggunakan Kotlin (dalam 20 baris kode)


Artikel kali ini diterjemahkan dari artikel asli dalam bahasa Inggris, “Dagger 2 for Dummies in Kotlin (with 20 lines of code)” oleh Elye, link di bawah:


Below is my Indonesian translation from the original article “Dagger 2 for Dummies in Kotlin (with 20 lines of code)” by Elye, link below:

Saya seorang dummy, dan belajar Dagger 2 membikin otak saya melar!! Semua tutorial yang saya baca membahas terlalu banyak hal, seperti Dependency Injection, Provider, Component, Module, Scope… membuat otak meledak!

Selain itu, kebanyakan tutorial memakai Java. Kotlin adalah pilihan yang lebih baik saat ini. Saya perlu berbulan-bulan untuk memahami Dagger 2 di Kotlin. Jadi saya berjanji pada diri sendiri bahwa nanti begitu saya paham, saya harus membuat tutorial yang super sederhana, sesimpel ABC, untuk orang-orang seperti saya yang mungkin membutuhkan.

Karena alasan itu, saya membuat aplikasi Android paling sederhana yang memakai Dagger 2… Hanya 2 halaman, kurang dari 20 baris 😎 . (tolong jangan membuat yang lebih simpel lagi, biarkan saya memegang satu rekor dunia 😝).

Banyak tutorial menampilkan kode berhalaman-halaman, dan tautan hubungan antar classes. Otak saya capek, karena stack saya dangkal 😝

(Untuk para ahli, saya minta maaf untuk cara saya yang tidak umum untuk memperkenalkan Dagger 2, karena ini benar-benar untuk dummies, dan hanya meminta pengetahuan yang minim bagi pembacanya)

Ayo mulai dari pemrograman (sangat) sederhana (… di Android)

Oke, saya akui, saya lumayan punya pengetahuan pemrograman. Saya tahu soal Function, Class, Member Variables. Saya tahu cara membuat aplikasi Hello World dengan satu MainActivity (hey! … Anda bisa membuat aplikasi itu tanpa mengetik satu baris kode… Cukup memakai template Empty Activity di Android Studio 😁)

Untuk membuatnya lebih keren, saya bisa mengubah aplikasi Hello World tadi menjadi aplikasi Hello Dagger 2. Silahkan lihat kode di bawah (bagian yang ditebalkan adalah kode yang saya tambahkan kepada template yang sudah ada).

class MainActivity : AppCompatActivity() {

    val info = Info()

    override fun onCreate(savedInstanceState: Bundle?) {

        text_view.text = info.text

class Info  {
    val text = "Hello Dagger 2"

Maaf kalau saya membuat hal sederhana jadi lebih rumit dengan menggunakan class untuk menyimpan teks. Dan saya membuat member variable bernama infountuk menyimpannya. Nanti dia akan berperan penting untuk menjelaskan penggunaan Dagger 2.

Saya harap semua cukup simpel sejauh ini.

Memindah info keluar dari Activity…

Dalam aplikasi beneran, biasanya saya tidak hanya punya satu member variable info. Bisa jadi akan ada banyak lainnya. Atau barangkali saya ingin membagi info dalam banyak activities.

Untuk mempermudah, alih-alih membuat info di dalam class MainActivity, saya ingin membuat info (dan semua member variables lain) dibuat di tempat lain, lalu dikirim masuk. Hey, kamu tahu? Ada istilah untuk hal tersebut… Dependencies Injection 😉.

Gambar perbedaan membuat variable biasa dengan menyuntikkan variable menggunakan dependency injection.

Kedengaran sederhana… mungkin saya bisa membuat seperti ini?

class MainActivity(val info:Info) : AppCompatActivity()

Ups! Maaf, Android tidak mengizinkan saya menambah argumen kepada constructor Activity. Kalau saya mau, ada jalan yang lebih susah. Cek di Stack Overflow. Tapi meskipun saya mau pakai jalan susah tersebut, dia masih tetap mustahil dilakukan, karena kita melakukannya di Activity pertama di dalam aplikasi. Siapa yang akan mengirim parameternya?

Kalau Anda punya lebih banyak pengalaman di Android development, mungkin sekarang Anda sudah berpikir untuk menggunakan Application untuk membantu Anda… Tepat sekali, Anda benar! … Tetapi saya yang sedang bercerita di sini. Saya sedang bercerita soal Dagger 2, jadi mari kembali ke topik…

Berdasarkan kisah nyata…

Dengan adanya kebutuhan seperti kita bahas sebelumnya, beberapa orang mulai membayangkan, betapa enaknya kalau kita bisa secara ajaib membuat semua dependencies agar bisa di-inject (dalam kasus saya, dependency yang saya punya adalah info). Untuk Android, beberapa orang tersebut akhirnya membuat Dagger, yang dibuat oleh para ahli dari Square. Google menyukai karya mereka, dan membuat versi keduanya, Dagger 2.

Dagger 2 menjadi sangat populer… sampai suatu hari seorang dummy ingin belajar tentangnya… lalu memutuskan membuat blog post… Okay, cukup ngobrol sejarahnya…

Membuat info muncul secara ajaib?

Dengan adanya Dagger 2, kita sekarang bisa membuat info muncul secara ajaib. Mungkin akan lebih ajaib lagi kalau kita bisa menghapus baris val info = Info(), tapi hal ini TERLALU ajaib. Sudah di luar kemampuan Dagger 2, mungkin butuh David Copperfield.

Yang lebih realistis, kita masih harus memberitahu MainActivity untuk memiliki member variable bernama info, tetapi dia TIDAK PERLU membuat/menginisialisasinya (hal ini biarkan terjadi secara ajaib).

Dengan Kotlin, karena kita ingin info sebagai non-nullable item, maka info harus diinisialisasi dengan sesuatu. Tetapi karena kita tidak tahu dia mau diisi apa, dan kita menunggu kerja dari Dagger 2, maka kita perlu memberitahu Kotlin agar lebih santai dan tidak kuatir soal inisialisasi, dengan cara menambah modifier bernama lateinit.

Jadi singkatnya, cukup tambahkan lateinit var info: Info untuk mengganti kode kita sebelumnya.

class MainActivity : AppCompatActivity() {

lateinit var info: Info override fun onCreate(savedInstanceState: Bundle?) {

text_view.text = info.text}

class Info {
val text = "Hello Dagger 2"

Hey, jangan di-compile dulu kode di atas, nanti akan CRASH 💥!!! Penyebabnya karena info belum diinisialisasi.

Maaf karena belum ada keajaiban di sini. Jangan marah ke saya… Saya janji kita nanti tidak perlu inisialisasi di MainActivity, cukup Dagger 2 yang melakukannya.

Sekarang, menuju dunia Dagger 2…

Menambahkan libraries Dagger 2

Sebelum terlalu jauh, kita lakukan hal-hal membosankan dulu. Semua yang kita perlu pakai di luar hal-hal Android paling dasar, kita perlu tambahkan libraries-nya ke project kita. Dagger 2 juga begitu. Cukup ikuti langkah berikut.

Di file build.gradle aplikasi Anda (pilih untuk app, bukan project), tambahkan baris yang ditebalkan berikut:

apply plugin: ''
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'


dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
// ... other dependencies ...
implementation ''
kapt ''

Lalu lakukan sync di project Anda. Sekarang Anda bisa pakai Dagger 2.

Kembali ke kode… Memperkenalkan @Component

Sekarang kita kembali ke kode. Saya kenalkan Anda pada yang namanya @Component. Dia adalah satu cara untuk memberitahu Dagger 2, “Hey! Saya ingin satu kotak ajaib yang bisa melakukan keajaiban membuat member variable saya.”

Saya mendeklarasikan kotak ajaib tadi seperti ini, sebagai interface:

interface MagicBox

Saya menyebutnya MagicBox agar berbeda dengan tutorial lain, agar Anda tahu kalau dia bisa dikasih nama apa saja. Tutorial lain akan memberinya nama <Something>Component. Dalam proyek bersama-sama dengan orang lain, saya sarankan memakai nama seperti itu, jadi orang lain bisa paham kode anda.

Saya juga perlu memberi tahu kotak ajaib tadi, bahwa dia dibuat untuk melakukan keajaiban pada MainActivity. Untuk melakukan itu, saya membuat function bernama poke , yang menerima MainActivity sebagai parameter di dalam MagicBox

interface MagicBox {
fun poke(app: MainActivity)

Catat bahwa saya memakai nama poke (suntik) agar berbeda dengan tutorial lain, jadi Anda tahu dia bisa diberi nama apa saja. Hampir semua tutorial yang saya baca, memakai nama inject. Sekali lagi saya sarankan memakai nama itu untuk proyek bersama, agar mudah dipahami.

Untuk membuat semuanya lebih jelas, bayangkan bahwa MagicBox bisa dipakai untuk menyuntik MainActivity, seperti di diagram di bawah, agar bisa memasukkan info ke dalam MainActivity. Kita cuma perlu memasukkan info ke dalam MagicBox, dan dia langsung bekerja! …Akan saya tunjukkan bagaimana info dimasukkan, sebentar lagi…

Contoh memasukkan variable ke dalam activity menggunakan Dagger 2

Mari kita bayangkan sebentar. Terutama jika saat ini Anda bertanya-tanya, untuk apa membuat MagicBox kalau kita bisa membuat info langsung di dalam MainActivity?

Sekarang bayangkan kita punya banyak members variable selain info, dan kita juga punya banyak activities dan classes yang membutuhkan mereka. Jadi diagramnya jadi seperti di bawah, dan MagicBox bisa mengisi semuanya sekaligus, tanpa satupun activities atau classes harus repot-repot membuat members variable-nya sendiri.

Contoh diagram memasukkan variable ke dalam banyak activity dan class menggunakan Dagger 2

👏👏👏 MagicBox mengurus semuanya untuk kita!! Baik sekali dia…

Oke, sekarang kembali ke kenyataan. Kita hanya butuh satu info di satu MainActivity, karena kita sedang membuat aplikasi Dagger 2 paling simpel di dunia.

Memasukkan info ke @Component… memakai @Inject

Oke, sekarang kita masukkan info ke @Component

Sekarang bisa jadi Anda berpikir, kalau kita harus secara manual memasukkan info ke @Component, terus apa spesialnya? Bukannya kita bisa membuat class untuk membuat yang seperti itu? Di mana ajaibnya?!!

Tunggu… keajaiban Dagger 2 segera muncul… sekarang!

Keajaibannya adalah, saya tidak perlu memasukkan info ke MagicBox. Saya cukup tambahkan keyword spesial (disebut juga annotation) ke dalam class Info, dan Dagger 2 langsung tahu ke mana dia harus mengambil Info.

Perkenalkan… @Inject!!

Ubah dulu class Info tadi dari

class Info  {
val text = "Hello Dagger 2"


class Info @Inject constructor() {
val text = "Hello Dagger 2"

Maaf kalau Anda harus mengetik lebih banyak dari @Inject, karena sebelumnya kita tidak membikin constructor. Karena @Inject harus ditambahkan di constructor, maka kita perlu menampilkan contructor di Kotlin memakai function constructor() di atas.

Setelah annotation @Inject ditambahkan, Dagger 2 sekarang sudah tahu caranya mengambil class Info lewat MagicBox. Gampang sekali caranya, sekarang Anda bisa tambahkan @Inject ke class manapun yang sudah ada, dan class object langsung terkonek dengan MagicBox secara ajaib saat dibuat.

Jika Anda penasaran bagaimana caranya… Itulah keajaiban Dagger 2. Tidak usah pusing-pusing memikirkannya, karena kita cuma dummies 😝.

Membangun MagicBox…Keajaiban Dagger 2 yang lain

Semua belum selesai. Kita sudah mendefinisikan MagicBox, dan menyambungkan Info ke MagicBox. Kita sudah punya function poke untuk menyuntik MainActivity.

Tapi sejauh ini, kita belum menyuntiknya!

Sebelum kita bisa melakukan itu, kita harus membangun MagicBoxnya dulu…

Tapi MagicBox adalah sebuah interface. Siapa yang harus membuat implementasinya?

Jangan takut!!! Inilah keajaiban Dagger 2 berikutnya. Karena kita sudah memberinya nama MagicBox, kita cukup menjadikan Dagger 2 sebagai penguasa dari MagicBox, dengan cara memanggil Static Class yang secara ajaib sudah dibuatkan, bernama DaggerMagicBox.

Kelas ini akan memberi anda sebuah Builder untuk membangun MagicBox. Cukup panggil seperti berikut:


Jangan kuatir kalau Android Studio protes karena dia tidak mengenali DaggerMagicBox, karena sampai pada titik itu dia memang belum diciptakan. Cukup percayakan pada Lord Dagger 2, dan dia akan membereskan semua. Compile/Run... dan seperti dijanjikan, DaggerMagicBox diciptakan saat proses kompilasi.

Sekarang Anda bisa main suntik

Oke, menyuntik MainActivity bisa dilakukan seperti berikut (intinya: buat MagicBox, panggil function poke. Masukkan MainActivity sebagai parameter memakai this):

override fun onCreate(savedInstanceState: Bundle?) {
text_view.text = info.text

Idealnya, sampai di sini aplikasi langsung jalan.

Compile… run… CRASH 💥 !!! Android Studio protes lagi, info belum diinisialisasi!!!

Tolong, tolong jangan tembak saya… saya tidak bohong soal keajaiban Dagger 2. Ijinkan saya tambahkan satu step lagi…

@Inject lagi!!!

dalam MainActivity, kita sudah definisikan info seperti ini

lateinit var info: Info

Dagger 2 tidak ingin membajak member variable manapun dengan membuat tanpa ijin saya. Ini sebenarnya bagus, karena kadang bisa jadi saya ingin bikin info saya sendiri.

Jadi untuk memberitahu Dagger 2, member variable mana yang perlu dia ciptakan, kita pakai kata kunci @Inject di depan namanya:

@Inject lateinit var info: Info

SELESAI!!! Sudah cukup. Compile, semua jalan!!!

Android app dengan Dagger 2 tersimpel di dunia

Dengan kode tadi, selesai sudah dalam satu file (kurang dari 20 baris, dan hanya 4 baris Dagger 2):

class MainActivity : AppCompatActivity() {

    @Inject lateinit var info: Info

    override fun onCreate(savedInstanceState: Bundle?) {
        text_view.text = info.text

class Info @Inject constructor() {
    val text = "Hello Dagger 2"

interface MagicBox {
    fun poke(app: MainActivity)

Anda bisa ambil contoh project-nya di

Apakah semua selesai?

Oh tentu tidak. Ini hanya ujung dari gunung es Dagger 2. Tapi untuk seorang dummy seperti saya, mencerna hal sederhana begini adalah awal yang bagus, dan otak saya bisa istirahat sejenak.

Saya tahu kalau saya hanya membagi hal paling dasar (dan saya dengar sebagian orang memprotes, karena saya tidak adil kepada Dagger 2. Maaf… saya akan tambahkan lagi detailnya kalau pembaca tertarik), dan saya memang sengaja tidak membahas hal-hal penting seperti @Module, @Singleton, @Scope, dan lainnya. Mereka tidak dibahas supaya saya bisa membuat semuanya lebih mudah dipahami para newbies. Tapi mereka memang penting untuk membuat Dagger 2 semakin berguna.

Kalau Anda tertarik, Anda bisa belajar lebih lanjut di sini.

Atau, Anda juga bisa mencari tutorial lain, karena saya merasa tutorial saya sudah memberi Anda pemahaman dasar bagaimana hal-hal di Dagger 2 saling menyambung. Anda bisa jadi sudah siap untuk membaca tutorial lain yang lebih padat.

Dapatkan Artikel-artikel Seperti ini Di Email Anda


Optimize Later

In other words, you are unlikely to know up front whether an optimisation will be of any real benefit. Just write the code the simplest way. If, eventually after profiling you discover a bottleneck optimise that.

from Optimize Later

(Also related: Premature Optimization)

I am currently designing an app that will make heavy use of the REST API. During the process I was a bit worried that the app will need to make multiple API requests at once and becomes too slow. But it seems like it’s no use worrying about now. We’ll have to see how the actual app runs, and work on optimizing it if it’s ever needed.


JavaScript Frameworks in a Nutshell

It can feel like each day a new JavaScript framework is introduced, and the fast-paced development becomes difficult to keep up with. I came across a comment on Hacker News that explains the reasoning, the why of a particular framework. I reproduced the comment below and added some links to help make research easier:

While it might look like the frontend is going around in circles, there are major & minor differences between the technologies, and they have each introduced novel (to JS at least) things… Off the top of my head (this timeline might not be right but it’s how I went through it at least):

Backbone got you away from jquery spaghetti, helped you actually manage your data & models. Often paired with Marionette to handle views (if you squint this is the precursor to components) and bring some order in that part of the app.

Angular bundles backbone’s data handling (services), Marionette’s orderly view separation (views, directives), but they made a fatal mistake in the apply/digest cycle and maybe encouraging a bit too much complexity. Angular is everything bundled together, with consistent usage/documentation/semantics, a familiar programming pattern (MVC), and a large corporate sponsor in Google it caught on like wildfire.

Knockout sees Angular’s MVC, laments it’s complexity and focuses on MVVM — simple binding of a model and a relatively simple reactive-where-necessary controller

React comes along and suggests an even simpler world where the only first class citizen is components and everything else comes separate (this isn’t necessarily new, there is an article out there comparing it to COM subsystems for windows). React is almost always used with react-router and some flux-pattern supporting data management lib — these are also departures from how angular, backbone and knockout structured in-application communication (backbone was pure event bus, angular had the usual MVC methods, knockout was just callbacks — if you have a handle to an observable when you change it things reload).

Vue sees the complexity that React grew to (case in point: shouldComponentUpdate) and devises a much simpler subset along with a new way to handle reactivity — using the `data` method of a Vue component. Vue made a lot of decisions that helped it stay simple and small yet very productive and this is in large part thanks to React existing before hand.

Svelte comes on the scene and realizes that truly optimal rendering could be achieved by just compiling away the excess and eschewing the virtual DOM all together in most cases. No need to track all the structure if you compile the code with the updates necessary. Don’t quote me on this but I think a whole host of things influenced this project — smarter compilers, better type systems, the ideas around zero cost abstractions & doing more work at build time.

Ember (formerly SproutCore) is actually an anomaly because it tries it’s best to be both angular like (so large, and usable by large teams) and keeping up with the tech as it evolves (see: Glimmer, Octane). Ember also has some innovations, like Glimmer’s VM-based approach — turns out you can just ship binary data representing how to draw your components to the browser and skip a bunch of JS parsing, if you bring your own virtual machine that is optimized to draw the components.

hardwaresofton on Hacker News