User Tools

Site Tools


ch03-places-to-put-things

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
ch03-places-to-put-things [2017/08/24 00:58]
mithat [Identifier rules]
ch03-places-to-put-things [2017/08/24 02:03] (current)
mithat [Potential pitfalls]
Line 150: Line 150:
 <file java variables3b.pde>​ <file java variables3b.pde>​
 void setup () { void setup () {
-  int rec_width+  int rect_width
-  int rec_height;+  int rect_height;
   ​   ​
-  ​rec_height ​= 33; +  ​rect_height ​= 33; 
-  ​rec_width ​rec_height ​+ 20;+  ​rect_width ​rect_height ​+ 20;
   ​   ​
-  rect(0, 0, rec_widthrec_height);+  rect(0, 0, rect_widthrect_height);
 } }
 </​file>​ </​file>​
  
-The only difference in the the three versions of the program is the names we have given to the variables; the programs are functionally identical. However, of the three versions a lot of programmers would probably prefer ''​%%variables3b.pde%%''​ because the variables'​ names actually describe what they are used for--they are **descriptive identifiers**. ​Using descriptive identifiers isn't a language rule---you ​are free to use descriptive identifiers or not. However, they make reading and understanding your code much easier. And the easier your code is to read, the easier it will be for someone (even you) to fix and expand it.+The only difference in the the three versions of the program is the names we have given to the variables; the programs are functionally identical. However, of the three versions a lot of programmers would probably prefer ''​%%variables3b.pde%%''​ because the variables'​ names actually describe what they are used for---they are **descriptive identifiers**. ​You are free to use descriptive identifiers or not. However, they make reading and understanding your code //much// easier. And the easier your code is to read, the easier it will be for someone (even you) to fix and expand it.
  
-Closely related to this are **coding conventions**---mutually agreed upon standard practices for writing code. Coding conventions are not rules; rather they are standard practices that have emerged over time and/or have been agreed upon ahead of time by a team of programmers. A common ​coding convention for variable names in Processing is to use CamelCase (i.e., capitalizing ​the first letter of adjacent words) with the first letter of the first word in lower case. Rewriting the example we have been working with so far using the CamelCase convention would yield:+Closely related to this are **coding conventions**---mutually agreed upon standard practices for writing code. Coding conventions are not language ​rules; rather they are standard practices that have emerged over time and/or have been agreed upon ahead of time by a team of programmers. A popular ​coding convention for variable names in Processing is to capitalize ​the first letter of adjacent words but keep the first letter of the first word in lower case. This format is sometimes referred to as //​CamelCase//​. Rewriting the example we have been working with so far using the CamelCase convention would yield:
  
 <file java variables3c.pde>​ <file java variables3c.pde>​
 void setup () { void setup () {
-  int recWidth+  int rectWidth
-  int recHeight;+  int rectHeight;
   ​   ​
-  ​recHeight ​= 33; +  ​rectHeight ​= 33; 
-  ​recWidth ​recHeight ​+ 20;+  ​rectWidth ​rectHeight ​+ 20;
   ​   ​
-  rect(0, 0, recWidthrecHeight);+  rect(0, 0, rectWidthrectHeight);
 } }
 </​file>​ </​file>​
  
-An alternative convention ​(used in program ​''​%%variables3b.pde%%''​) might be to use all lower case letters for variable names and use the underscore character to separate words. CamelCase is the defacto standard with Processing ​and what I will use in the remainder of this text.+An alternative convention ​is often referred to as //snake case//: using all lower case letters for variable names with the underscore character separating words. This format was used in programs ​''​%%variables3a.pde%%''​ and ''​%%variables3b.pde%%''​ aboveSince CamelCase is the defacto standard with Processing, we will use in the remainder of this text.
  
 ===== Data types ===== ===== Data types =====
Line 183: Line 183:
  
 <code java> <code java>
-int recWidth+int rectWidth
-int recHeight;+int rectHeight;
 </​code>​ </​code>​
-are variable declarations---they announce to the Processing compiler that you plan to use variables with names ''​%%recWidth%%''​ and ''​%%recHeight%%''​. So, what does ''​%%int%%'' ​mean? Also, we noted earlier ​that variables in many programming languages, including Processing, can store data that isn't numeric. ​How does that work? To answer both of these questions, we need to know about **data types**.+are variable declarations---they announce to the Processing compiler that you plan to use variables with names ''​%%rectWidth%%''​ and ''​%%rectHeight%%''​. So, you might be wondering ​what that ''​%%int%%'' ​means. ​Also, you might remember ​that we said variables in many programming languages, including Processing, can store data that isn't numeric. ​You might be wondering how that works. ​To answer both of these questions, we need to learn about **data types**.
  
