CSC 1029 Week 08 Common Code Mistakes & Log Files PDF

Document Details

DivineZebra9695

Uploaded by DivineZebra9695

Red Rocks Community College

Tags

C++ programming coding errors debugging computer science

Summary

This document is a collection of notes on common coding errors, debugging techniques, and outputting information. It includes examples from various sources such as \"www.cprogramming.com\" and \"https://pythontutor.com\" to illustrate common mistakes. The notes are designed for use in a computer science class, particularly in C++ programming.

Full Transcript

CSC 1029 COMMON CODE MISTAKES OBJECTIVES AGENDA: WEEK 08 Identify common coding errors like off-by-one 1. Module: The way of the program errors, incorrect data type usage, logic flaws, & Problem Solving and potential infinite loops. Ex...

CSC 1029 COMMON CODE MISTAKES OBJECTIVES AGENDA: WEEK 08 Identify common coding errors like off-by-one 1. Module: The way of the program errors, incorrect data type usage, logic flaws, & Problem Solving and potential infinite loops. Explain the causes and consequences of these 2. Common Coding Mistakes common errors in different programming 3. Common Coding Mistake contexts. Examples Develop strategies to proactively avoid these mistakes by implementing best practices during 4. Outputting Information code development. 5. Logging Categorize common coding errors based on their nature, like syntax errors, runtime errors, 6. TODO and logical errors. 7. Resources for Help Apply debugging techniques to identify and resolve coding errors in provided code examples. MODULE: THE WAY OF THE PROGRAM Complete the chapter module 1.1. The Way of the Program 1.2. What is a Programming Language? 1.3. What is a Program? 1.4. What is Debugging? 1.5. Formal and Natural Languages 1.6. The First Program 1.7. Glossary www.cprogramming.com COMMON CODING MISTAKES /tutorial/common.html Overstepping Undeclared Mismatched array Variables code blocks boundaries Unitialized Undeclared Misusing the && Variables Functions and || operators Setting Variable Using single to uninitalized equal sign to value check equality COMMON INPUT ERROR Not understanding how the delimiter is used to read data from the stream. The difference when using >> and using getline Fix and Run FORGETTING TO INCLUDE HEADER FILE It's easy to forget to include necessary header files such as or , leading to compilation errors when using functionality from those libraries. Match the function with the correct header file 1. pow 2. std::setprecision 3. toupper 4. transform 5. std::stoi SYNTAX ERRORS Missing semicolons at the end of statements or placing them incorrectly can cause compilation errors. Fix and Visualize MEMORY MANAGEMENT ERRORS & PTRS Memory management errors: o Students often forget to properly allocate and deallocate memory using new, delete, malloc, free, etc., leading to memory leaks or access violations. Improper pointer usage: o Mishandling pointers by dereferencing null pointers, accessing out-of-bounds memory, or failing to deallocate dynamically allocated memory properly can result in crashes and memory leaks. Fix and Visualize USING UNINITIALIZED VARIABLES Forgetting to initialize variables before using them can lead to unpredictable behavior and bugs that are hard to trace. Fix and Visualize MISUNDERSTANDING ARRAY INDEXING C++ arrays are zero-indexed, meaning the first element is at index 0. Students sometimes forget this and access arrays incorrectly, leading to bugs. Fix and Visualize PASS BY VALUE AND PASS BY REFERENCE Confusion between pass by value and pass by reference: o Failing to understand the difference between passing arguments by value and by reference can lead to inefficiencies or unintended modifications of data. Fix and Visualize MIXING UP = AND == Confusing assignment (=) with comparison (==) in conditional statements can lead to logical errors in the code. Fix and Visualize IGNORING RETURN VALUES Neglecting to check return values of functions, especially those that may fail, can lead to unexpected behavior or bugs going unnoticed. Fix and Visualize NOT USING CONST CORRECTLY Misusing const can lead to unintentional modifications of variables or objects that were meant to be constant. Fix and Visualize FAILURE TO HANDLE ERRORS Ignoring error handling can lead to programs that crash or behave unexpectedly in exceptional conditions. Fix and Visualize INEFFICIENT ALGORITHMS Using inefficient algorithms or data structures can lead to poor performance of the program, especially when dealing with large datasets. Fix and Visualize MIXING C AND C++ C++ offers features that make code safer and more robust compared to C, but students may sometimes mix C-style code with C++, which can lead to inconsistencies or bugs. Fix and Visualize NOT UNDERSTANDING OOP In C++, students may struggle with concepts like inheritance, polymorphism, and encapsulation, leading to poor design choices or misuse of these features. Fix and Visualize OUTPUTTING INFORMATION In C++, std::cout, std::cerr, and log files are all used for outputting information, but they serve different purposes: o Use std::cout for normal output intended for the user. o Use std::cerr for error messages and diagnostic information. o Use std::clog for "logging" o Use log files for persistent storage of program information, debugging, and analysis. STD::COUT Standard output stream: Used for general output, typically directed to the console. Buffered: Output is buffered, meaning it might not immediately appear on the console. You can use std::flush or std::endl to force the output to be displayed. Redirection: You can redirect the output of std::cout to a file or another stream using shell redirection operators (>). STD::CERR Standard error stream: o Used for error messages and diagnostic information. Unbuffered: o Output is typically unbuffered, meaning it's displayed immediately on the console, ensuring that error messages are seen promptly. Redirection: o Like std::cout, you can redirect std::cerr to a file using shell redirection operators (>). STD::CLOG Standard output stream for logging o Similar to std::cerr, but buffered, meaning output may be held until the buffer is full or flushed manually. Buffered: Output is buffered, meaning it might not immediately appear on the console. You can use std::flush or std::endl to force the output to be displayed. Redirection: You can redirect the output of std::clog to a file or another stream using shell redirection operators (>). LOG FILE Persistent storage: o Log files are files on disk used to store information about the execution of your program. Flexibility: o You can customize the format, content, and level of detail in your log messages. Debugging and analysis: o Log files are useful for debugging problems, analyzing program behavior, and tracking events. https://github.com/OWASP/CheatSheetSeries/blob/master/cheatshe ets/Logging_Cheat_Sheet.md EARN YOUR PRE-WORK GRADE Post your weekly discussion question and research solution to D2L TODO Complete Week 08 Content Module in D2L to 100% WHAT'S COMING UP NEXT...WEEK 09 QUESTIONS | CLARIFICATIONS | HELP Student Office Hours: Schedule Meeting with Julie o By Appointment (both on-campus and remote via Zoom) o Drop-In Times Available (on-campus) Email: [email protected] RRCC On Campus Tutoring: https://www.rrcc.edu/learning- commons/tutoring 24/7 Online Tutoring: D2L > Content > Resources for Help

Use Quizgecko on...
Browser
Browser