Code Complete 2: Fundamental Data Types (Part 3)
Tags: codenewbie, codequality, productivity
Published: 2020-09-23 16:30:34

In this blog post, I'll discuss the fundamental Data types in Steve McConnell's book Code Complete. 

##**Chapter 12: Fundamental Data Types**
**Numbers in General**
By replacing magic numbers with constants, changes are reliable, changes can be made easily, and your code is more readable.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/321y7qsfvmfpml83988t.jpg)

**Floating-Point Numbers**
<ul><li>To increase accuracy when adding numbers with differing magnitudes, add them starting with the smallest values.</li>
<li>Avoid equality operations and anticipate rounding errors; cope by switching to greater precision, BCD, or integer variables.</li></ul>

**Characters and Strings**
To avoid endless strings in C, initialize strings to null, and use strncpy() instead of strcpy().

**Enumerated Types**
<ul><li>Use enumerated types for more type-checking, and as a richer alternative to boolean variables.</li>
<li>Explicitly assign their values to specify first and last values for iteration, and an invalid or "null" type.</li></ul>

**Arrays**
In C, use or define an ARRAY_LENGTH() macro as #define ARRAY_LENGTH(x) (sizeof(x) / sizeof(x[0])).

**Creating Your Own Types**
Don't name a type created using typedef after the underlying data type, and don't refer to predefined types.

##**Chapter 13: Unusual Data Types**
**Structures**
By passing only one or two fields from a structure into a method, you promote information hiding from the method.

**Pointers**
<ul><li>Symptoms of pointer errors tend to be unrelated to causes of pointer errors.</li>
<li>By isolating pointer operations to methods, you minimize the possibility of propagating careless mistakes through your program.</li>
<li>Allocating dog tags allow you to check for freeing memory twice or overwriting memory beyond the last byte.</li>
<li>Free pointers at the same scoping level as they were allocated, such as in the same method, or a constructor/destructor pair.</li>
<li>Set a pointer to NULL after deallocation; writing to it produces an error, and deallocating twice is more easily caught.
<li>In C++, a reference cannot point to NULL and the object it refers to cannot be changed.</li>
<li>In C, you can use char or void pointers for any type of variable.</li></ul>

**Global Data**
<ul><li>Passing a global variable to a method, and then referring to both the parameter and global variable is especially tricky.</li>
<li>Initialization order among different "translation units," or files, is not defined in languages like C++.</li>
<li>Try to contain a global variable as a class variable, and provide an accessor for any other code that needs it.</li>
<li>Replace global data with access methods to centralize control over it and protect yourself against changes.</li>
<li>Build access methods at the level of the problem domain rather than at the level of the implementation details.</li></ul>

**My Key takeaways in these two chapters are;**
<ul><li>Working with specific data types means remembering many individual rules for each type.</li> 
<li>Creating your own types makes your programs easier to modify and more self-documenting, if your language supports that capability.</li>
<li>When you create a simple type using typedef or its equivalent, consider whether you should be creating a new class instead.</li>
<li>Structures can help make programs less complicated, easier to understand, and easier to maintain.</li>
<li>Whenever you consider using a structure, consider whether a class would work better.</li>
<li>Pointers are error-prone. Protect yourself by using access routines or classes and defensive-programming practices.</li>
<li>Avoid global variables, not just because they’re dangerous, but because you can replace them with something better.</li>
<li>If you can’t avoid global variables, work with them through access routines.</li>
<li>Access routines give you everything that global variables give you, and more.</li></ul>