-Most programming languages, including ​Processing, are **typed ​languages**. A typed language is one that differentiates between different types of data: an integer ​number is one type of data, a character such as '​r'​ is a different type of data, and so on. Typed languages typically predefine a number of data types and let you define additional types.+Processing ​is what programmers call a **typed ​language**. A typed language is one that differentiates between different types of data: number is one type of data, a character such as '​r'​ is a different type of data, and so on. Typed languages typically predefine a number of data typesand they usually ​let you define additional ​custom ​types.
  
-In Processing, when you declare ​a variable, ​you must also state //the type of data// you will store in the variable. You are free to change ​the //value// of the data in a Processing variable whenever you want. Howeveryou are not free to change ​the //type// of data you store in it.((Some typed languages can automatically change the type of data stored in a variable. In other words, sometimes a given variable might be used to store a number and at another time to store a character or some other type of data. Processing isn't one of those. +When Processing ​creates ​a variable, ​that variable will only hold one type of data for its entire life. The value stored ​in the variable ​can change, ​but the type of the value cannot.((Some typed languages can automatically change the type of data stored in a variable. In other words, sometimes a given variable might be used to store a number and at another time to store a character or some other type of data. Processing isn't one of those.)) ​So, when you declare a variable in Processing, one of the things you need to do is indicate //the type of data// you will store in the variable.
-))+
  
 The syntax for declaring a variable in Processing is: The syntax for declaring a variable in Processing is:
Line 198: Line 197:
 <​data-type-of-variable>​ <​variable-identifier>;​ <​data-type-of-variable>​ <​variable-identifier>;​
 </​code>​ </​code>​
-To declare a variable named ''​%%numberOfApples%%''​ that will be used to store integer data, you would use the statement:+To declare a variable named ''​%%numberOfApples%%''​ that will be used to store a whole number, you would use the statement:
  
 <code java> <code java>
 int numberOfApples;​ int numberOfApples;​
 </​code>​ </​code>​
-The identifier ''​%%int%%''​ is a special Processing term (a **keyword**) that is used to identify the integer data type. You might remember from mathematics that an integer is any number (positive, negative, or zero) that has no fractional part (..., -3, -2, -1, 0, 1, 2, ...). It has effectively the same meaning in Processing. We'll discuss some of the other data types available in Processing shortly.+The identifier ''​%%int%%''​ is a special Processing term (a **keyword**) that is used to identify the //integer// data type. You might remember from mathematics that an integer is any number (positive, negative, or zero) that has no fractional part (..., -3, -2, -1, 0, 1, 2, ...). It has effectively the same meaning in Processing. We'll discuss some of the other data types available in Processing shortly.
  
 When you declare variables in a language like Processing, you are actually doing three things. You are: When you declare variables in a language like Processing, you are actually doing three things. You are:
  
   * Telling the system that you plan to use a chunk of computer memory to store some stuff (i.e., the "​box"​ inside which you store values).   * Telling the system that you plan to use a chunk of computer memory to store some stuff (i.e., the "​box"​ inside which you store values).
-  * Binding an identifier (i.e., ​give a name to) to the box.+  * Binding an identifier (i.e., ​giving ​a name to) to the box.
   * Telling the system what type of data it can put into the box.   * Telling the system what type of data it can put into the box.
  
Line 222: Line 221:
 === int === === int ===
  
-The ''​%%int%%''​ type is used to represent integer values---any number that has no fractional part (..., -3, -2, -1, 0, 1, 2, ...). In mathematics,​ integer values have no upper or lower limit; in Processing they do. An int cannot be smaller than -2,​147,​483,​648 nor be larger than 2,​147,​483,​647. ​An int occupies 32 bits in memory. Example:+The ''​%%int%%''​ type is used to represent integer values---any number that has no fractional part (..., -3, -2, -1, 0, 1, 2, ...). In mathematics,​ integer values have no upper or lower limit; in Processing they do. An int cannot be smaller than -2,​147,​483,​648 nor be larger than 2,​147,​483,​647. ​If you're curious about such things, an int occupies 32 bits in memory. 
 + 
 +Example:
  
 <code java> <code java>
Line 228: Line 229:
 numBeers = 99; numBeers = 99;
 </​code>​ </​code>​
 +
 === long === === long ===
  
-A ''​%%long%%''​ is identical to an ''​%%int%%''​ except that the limits are wider: -9,​223,​372,​036,​854,​775,​808 to 9,​223,​372,​036,​854,​775,​807. So why would you want to use ''​%%int%%''​ when you've also got ''​%%long%%''​ available? Because ''​%%long%%''​ variables occupy 64 bits of memory, twice as much as an ''​%%int%%''​. **TODO verify:** Also, on most platforms calculations made using ''​%%int%%''​ types will be faster than the same calculations made using ''​%%long%%''​. The ''​%%long%%''​ type is available for your use in Processing if you need it, but it is not used by any of the functions ​available in Processing. Example:+A ''​%%long%%''​ is identical to an ''​%%int%%''​ except that the limits are wider: -9,​223,​372,​036,​854,​775,​808 to 9,​223,​372,​036,​854,​775,​807. So why would you want to use ''​%%int%%''​ when you've also got ''​%%long%%''​ available? Because ''​%%long%%''​ variables occupy 64 bits of memory, twice as much as an ''​%%int%%''​. The ''​%%long%%''​ type is available for your use in Processing if you need it, but it is not used by any of the native Processing ​functions. 
 + 
 +Example:
  
 <code java> <code java>
Line 236: Line 240:
 milesLeftToPluto = 3100000000L;​ milesLeftToPluto = 3100000000L;​
 </​code>​ </​code>​
 +
 When you want to use a ''​%%long%%''​ literal constant, you must add the letter '​L'​ (upper or lower case) after the value. When you want to use a ''​%%long%%''​ literal constant, you must add the letter '​L'​ (upper or lower case) after the value.
  
Line 246: Line 251:
 catLivesLeft = 9; catLivesLeft = 9;
 </​code>​ </​code>​
 +
 === float === === float ===
  
Line 254: Line 260:
 zeroToSixtyTime = 7.2; zeroToSixtyTime = 7.2;
 </​code>​ </​code>​
-Floating point numbers and floating point calculations in computing are full of subtle issues that don't show up in pure math. These issues are due to the fact that in pure math floating point numbers have infinite precision, but in computing the precision is limited. 
  
 Now is a good time to point out that there is a difference between the literal constant ''​%%3.0%%''​ and the literal constant ''​%%3%%''​. The latter specifies a ''​%%float%%''​ literal constant while the latter an ''​%%int%%''​ literal constant. They are different things. Now is a good time to point out that there is a difference between the literal constant ''​%%3.0%%''​ and the literal constant ''​%%3%%''​. The latter specifies a ''​%%float%%''​ literal constant while the latter an ''​%%int%%''​ literal constant. They are different things.
Line 265: Line 270:
 integerThree = 3; integerThree = 3;
 </​code>​ </​code>​
 +
 === double === === double ===
  
 The ''​%%double%%''​ type is identical to the ''​%%float%%''​ except that it has double the precision---and double the memory requirements. The double type is available for your use in Processing if you need it, but it isn't used otherwise. The ''​%%double%%''​ type is identical to the ''​%%float%%''​ except that it has double the precision---and double the memory requirements. The double type is available for your use in Processing if you need it, but it isn't used otherwise.
 +
 +<WRAP center round box 80%>
 +=== Geek break: Floating point numbers ===
 +Floating point numbers calculations that use them in computing are full of subtle issues that don't show up in pure math. As an example, try running this program:
 +
 +<file java floatingpoint.pde>​
 +void setup () { 
 +  double a;
 +  double b;
 +
 +  a = 0.1;
 +  b = 3.0;
 +  ​
 +  println(a * b);
 +}
 +</​file>​
 +
 +The laws of mathematics say the program should print 0.3, but the actual result is different. This odd behavior is caused by two things: (1) in pure math floating point numbers have infinite precision, but in computing floating point precision is limited, and (2) Processing'​s numbers are coded using a base two system (i.e., //binary//) whereas the numbers we humans use are coded in base ten (i.e., //​decimal//​).
 +
 +So, the takeaway from this is that in computing, you should consider floating point as //​approximations//​ to what they are in the math world. For the kinds of things you're likely to do with them for in Processing, this aspect of floating point numbers is unlikely to present a problem. However, there are situations where it does.
 +</​WRAP>​
  
 === char === === char ===
  
