Blogs

Posted by: {authorName}

Recently, with my blog ‘A look at mobile websites’, I gave a insight about the growth of use of smart mobile devices. These devices, which are glued to human hands, ears and eyes for more hours in the day than ever before, run on mobile operating systems, like iOS, Android, Symbian, Windows 7, etc.

I thought it would be beneficial to dedicate this blog to introduce you to the fastest-growing mobile operating system in Google's Android.

What is Android?

Android is a software stack for mobile devices that includes an operating system, middleware and key applications. The Android OS can be used as an operating system for cellphones, netbooks and tablets, including the Dell Streak, Samsung Galaxy Tab and other devices.

Skills you’ll need to dive into Android programming:

  • For technical and programming skills, knowing Java will make you more comfortable. Also understanding of common, object-oriented languages like C++ and C# will make someone pick things up quickly, as apps you develop in Android are heavily dependent on the said frameworks.
  • Some experience with a tag-based view technology like HTML or Adobe's Flex will makes the views very familiar.

Difficulties you’ll encounter:

  • Layouts would be the hardest initial things to overcome as an Android developer.  It required digging into the source code to make things look the way you wanted. There were things that were just not skinnable, or required making a completely new component, or changing the design.
  • Taking full-size pictures in the app and using those pictures is harder than it should be.
  • Another thing that is almost unbelievable is how much of a pain it is to work with maps. It requires signing up for an API key per computer to even see maps. Compare that to iPhone map development with Google maps, where you don't have to do any of that. They just work.

Tools you should have on hand:

  • It also really helps to have a device, especially when doing things with location or with the camera.

Things you need to consider as you are creating Android-based apps for multiple devices:

  • The big one is layout. If you fudge a layout to just get it working on a single device, it's going to get you later. You'll also need to prepare for different APIs not being supported, like the camera for instance. Designing the application well is going to make you think harder about the core functionality and use cases you want to support.

Best support resources for Android programmers:

  • The Android Developers site would be a great help. It has all the associated documents, code and the Google groups.
  • And as Android is open source, you can easily run searches and see what Google engineers did to make the core apps do what they do.
That’s it for a start on programming in Android. Contact KAYWEB if you need an Android app developed for you or your business/organisation.

Posted by: {authorName}

Seems that the there's not much we as software programmers can do with the statements...

But did you know that double-digit percentage optimisation results can be achieved with a few simple steps?

Having optimally-running software is quite a bit of challenge. The main difficulty in achieving maximum speed lays in small implementation details, that, if not treated properly on time, build up and often are difficult to track down.

In a nutshell, writing a fast program means coding the same logic in fewer steps required for the computer to perform.

The following are several simple hints for programmers related with the well-known statements.

Program codes seldom follow a unidirectional path if statements are probably the most used statements of all. The syntax of the statement is:

if <condition holds true> then <do something with THEN (TRUE) caluse>
otherwise <do something with ELSE (FALSE) clause>


Firstly, some sign legends:
  • == - EQUALS TO
  • != - NOT EQUALS TO
  • && - AND clause
  • || - OR clause
  • ! - NOT (NEGATION) clause

Usually, the condition of the if statement is comprised of multiple atomic (unbreakable into simpler pieces) conditions with optional NOT negation clause that are joint together with AND or OR clauses.

Example:
1. (a == b) && (c != d)
2. (a != b) && (c != d)
are both valid if conditions. Let's analyse the number of steps needed to gain a result.

(a == b) && (c != d) - in human language this means that to perform THEN clause a and b must be the same and c and d must be different.
This condition is equivalent to (a == b) && !(c == d). So
  1. Check if a is equal to b
  2. Check if c is equal to d
  3. Negate the result of 2.
  4. Output TRUE if 1. and 3. are TRUE
All in all 4 steps.

(a != b) && (c != d) - in human language this means that to perform THEN clause a and b must be different and so must c and d.
This condition is equivalent to !(a == b) && !(c == d). So
  1. Check if a is equal to b
  2. Negate the result of 1.
  3. Check if c is equal to d
  4. Negate the result of 3.
  5. Output TRUE if 2. and 4. are TRUE
All in all 5 steps.

Disjunctive Normal Forms (DNF) and Conjunctive Normal Forms (CNF) are methods to transform a condition into an equivalent condition (which may result to better performance in computer terms).

DNFs contain atomic conditions that are related to each other with OR clauses.
CNFs contain atomic conditions that are related to each other with AND clauses.

Following are statements that we will use to figure out how to optimise conditions of if statements.

