User Tools

Site Tools


programming_fundamentals_with_processing: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 revisionPrevious revision
Next revision
Previous revision
programming_fundamentals_with_processing:ch03-places-to-put-things [2017/07/26 17:53] – [Math versus programming] mithatprogramming_fundamentals_with_processing:ch03-places-to-put-things [2017/07/26 18:06] (current) mithat
Line 36: Line 36:
 int b; int b;
 </code> </code>
-do exactly that--they are **variable declarations**. They tell the Processing compiler that you plan to use two variables, one named ''%%a%%'' and the other named ''%%b%%''.((Some programming languages automatically create new variables for you as soon as you use them. Processing isn't one of those.+do exactly that---they are **variable declarations**. They tell the Processing compiler that you plan to use two variables, one named ''%%a%%'' and the other named ''%%b%%''.((Some programming languages automatically create new variables for you as soon as you use them. Processing isn't one of those.
 )) ))
  
Line 72: Line 72:
 One way we can learn the value of a variable is to output it to the console as in the code below: One way we can learn the value of a variable is to output it to the console as in the code below:
  
-**program ''%%variables2.pde%%'':** +<file java variables2.pde>
- +
-<code java>+
 void setup () { void setup () {
   int a;   int a;
Line 85: Line 83:
   println(a);   println(a);
 } }
-</code>+</file>
 or, if you like, or, if you like,
  
-**program ''%%variables2a.pde%%'':** +<file java variables2a.pde>
- +
-<code java>+
 void setup () { void setup () {
   int a;   int a;
Line 103: Line 99:
   println(a);   println(a);
 } }
-</code>+</file>
 You can also use variables in places where Processing expects literal constants. Here's an example: You can also use variables in places where Processing expects literal constants. Here's an example:
  
-**program ''%%variables3.pde%%'':** +<file java variables3.pde>
- +
-<code java>+
 void setup () { void setup () {
   int a;   int a;
Line 118: Line 112:
   rect(0, 0, a, b);   rect(0, 0, a, b);
 } }
-</code>+</file>
 This will draw a rectangle whose upper left corner is at the origin of the canvas, whose width is the value of ''%%a%%'' (53 pixels) and whose height is the value of ''%%b%%'' (33 pixels). This example shows that we can use variables whose values might be the result of calculations based on other stuff to control how things are drawn. That should seem at least a little bit interesting. This will draw a rectangle whose upper left corner is at the origin of the canvas, whose width is the value of ''%%a%%'' (53 pixels) and whose height is the value of ''%%b%%'' (33 pixels). This example shows that we can use variables whose values might be the result of calculations based on other stuff to control how things are drawn. That should seem at least a little bit interesting.
  
Line 129: Line 123:
 $x = \frac{{ - b \pm \sqrt {b^2 - 4ac} }}{{2a}}$ $x = \frac{{ - b \pm \sqrt {b^2 - 4ac} }}{{2a}}$
  
-However, most programming languages let you give variables long names--subject to certain rules. **TODO verify:** The rules in Processing say that an identifier must start with an upper or lowercase letter and then can be made up of any combination of upper or lowercase letters, numbers, and the underscore character. Note that spaces in identifiers are not allowed.+However, most programming languages let you give variables long names---subject to certain rules. **TODO verify:** The rules in Processing say that an identifier must start with an upper or lowercase letter and then can be made up of any combination of upper or lowercase letters, numbers, and the underscore character. Note that spaces in identifiers are not allowed.
  
-There is also a HolyList® of identifiers that Processing has reserved for its own use--so called **reserved words**. You cannot use reserved words for your own identifiers. For example, ''%%setup%%'' and ''%%int%%'' are reserved words. A list of Processing's reserved words is in Appendix A.+There is also a HolyList® of identifiers that Processing has reserved for its own use---so called **reserved words**. You cannot use reserved words for your own identifiers. For example, ''%%setup%%'' and ''%%int%%'' are reserved words. A list of Processing's reserved words is in Appendix A.
  
 Finally, Processing is a **case sensitive** language, meaning that the identifier ''%%myvar%%'' is different from the identifiers ''%%myVar%%'' and ''%%MYVAR%%''. Finally, Processing is a **case sensitive** language, meaning that the identifier ''%%myvar%%'' is different from the identifiers ''%%myVar%%'' and ''%%MYVAR%%''.
Line 139: Line 133:
 Knowing these rules, we could rewrite ''%%variables3a.pde%%'' as follows: Knowing these rules, we could rewrite ''%%variables3a.pde%%'' as follows:
  
-**program ''%%variables3a.pde%%'':** +<file java variables3a.pde>
- +
-<code java>+
 void setup () { void setup () {
   int var_one;   int var_one;
Line 151: Line 143:
   rect(0, 0, var_two, var_one);   rect(0, 0, var_two, var_one);
 } }
-</code>+</file>
 or, or,
  
-**program ''%%variables3b.pde%%'':** +<file java variables3b.pde>
- +
-<code java>+
 void setup () { void setup () {
   int rec_width;   int rec_width;
Line 166: Line 156:
   rect(0, 0, rec_width, rec_height);   rect(0, 0, rec_width, rec_height);
 } }
-</code> +</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.+
  
-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 programmersA common coding convention for variable names in Processing is to use CamelCase (i.e.capitalizing the first letter of adjacent wordswith the first letter of the first word in lower caseRewriting the example we have been working with so far using the CamelCase convention would yield:+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 easierAnd the easier your code is to read, the easier it will be for someone (even youto fix and expand it.
  
-**program ''%%variables3c.pde%%'':**+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:
  
-<code java>+<file java variables3c.pde>
 void setup () { void setup () {
   int recWidth;   int recWidth;
Line 183: Line 172:
   rect(0, 0, recWidth, recHeight);   rect(0, 0, recWidth, recHeight);
 } }
-</code+</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. I personally prefer this to CamelCase because I think it's easier to read. However, CamelCase is the defacto standard with Processing and what I will use in the remainder of this text.+ 
 +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.
  
 ===== Data types ===== ===== Data types =====
Line 194: Line 184:
 int recHeight; int recHeight;
 </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 ''%%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**.
  
 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. 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.
Line 230: Line 220:
 === 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. An int occupies 32 bits in memory. Example:
  
 <code java> <code java>
Line 275: Line 265:
 === 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.
  
 === 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--a system that translates characters into numeric codes. Character literal constants are created with single quotes. Examples:
  
 <code java> <code java>
Line 323: Line 313:
  
  
----- +<WRAP center round box 80%>
 === Geek break: Why doesn't this stuff happen automatically? === === Geek break: Why doesn't this stuff happen automatically? ===
  
Line 334: Line 323:
  
 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.
 +</WRAP>
  
- 
----- 
  
 ===== Syntactic sugar ===== ===== Syntactic sugar =====
Line 349: Line 337:
 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 way---as 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 361: Line 349:
 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 370: Line 358:
 ==== 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.
  
  
programming_fundamentals_with_processing/ch03-places-to-put-things.1501091593.txt.gz · Last modified: 2017/07/26 17:53 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki