💻 Programming

Bug

Bug

A software error. Divided into logic bugs, runtime bugs, and compile bugs. Resolved by debugging.

📖 Detailed Explanation

A bug is an error or defect that causes software to behave differently from what is expected. The term spread widely after Grace Hopper of Harvard University discovered in 1947 that an actual moth had become trapped in a relay of the Mark II computer and caused a malfunction, and first used the term "debugging."

Bugs are broadly classified into three types. A compile bug is a case where the program cannot be built because of a syntax error, while a runtime bug causes a crash or an exception during execution. A logic bug is the type that is hardest to find, in which the program runs but outputs incorrect results.

The severity of a bug is classified as Critical, Major, Minor, or Trivial. A critical bug is a case where the entire system stops or data loss occurs, while a minor bug is a small problem that has no significant impact on functionality. Bugs are managed systematically through a Bug Tracking System.

The process of finding and fixing bugs is called debugging. In modern development, bugs are prevented and found quickly by using IDE debuggers, logging, unit tests, static analysis tools, and the like. As the saying "there is no such thing as bug-free software" goes, bug management is at the core of software quality.

💻 Code Example

❌ Code with a bug
# 버그 1: Off-by-one 에러 (경계값 오류)
def get_average(scores):
    total = 0
    for i in range(1, len(scores)):  # 버그: 0이 아닌 1부터 시작
        total += scores[i]
    return total / len(scores)  # 버그: 잘못된 평균 계산

# 버그 2: None 체크 누락
def get_user_name(user):
    return user.name.upper()  # 버그: user가 None이면 크래시

# 버그 3: 부동소수점 비교 오류
def check_balance(amount):
    if 0.1 + 0.2 == 0.3:  # 버그: False 반환 (부동소수점 오차)
        return "정확함"
    return "오차 발생"

print(get_average([90, 85, 88, 92]))  # 88.33이 아닌 66.25 출력
print(get_user_name(None))  # AttributeError 발생
print(check_balance(100))  # "오차 발생" 출력
✅ Fixed code
# 수정 1: 올바른 범위와 계산
def get_average(scores):
    if not scores:  # 빈 리스트 체크 추가
        return 0
    total = 0
    for i in range(0, len(scores)):  # 수정: 0부터 시작
        total += scores[i]
    return total / len(scores)
    # 또는 더 간단하게: return sum(scores) / len(scores)

# 수정 2: 방어적 프로그래밍
def get_user_name(user):
    if user is None:  # 수정: None 체크
        return "Unknown"
    if not hasattr(user, 'name') or user.name is None:
        return "Unknown"
    return user.name.upper()

# 수정 3: 부동소수점 안전 비교
import math

def check_balance(amount):
    if math.isclose(0.1 + 0.2, 0.3):  # 수정: isclose() 사용
        return "정확함"
    return "오차 발생"

print(get_average([90, 85, 88, 92]))  # 88.75 출력 (정확)
print(get_user_name(None))  # "Unknown" 출력 (안전)
print(check_balance(100))  # "정확함" 출력 (정확)

🗣️ Say It Like This in Practice

💬 In a Meeting
"Two critical bugs were found in this sprint: one is a race condition in the payment module, and the other is a missing session timeout handling. We will deploy a hotfix today."
💬 In an Interview
"I have experience resolving a difficult bug that occurred in production. It was a memory leak that occurred only at certain times of day, and through profiling tools and log analysis, I found that a closure in a timer callback kept referencing an object."
💬 In a code review
"Here, the method is called directly without a null check. This kind of pattern causes NullPointerException bugs. It would be better to use Optional or a null-safe operator."

Take a mock interview on this topic → WIA SOOM AI Interview · Start for free

⚠️ Common Mistakes & Cautions

❌
Do not hide or postpone bugs

If you put it off with the thought "I'll fix it later," bugs pile up and become technical debt. Register them in the bug tracker as soon as you find them, and handle them by setting priorities according to severity.

❌
Do not fix only the symptoms; find the root cause

Simply adding try-catch where an error occurs only hides the bug. Use Root Cause Analysis to understand why the bug occurred and resolve it fundamentally.

✅
Document the reproduction steps clearly

In a bug report, clearly record "in what environment, by operating in what order, what result appears." Bugs that can be reproduced are much easier to fix.

✅
Add a regression test after fixing a bug

When you fix a bug, writing that case as test code can prevent the same bug from occurring again (regression).

🔗 Related Terms

📚 Learn More