building high productivity applications
TRANSCRIPT
Talk: Building High Productivity ApplicationsSeptember 17, 2016
Hutomo SugiantoTech EvangelistKudo Teknologi Indonesiae: [email protected]
Concept: Software System vs Software
Software/ Application
Software “System”
Architecture/ Construction
Interface and Integration
Common problems in Software Development Term
Background
Bad Code:Hard to understand
Poor Collaboration
Code Complexity
One of the solutions
Crystal Clear:Easy to understand
High Quality Code
Improve Collaboration
Minimum Complexity
Building High Productivity Application
Outline
Control Structure: What
Control Issues
Dealing with Complexity: Why Control Structure
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!
Control StructureBig contributor to overall program complexity.
Conditionals/selection:
● if
● if/else
●switch
Loop/repetition:
●while
●do/while
●for, foreach
Control Issues ●Boolean Expressions
●Taming Dangerously Deep Nesting
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();}
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 }
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
}
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}
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.
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}
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.
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);
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;}
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;}
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;}
High Productivity Applications
●Why Control Structures
●Minimum Complexity = Improve Collaboration
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.
Build High Productivity Application
Remember this?
Crystal Clear:Easy to understand
High Quality Code
Improve Collaboration
Minimum Complexity
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/
Make things as simple as possible—but no simpler.—Albert Einstein