1. Talk: Building High Productivity
Applications
September 17, 2016
Hutomo Sugianto
Tech Evangelist
Kudo Teknologi Indonesia
e: hutomo@kudo.co.id
2. Concept: Software System vs Software
Software/
Application
Software
“System”
Architecture/
Construction
Interface and
Integration
3. Common problems in Software Development Term
Background
Bad Code:
Hard to understand
Poor
Collaboration
Code
Complexity
4. One of the solutions
Crystal Clear:
Easy to understand
High Quality
Code
Improve
Collaboration
Minimum
Complexity
Building High Productivity Application
6. Control Structure
A control structure is a block of programming that analyzes variables and chooses
a direction in which to go based on given parameters.
● Code Flow - top to bottom
● Hit a point where it needs to make a decision
● Strict set of rules to decide which direction to go
● So, this decision that must be made, that will in turn effect the flow of code, is
known as a control structure!
7. Control Structure
Big contributor to overall program complexity.
Conditionals/selection:
● if
● if/else
● switch
Loop/repetition:
● while
● do/while
9. Boolean Expressions
● Use True or False for Boolean
Tests
Use the identifiers true and false
in boolean expressions rather
than using values like 0 and 1.
Bad Example:
<?php
$printerError = true;
if ($printerError == 0)
{
initializePrinter();
}
if ($printerError == 1)
{
notifyUserOfError();
}
10. Boolean Expressions
● Compare boolean values to true
and false implicitly
You can write clearer tests by
treating the expressions as
boolean expressions
<?php
// this code
while (!$done ) {
// put code here
}
while ( $a > $b ) {
// put code here
}
// is better than
while ( $done = false ) {
// put code here
}
while ( ($a > $b) = true ) {
// put code here
}
11. Boolean Expressions
● Making complicated boolean
expression simple
1. Break complicated tests into
partial tests with new boolean
variables
<?php
// this code
function status($value = false){
return $value;
}
$status = status((3+(5^2))*0);
if($status) {
// put code here
}
// is better than
if(status((3+(5^2))*0)){
// put code here
}
12. Boolean
Expressions
● Making complicated
boolean expression
simple
2. Move complicated
expression into boolean
functions
<?php
// this code
function userStatus($registered, $verified, $spam){
if($registered AND $verified AND !$spam){
return true;
}
else {
return false;
}
}
if(userStatus(true, false, true)){
// put code here
}
// is better than
if($userRegistered AND $userVerified
AND ($spam == false))
{
// put code here
}
13. Boolean Expressions
● Guidelines for Comparisons to 0
Programming languages use 0 for several purposes. It’s a numeric value. It’s a
null terminator in a string. It’s false in logical expressions. Because it’s used for
so many purposes, you should write code that highlights the specific way 0 is
used.
14. Boolean Expressions
● Guidelines for Comparisons to 0
1. Compare logical variables
implicitly
2. Compare numbers to 0
you should compare numeric
expressions explicitly.
<?php
// 1. Compare logical variables
// implicitly
while ( !$done ) {
// put code here
}
// Compare numbers to 0
while ( $balance != 0 ) {
// put code here
}
// rather than
while ( $balance ) {
// put code here
}
15. Taming Dangerously Deep Nesting
Excessive indentation, or “nesting,” has been pilloried in computing literature for
25 years and is still one of the chief culprits in confusing code.
Deep nesting works against Managing Complexity. That is reason enough to avoid
deep nesting.
It’s not hard to avoid deep nesting. If you have deep nesting, you can redesign the
tests performed in the if and else clauses or you can refactor code into simpler
routines.
16. Taming Dangerously Deep Nesting
1. Simplify a nested if by using a
break block.
This technique is uncommon enough
that it should be used only when your
entire team is familiar with it and
when it has been adopted by the
team as an accepted coding practice.
<?php
function validateInput($input = [])
{
if(empty($input[‘name’])){
return false;
}
if(empty($input[‘email’])){
return false;
}
}
$input = [‘name’ => ‘CodeSaya’];
validateInput($input);
17. Taming Dangerously Deep Nesting
2. Convert a nested if to a set of if-
then-elses
Suppose you have a bushy decision
tree like this:
This test is poorly organized in several ways,
one of which is that the tests are redundant.
<?php
// bad example
if ( 10 < $quantity ) {
if ( 100 < $quantity ) {
if ( 1000 < $quantity
) {
$discount =
0.10;
}
else {
$discount =
0.05;
}
}
else {
$discount = 0.025;
}
}
18. Taming Dangerously Deep Nesting
2. Convert a nested if to a set of if-
then-elses
This solution is easier than some
because the numbers increase neatly
(simple but clever way).
<?php
// good example
if ( 1000 < quantity ) {
discount = 0.10;
}
else if ( 100 < quantity ) {
discount = 0.05;
}
else if ( 10 < quantity ) {
discount = 0.025;
}
else {
discount = 0;
}
19. Taming Dangerously Deep Nesting
3. Convert a nested if to a
case statement
You can recode some kinds
of tests, particularly those
with integers, to use a case
statement rather than chains
of ifs and elses.
<?php
switch(true){
case ($quantity >=0 AND $quantity <= 10):
$discount = 0.0;
break;
case ($quantity >= 11 AND $quantity <= 100):
$discount = 0.025;
break;
case ($quantity >= 101 AND $quantity <= 1000):
$discount = 0.05;
break;
default:
$discount = 0.10
Break;
}
21. Why Control Structure
One reason so much attention has been paid to control structures is that they are
a big contributor to overall program complexity. Poor use of control structures
increases complexity; good use decreases it.
Intuitively, the complexity of a program would seem to largely determine the
amount of effort required to understand it.
22. Build High Productivity Application
Remember this?
Crystal Clear:
Easy to understand
High Quality
Code
Improve
Collaboration
Minimum
Complexity
23. Further Reading
Kindly visit us on developers.kudo.co.id
http://php.net/manual/en/language.control-structures.php
https://howtoprogramwithjava.com/the-5-basic-concepts-of-any-programming-
language-concept-2/
24. Make things as simple as possible—but no simpler.
—Albert Einstein