What is great in Kotlin? Data class!

article-2202821-150213ff000005dc-976_634x391

One of the great things in Kotlin is the Data class.

When you are working with a data class, you spend a lot of time to write getters and setters and equal operator and so on…

Kotlin show all his power in this situations, in fact you can write only something like this:


data class Person (val name: String, val age:Int)  //Formattare

And.. it’s all!

If you want to add a new parameter, you can add and then all it’s ok! You don’t waste your time to write new getters and setters and modify the equal operator.

Now, we want to use this data class: it’s so simple!


fun getUser(): User {
    return User("Alex", 1)
}

fun main(args: Array<String>) {
    val user = getUser()
    println("hello my name is  ${user.name}, and I'm ${user.id}")

    // or

    val (name, id) = getUser()
    println("hello my name is $name, and I'm $id")

    // or

    println("hello my name is ${getUser().component1()}, and I'm ${getUser().component2()}")
}

I think this is a cool feature of kotlin because, in this way, the code is less verbose than other lenguages.

How to set up kotlin into android project

In this post I want to show how to start an android project with some kotlin classes:

Start android studio and install the kotlin plugin: go to “search everywhere” and write plugin. In Plugins select kotlin and install it.

Schermata 2016-03-30 alle 18.11.37

Schermata 2016-03-30 alle 18.13.02.png

After this, create a new Android project and then, into search everywhere, type Configure kotlin and select Configure kotlin in project. This operation modify your build.grandle and add some rows:

Schermata 2016-03-30 alle 18.13.43

Schermata 2016-03-30 alle 18.19.18.png

After this operation it’s possible to convert java code into kotlin code, so it’s possible to convert an activity written in java, into activity written in kotlin: Select the activity to convert and then select  “Convert java file to kotlin file” from Code menu

Schermata 2016-03-30 alle 18.20.43.png

 

Now we can start to write our kotlin code in this android project.

Kotlin – first steps –

What is Kotlin?

From the jetbrains blog :

Kotlin is a pragmatic programming language for JVM and Android that combines OO and functional features and is focused on interoperability, safety, clarity and tooling support.

Being a general-purpose language, Kotlin works everywhere where Java works: server-side applications, mobile applications (Android), desktop applications. It works with all major tools and services…

I want to try this new programming language for two reasons:

  • I want to study something new
  • I was born as c++ developer and, for me, Kotlin is very simple to read and to understand, so i can use this language where java is used.

The number of github Kotlin projects is esponenctially increased in the last few years, so this is another good reason to start to learn this programming language.

kotlinadoption

 

Well, after this introduction about “why” i want to learn Kotlin, let’s start with something interesting for the developer.

Like every fist step into new programming language I started from “HelloWorld”:

fun main(args: Array&lt;String&gt;){
    println(&quot;Hello World&quot;);
}

These two lines of code show how to create a function (fun), how to declare the parameters accepted by the function and hoe to write something on the standard output.

For me is very simple and intuitive.

I want to go deeper into the Kotlin world!

 

 

 

Apache Thrift

apache-thrift-logo

What is Apache Thrift? Thrift is a framework. With this framework the programmers can create high-performance cross-language services.

With Thrift you can write the specifications of the contract between two or more microservices (client side and server side), and then you can generate the stub code for the right programming language.

The programming languages supported by Apache Thrift are:

AS3 C C++ C#
D Dart Delphi Erlang
Go Haskell Haxe Java
JavaScript Lua Node.js Objective-C
OCaml Perl PHP Python
Ruby Smalltalk TypeScript

The “contract”is written in IDL (Interface Definition Language). Into this file the developer specify the abstract data type. After this definition, the IDL can be compiled into source code. This step seems to be a wasting time, but is useful because the developer must consider the interface in isolation, without thoughts on code.

I think Apache Thrift is a very useful framework for this reasons:

  • The design stage of the micorservice is independent from the implementation stage
  • Is useful in heterogenic  teams: all developers can work on a project and use the preferred programming language.
  • In this way, the designer, create a contract between the services that is clear and documented.

