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
programming_fundamentals_with_processing:ch03-places-to-put-things [2017/07/26 18:02] – [Processing's composite types] mithatprogramming_fundamentals_with_processing:ch03-places-to-put-things [2017/07/26 18:06] (current) mithat
Line 123: 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 158: Line 158:
 </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**. 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 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 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:
  
 <file java variables3c.pde> <file java variables3c.pde>
Line 184: 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 220: 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 265: 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 337: 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 349: 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 358: 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.1501092159.txt.gz · Last modified: 2017/07/26 18:02 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki