Monday, March 7, 2011

Code Complete 2 by Steve McConnell

Rating: The Bible of Programming. Every developer can learn a few things from it
and have an occasional laugh.

Concepts:
  • heuristics vs algorithms
  • metaphors for making software: 
    • penmanship: writing code, 
    • farming: growing a system, 
    • oyster farming: system accretion, 
    • construction: building software.
  • good practices differ for different kinds of software projects: business systems, mission-critical systems, embedded life-critical systems - page 31
  • typical architectural components: program organization, major classes, data design, business rules, user interface design, resource management, security, performance, scalability, interoperability, internationalization/localization, input/output, error processing, fault tolerance, architectural feasibility, over-engineering, buy vs. build decisions, reuse decisions, change strategy, general architecture quality.
  • technology wave - page 66
  • programming into a language (good) vs programming in a language (bad) - page 68
  • a "wicked" problem - one that can be clearly defined only by attempting to solve at least part of it - page 74
  • accidental and essential difficulties - page 77
  • desirable characteristics of a design: 
    • minimal complexity, 
    • ease of maintenance, 
    • loose coupling, 
    • extensibility, 
    • re-usability, 
    • high fan-in: having a high number of classes that use a given class, 
    • low-to-medium fan-out: having a given class use a low-to-medium number of other classes  (high cohesion), 
    • portability, 
    • leanness: designing the system so that it has no extra parts, 
    • stratification - design the system so you view it at one level without dipping into other levels,
    • standard techniques - page 81
  • design building blocks: 
    • major heuristics: find real-world objects, form consistent abstractions, encapsulate implementation details, inherit - when it simplifies the design, hide secrets, identify areas likely to change, keep coupling loose, look for common design patterns
    • minor heuristics: aim for strong cohesion, build hierarchies, formalize class contracts, assign responsibilities, design for test, avoid failure, choose binding time consciously, make central points of control, consider using brute force, draw a diagram, keep your design modular
  • design practices: iterate, divide and conquer, top-down and bottom-up approaches, experimental prototyping, collaborative design
  • Abstract Data Type (ADT) - page 126
  • semantic encapsulation - page 141
  • Liskov Substitution Principle (LSP) - you shouldn't inherit from a base class unless the derived class truly "is a" more specific version of the base class - page 144
  • "mixins" - classes, usually abstract, meant to be inherited from in multiple inheritance - page 149
  • shallow copies (reference objects) vs deep copies (value objects) - page 152
  • "gold-plating" - creation of functionality that isn't required and that unnecessarily adds complexity - page 154
  • "god class" - a class not to create - page 155
  • types of acceptable cohesion in a routine - page 168:
    • functional - performs one and only one operation - the ideal,
    • sequential - contains operations on shared data that must be performed in a specific order,
    • communicational - operations share data, but are otherwise unrelated,
    • temporal  - contains operations that are done at the same time.
  • types of unacceptable cohesion in a routine - page 170:
    • procedural - operations performed in order matching UI,
    • logical - performs one of contained operations based on an input flag,
    • coincidental - no relationship between operations.
  • defensive programming
  • assertions - "Error handling typically checks for bad input data; assertions check for bugs in the code." - page 191, but "For highly robust code, assert and then handle the error anyway" - page 193
  • preconditions
  • postconditions
  • robustness vs. correctness - page 197
  • barricade - page 203
  • debugging aids - page 205
  • PPP - pseudocode programming process - page 215
  • Principle of Proximity - keep related actions together - page 242
  • window of vulnerability, variable span - average number of statements between variable references, variable live time - max number of statements between references - page 245
  • hybrid coupling - two variables in one - page 256
  • the telephone test - "[...] if you can't read your code to someone over the phone, rename your variables to be more distinctive [...]" - page 283
  • write-time convenience vs. read-time convenience - page 285
  • dog-tag - a field added for error checking - page 326
  • tramp data - data passed to a routine only to be passed to another routine - page 338
  • guard clause - code that exits a routine early if it is not ok to proceed, to avoid deep nesting - page 392
  • safety counter - a counter variable that prevents infinite loops - page 396 and earlier
  • cyclic recursion - A calls B, B calls C, C calls A - page 396
  • table-driven approach - lookup information in a table rather than using logical statements - page 411
  • indexed access tables - page 425
  • stair-step access tables - page 426
  • DeMorgan's Theorems - use to simplify boolean tests - page 436
  • structured programming - you can build any program out of a combination of sequences, selections, and iterations - page 454
  • McCabe's cyclomatic complexity metric - the sum of decision points in a routine - 10+ is possibly too complex - page 458
  • quality gates - page 467
  • collaborative construction - page 480
  • formal inspections - page 485
  • walk-through, code reading, dog-and-pony show - page 492
  • black-box/white-box testing - page 500
  • structured basis testing - page 507
  • data flow testing - page 509
  • equivalence partitioning, error guessing, boundary analysis, compound boundaries, classes of bad/good data- from page 512
  • system perturbers - page 527
  • confessional debugging - page 547
  • brute-force debugging - page 548
  • voodoo programming - page 553
  • psychological distance - page 556
  • code-tuning: loops: unswitching, jamming, unrolling, sentinel values - page 616
  • strength reduction - page 630
  • phased vs incremental integration - page 691
  • top-down vs bottom up integration - page 694
  • integration types: sandwich, risk-oriented, feature-oriented, T-shaped - page 698
  • Fundamental Theorem of Formatting - page 732


Bottom line:

  • "Specifying requirements adequately is a key to project success [...]" page 39
  • "Once you understand that all technical goals in software are secondary to managing complexity, many design considerations become straightforward." - page 79
  • "Design is heuristic. Dogmatic adherence to any single methodology hurts creativity and hurts your programs." - page 123
  • "If a routine has bad cohesion, it's better to put effort into a rewrite to have better cohesion than investing in a pinpoint diagnosis of the problem." - page 169
  • "The name of a routine is an indication of its quality." - page 186
  • Define quality goals - page 469
  • "[...] effective software-quality program must include a combination of techniques [...]" - page 473
  • "[...] inspections quickly brought all the developers up to the level of the best developers [...]" - page 482
  • "The first priority of visual layout is to illuminate the logical organization of the code." - page 775.
  • "Holy Grail of legibility: self-documenting code. [...] In well-written code, comments are the icing on the readability cake." - page 779
  • Good comments: "[...] information that can't be expressed in code, intent comments, and summary comments." - page 788
  • "If you're figuring out code instead of reading it, it's too complicated. If it's hard, it's wrong. Make it simpler." - page 849
  • "A blanket attempt to avoid mistakes is the biggest mistake of all." - page 852


:-) Funny bits:

  • "In programming, if your requirements are contaminated, they contaminate the architecture, and the architecture in turn contaminates construction. This leads to '''grumpy, malnourished programmers''' and radioactive, polluted software that's riddled with defects."
  • "It's as if you were a contractor called to work on a house. Your customer says, "What will it cost to do the work?" You reasonably ask, "What do you want me to do?" Your customer says, "I can't tell you, but how much will it cost?" You '''reasonably''' thank the customer for wasting your time and go home."
  • Comparing productivity of higher and lower level languages: "You save time when you don't need to have '''an awards ceremony''' every time a C statement does what it's supposed to." - page 62
  • "You'd probably want to '''tar and feather''' a teacher who gave you a programming assignment, then changed the assignment as soon as you finished the design, and then changed it again just a s you were to turn in the completed program. But the very process in an everyday reality in professional programming." - page 75
  • "[...] the road to programming hell is paved with global variables [...]" - page 95
  • "A vague, wishy-washy [routine] name is like a politician on the campaign trail. It sounds as if it's saying something, but when you take a hard look, you can't figure out what it means." - page 222
  • "[About memory initialization] Alternatively, Brian Kernighan and Rob Pike suggest using the constant 0xDEADBEEF as a memory filler that's easy to recognize in a debugger [...]" - page 244
  • "You can't give a variable a name the way you give a dog a name - because it's cute or it has a good sound." - page 259
  • The optimum length for a name seems to be somewhere between the lengths of x and maximumNumberOfPointsInModernOlympics." - page 262
  • "It's OK to figure out murder mysteries, but you shouldn't need to figure out code." - page 267
  • "FALSE [...] would be a bad abbreviation for 'Fig and Almond Season'" - page 285
  • "By the time structured programming came and went, the term 'structured' had been applied to every software-development activity, including structured analysis, structured design, and '''structured goofing off'''." - page 454
  • "[...] compilers are dissembling little rascals, [...]" - page 549
  • "Some compilers get so excited after detecting the first error that they become giddy and overconfident; they prattle on with dozens of error messages that don't mean anything." - page 550
  • "Code as if whoever maintains your program is a violent psychopath who knows where you live." - page 777


Interesting observations:

  •  7 +/-2 is a number of discrete items a person can remember while performing other tasks. If a class contains more than about seven data members, consider splitting it into smaller classes. - page 143
  •  "A good way to think of assertions is as executable documentation [...]" - page 191
  •  "[...] code reviews were several times as cost-effective as testing [...]" - page 473



Criticism:

  • The Java Singleton example on page 151 is not correct, because it does not close the class for inheritance
  • The "Avoid classes named after verbs" recommendation on page 155 does not mention the Strategy pattern, where naming your behavior classes after verbs may be desired
  • The Visual Basic example of a centralized exception reporter on page 202 does not follow the rules of defensive programming (no parameter validation) and the law of Demeter ("thisException.TargetSite.Name"). ReportException() may itself throw an exception in some cases.
  • Some abbreviation guidelines on page 282, like "Remove all nonleading vowels", "Keep the most noticable sound in each syllable" make the code difficult to read for non-native English speakers.
  • The concept of coding into a language may be misused to make a C# program look and act like a COBOL program.
  • The author mentions C++, and Java often, but skips C# which has the same features - page 439
  • Writing numeric expressions in number-line order makes them difficult to read: you have to start in the middle of the line and read to the left and right at the same time. It looks good when you draw it, but it is awkward to read - page 440

No comments:

Post a Comment