Programming Essentials: Writing, Testing, and Debugging Code

UPI Study › Courses › System Analysis and Design › Free lessons › Lesson 7.3 Free with a free account

Programming Essentials: Writing, Testing, and Debugging Code

System Analysis and Design · Chapter 7: Implementation, Testing, and MaintenanceSection 7.3 · lesson 48 of 699 min · free with a free account

← Writing It Down: Documentation and Communication Best PracticesNext lesson: Integration Testing: Combining Modules and Validating Their Interactions Step by Step →

Earn 3 college credits for System Analysis and DesignRecommended for credit by NCCRS · Computer Science 390 · self-paced · one payment, lifetime access

In this lesson

Coding standards, structured and modular code, quality, unit tests, and debugging.

  • Explain why coding standards matter, and what they cover
  • Explain structured and modular programming
  • Identify markers of code quality beyond running without errors
  • Explain unit testing, and apply core debugging techniques

Transcript

Everything said in the video, section by section (1,260 words). Read along, or search it.

Implementation, Testing, and Maintenance

Programming Essentials

This section is about the code itself. How it is written, tested, and debugged. We will cover coding standards, and two ways of organizing a program, structured and modular programming. Then what makes code good, rather than just working. And finally, unit tests, and the core techniques for tracking down a bug.

Coding Standards

Not What Code Does, but How It Is Written

A coding standard is a set of agreed rules, for how code should be written within a team or organization. Notice what it governs. Not what the code should do, but how it is written while doing it. Why does that matter? Because code is read far more often than it is written. Think of a developer joining the project months later, or a teammate fixing a bug at two in the morning. They benefit enormously from code that follows a consistent, predictable style. Not code that reflects whatever habits its original author happened to have. Coding standards are one of the most direct ways a team supports maintainability, right at the level of the code.

What a Standard Covers

4 Elements of a Coding Standard

A typical coding standard covers four elements. Naming conventions give consistent rules for naming variables, functions, and files. So a project always uses one form, like calculate total, instead of mixing three different spellings of the same name. Indentation and formatting keep spacing, line breaks, and bracket placement consistent. Every nested block is indented by the same number of spaces. Commenting practices set when and how comments explain code. A good comment above a complex calculation explains why it works the way it does, not just what it does. And file and module organization sets where different kinds of code live, such as keeping database code in its own folder, apart from the user interface.

Structured Programming

Just 3 Control Structures

Structured programming builds a program out of just three control structures. Sequence, where steps run one after another. Selection, where a decision, such as an if-else branch, chooses between paths. And iteration, where a loop repeats a set of steps. Why restrict a program to only these three? Because they stop code from jumping around unpredictably. The flow stays easy for another person to read, from top to bottom.

Modular Programming

One Module, One Task

Modular programming takes a different angle. It is about organization. A program is broken into separate, self-contained modules, functions or procedures, each handling one specific task. A module dedicated to a single task is easier to write correctly. It is easier to test in isolation. And it is easier to reuse elsewhere, compared with one large block of code trying to do everything at once. This is a different idea from encapsulation, from the object-oriented approach. A module organizes code by task. Encapsulation bundles data and behavior together inside an object. The two ideas can, and often do, work side by side.

Figure 7.1

A Structure Chart

A structure chart shows modular decomposition visually. At the top is the module process order. It handles the overall task by calling lower-level modules. Validate order, calculate total, update inventory, and send confirmation. Each handles one piece of the job. Those modules can call further modules of their own. Here, calculate total calls apply discount, and calculate tax. Each arrow shows which module calls which. Process order does not need to know how calculate total works inside. Only that it can call it, and rely on the result. That is the same top-down thinking used to break any large problem into smaller, independently understandable pieces.

Code Quality

Running Is Not the Same as Good

Code that runs without crashing is not automatically good code. Five traits distinguish code that holds up over a project's life. Readability. Another developer can follow what the code does, without excessive effort. Descriptive names, not cryptic abbreviations. Efficiency. The code uses time and resources reasonably, avoiding a slow approach when a faster one is just as simple. Robustness. The code handles unexpected or invalid input, without crashing. Such as checking that a quantity is a positive number before using it. Reusability. A module can be used in another context without being rewritten, like a validate email function any part of the system can call. And documentation. Comments explain non-obvious decisions, such as why an unusual workaround was needed. A function that works, but uses cryptic names and has no comments, is still low quality. It lacks readability.

Unit Testing

Known Input, Expected Output

Unit testing checks the smallest testable pieces of a program, usually individual functions, in isolation from the rest of the system. It is the same unit testing level from the V-Model. Here we see what a unit test actually looks like. A unit test supplies a known input, and checks whether the result matches the expected output. Take a calculate total function that gives a ten percent discount on orders over one hundred dollars. An order of fifty should come back as fifty, because it is below the discount threshold. An order of one hundred fifty should come back as one hundred thirty-five, with the discount applied. And an order of zero, the edge case of an empty order, should come back as zero. Because unit tests are automated, the whole set can be re-run every time the code changes. That quickly flags when a change in one part broke something that used to work elsewhere. This is called regression testing.

Debugging

5 Core Techniques

Debugging is finding and fixing a defect, once a program is not behaving as expected. Five techniques come up in almost every debugging session, whatever the language. First, reproduce the bug reliably. Find the exact steps that trigger it every time, so a fix can actually be verified. Second, read error messages carefully. A message and its stack trace usually point toward where the problem started. Third, isolate the problem. Narrow down which part of the code is responsible, often by testing smaller pieces separately. Fourth, step through with a debugger. Pause at a chosen line, called a breakpoint, and inspect variable values, one step at a time. And fifth, explain the code aloud. Describing it line by line, even to no one, often surfaces the bug. This is sometimes called rubber duck debugging.

