Posts

Clean Code: Meaningful Names

Image
     Names are everything! Files, classes, functions, variables all need a name, therefore we should cate about good names. Good names save more time than they take. Coming up with good descriptive names that don't confuse the developer who has to read the code in the future may come with no effort to some, but for others it may feel like a constant struggle. Readable, and intuitive names are a foundation of clean code. Let's take a look at some key principles that will help you come up with good names in your code. Intention Revealing      The name of a variable, function or class must answer questions. Why does it exist? What it does? How is it used? A variable named instance or number is not descriptive enough and requires the user (developer) to figure out the context and the type of the variable in order to know what it does, a better approach would be changing the names to userAccount and balance respectively. Naming should be explicit and explain...

Clean Code: Introduction

Image
Two years ago I have switched from Java/Kotlin Android development to JavaScript Full Stack web development. Because of that I have a lot of new insights that I want to share with the reader. One of the biggest realizations I have had is no matter what platform or language or framework you are using, the fundamental Clean Code principles DO NO CHANGE.  These series is dedicated to [re]discovering the Clean Code principles. Strongly influenced by "Clean Code" by Robert C. Martin, I will be covering the most important principles to writing clean, readable and maintainable code. Good developers are modern craftsman. Knowing how to make your code do what you want it to do is easy. Making that code maintainable is the real challenge. As new abstract languages and frameworks are created, and as number of programmers doubles every 5 years, knowing how to write clean code is the most important challenge for any developer. Good code matters! Working mess is NOT better than nothing. It...

Building a Cryptocurrency with Kotlin : Part 1

Image
Kotlin has been becoming more and more mature with every release. We arguably have the most concise modern programming language that currently exists. With language idioms like data classes, extensions, kotlin objects, delegated properties, lambdas and of course, the latest version of Kotlin adds coroutine support. Lets talk about Node.js for a second.  Node.js  helped us to create many amazing websites and services, and provides numerous modules to make our lives as developers easier. But just because a tool is good enough does not mean we should stop innovation there. Kotlin is now Node.js' competitor because Kotlin's powerful language idioms enable us to create a powerful and fast framework for building asynchronous servers and clients, called  Ktor . Lets go back in time for a moment. Back in 2008 a small C++ side project was started by Satoshi Nakamoto. This project would change the world for ever. 10 years later Bitcoin managed to inspire dozens of crypto curren...

Android Architecture Components meets Data Binding : Lifecycle awareness!

Image
Finally Android Data Binding library got updated to play nice with Android Architecture Components library and became life-cycle aware! This means that any changes to the LiveData objects will be reflected in the layouts... automagically! :) * Tested on Android Studio 3.1 - canary 6 Whats new? To make the binding life-cycle aware you need to pass the life-cycle owner to the ViewDataBinding by calling the  setLifecycleOwner method. The ViewModel: Our view model consists of the LiveData object that is simply and observable data container, where the observer gets notified of data changes. The model: Our model has one field that is used to store user's name. A POJO. The layout: -Out layout consists of a text field that is bound to the user's name. And will be updated when our LiveData container posts a change. -We also have an EditText that is two-way bound to the user's name, meaning when text gets entered, the name is updated and vise-versa, when the use...

Android Lists Made Easy: DataBinding + RecyclerViewBindingAdapter Library

Image
RecyclerViewBindingAdapter Library This library provides a powerful yet reusable RecyclerView adapter that leverages ObservableArrayList and DataBinding to simplify your life. Tell the adapter which layouts your models map to and then just modify your observable list and the adapter takes care of the rest. Its that simple! Check out the library and example app source code here . Step 1: Add  jitpack  repository to you project's  build.gradle file Step 2: Enable Android data binding and add library dependency in the module's build.gradle file that you will be using the library in. You will need data binding to create layouts bound to  ViewModel s . The adapter dynamically creates all the rows and manages recycling of the layouts. Step 3: Create  ObservableArrayList  that will hold all the view models  and  initialize EasyRecyclerAdapter with it. Set up the mappings between the layouts you intend to show in the list ...

Android Architecture Components : ViewModel

Image
ViewModel : UI-Related Data Object One of the biggest challenges for an Android developer is keeping data across configuration changes such as screen rotation or during the recreation of Activity / Fragments after its been destroyed and recreated by the framework. Luckily the new Android Architecture Components library has ViewModel class that is intended to store and manage UI-related data so that the data survives configuration changes.  Benefits Keeps data across UI recreation No need to repeat API calls and Database queries on UI recreation  Keeps  Activity / Fragments clean since work is now delegated to ViewModels Share ViewModels between Fragments Subclass ViewModel To create a view-model just subclass ViewModel . View-models are responsible to provide data and keep reference to it. Our TimerViewModel lazily initializes TimerLiveData which is a LiveData implementation of a timer that counts seconds, read more about TimerLiveData a...

Android Architecture Components : LiveData

Image
Live Data : Life-cycle aware Observables As an Android developer you often have to work with data that is dynamic and changing with user input, device configuration changes, or even time changes. On top of this as a developer you have to respect the life-cycle of an Activity of Fragment that contains this dynamically changing data: stop updates onStop() or onPause() and restart the updates onStart() and onResume() . Examples of this is location data, countdown timers, user's list selections and any other type of dynamically changing data. Lets see how we can make these tasks easier using LiveData . We will demonstrate LiveData by creating a self contained timer. Subclass LiveData LiveData object is set active when the observer's lifecycle is stated or resumed . And the LiveData object is set inactive when the observer's lifecycle is stopped or pause . In our TimerLiveData we start timer when  onActive() is called and stop the timer when  onInactive() ...