Sunday, June 16, 2019

My Career as Android Developer

Seven years ago, I started my career as an Android developer. Back then mobile development was still a relatively new field. I remember using Eclipse as an IDE and how I used to build the app through this complex pom files. Also, there were limited Layouts, mainly Linear and Relative Layouts only. Dealing with SQLite database was a nightmare with all -non debuggable- SQL statements you have to write to build your tables, insert or update fields.

Now, things have changed a lot for mobile development. A lot of new tools are developed and technologies introduced to support this fast growing market.

Android Studio along with gradle build were two of the greatest tools introduced for Android development as it solved a lot of issues in Eclipse as performance and complexity. Also, Firebase was introduced in 2016, a great platform that gathered a lot of essential tools for mobile development as real-time database, authentication and push notifications.


What I personally like most and used a lot in my coding are these following tools:
  1. Rxjava: rxjava made async tasks/calls much easier and less complex and buggy. Earlier we had to use AsyncTask classes with all its implementation issues, but now with the help of rx you can add your async methods within your code and receive the response on the callback method.
  2. Data Binding: Data binding helped reduce a lot of unnecessary code calling UI elements from xml file in the Activity view by linking model objects directly with the xml.
  3. Retrofit/Volley: If you ever needed to call Restful API services, then you will not find better than Retrofit or Volley for this task. They efficiently handle all http tasks, threading and response handling.
  4. Room: This is the most important library in my work. It removed all the complex SQL headache from the developers mind and made SQLite handling as easy as it can get. Now you can build your tables, ad, edit or remove items in the database with just simple annotations in your model object and a DAO class.
  5. MVVM: The best architect design I worked with. I depends entirely on event based programming and solves a lot of the previous MVP. MVVM works like a charm with all the above RxJava, Data binding, Retrofit and Room to build a complete event based program where every task is carried asynchronously and views get the data through observables.
Currently I develop using Kotlin which has some advantages over Java as Null checks. I used it while developing my latest app “AlAdwaa Education” which is an educational platform for helping students study and revise their lessons.

Nowadays, I am willing to join some remote jobs platforms as I would like to have  more control over my time and work location. One of the best ones I would like to join is Toptal, as I am willing to be featured in their top skilled Android developers list. I believe this step will boost my developing career as I will work with international teams scattered all over the world, which will require a lot of time management and strict task tracking.



Tuesday, February 19, 2019

Points you should take care of for a defensive programming

During my career as an Android programmer I have noticed a lot of programmers don't consider - or know - the concept of Defensive Programming while writing their code, which leads to their code generating unexpected and unhandled bugs, either during testing phase or even after publishing their applications.

As a programmer you should - or al least do your best to - avoid your app crashes in users hands. As for my self, I could - to some extend - accept an app with some misfunctioning feature and wait for the developers to fix it; but I could never accept an app that crashes and will immediately uninstall freeing some memory for a better developed app.

What is Defensive Programming?


If you looked up the term "Defensive Programming" you will find this according to Wikipedia's:

"Defensive programming is a form of defensive design intended to ensure the continuing function of a piece of software under unforeseen circumstances. Defensive programming practices are often used where high availability, safety or security is needed."

In other words, "Defensive Programming" is technique to develop a software that doesn't crash if the users performed an unforeseen action or received some unexpected sort  of data. Using these techniques you can ensure the continuity of your software - or otherwise gracefully handle exceptional conditions - while you find out a solution for the aroused issue. BUT DOESN'T CRASH.

Below are some points to take into consideration while developing:

1) Try and catch


try{
}catch(Exception ex){
...
}

A lot of developers  don't consider using try & catch blocks unless forced by an IDE error alert. Well, you should!!

Example of cases you should use try & catch with are:

a) Whenever you write a code that involves data types transformations such as parsing Integer to String and vise versa as this might throw "NumberFormatException".

Also handling JSON strings and mapping them to other Objects.

b) Code that will perform actions away of your main thread and return back with response, such as communicating with remote servers over HTTP or even your local SQL database, where you don't have a full control or knowledge of the coming response.

Note: you should not treat "try & catch" as a final solution for the problem, however; it should be considered as a temporary plug to avoid crashes while you investigate and handle the issue in a better way.

2) Null checks


if(object != null){

}

"NullPointerException" is the most popular and most occurring exception. Don't underestimate the above simple null check whenever you doubt your variable/object might return null. Due to inability to distinguish nullable and non-nullable reference types. Because of that, many programmers code defensively against them. So much that in many projects almost each public method and constructor is populated by this sort of checks.

If allowed to slip through, nulls can lead to obscure errors down the road. But you still can significantly reduce the number of such validations.

One way to do that is the Optional<> operator introduced in Java 8.

3) Variables initial values


private String name = "";

Unless intended, always set initial values for variables you declare to be set in a later stage in your code. For example: a user name you are fetching from an API call, to avoid exceptions such as NullPointer and NumberFormat if this variable failed to get set due to a failing request.

4) Switch default case


switch(number){
          case 1:
             ...
          break;
          case 2:
             ...
          break;
          case 3:
             ...
          break;

          default:
             …
}

Don't forget to add a default case if you doubt your variable might be assigned a value not handled in your cases.

5) IDE code warnings


Always check lint warnings raised by your IDE and solve them whenever you can. Your IDE can warn you about various aspects in your code:

- Possible NullPointerException
- Unchecked type conversion

Conclusion


If you intend to publish an application to users you should consider the fact that users might use it in a way you didn't cover in your test scenarios leading to the possibility of crashes.
You can reduce this possibility by following simple defensive rules while developing your code, that will help keeping your app functioning under unexpected conditions and of course keep your users satisfied.