Code Complete Book Review (part 3 of 7)

  • Variables
    • General Issues in Using Variables
      • Data Literacy – Know what types are available in your framework. Not every collection is a list.
      • Implicit Declarations – Dangerous because the compiler won’t warn you when you are misusing variables, (or just misspelling them)
      • Guidelines for Initializing Variables
        • Initialize the variable as it is declared, or right afterwards
        • Turn on "Treat warnings as errors" in your IDE. This will identify un-initialized, and unused variables
        • use final or const when possible. This will protect your variable from change and allow developers later on to quickly understand your code.
      • Scope
        • Minimizing Span- Span is the distance between statements where are variable changes. By minimizing this distance, you can debug or trace a variable quickly because all of the changes will be in close proximity
        • Minimizing Live Time – Distance between the first and last statements involving a variable. By minimizing this distance, you reduce the complexity of managing the variable.
        • Minimizing Scope- minimizing scope reduces access to the variable throughout the software. This aids in maintenance, as it decouples the variable from the rest of the program and changes can be localized th the routine or class.
      • Persistence – This is the lifetime of a variable.
        • control block like for, if-then, using
        • routine/class lifetime
        • until garbage collected. This can be very dangerous as the garbage collector is non-deterministic. Always check for items to be sure they remain in memory
        • For the life of the program. These are static variables
        • forever. These variables are persisted to the filesystem/db/somewhere else. They are reloaded every time the program starts.
      • Binding Time- This is the time at which a variable is set to its value. This can happen at code time, compile time, load time, instantiation time, or just in time. This is a tradeoff
        • Early binding – This is the lowest complexity and makes it easy to predict how a program will act.
        • Late binding – This provides more flexibility as the program can be altered without having to change the code by changing settings files. This adds additional complexity to debugging and deployment though.
      • Relationship between Data Types and Control Structures
        • Sequential Data
        • Selective Data
        • Iterative Data
      • Using each variable for exactly one purpose
        • Don’t reuse a temp variable later in the routine
        • don’t give variables "hidden meanings"
    • The Power of Variable Names
    • Fundamental Data Types
    • Unusual Data Types – These include structures, pointers, and global variables.

    This material is copied and/or adapted from the Code Complete 2 Website at cc2e.com. This material is Copyright © 1993-2007 Steven C. McConnell. Permission is hereby given to copy, adapt, and distribute this material as long as this notice is included on all such materials and the materials are not sold, licensed, or otherwise distributed for commercial gain.

  • Leave a Reply

    Name *
    Email *
    Website