1.
DNFs result to TRUE value if at least one of its conditions is TRUE,
CNFs result to TRUE value if and only if all of its conditions are TRUE.
Likewise,
CNFs result to FALSE value if at least one of its conditions is FALSE,
DNFs result to FALSE value if and only if all of its conditions are FALSE.

2.
DNFs can be converted into CNFs and vice versa using the following rules. I will use <==> sign to mark equivalency.
  • A || B <==> !(!A && !B)
  • A && B <==> !(!A || !B)


Having the two statements above we can deduct that the (a != b) && (c != d) condition can be performed faster by performing less steps. Really,
  • (a != b) && (c != d) <==> !(a == b) && !(c == d) ,
  • which by the DNF to CNF interconversion rule <==> !( !(!(a == b)) || !(!(c == d)) ).
  • ! applied twice neutralises itself thus we have !( (a == b) || (c == d) ).

Analysing the result we have the following steps
  1. Check if a is equal to b
  2. Check if c is equal to d
  3. Take TRUE if either 1. or 2. or both are TRUE
  4. Take the negated result of 3.

So instead of 5 steps we managed to do the same thing in 4 steps. Taking a close look at both versions
!(a == b) && !(c == d) and !( (a == b) || (c == d) )
we can clearly see that the optimum one has less NEGATION clause. So the verdict is:
  1. NEGATION clauses can be trimmed
  2. Excessive NEGATION clauses can slow down the code.

Modern programming languages are going one extra step to optimise the code further by making good use of the 1. statement about DNFs and CNFs:
  • For DNFs if the code encounters a TRUE atomic clause it disregards the rest of the clauses as they will not influence the final result,
  • For CNFs if the code encounters a FALSE atomic clause it disregards the rest of the clauses as they will not influence the final result.
So, approaching wisely here, we can in less than the 4 steps achieve the same results as above:
  • For DNFs put the likely TRUE clauses before the rest of the clauses,
  • For CNFs put the likely FALSE clauses before the rest of the clauses.

Let's consider !( (a == b) || (c == d) ) again.
  1. Check if a is equal to b
  2. If 1. is TRUE negate 1. and take the result as an answer (2 STEPS ONLY), otherwise If 1. is FALSE Check if c is equal to d
  3. Take TRUE if either 1. or 2. or both are TRUE
  4. Take the negated result of 3.

Considering that 2. has 50/50 chance to be TRUE or FALSE we finally have that on average the whole statement can be performed in ( 2 + 4 ) / 2 = 3 steps.

So we gained approximately 40% performance improvement in obtaining a result for the condition (a != b) && (c != d).

Additionally, if an
if (condition) then else
can be transformed into an equivalent
if (condition) then
form then the code will avoid an unnecessary calculation which is also an optimisation measure.

Further to come,
1. Does unary operators have underwater reefs.
2. Memory usage versus quick execution, an example.

Posted by: {authorName}

For different reasons, I encounter this problem every year and kept forgetting how to solve this. So I thought I would write this solution as a blog so I always remember. :)

First of all, the answer to the question is you cannot. Why?

Because $_GET and $_POST (and a few others) are HTTP variables. Meaning they can only be accessed if you are running the PHP file behind a web server. If you are trying to run a PHP file via CLI(command Line Interface), you'll not get those $_GET and $_POST variables.

But all is not lost. There is a work around to handle that problem and that is to use $argc and $argv variables.

$argc is the number of parameters passed in the command line while $argv holds the actual parameters stored in a zero based index array.

Example:
[ian@mycomputer]$ /usr/bin/php myscript.php hello world

$argc value will be 3. $argv will be an array of 3 strings containing "myscript.php", "hello" and "world."

Now back to our problem. To solve it, we will be using $argc and $argv to fill up the $_GET or $_POST variable. How?

Using a browser, to fill up the $_GET variable, you add the data by appending text to the URL.

Example:
http://test.com/myscript.php?name=ian&age=16

$_GET variable will contain the index 'name' with value 'ian' and index 'age' with value '16'.

To get the same thing using CLI, we use the following code:

function ArgsToGet($argv)
{
$gets = explode('&', $argv[1]);
foreach($gets as $g)
{
$g = explode('=', $g);
$_GET[$g[0]] = $g[1];
}
}

var_dump($argv);
ArgsToGet($argv);
var_dump($_GET);

?>

Example:
[ian@mycomputer]$ /usr/bin/php myscript.php 'name=ian&age=16'

Using the code above, the command will output will be
array(2) {
[0]=>
string(8) "test.php"
[1]=>
string(15) "name=ian&age=16"
}
array(2) {
["name"]=>
string(3) "ian"
["age"]=>
string(2) "16"
}

The variable $_GET will have the values same as it was when using a browser.