Upgrade to Pro — share decks privately, control downloads, hide ads and more …

I wrote some code...you won't believe what happened next

I wrote some code...you won't believe what happened next

Have you ever been confused about why the code you wrote doesn’t do what you expect? Are you learning to code and don’t want to get caught out? Many people learn to code by example without understanding why the code works – or doesn’t! I hope to inspire and equip you to learn more about what your code is doing.

I will use interactive examples and plain language to explain how learning coding principles can help debugging, help you understand documentation, and make you a better developer. And we’ll learn some basic but invaluable principles that will help de-mystify your code and solve problems faster.

Ross Wintle

April 07, 2019
Tweet

More Decks by Ross Wintle

Other Decks in Technology

Transcript

  1. +

  2. To help you • Code better • Fix problems faster

    • Learn more quickly • Understand documentation • Become curious!
  3. Expressions • 2 • "Hello World!" • $home or home

    • [ 1, 2, 3 ] • { post_title: "Programming principles" } • $posts[0] • $post->post_title or post.post_title
  4. Expressions • 2 • "Hello World!" • [ 1, 2,

    3 ] • { post_title: "Programming principles" } Literals: Exactly what you type
  5. Expressions • $home or home • $posts[0] • $post->post_title or

    post.post_title Variables: Names for other things
  6. Combining Expressions "The first landing was " + currentYear -

    moonLandingYear + " years ago" Expression Expression Operator Operator Expression Operator Expression Operator Expression Expression Operator Expression Expression Operator Expression Expression
  7. Statements Control flow/order/execution if ( $have_posts() ) {
 the_post();
 ...


    } while ( $have_posts() ) {
 the_post();
 ...
 } foreach ( $posts as $post ) {
 echo $post->post_title;
 }
  8. Putting it all together if ( bored_now() and hometime() )

    { Expression Expression Operator Expression exit; } Statement Statement
  9. I wrote this code… $a is [1,2] … but what

    is $b? $a = [1,2,3]; $b = $a; array_pop($a); (PHP)
  10. I wrote this code… $a is [1,2] … but what

    is $b? $a = [1,2,3]; $b = $a; array_pop($a); (PHP)
  11. I wrote this code… $a is [1,2] … but what

    is $b? $a = [1,2,3]; $b = $a; array_pop($a); (PHP)
  12. I wrote this code… $a is [1,2] … but what

    is $b? $a = [1,2,3]; $b = $a; array_pop($a); (PHP)
  13. I wrote this SAME code… a is [1,2] … what

    is b? var a = [1,2,3]; var b = a; a.pop(); (JavaScript)
  14. References (with objects) Which side is anakin? var anakin =

    { side: "light" }; var vader = anakin; vader.side = 'dark' (JavaScript)
  15. I wrote this code… …what happened next? $number = 1;

    if ($number = 5) { echo “It’s five”; } (PHP)
  16. I wrote this code… …what happened next? $number = 1;

    if ($number = 5) { echo “It’s five”; } (PHP)
  17. I wrote this code… …what happened next? $number = 1;

    if ($number = 5) { echo “It’s five”; } (PHP)
  18. I wrote this code… …what happened next? $number = 1;

    if ($number = 5) { echo “It’s five”; } (PHP)
  19. $number = 1; if ( 5 == $number ) {

    echo “It’s 5”; }
  20. $number = 1; if ( 5 = $number ) {

    echo “It’s 5”; } PHP Parse error: Syntax error, unexpected '='
  21. $number = 1; if ( 5 = $number ) {

    echo “It’s 5”; } Yoda notation
  22. Types in programming Numbers 0 1 2 3 0 -1

    -2 -3 0 0.1 0.2 0.3 Strings “Hello world” “Ross” “WordCamps are amazing” “42” Booleans true false In PHP: • Integers • Floats
  23. Types in programming Objects Arrays Make your own types Lists

    of things Note: In JavaScript Arrays are a kind of object
  24. Types Values have types, not variables In some other languages


    VARIABLES are typed $number
 (Integer) “Hello World”
  25. Types Values have types, not variables In PHP and JS


    VALUES are typed $number “Hello World” (string) 42 (integer)
  26. Anything to a number Number("5") Explicit (manual) type conversion Anything

    to a string String(5) Anything to a Boolean Boolean(5) Many other ways! JavaScript Better string to number:
 parseInt("3.141", 10)
 parseFloat("3.141")
  27. Anything to a whole number (int)”3.141” Explicit (manual) type conversion

    Anything to a decimal number (float)”3.141” Anything to a Boolean (bool)”yes” Many other ways! PHP Anything to a string
 
 (string)3.141
  28. Checking types • typeof thing
 (JavaScript) • getType(thing)
 (PHP) •

    is_bool() / is_int() / is_string() / is_array() (PHP)
  29. Comparing types a == b compare and let the language

    sort the types out a === b compare both values AND types are the same
  30. Types • Be aware! • Know how to test or

    check the type • Know how to convert types • Know the difference between `==` and `===`
  31. I wrote this code… …what happened next? "I am "

    + 2019 - 2000 + " years old" (JavaScript)
  32. "I am " + 2019 - 2000 + " years

    old" Operator Associativity
  33. ("I am " + 2019) - 2000 + " years

    old" Operator Associativity
  34. Anatomy of a function function getPostTitle( $post ) { $title

    = $post->post_title; return $title; } Name
  35. Anatomy of a function function getPostTitle( $post ) { $title

    = $post->post_title; return $title; } Parameter(s)
  36. Anatomy of a function function getPostTitle( $post ) { $title

    = $post->post_title; return $title; } Function code
  37. Anatomy of a function function getPostTitle( $post ) { $title

    = $post->post_title; return $title; } Returned value (output)
  38. Anatomy of a function function getPostTitle( $post ) { $title

    = $post->post_title; return $title; } Name Parameter(s) Function code Returned value (output)
  39. I wrote this code… function doubleAll( $array ) { foreach(

    $array as $index => $item ) { $array[$index] = $item * 2; } } (PHP)
  40. I wrote this code… function doubleAll( $array ) { foreach(

    $array as $index => $item ) { $array[$index] = $item * 2; } } (PHP) $a = [1,2,3]; doubleAll($a);
  41. I wrote this code… function doubleAll( $array ) { foreach(

    $array as $index => $item ) { $array[$index] = $item * 2; } } (PHP) $a = [1,2,3]; doubleAll($a);
  42. I wrote this code… function doubleAll( $array ) { foreach(

    $array as $index => $item ) { $array[$index] = $item * 2; } } (PHP) $a = [1,2,3]; doubleAll($a); …what is $a?
  43. Parameters work like assignment function doubleAll( $array ) { foreach(

    $array as $index => $item ) { $array[$index] = $item * 2; } }
 $a = [1,2,3]; doubleAll($a);
  44. function doubleAll( $array ) { foreach( $array as $index =>

    $item ) { $array[$index] = $item * 2; } }
 $a = [1,2,3]; doubleAll($a); Parameters work like assignment
  45. function doubleAll( $array ) { foreach( $array as $index =>

    $item ) { $array[$index] = $item * 2; } }
 $a = [1,2,3]; doubleAll($a); Parameters work like assignment
  46. function doubleAll( $array ) { foreach( $array as $index =>

    $item ) { $array[$index] = $item * 2; } return $array; }
 $a = [1,2,3]; $a = doubleAll($a); Returns are important!
  47. We learned… Expressions Statements Operators Precedence Associativity Variables Assignment References

    Values Literals Types Converion/Casting/Coercion Functions Returns Parameters Scope
  48. Be curious! PHP: • php -a • psysh • https://3v4l.org/

    • wp-cli shell Tools for tinkering JavaScript: • Dev tools • JSFiddle/JSBin • node