UPI Study › Courses › Software Engineering › Free lessons › Lesson 12.2 Free with a free account
Writing Code That Holds Up: Reliable Programming Practices
Software Engineering · Chapter 12: Reliability Engineering and SecuritySection 12.2 · lesson 76 of 1036 min · free with a free account
Free preview
Create a free account to keep watching
It takes a minute and costs nothing. Your free account unlocks all of Chapter 1 and 3 more lessons of Software Engineering.
Lesson complete
Earn the 3 college credits for Software Engineering
Enrolling adds a quiz after each lesson, the graded work and the official transcript. Recommended for credit by NCCRS.
Next lesson: Measuring How Well Systems Perform Over Time →← Reliability Engineering: Definitions, Goals, and Getting StartedNext lesson: Measuring How Well Systems Perform Over Time →
In this lesson
Behaving sensibly when a file is missing, a call times out, or input is unexpected.
- Explain what makes code reliable, beyond the happy path
- Describe defensive programming, and where it matters most
- Distinguish failing safely from quietly hiding a problem
- Apply coding habits that make failures easy to notice and diagnose
Transcript
Everything said in the video, section by section (776 words). Read along, or search it.
Reliability Engineering and Security
Writing Code That Holds Up: Reliable Programming Practices
Code that works when everything goes right is easy. Code that still behaves sensibly when a file is missing, a network call times out, or a user types something unexpected, is a harder standard. That standard is what this section is about.
Correct vs. Reliable
When Things Don't Go to Plan
Code that produces the right answer when everything goes according to plan is correct. But it is not necessarily reliable. Reliable code keeps behaving sensibly when the things around it do not go according to plan. A missing file. A malformed input. A network call that never returns. Two closely related practices build that reliability into code as it is written. Defensive programming assumes that things will go wrong. And error handling decides what to do, once they actually do.
Table 1
Defensive Programming Techniques
Defensive programming means writing code that does not simply trust its inputs, its callers, or its environment. Instead, the code checks its assumptions explicitly. Input validation checks that incoming data is the right type, shape, and range, before it is used. Preconditions state explicitly what must be true for a function to behave correctly, and check it. Boundary checking confirms values fall within valid limits, like array indices. And fail-fast means stopping immediately and visibly when an assumption is violated, rather than continuing on bad data.
Figure 1
Check at the Trust Boundary
Defensive programming matters most at a trust boundary. That is any point where data arrives from outside the code's direct control, such as user input, a network call, or a file on disk. Code on the far side of the boundary can reasonably assume the data has already been checked. Code sitting at the boundary itself cannot assume anything of the kind. That is where the checks belong.
Figure 2
No Handling vs. Specific Handling
Defensive checks catch a problem. Error handling decides what happens next. Here is the same function, written two ways. It reads a file, and turns its contents into a number. The risky version has no error handling at all. The moment the file is missing, or its contents are not a number, it crashes with a raw, unhelpful exception. Whoever called it has no clean way to respond. The defensive version catches each specific failure, and raises a clear, purpose-built error that says exactly what went wrong. And notice what it does not do. It never silently swallows the error and returns a default value, as if nothing happened.
Think About It
A Crash vs. a Silent Default
Think about this. Why is silently returning a default value, such as zero, usually worse than crashing loudly? A crash is visible immediately, right where the problem occurred. A silently substituted default lets bad data flow quietly through the rest of the system. It then causes a confusing failure somewhere else entirely, far from its actual cause.
4 Habits
Reliable Coding Habits
A handful of habits, applied consistently, cover most of what separates reliable code from code that merely works most of the time. Fail fast. Stop as soon as an assumption is violated, rather than running on data already known to be bad. Never fail silently. A caught error should be handled, logged, or re-raised with context. It should never simply be discarded. Catch specific errors, not everything. Catching a broad error type risks hiding failures the code was never prepared to handle. And validate at every trust boundary, even if the data was already validated somewhere upstream.
Apply It
A Default Age of 18
Let's apply that. A function reads a user-submitted age field. It catches any exception that occurs, and returns a default age of eighteen, without logging anything. What are two problems with this approach? First, catching any exception is too broad. It risks hiding failures the code was never prepared to handle. Second, silently returning a default without logging hides the failure entirely. A bad age can flow through the rest of the system, instead of surfacing where it occurred.
Key Terms
The Vocabulary of This Section
Let's lock in the key terms. Defensive programming is writing code that explicitly checks its assumptions about inputs, callers, and the environment, rather than trusting them. A trust boundary is any point where data enters code from outside its direct control. That is where defensive checks matter most. Fail-fast is stopping immediately and visibly when an assumption is violated. And error handling is deciding how code responds once a failure is detected, by handling it, logging it, or re-raising it with context.
Key Takeaways
What to Carry Forward
To pull this together. Reliable code keeps behaving sensibly when something goes wrong, not just when everything goes according to plan. Defensive programming checks assumptions explicitly, and it matters most at trust boundaries. Good error handling catches specific failures and responds to them clearly. It never hides a failure behind a silent default. And four habits cover most of it in practice. Fail fast, never fail silently, catch specific errors, and validate at every trust boundary.
About Software Engineering at UPI Study
Software Engineering (Computer Science 301) is a self-paced online course of 103 video lessons in 15 chapters, from process models and agile to requirements, design, user interfaces, reliability, security and project management. It carries a NCCRS credit recommendation for 3 semester credits.
Every lesson transcript is free on pages like this one, and the videos are free with a free UPI Study account. Enrolling adds a practice quiz after each lesson, the graded work and final exam that earn the credit, lifetime access, and an official transcript on completion. UPI Study has helped over 130,000 students with college credits. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.
Enroll now → Course details Download the syllabus All Computer Science courses Every course with PRO →
Why take it here
Why students take Software Engineering with UPI Study
Where the credit goes
Universities that work with NCCRS
Software Engineering carries a NCCRS credit recommendation. These universities work with NCCRS; some are UPI Study's signed partners. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.
Thomas Edison State UniversitySigned partner
University of the PeopleSigned partner
Charter Oak State CollegeSigned partner
Strayer UniversitySigned partner
Columbia Southern UniversitySigned partner
Indiana TechSigned partner
Penn StateWorks with NCCRS
Southern New Hampshire UniversityWorks with NCCRS
SUNYWorks with NCCRS
+many more cooperating universitiesFind my college →Find my college → Not listed? Ask Athena in the corner of this page, or book a 15-minute call.
FAQ
Questions students ask us
About Writing Code That Holds Up: Reliable Programming Practices
What is defensive programming?
What is a trust boundary in programming?
Why is returning a silent default value worse than crashing?
Why should code catch specific exceptions instead of all exceptions?
About the Software Engineering course
How many college credits is Software Engineering?
How much does Software Engineering cost and what is included?
Which universities accept UPI Study credits?
How long does Software Engineering take?
How are the exams done?
How do my credits reach my college?
Ready for the full course? Earn 3 credits in Software Engineering.
103 video lessons, a quiz after each one, and the graded work that earns the credit. Recommended for credit by NCCRS.
3 credits · NCCRSEnroll →