building high productivity applications

24
Talk: Building High Productivity Applications September 17, 2016 Hutomo Sugianto Tech Evangelist Kudo Teknologi Indonesia e: [email protected]

Upload: hutomo-sugianto

Post on 16-Apr-2017

211 views

Category:

Engineering


1 download

TRANSCRIPT

Page 1: Building high productivity applications

Talk: Building High Productivity ApplicationsSeptember 17, 2016

Hutomo SugiantoTech EvangelistKudo Teknologi Indonesiae: [email protected]

Page 2: Building high productivity applications

Concept: Software System vs Software

Software/ Application

Software “System”

Architecture/ Construction

Interface and Integration

Page 3: Building high productivity applications

Common problems in Software Development Term

Background

Bad Code:Hard to understand

Poor Collaboration

Code Complexity

Page 4: Building high productivity applications

One of the solutions

Crystal Clear:Easy to understand

High Quality Code

Improve Collaboration

Minimum Complexity

Building High Productivity Application

Page 5: Building high productivity applications

Outline

Control Structure: What

Control Issues

Dealing with Complexity: Why Control Structure

High Productivity Applications

Page 6: Building high productivity applications

Control StructureA 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!

Page 7: Building high productivity applications

Control StructureBig contributor to overall program complexity.

Conditionals/selection:

● if

● if/else

●switch

Loop/repetition:

●while

●do/while

●for, foreach

Page 8: Building high productivity applications

Control Issues ●Boolean Expressions

●Taming Dangerously Deep Nesting

Page 9: Building high productivity applications

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();}

Page 10: Building high productivity applications

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 }

Page 11: Building high productivity applications

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

}

Page 12: Building high productivity applications

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 thanif($userRegistered AND $userVerified AND ($spam == false)){// put code here}

Page 13: Building high productivity applications

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.

Page 14: Building high productivity applications

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 0while ( $balance != 0 ) {

// put code here}

// rather thanwhile ( $balance ) {

// put code here}

Page 15: Building high productivity applications

Taming Dangerously Deep NestingExcessive 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.

Page 16: Building high productivity applications

Taming Dangerously Deep Nesting1. 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);

Page 17: Building high productivity applications

Taming Dangerously Deep Nesting2. 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 exampleif ( 10 < $quantity ) {

if ( 100 < $quantity ) {if ( 1000 < $quantity

) {$discount =

0.10;}else {

$discount = 0.05;

}}else {

$discount = 0.025;}

}else {

$discount = 0.0;}

Page 18: Building high productivity applications

Taming Dangerously Deep Nesting2. 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 exampleif ( 1000 < quantity ) {

discount = 0.10;}else if ( 100 < quantity ) {

discount = 0.05;}else if ( 10 < quantity ) {

discount = 0.025;}else {

discount = 0;}

Page 19: Building high productivity applications

Taming Dangerously Deep Nesting3. 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;}

Page 20: Building high productivity applications

High Productivity Applications

●Why Control Structures

●Minimum Complexity = Improve Collaboration

Page 21: Building high productivity applications

Why Control StructureOne 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.

Page 22: Building high productivity applications

Build High Productivity Application

Remember this?

Crystal Clear:Easy to understand

High Quality Code

Improve Collaboration

Minimum Complexity

Page 23: Building high productivity applications

Further ReadingKindly 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/

Page 24: Building high productivity applications

Make things as simple as possible—but no simpler.—Albert Einstein