From "Code Complete"
🎧 Listen to Summary
Free 10-min PreviewEffective Variable Declarations and Data Literacy
Key Insight
Creating effective data necessitates a strong understanding of various data types, which forms a core component of a programmer's skill set. A 'Data Literacy Test' helps assess this knowledge by presenting terms such as 'abstract data type', 'array', 'boolean variable', 'pointer', 'string', 'tree', and 'typedef'. Participants score 1 for familiar terms and 0.5 for unsure ones, with scores interpreted ranging from 0 (beginner, requiring further study) to 25-29 (expert, suggesting an ability to write a book). Notably, scores of 30 or higher are deemed 'pompous fraud' due to the inclusion of made-up terms like 'elongated stream', designed to test 'Intellectual Honesty'.
Streamlining variable declarations, though seemingly minor, significantly impacts project efficiency and reduces frustration. A major hazard discussed is 'implicit declarations', a feature in some languages (e.g., Microsoft Visual Basic without `Option Explicit`) where variables are automatically declared upon first use. This can lead to insidious errors, such as mistyping `acctNo` for `acctNum`, where both variables are implicitly created and used, making debugging difficult. Explicit declaration, conversely, requires two mistakes (using an undeclared variable and then declaring it separately) for such a problem to occur, virtually eliminating the 'synonymous-variables' issue.
To mitigate the risks of implicit declarations, programmers should disable them when possible (e.g., using `Option Explicit` in Visual Basic). A consistent practice is to explicitly declare all new variables as they are typed, even if the language doesn't enforce it. Establishing clear naming conventions for common suffixes or types can prevent confusion between similar variable names. Additionally, utilizing compiler cross-reference lists or utility programs is beneficial, as these tools can list all variables in a routine, helping to spot similar names like `acctNum` and `acctNo`, and identifying declared but unused variables, thereby promoting more careful data handling.
📚 Continue Your Learning Journey — No Payment Required
Access the complete Code Complete summary with audio narration, key takeaways, and actionable insights from Steve McConnell.