-The ''​%%char%%''​ type is used to represent a single alphanumeric or symbolic character---what you might think of as a single letter, number, question mark, etc. A ''​%%char%%''​ occupies 16 bits and uses Unicode encoding--a system that translates characters into numeric codes. Character literal constants are created with single quotes. Examples:+The ''​%%char%%''​ type is used to represent a single alphanumeric or symbolic character---what you might think of as a single letter, number, question mark, etc. A ''​%%char%%''​ occupies 16 bits and uses Unicode encoding, which is a system that translates characters into numeric codes. Character literal constants are created with single quotes. Examples:
  
 <code java> <code java>
Line 291: Line 318:
 integerThree = 3; integerThree = 3;
 </​code>​ </​code>​
 +
 === boolean === === boolean ===
  
Line 299: Line 327:
 isDone = false; isDone = false;
 </​code>​ </​code>​
 +
 === color === === color ===
  
-The ''​%%color%%''​ type is used to represent colors. Opaque colors can be specified using Web color format (e.g., #E300CC). You can also specify a color with an alpha channel (for transparency) in hexadecimal notation (e.g., 0x99E300CC). Examples:+The ''​%%color%%''​ type is used to represent colors. Opaque colors can be specified using Web color format (e.g., ​''​#E300CC''​). You can also specify a color with an alpha channel (for transparency) in hexadecimal notation (e.g., ​''​0x99E300CC''​). Examples:
  
 <code java> <code java>
Line 320: Line 349:
 Processing is an example of a **statically typed** language. In statically typed languages, the type of data that can be stored in a variable cannot change once the variable has been created. The most common way to indicate the data type that can be stored in a variable is through declaration. Other examples of statically types languages are C, C++, C#, and Java. Processing is an example of a **statically typed** language. In statically typed languages, the type of data that can be stored in a variable cannot change once the variable has been created. The most common way to indicate the data type that can be stored in a variable is through declaration. Other examples of statically types languages are C, C++, C#, and Java.
  
-Contrasting with this are **dynamically typed** languages. In dynamically typed languages the type of data that can be stored in the variable is free to change at any time. It can store a number in one statement and a string of characters ​in the next. Since the type of data stored in a variable can change, there'​s no need to explicitly state what type of data you plan to shove into the box. Because of this, many dynamically typed languages will automatically create a variable for you the first time you try to store something in it, and they will automatically change the type associated with the variable as needed. Many programmers are attracted to the simpler syntax that dynamic typing permits. Popular dynamically typed languages include ​Javascript ​and Python.+Contrasting with this are **dynamically typed** languages. In dynamically typed languages the type of data that can be stored in the variable is free to change at any time. It can store a number in one statement and a character ​in the next. Since the type of data stored in a variable can change, there'​s no need to explicitly state what type of data you plan to shove into the box. Because of this, many dynamically typed languages will automatically create a variable for you the first time you try to store something in it, and they will automatically change the type associated with the variable as needed. Many programmers are attracted to the simpler syntax that dynamic typing permits. Popular dynamically typed languages include ​JavaScript ​and Python.
  
-So, dynamically typed languages seem pretty cool. Why don't all languages work that way? One disadvantage of dynamically typed languages is that all the automation associated with them consumes computing resources while the program is running. That means slower execution and/or larger memory requirements. Another, and possibly more important, disadvantage ​is that dynamic typing can be dangerous. Static typing provides an extra safety net by ensuring that new variables are created only when you really wantrather than because you missspeelled a variable that you already created. They also make sure that only code that is designed for a specific type operates on that type. For example, it might make perfect sense to divide two numbers, but it doesn'​t really make sense to divide one string by another string.+So, dynamically typed languages seem pretty cool. Why don't all languages work that way? One disadvantage of dynamically typed languages is that all the automation associated with them consumes computing resources while the program is running. That means slower execution and/or larger memory requirements. Another ​reason ​is that dynamic typing can be dangerous. Static typing provides an extra safety net by ensuring that new variables are created only when you really want rather than because you missspeelled a variable that you already created. They also make sure that only code that is designed for a specific type operates on that type. For example, it might make perfect sense to divide two numbers, but it doesn'​t really make sense to divide one string by another string.
  
 These might seem like small issues when you are just starting programming,​ but you quickly learn to appreciate them when your programs become longer and more complex. These might seem like small issues when you are just starting programming,​ but you quickly learn to appreciate them when your programs become longer and more complex.
Line 330: Line 359:
 ===== Syntactic sugar ===== ===== Syntactic sugar =====
  
-**Syntactic sugar** is any syntax rule that doesn'​t introduce new power or concepts into the language but rather ​makes using existing concepts easier, more compact, or more semantically obvious. Two examples of syntactic sugar in Processing are variable initialization and multiple declarations.+**Syntactic sugar** is any syntax rule that doesn'​t introduce new power or concepts into the language but instead just makes using existing concepts easier, more compact, or more semantically obvious. Two examples of syntactic sugar in Processing are variable initialization and multiple declarations.
  
 ==== Variable initialization ==== ==== Variable initialization ====
Line 339: Line 368:
 int foo = 66; int foo = 66;
 </​code>​ </​code>​
-The above statement declares an integer variable named ''​%%foo%%''​ and gives it an initial value of 66 in one statement. You can initialize any of Processing'​s primitive types this way---as long as the value on the right of the equals sign is compatible with the variable'​s type.+The above statement declares an integer variable named ''​%%foo%%''​ and gives it an initial value of 66 in one statement. You can initialize any of Processing'​s primitive types this wayas long as the value on the right of the equals sign is compatible with the variable'​s type.
  
-If you try to access the value of a variable without initializing it or otherwise giving it a value, the Processing IDE will complain at you. This is because it is widely considered bad programming practice to rely on the default values given to variables by a language. The reason for this is that many languages do not specify //any// default values for variables---at least for some categories. In other words, the "​default"​ value given to a variable you declare might be a random and/or arbitrary value.((The language on which Processing is built, Java, [[http://​docs.oracle.com/​javase/​tutorial/​java/​nutsandbolts/​datatypes.html|//does//]] specify default values for certain kinds of variables. However, the kinds of variables we have been using up to now and will use for most of this text do not fit into that category.+If you try to access the value of a variable without initializing it or otherwise giving it a value, the Processing IDE will complain at you. This is because it is widely considered bad programming practice to rely on the default values given to variables by a language. The reason for this is that many languages do not specify //any// default values for variables---at least for some categories. In other words, the "​default"​ value given to a variable you declare might be a random and/or arbitrary value.((The language on which Processing is built, Java, //[[http://​docs.oracle.com/​javase/​tutorial/​java/​nutsandbolts/​datatypes.html|does]]// specify default values for certain kinds of variables. However, the kinds of variables we have been using up to now and will use for most of this text do not fit into that category.
 )) ))
  