Useful links about this argument are:

 

Docker: Install on linux

Docker is a technology that allow you to package an application with all it needs to run correctly into a standardized unit called container.

This idea is very useful because you can develop your application and then deploy that into the correct environment and don’t be in trouble about the target machine and the other software installed.

First of all you need to install the docker environment on your machine. I try docker on a linux mint virtualized machine. Docker doesn’t have many requirements but a reasonable modern kerlel is advised. For this point is important to verify if the wget is correctly installed, on command line write:

which wget

if the prompt respond something like this:

Schermata del 2016-01-17 08:00:05

you have wget on your machine and now we can install docker, great!

wget -qO- https://get.docker.com/ |sh

then, you linux, starts to download the file and install it.

When the installation is finished you see something like this on your consolle:

Schermata del 2016-01-17 07:56:04

now we can verify the installation:

sudo docker run hello-world

Now Docker try to find the hello-world container on you machine, if don’t find it the engine stats to download from Docker hub. At the end you read this:

Schermata del 2016-01-17 08:03:40.png

It’s important to underline that, in this way, you need to run docker with sudo. If you would like to run Docker as a non-root user, should consider adding your user to the docker group with something like:

sudo usermod -aG docker klaus

Rememeber that logout and new login is necessary!

About interface event handling

When I work with user interface, if into a event handling method there is some code that not manages interface elements but implements logic, I prefer not write that code directly into the method but I create other method with the logic and then recall this method into the event handling method.
This implementation is a little bit complex and verbose than a direct implementation but is more open and reusable for the future development.
Consider this scenario and suppose to use c++ as programming language: we have a method that manage the clik on a certain button, called MyButton, on my user interface. The method for click management is MyButtonOnClick(TObject *Sender):

void MyClass::MyButtonOnClick(TObject *Sender)
{ 
   if(!sender)
      return;
   
   int param1 = getParam1();
   int param2 = getParam2();
   //this is the logic write directly into the event handling method
   int ret = param1 + param2;
   
   ResultLabel->Text = StringToInt(ret);  

}

In MyButtonOnClick there is a piece of code that implement addition of two params (from other interface elements or from other methods). This addition can be wrapped into other method so I can reuse that.

My implementation begin something like this:

int MyClass::AddParam(int param1,int param2)
{
   int ret = param1 + param2;
   return ret;   
}

void MyClass::MyButtonOnClick(TObject *Sender)
{ 
   if(!sender)
      return;
   
   int param1 = getParam1();
   int param2 = getParam2();

   int ret = AddParam(int param1,int param2);
   
   ResultLabel->Text = IntToString(ret);  

}

In this way i can reuse the AddParam Method into other methods.

The example is very simple but explain clearly the initial these.

PHP soap client example

Hi guys, today I want to focused on a simple php thing: soap client. I found this very important in my last project so I want to share with you something about this. First of all it’s important to understand how vsld describe the method that client can call. An example of vsld is the following:

 
<message name="UserEnabled">
   <part name="parameters" element="tns:UserEnabled">;
</message>

<xs:element name="UserEnabled">
<xs:complexType>
<xs:sequence>
<xs:element name="userId"type="xs:int" />
</xs:sequence>
</xs:complexType>
</xs:element>

This is the description of a method interface for a generic soap server that manages some connection. In particular this method enable user at the operations on that server. Now we can implement the client for this vsdl, is very easy code:

$client = new SoapClient("http://serviceaddress/data/user.wso?wsdl");
$result = $client→UserEnabled(array('userId'=>$userId));

We can call the method exposed in the vsdl like a method of the instantiated soap client! Is very powerful!

Now, If you want to use this into a method, an example of implementation is something like this:

/**
 *
 * @param integer $userId
 * @return boolean
 */
 public function UserEnabled($userId)
 {
   $result = $this->signAction->UserEnabled(array('userId'=>(int)$userId));
   $ret = false;
   if($result->res == "OK")
   {
     $ret = true;
   }
   return $ret;
 }

See you soon bye bye!