[Pragmatic Programmer] Chapter 4. pragmatic paranoia
· Tech· CS
CS
DAY 7 Today’s reading range: Chapter 4. pragmatic paranoia
? What do you want to remember from the book?
Perfect software cannot be created.
- Pragmatic programmers write code defensively to guard against their own mistakes.
Design according to the contract. Design By Contract, DBC
- What exactly is a program? It refers to a program that does exactly as much, not more or less, than what it claims to do.
- 1) Precondition: Something that must be true for the routine to be called. 2) Postcondition: What the routine guarantees it will do. Infinite repetition is not allowed. 3) Class invariant: The class guarantees that this condition is always true when viewed from the caller's particle. The invariant must be true when the routine terminates and control returns to the caller.
- Among the iContract syntax, variable@pre can be used to obtain the original value of a variable at the time of method entry.
- Before starting lazy code, be strict about what you will accept and promise a minimum of what you will give away.
- Liskov Substitution Principle Subclasses should be usable through the base class interface without the user knowing the differences.
- Because the runtime system and libraries are not designed to support contracts, these calls are not checked.
- Nana for C, C++, iContract for Java ▶ Language supporting DBC, preprocessor
- Loop invariants, semantic invariants
Make it stop working early.
- Obviously, it is sometimes not appropriate to simply terminate a running program. There may be resources left that have not been disposed of, log messages may need to be written, open transactions may need to be cleaned up, or there may be a need to interact with other processes.
Use assertions to prevent impossible situations.
- Danjeong checks for things that should never happen.
- If you need to tear down a resource, let assertion failure raise an exception, longjump to the exit, or call an error handler.
- It is a misunderstanding that assertions cause overload.
// 잘못된 방식
while(liter.hasMoreElements()){
Test.ASSERT(iter.nextElement()!=null);
Object obj = iter.nextElement();
}
// 아래 방식을 따라 작성하자.
// .next 호출은 이 호출이 돌려주는 원소 다음으로 반복자를 이동시키는 부작용이 있다.
// 따라서 컬랙션 원소의 절반만 처리하게 되기 때문이다.
while(liter.hasMoreElements()){
Object obj = iter.nextElement();
Test.ASSERT(obj!=null);
}
Use exceptions for exceptional problems.
- There are cases where exception handling using try and catch rather than if and else statements is much cleaner.
- An exception means that the movement of control is not immediate and local. It's kind of like a chain.
- In a client-server application that uses Java's RMI function, it is also possible to implement an error handler interface by wrapping the remote object in a non-remote class.
Finish what you started.
- This simply means that the routine or object that allocates the resource must also be responsible for tearing it down.
- Ideally, the routine that allocates resources should also be responsible for freeing them. Let’s refactor well.
- Overlapping allocation 1) Disassemble resources in the reverse order of allocation. 2) If you allocate the same set of resources in multiple places in your code, always keep the allocation order the same.
- A class represents a resource, the constructor provides a specific object of that resource type, and the destructor removes it from the current scope.
- Unlike C++, Java uses a lazy approach to automatic object deletion. Garbage collection deletes objects without references, and the finally clause is always executed to balance resources.
? How do you feel about what you read today?
The book I read today was longer than I expected. Looking at the table of contents, it seems simple, but it is a book that you will ponder over and over again. There must be a reason why it was selected as a Nogaebuk book...!
25. The balance of resource use part seemed to be the most helpful in the future. Resource allocation and how to refactor when developing it yourself were also explained in detail. I think it's probably something I should consider important during development later, and something I should learn and make it my own. Let's balance our resources!
❓ Anything you have questions about or something you don’t understand
DBC is a powerful tool, but wouldn't it be widely used?
- This is because, among numerous cases, contracts cannot be specified in a specific format and cannot currently be checked with static analysis tools. This is because despite people's demands, DBC still cannot satisfy people's needs. Instead, TDD is well-documented as a method in the current situation, and DBC is not necessarily a necessary part in situations where TDD is used.
- However, since TDD and DBC can be seen as complementary, some say that DBC should also be known.
http://www.kyobobook.co.kr/product/detailViewKor.laf?mallGb=KOR&ejkGb=KOR&barcode=9788966261031
Comments
No comments yet. Be the first!
164 posts in 테크
- 2233D Gaussian Splatting vs Unreal Engine: Two Ways to Build a 3D World — and Where Each One Ships
- 222LLMs Inside Unreal Engine: The New Skills Game Developers Need in 2026
- 220Living With Claude Fable 5: How the Most Capable Model Changes the Way You Actually Work
- 219Luma's Bet: From Video Generator to a Single Model That Thinks in Pixels
- 215The Best AI Video Models in 2026: Types, Differences, and Where This Is All Going
1–5 / 164