Line 351: Line 380:
 char firstInitial,​ secondInitial;​ char firstInitial,​ secondInitial;​
 </​code>​ </​code>​
-declare two variables---''​%%firstInitial%%''​ and ''​%%secondInitial%%''​---which are both character types. Notice the comma between the two variable names.+declare two variables''​%%firstInitial%%''​ and ''​%%secondInitial%%''​which are both character types. Notice the comma between the two variable names.
  
 You can even combine variable initialization and multiple declaration in the same statement: You can even combine variable initialization and multiple declaration in the same statement:
Line 360: Line 389:
 ==== Potential pitfalls ==== ==== Potential pitfalls ====
  
-Syntactic sugar can be a tricky thing. On the one hand, syntactic sugar makes it easier to write your programs---it increases **writability**. When done right, it also makes programs easier to read--it increases **readability**. However, when a language has too much syntactic sugar, then the number of ways to express the same idea increases to such a level that it ends up making it //harder// to read--because the reader has to know all the intricacies of all the different possible ways of doing things.+Syntactic sugar can be a tricky thing. On the one hand, syntactic sugar makes it easier to write your programs---it increases **writability**. When done right, it also makes programs easier to read---it increases **readability**. However, when a language has too much syntactic sugar, then the number of ways to express the same idea increases to such a level that it ends up making it //harder// to read because the reader has to know all the intricacies of all the different possible ways of doing things.
  
  
ch03-places-to-put-things.1503536312.txt.gz · Last modified: 2017/08/24 00:58 by mithat