Debugging Tools

Debuggers and Logging

Two tools support this work. The most common is a debugger, built into most development environments. It lets a developer set breakpoints, step through code one line at a time, and watch variable values change. Simpler, but still widely used, is logging. Logging means inserting statements that print variable values, or progress messages, while the program runs. It leaves a trail to follow, when a full debugger is not convenient. So what does setting a breakpoint and inspecting variables reveal? Exactly where a variable's value stops matching what was expected. That narrows down where the defect really is.

Key Terms

The Vocabulary of This Section

Let's lock in the key terms. A coding standard is a set of agreed rules for how code is written. Structured programming uses only sequence, selection, and iteration. Modular programming breaks a program into self-contained modules, each handling one task. A structure chart shows which module calls which. A unit test checks one piece of code in isolation. Regression testing re-runs the tests after every change. And a breakpoint is a chosen line where a debugger pauses execution.

Key Takeaways

What to Carry Forward

To pull this together. Coding standards give a team a consistent, predictable style, supporting maintainability at the level of the code. Structured programming limits code to sequence, selection, and iteration. Modular programming breaks a program into self-contained, single-purpose modules. Good code is readable, efficient, robust, reusable, and documented, not just functional. A unit test supplies known input to a small piece of code, and checks the result against the expected output. And debugging relies on reproducing the bug, reading error messages, isolating the problem, and stepping through with a debugger or logging.

About System Analysis and Design at UPI Study

System Analysis and Design (Computer Science 390) is a self-paced online course of 69 video lessons in 9 chapters, from the analyst's role and requirements to UML modeling, interface and database design, testing, maintenance 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.

Compare: Software Engineering vs Systems Analysis

Why take it here

Why students take System Analysis and Design with UPI Study

From business problem to working systemSystem Analysis and Design teaches how analysts study a business need and turn it into a system design: requirements, UML and data-flow models, interface and database design, implementation and project management. It is a core course in information systems and IT degrees.
Credit at a fraction of the priceThe same 3 credits can cost $750 or more at a US college. Here it is one payment, self-paced, with lifetime access.
Finish your degree soonerTransfer credits reduce the courses you still owe, so you can graduate earlier or lighten a heavy semester.
Strengthen an applicationA completed college-level course on an official transcript shows admissions and scholarship committees you can do the work.
Study on your own scheduleNo start dates, no deadlines. Watch the lessons, practice, and take the final when you are ready.
130,000+students helped with college creditsACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.

Where the credit goes

Universities that work with NCCRS

System Analysis and Design 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.

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 Programming Essentials: Writing, Testing, and Debugging Code

What is the difference between structured and modular programming?
Structured programming controls the flow of a program, while modular programming organizes it into pieces. Structured programming limits code to three control structures: sequence, selection such as if-else, and iteration such as loops, so it reads top to bottom without unpredictable jumps. Modular programming breaks a program into self-contained modules, each handling one task, which makes each piece easier to write, test, and reuse.
What makes code good quality beyond just running without errors?
Good code is readable, efficient, robust, reusable, and documented, not merely functional. Readable code uses descriptive names that another developer can follow. Efficient code avoids needlessly slow approaches. Robust code handles invalid input, such as a negative quantity, without crashing. Reusable modules work in other contexts, and comments explain non-obvious decisions. A working function with cryptic names and no comments is still low quality.
What is a unit test, with an example?
A unit test checks the smallest testable piece of a program, usually one function, in isolation by supplying a known input and comparing the result with the expected output. For a function that gives a 10 percent discount on orders over 100, an order of 50 should return 50, an order of 150 should return 135, and an empty order of 0 should return 0.
What are the basic steps of debugging code?
Debugging usually starts by reproducing the bug reliably so a fix can be verified. Next, read error messages and stack traces, which often point to where the problem began. Then isolate the faulty part by testing smaller pieces, and step through the code with a debugger, pausing at breakpoints to inspect variables. Logging statements and explaining the code aloud, known as rubber duck debugging, also help.

About the System Analysis and Design course

How many college credits is System Analysis and Design?
System Analysis and Design (Computer Science 390) carries a NCCRS credit recommendation for 3 semester credits. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.
How much does System Analysis and Design cost and what is included?
$250, paid once, with lifetime access: all 69 video lessons, a practice quiz after each, the graded work and final exam, and an official transcript when you finish. Students who want several courses choose UPI Study PRO instead.
Which universities accept UPI Study credits?
UPI Study has signed partner agreements with universities such as Thomas Edison State University, University of the People and Charter Oak State College, and works with universities such as Penn State, SNHU and SUNY. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities. Use "Find my college" on this page to check yours.
How long does System Analysis and Design take?
It is self-paced with no deadlines. The 69 lessons total about 9 hours of video; most students add reading and practice time and finish in a few weeks to a few months, on their own schedule.
How are the exams done?
Quizzes and assignments are online in the course. The final exam is taken online with proctoring; a person on the UPI Study team checks assignments, usually within two days.
How do my credits reach my college?
When the course shows 100% complete, request the official transcript from your dashboard. It is sent to the college you name, and you can send it to more than one. Check your college first with "Find my college" on this page, or ask Athena.

Ready for the full course? Earn 3 credits in System Analysis and Design.

69 video lessons, a quiz after each one, and the graded work that earns the credit. Recommended for credit by NCCRS.

System Analysis and Design
3 credits · NCCRS
Enroll →