Dev debates: small commits or big commits? Tests before or after?
By Google for Developers
Key Concepts
- Code commenting practices (many small vs. one large)
- Test-driven development (TDD) vs. writing tests after code
- Personal approaches to testing and commenting
Code Commenting: Many Small vs. One Large
The discussion revolves around the optimal way to comment code. Two primary approaches are presented:
- Many Small Comments: This involves scattering numerous brief comments throughout the code to explain specific lines or small blocks of logic. The transcript repeatedly emphasizes "many small comments."
- One Large Comment: This refers to a single, extensive comment, possibly at the beginning of a function or file, aiming to provide a comprehensive overview. The phrase "one large comments" and "one big comment" are used.
The preference expressed in the transcript leans towards "many small comments," with the speaker questioning "How many small comments?" and affirming "Many small comments." There's a brief consideration of "Oh one large comments," but the sentiment seems to favor the granular approach.
Test Writing: Before vs. After Code
A significant portion of the transcript addresses the timing of writing test cases relative to writing the actual code.
- Writing Tests After Code: This is described as a common personal practice, where tests are written "after" the code is perceived as "perfect." The speaker states, "After is like when I feel my code is perfect. Then I write test cases to validate it." However, this approach leads to a realization: "But then I realize that okay I should rewrite it." This suggests that writing tests after the fact can reveal flaws in the original code, necessitating revisions.
- Writing Tests Before Code (Test-Driven Development - TDD): This is presented as the ideal or intended method. The question "Writing test before after" and the statement "It's supposed to be before" highlight this.
- Hybrid/Struggling Approaches: The transcript acknowledges the difficulty in adhering to the "before" approach, with phrases like "I try before but I end up after" and "Before after after never." This indicates a common struggle where developers intend to write tests first but often fall back to writing them later.
Key Arguments and Perspectives
The core arguments and perspectives presented are:
- Preference for Granular Explanations: The emphasis on "many small comments" suggests a belief that detailed, line-by-line explanations are more effective for understanding code than a single, overarching comment.
- Critique of Post-Code Testing: The realization that writing tests after code requires rewriting highlights a potential inefficiency and a flaw in the "write tests after" methodology. It implies that tests written retrospectively might not adequately guide the initial code design.
- Ideal vs. Reality in TDD: The transcript acknowledges the theoretical benefit of writing tests before code ("It's supposed to be before") but also the practical challenges many developers face in consistently implementing this.
Notable Statements
- "If the code works push it." - This statement, though brief, implies a pragmatic approach where functional code might be prioritized for deployment, potentially even before thorough testing or commenting.
- "After is like when I feel my code is perfect. Then I write test cases to validate it. But then I realize that okay I should rewrite it." - This quote encapsulates the common pitfall of writing tests after code.
- "I try before but I end up after." - This statement reflects the common struggle with adhering to test-driven development.
Technical Terms and Concepts
- Comments: Explanatory text within code that is ignored by the compiler or interpreter. Used to clarify logic, intent, or usage.
- Test Cases: A set of conditions or variables under which a tester will determine whether a system under test satisfies requirements or works correctly.
- Validate: To check or prove the validity or accuracy of something. In this context, validating code with test cases.
- Rewrite: To write something again, typically to improve it.
Logical Connections
The discussion on commenting and testing are linked by the overarching theme of code quality and maintainability. Both practices aim to make code easier to understand, debug, and modify. The struggle with writing tests before code directly impacts the perceived "perfection" of the code, which then influences the decision to write tests afterward.
Data, Research Findings, or Statistics
No specific data, research findings, or statistics are mentioned in this transcript. The discussion is based on personal experiences and common developer practices.
Synthesis/Conclusion
The transcript highlights a common internal debate among developers regarding code commenting and testing methodologies. While the ideal is to write tests before code (TDD) and to provide clear, granular comments ("many small comments"), the reality often involves writing tests after the code is written, leading to potential rewrites. The pragmatic statement "If the code works push it" suggests a tension between ideal practices and the pressure of delivery. The overall sentiment leans towards the benefits of detailed commenting and the challenges of consistently implementing test-driven development.
Chat with this Video
AI-PoweredHi! I can answer questions about this video "Dev debates: small commits or big commits? Tests before or after?". What would you like to know?