How to install Intellij IDEA on Linux

This guide should work on any Linux distribution, And i will assume you have java installed on your machine

1- Obtain your copy from Intellij IDEA and move it to your home directory

2- Extract the tar file and move it to /opt change the with the right file name


sudo -i #we'll need root shell  during installation

tar -xvzf ideaIC-11.1.3.tar.gz /opt

3- Add read permissions to all files

chmod -R +r /opt/idea-IC-117.798

4- Create an executable file


touch /usr/bin/IDEA

chmod 755 /usr/bin/IDEA

#Use vim or your favorite text editor

vim /usr/bin/IDEA

## Paste the following lines

#!/bin/sh
export IDEA_HOME="/opt/idea-IC-117.798/bin"

$IDEA_HOME/idea.sh $*

5- Create Gnome/KDE menu icon


## Create this file with your favorite text editor or continue with

/usr/share/applications/IDEA.desktop

#Paste the following lines to the file

[Desktop Entry]
Encoding=UTF-8
Name=intellj IDEA
Comment=Intellij IDEA IDE
Exec=IDEA
Icon=/opt/idea-IC-117.798/bin/idea.png
Terminal=false
Type=Application
Categories=GNOME;Application;Development;
StartupNotify=true

6- Enjoy your IDE and happy coding 🙂

 

Note: In future releases the steps remains the same, just change the folder name  “idea-IC-117.798”

TIL Command and Query Responsibility Segregation

When most people talk about CQRS they are really speaking about applying the CQRS pattern to the object that represents the service boundary of the application. Consider the following pseudo-code service definition.

CustomerService


void MakeCustomerPreferred(CustomerId)
 Customer GetCustomer(CustomerId)
 CustomerSet GetCustomersWithName(Name)
 CustomerSet GetPreferredCustomers()
 void ChangeCustomerLocale(CustomerId, NewLocale)
 void CreateCustomer(Customer)
 void EditCustomerDetails(CustomerDetails)

Applying CQRS on this would result in two services

CustomerWriteService


void MakeCustomerPreferred(CustomerId)
 void ChangeCustomerLocale(CustomerId, NewLocale)
 void CreateCustomer(Customer)
 void EditCustomerDetails(CustomerDetails)

CustomerReadService


Customer GetCustomer(CustomerId)
 CustomerSet GetCustomersWithName(Name)
 CustomerSet GetPreferredCustomers()

That is it. That is the entirety of the CQRS pattern. There is nothing more to it than that… Doesn’t seem nearly as interesting when we explain it this way does it? This separation however enables us to do many interesting things architecturally, the largest is that it forces a break of the mental retardation that because the two use the same data they should also use the same data model.

Source, Wikipeida

TIL Argument vs Parameter

OK, I know naming idioms isn’t really a big deal. but yet it’s fun and useful to know 🙂

Parameter is the variable name in function definition/deceleration

Argument is the name of the variable when calling the function


void function( int Var1, int Var2 ) { //  do something

anotherFunction( int AnotherVar1, int AnotherVar2);

}

Var1, Var2 are called parameters

AnotherVar1, AnotherVar2 are called arguments …

 

Nowhere is better than wikipedia for more details 🙂

Recursion and Tail-recursion

What is recursion?


The concept is general. Stories inside stories, movies inside movies, images inside an images….

Formal definition?
Recursion is function that call itself.

why would a function call itself?
There are LOTS of algorithms for which the simplest and easiest to maintain version is recursive.
Sorting/searching Algorithms, Most data structures.

A factorial number is a simple recursive function,
Its the product of all positive integers less than or equal to n. For example

5! = 5 * 4 * 3 * 2 * 1

First thing to think about is the base case, When to stop?
When we reach 1 …

if Number == 1; then the answer is 1

if Number == 1; then the answer is 1
factorial(Number) = Number * factorial(Number -1)

In a C-style language it would be

factorial(Number)
if x == 1
return Number
else
return Number * factorial(Number - 1)

This function will be executed like this

5! = 5 * factorial(4)
5! = 5 * 4 * factorial(3)
5! = 5 * 4 * 3 * factorial(2)
5! = 5 * 4 * 3 * 2 * factorial(1) #Where it meets the base case
5! = 5 * 4 * 3 * 2 * 1 = 120 … #Congratulations!
Is it a good practice to keep that much of operations in memory?

Do you see the growth rate of this function? It grows as much as there are elements to multiply
2!= 2, 3!= 6, 4!= 24, 5!= 120… 9!= 362880
Dare we calculate 120! ? 🙂 …

– Another concept introduced here is Tail Recursion to transform the above linear process to an iterative one.

In order to eliminate this stacking of operation we want to reduce them,
So we will need to hold an extra temporary variable as a parameter in our function, They call it Accumulator

Accumulator is place to hold our calculation results as they happen.

A Tail recursion version of our function

tail_factorial(Number, Accumulator = 1 )
if Number == 1    #Our base case
return Accumulator
else
return tail_factorial(Number -1, Accumulator * Number)

tail_factorial(4) execution looks like

tail_factorial(4)     = tail_factorial(4, 1)
tail_factorial(4, 1)  = tail_factorial(4-1, 4*1)
tail_factorial(3, 4)  = tail_factorial(3-1, 3*4)
tail_factorial(2, 12) = tail_factorial(2-1, 2*12)
tail_factorial(1, 24) = tail_factorial(1-1, 1*24) #Reach the base case
tail_factorial(0, 24) = 24

Do you see the difference? Now we never need to hold more than two terms in memory!