{"id":3656,"date":"2017-10-01T05:00:33","date_gmt":"2017-10-01T05:00:33","guid":{"rendered":"http:\/\/www.nikola-breznjak.com\/blog\/?p=3656"},"modified":"2017-09-05T23:26:07","modified_gmt":"2017-09-05T23:26:07","slug":"code-complete-2-steve-mcconnell-fundemental-data-types","status":"publish","type":"post","link":"https:\/\/nikola-breznjak.com\/blog\/books\/programming\/code-complete-2-steve-mcconnell-fundemental-data-types\/","title":{"rendered":"Code Complete 2 \u2013 Steve McConnell \u2013 Fundemental Data Types"},"content":{"rendered":"<p>I just love Steve McConnell&#8217;s classic book <a href=\"http:\/\/amzn.to\/2vdwv5v\">Code Complete 2<\/a>, and I recommend it to everyone in the Software &#8216;world&#8217; who&#8217;s willing to progress and sharpen his skills.<\/p>\n<p>Other blog posts in this series:<\/p>\n<ul>\n<li><a href=\"http:\/\/www.nikola-breznjak.com\/blog\/books\/programming\/code-complete-2-steve-mcconnell-part-1-laying-foundation\/\">Part 1 (Chapters 1 &#8211; 4): Laying the Foundation<\/a><\/li>\n<li><a href=\"http:\/\/www.nikola-breznjak.com\/blog\/books\/programming\/code-complete-2-steve-mcconnell-design-construction\/\">Chapter 5: Design in Construction<\/a><\/li>\n<li><a href=\"http:\/\/www.nikola-breznjak.com\/blog\/books\/programming\/code-complete-2-steve-mcconnell-working-classes\/\">Chapter 6: Working Classes<\/a><\/li>\n<li><a href=\"http:\/\/www.nikola-breznjak.com\/blog\/books\/programming\/code-complete-2-steve-mcconnell-high-quality-routines\/\">Chapter 7: High-Quality Routines<\/a><\/li>\n<li><a href=\"http:\/\/www.nikola-breznjak.com\/blog\/books\/programming\/code-complete-2-steve-mcconnell-defensive-programming\/\">Chapter 8: Defensive programming<\/a><\/li>\n<li><a href=\"http:\/\/www.nikola-breznjak.com\/blog\/books\/programming\/code-complete-2-steve-mcconnell-pseudocode-programming-process\/\">Chapter 9: Pseudocode Programming Process<\/a><\/li>\n<li><a href=\"http:\/\/www.nikola-breznjak.com\/blog\/books\/programming\/code-complete-2-steve-mcconnell-general-issues-using-variables\/\">Chapter 10: General Issues in Using Variables<\/a><\/li>\n<li><a href=\"http:\/\/www.nikola-breznjak.com\/blog\/books\/programming\/code-complete-2-steve-mcconnell-general-issues-using-variables\/\">Chapter 11: General Issues in Using Variables<\/a><\/li>\n<\/ul>\n<p>The fundemental data types are the basic building blocks for all other data types.<\/p>\n<h3>Numbers in General<\/h3>\n<p><strong>Avoid &#8220;magic numbers&#8221;<\/strong> &#8211; if you can program in a language that supports named constants, use them instead. A good rule of thumb is that the only literals that should occur in the body of a program are 0 and 1.<\/p>\n<h3>Integers<\/h3>\n<p>Bear these considerations in mind when using integers:<br \/>\n+ Check for integer division &#8211; when you&#8217;re using integers, 7\/10 does not equal 0.7. It ussually equals 0<br \/>\n+ Check for integer overflow &#8211; you need to be aware of the largest possible integer. The larges possible unsigned integer is often 2<sup>32<\/sup>.<br \/>\n+ Check for overflow in intermediate results<br \/>\n    + ( 1000000 * 1000000 ) \/ 1000000 = -727<br \/>\n    &#8211; That&#8217;s because the intermediate result is too large for the integer data type<\/p>\n<h3>Floating-Point Numbers<\/h3>\n<p>Avoid additions and subtractions on numbers that have greatly different magnitudes, 1,000,000.00 + 0.1 probably produces an answer of 1,000,000.00 because 32 bits don&#8217;t give you enough significant digits to encompass the range between 1,000,000 and 0.1.<\/p>\n<p>Change to binary coded decimal (BCD) variables. This is a roll-your-own approach to BCD variables. This is particlarly valuable if the variables you&#8217;re using represent dollars and cents or other quantities that must balance precisely.<\/p>\n<h3>Boolean Variables<\/h3>\n<p><strong>Use boolean variables to document your program.<\/strong> Insted of merely testing a boolean expression, you can assign the expression to a variable that makes the implication of the test unmistakable.<\/p>\n<h3>Enumerated Types<\/h3>\n<p>An enumerated type is a type of data that allows each member of a class of objects to be described in English. You can <strong>use enumerated types for readability<\/strong>. Instead of writing statements like <code>if chosenColor == 1<\/code> you can write more readable expressions like <code>if chosenColor == Color_Red<\/code>.<\/p>\n<blockquote><p>\n  Anytime you see a numeric literal, ask whether it makes sense to replace it with an enumerated type\n<\/p><\/blockquote>\n<p>If language doesn&#8217;t have enumerated types, you can simulate them with global variables or classes.<\/p>\n<pre><code>Class Country {\n    private Country {}\n    public static final Country China = new Country();\n    public static final Country France = new Country();\n    public static final Country England = new Country(); \n}\n<\/code><\/pre>\n<h3>Named Constants<\/h3>\n<p>A named constant is like a variable except that you can&#8217;t change the constant&#8217;s value once you&#8217;ve assigned it. Named constanst enable you to refer to fixed quantities, such as the maximum number of employees, by name rather than a number &#8211; MAXIMUM_EMPLOYEES rather than, 100, for instance.<\/p>\n<h3>Arrays<\/h3>\n<p>Arrays are the simplest and most common type of structured data. An array contains a group of items that are all of the same type and that are directly accessed through the use of an array index.<\/p>\n<p><strong>Make sure that array indexes are within the bounds of the array.<\/strong> The most common problem arises when a program tries to access an array element that&#8217;s out of bounds.<\/p>\n<h2>Creating Your Own Types (Type Aliasing)<\/h2>\n<p>Programmer-defined data types are one of the most powerful capabilities a language can give you to clarify your understanding of a program. Here&#8217;s how you&#8217;d set up the type definition in C++:<\/p>\n<p><code>typedef float Coordinate;<\/code><\/p>\n<p>This type definition declares a new type, <code>Coordinate<\/code>, that&#8217;s functionally the same as the type <code>float<\/code>. To use the new type, you declare variables with it just as you would with a predefined type such as <code>float<\/code>.<\/p>\n<pre><code>Coordinate latitude;\nCoordinate longitude;\nCoordinate elevation;\n<\/code><\/pre>\n<p>Here are several reasons to create your own types:<br \/>\n+ To make modifications easier<br \/>\n+ To avoid excessive information distribution<br \/>\n+ To make up for language weakness &#8211; C doesn&#8217;t have a boolean type, so you can compensate by creating the type yourself: <code>typedef int Boolean<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I just love Steve McConnell&#8217;s classic book Code Complete 2, and I recommend it to everyone in the Software &#8216;world&#8217; who&#8217;s willing to progress and sharpen his skills.&hellip;<\/p>\n","protected":false},"author":1,"featured_media":3572,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[34],"tags":[],"class_list":["post-3656","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-programming"],"_links":{"self":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/3656","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/comments?post=3656"}],"version-history":[{"count":2,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/3656\/revisions"}],"predecessor-version":[{"id":3659,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/3656\/revisions\/3659"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media\/3572"}],"wp:attachment":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media?parent=3656"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/categories?post=3656"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/tags?post=3656"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}