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

Learn how to be a developer: StepTwo - Variable...

Learn how to be a developer: StepTwo - VariablesAndFunctions

The second part on my A Journey Of A Thousand Miles talks on how to become a Software Developer. This course is aimed for people who know absolutely nothing about developing but are eager to learn. The talks are aimed at giving people a no nonsese approach to learning.

Ricky Clegg

June 20, 2012
Tweet

More Decks by Ricky Clegg

Other Decks in Technology

Transcript

  1. {StepTwo: VariablesAndFunctions} This  is  the  second  part  in  the  ‘A

     Journey  Of  A  Thousand  Miles’   Series     Func7ons  and  Variables  are  the  building  blocks  of  everything  you   will  be  wri:ng  in  your  so:ware  solu7ons.  So  we  be<er  learn  what   they  are  and  what  we  can  do  with  them.   [email protected]          twi<er.com/ricky_clegg
  2. In this lesson we will learn all about Variables and

    Functions. What Variables are. What Functions actually do.
  3. Primitive types are the basic building blocks of programming languages.

    All data is a type of something. My age is 27 which is an integer and my name is ‘Ricky’ which is a string.
  4. Boolean   This  can  only  have  two  values.  True  or

     False.     String   In  most  languages  a  String  is  a  value  in  quotes.  E.G  “Hello  World”.   Just  think  of  it  as  words.  Any:me  your  data  is  a  word,  it  is  a  String   in  quotes.     Number   Is  a  numerical  value.  10  is  a  number.  A  10  wrapped  in  quotes  ‘10’   is   a   String   of   the   word   ten,   not   a   number.   This   is   a   common   problem  in  dynamic  languages  like  JavaScript.  10  +  ’10’  is  equal  to   ‘1010’  not  20.   [email protected]          twi<er.com/ricky_clegg {BasicDataTypes}
  5. A  Variable  is  a  like  a  container  for  data  in

     your  program.     In  Java  you  declare  a  variable  like  so:   String firstName = “Ricky”;   •   The    first  part  “String”  is  the  type  of  data  the  variable  contains.   •   The  second  part  “firstName”  is  the  name  or  id  of  the  variable.   •   The  “=“  is  an  operator  to  assign  a  value.   •   “Ricky”  is  the  value  we  are  making  firstName  equal  to.   •   Finally  the  “;”  is  how  you  signify  you  are  ending  the  line.   [email protected]          twi<er.com/ricky_clegg {VariableVariables}
  6. Look  at  the  Java  variable  below:     String age

    = 27;   Can  you  see  the  problem?   [email protected]          twi<er.com/ricky_clegg {VariableWeHaveAProblem}
  7. If  we  run  this  program  we  will  get  what  is

     known  as  a  compiler   error.  Which  is  the  compiler  no:fying  us  there  is  a  problem  before   we  have  even  run  the  program.     In  a  strongly  typed  language  we  can  specify  exactly  what  type  of   data  a  variable  can  contain.  We  have  told  the  compiler  that  “age”   should   be   a   String.   However   we   have   tried   to   assign   a   Number   value.     A   String   is   surrounded   by   quotes   (age   =   “27”).   A   Number   is   a   numerical  value  with  no  quotes.     Later  in  the  course  we  will  look  at  how  we  turn  String  “27”  into  a   Number  27.   [email protected]          twi<er.com/ricky_clegg {SorryYoureJustNotMyType}
  8. A Compiler is a computer program that converts your source

    code into a new format. For example the Flex compiler converts your ActionScript into a SWF file.
  9. A STRONGLY TYPED language helps eradicate errors. It informs other

    developers your intent for a variable. In a strongly typed language it is good practice to specify the type of every single variable.
  10. Allowing   access   to   your   variables  

    “Encapsula7on”  is  one  of  the  keys  to   OOP.     Most  languages  have  the  concept  of   Global  and  Local  variables.     A  global  variable  can  be  accessed  by   your   code   at   any   7me   from   any   place.   [email protected]          twi<er.com/ricky_clegg {AWorldOfVariables} This  means  a  local  variable  is  only  available  at  certain  7mes  from  certain   loca7ons.  
  11. As  discussed  a  variable  acts  like  a  container.  This  is

     true  but  only   for  primi7ve  types.       [email protected]          twi<er.com/ricky_clegg {NoRoomAtTheInn}
  12. When  you  set  a  variables  value  to  an  object  it

     creates  a  reference   to   the   Object,   rather   than   containing   it.   This   means   you   can   reference  the  same  object  with  another  variable.     Person rickyClegg = new Person(); Person courseTutor = rickyClegg; [email protected]          twi<er.com/ricky_clegg {CreateAReference}
  13. Now we know quite a bit about variables and what

    we can put in them, we need to know where we can use them.
  14. A  func7on  or  otherwise  known  as  a  method  is  a

     block  of  code  that   performs  a  task.     Here  is  some  JavaScript:     function calculateTax(pay) { return pay * 0.2; } You  can  execute  (call)  a  func:on  as  many  7mes  as  you  want,  and  it  will   keep  running  the  code.   [email protected]          twi<er.com/ricky_clegg {MethodOrMadness}
  15. You can put any number of variables or calculations in

    a function. But if you want your code to be clean and readable we recommend not using more than 6 lines of code in a function.
  16. No  we  are  not  killing  our  code.  We  are  geUng

     the  data  stored  within  a  variable   or  a  func:on.     To  get  the  value  of  a  variable  you  simply  name  it.     myVar;   Func:ons  work  differently  and  you  need  to  execute  the  func:on  using  “(  )”.     The  func:on  calculatorTax  we  saw  a  few  slides  before  is  executed  like  so:     calculateTax(20000);   The  body  of  the  func:on  will  now  run.  No  code  aXer  this  call  will  be  run  un:l   the  func:on  has  finished.     All  func:ons  are  executed  with  (  )  without  them  you  are  talking  to  the  func:on   like  it  is  a  variable.   [email protected]          twi<er.com/ricky_clegg {Execution}
  17. Programming   languages   contain   keywords   or  

    reserved   words.   These  words  you  are  not  able  to  use  as  your  func:on  or  variables   names,  because  they  have  a  specific  meaning  in  the  language.   You  are  not  about  to  call  your  func:on  “var”  as  the  compiler  will   not  know  what  to  do.   function var() {} // ERROR [email protected]          twi<er.com/ricky_clegg {ThisSeatIsTaken}
  18. Here   is   a   list   of  

    words   that   for   reserved   for   the   JavaScript   Language:   [email protected]          twi<er.com/ricky_clegg {ReservedWords} •  break •  case •  catch •  continue •  debugger •  default •  delete •  do •  else •  finally •  for •  function •  if •  in •  instanceof •  new •  return •  switch •  this •  throw •  try •  typeof •  var •  void •  while •  with
  19. Return  is  a  very  useful  keyword  used  with  func:ons  to

     send  data   back  to  the  caller.       Here  is  some  global  JavaScript:     function getVersion() { return “1.0.0”; } var appVersion = getVersion(); The   data   can   then   be   stored   in   a   variable   for   you   to   use   elsewhere.   [email protected]          twi<er.com/ricky_clegg {ReturnToSender}
  20. [keyword] [name]([arguments]) { } Programming  languages  have  reserved  words  that

     you  cannot  use   in   your   code.   These   words   differ   between   languages   and   have   different  ac:ons.     var  is  a  reserved  word  in  JavaScript  for  declaring  a  variable.   return  is  a  reserved  word  for  passing  data  back  to  the  caller  of  a   func:on.   [email protected]          twi<er.com/ricky_clegg {FunctionSyntax}
  21. Functions have the ability to set data within their block

    and to return data. To keep our code clean we use Command Query Separation. This means if a function returns a value it should not change the state of the application. If it changes the state of the application it should NOT return a value.
  22. No  this  is  nothing  to  do  with  your  bad  temper!

        Func:ons  can  be  given  variables  to  make  them  dynamic.  This  is  data  that  you   can  use  in  our  func:on  body  to  achieve  a  goal.     Looking  back  at  our  calculator  tax  example:   function calculateTax(pay) { return pay * 0.2; }   We  pass  in  the  argument  pay.  With  arguments  we  do  not  have  to  use  the  var   keyword  to  declare  them.     Parentheses  signify  a  group.    Items  are  separated  by  a  comma  syntax.     function calculateTax(pay, taxPercentage) { return pay * taxPercentage; }       [email protected]          twi<er.com/ricky_clegg {Arguments}
  23. Three is the magic number. Keep your code clean and

    easily readable by NEVER using more than three arguments. Even when those around you are using more. Stick to THREE
  24. Just  like  variables  we  can  data  type  our  func:ons  in

     strongly  typed  languages.     Java:   public float function calculateTax(float pay, float taxPer) { return pay * 0.2; }   Ac7onScript:   public function calculateTax(pay:Number, taxPer:Number):Number { return pay * 0.2; } Execu:ng  func:ons  in  both  these  languages  is  the  same  and  passing  parameters   to  a  func:on  is  the  same  as  we  declare  them,  with  a  comma  separated  list.   var taxPaid = calculateTax(20000, 17.5); Parameters  (arguments)  must  be  passed  in  the  correct  order.         [email protected]          twi<er.com/ricky_clegg {WhatAreWeArguingAbout?}
  25. When  calcula7ng  with  numbers  in  most  modern  languages,  we  are

     given  a  list   of  reserved  types.     These  are  basically  exactly  as  you  would  expect  from  a  calculator.      +    Addi:on    -­‐  Subtrac:on    *  Mul:plica:on    /  Division    %  Modulus  (Division  Remainder)    ++  Increment  (NEVER  USE  THIS)    -­‐-­‐  Decrement  (NEVER  USE  THIS)     You  can  group  mathema:cal  execu:ons  with  (  )  and  they  will  be  calculated  first.     var num = 10 + 35 + (26 – 10);       [email protected]          twi<er.com/ricky_clegg {MathematicalOperators}
  26. This is the end of lesson two. This session has

    explained all about the basic forms of your code. Variables and simple functions are now familiar to you... You can now code some global JavaScript.