// IITM BS ยท SOFTWARE ENGINEERING ยท WEEK 7

git commit -m
"Code likho, Debug karo,
Clean rakho
"

Week 7 ka poora syllabus ek jagah โ€” Version Control (Git), Issue Tracking & Code Review, Debugging, Software Metrics aur Clean Code. Saath me Graded Assignment ke saare 12 questions, answers + explanation ke saath. Hinglish me, taaki padhna easy rahe. ๐Ÿš€

01 ยท Tools & Frameworks 02 ยท Version Control 03 ยท Branch / Merge / Rebase 04 ยท Issues & Code Review 05 ยท Debugging 06 ยท Software Metrics 07 ยท Clean Code ๐ŸŽฏ Assignment Q&A
01

Development Tools & Frameworks

// week ki shuruaat โ€” code likhne ke tools

Week 7 basically "software development in practice" ke baare me hai. Ab tak humne design, requirements waghera dekha โ€” is week hum dekhte hain ki actual me code likhte, share karte, debug karte aur measure kaise karte hain.

Text Editor / IDE

Code likhne ke liye. IDEs (jaise VS Code) me linters, debuggers, git integration sab built-in hota hai โ€” productivity boost.

Compiler / Interpreter

Source code ko machine-executable form me convert karta hai. Ye code likhne ka tool nahi, code run karne ka tool hai.

Framework ๐Ÿ—๏ธ

Ready-made structure + pre-built modules deta hai (authentication, routing etc.) taaki aap wheel reinvent na karo. Standard design patterns follow karne me help karta hai.

Version Control System

Code ke versions manage karne ke liye โ€” Git. Iski detail neeche.

Flask kya hai?

Flask ek web application framework hai Python ka, aur ye ek micro-framework hai. "Micro" ka matlab โ€” ye aap par zyada restrictions impose nahi karta: kaunsa database use karna hai, kaunsa web server โ€” sab aapki choice. Lightweight aur flexible.

EXAM TRAP โš ๏ธFlask ek mobile framework NahI hai, aur ye databases/servers par restrictions impose nahi karta โ€” assignment Q1 me yehi options galat the!
02

Version Control Systems (Git)

// teamwork + history + versions

Problem kya solve kar rahe hain?

Developers badi teams me kaam karte hain. Toh issues aate hain:

๐Ÿ”€ Ek hi project par concurrent changes kaise handle karein?
๐Ÿ—‚๏ธ Software ke multiple versions kaise manage karein? (Kya multiple copies rakhna sahi hai? Nahi!)
๐Ÿ•ต๏ธ Kisne, kab, kya change kiya โ€” track kaise karein?

Iska efficient solution = Version Control System (VCS).

Centralized vs Distributed VCS

Centralized VCSDistributed VCS (Git) โญ
Ek central server par repo hota haiPoora repo (full history ke saath) har developer ke paas locally mirrored hota hai
Server down = kaam ruk gayaServer par complete dependency nahi โ€” sab apni local copy ke owner hain
Purana approachAaj kal zyada popular

Git โ€” free & open source, Linus Torvalds ne 2005 me Linux kernel development ke liye banaya tha. GitHub = online Git repository hosting service (web GUI, access control, bug tracking, task management, CI, wikis).

Git ke 3 States โ€” sabse important concept! ๐Ÿ”‘

Modified โœ๏ธโ†’ git add โ†’Staged ๐Ÿ“ฆโ†’ git commit โ†’Committed โœ…โ†’ git push โ†’Remote โ˜๏ธ

Staging area kyu hai? Socho aapne 10 files modify ki, but 3 sirf testing ke liye thi. Staging se aap sirf wahi files select kar sakte ho jo actually commit karni hain. Baaki chhod do.

Core Git Commands

CommandKya karta hai
git initCurrent folder me naya repo initialize (naya project shuru karna)
git clone urlServer se target repo download / mirror karta hai locally
git add fileModified file ko staged area me daalta hai (current commit me include karna)
git commit -m "msg"Staged changes ko local database me commit karta hai, message ke saath
git pushLocal commits ko remote repository par bhejta hai
git pullRemote ke naye changes fetch karke local repo update karta hai
git statusWorking directory + staging area ka status dikhata hai
git diffCommits / working tree ke beech differences dikhata hai
git reset HEAD fileFile ko unstage karta hai
git checkout fileCommitted-but-not-pushed changes undo
git logCommit history dikhata hai
ASSIGNMENT SCENARIO ๐Ÿ’ก

git status me dikha: file1.py modified hai, aur untracked me data.txt (4 GB raw data โ€” share nahi karna) + lib/ folder (share karna hai). Best strategy:

$ git add lib file1.py
$ git commit -m "adding new files"

Sirf wahi cheezein git add karo jo share karni hain โ€” data.txt ko chhod do. git commit -a use karte toh problem, kyunki wo bhi sirf tracked files leta but selective control nahi milta jaisa yahan chahiye.

03

Branching, Merging & Rebase

// parallel kaam, ek final build

Branching

Branch = code ki ek independent line of development. Har feature / bug-fix ke liye alag branch banao, master ko stable rakho.

# nayi branch banao aur usme kaam karo
$ git checkout -b feature1
# changes karo...
$ git add changed_file.py
$ git commit -m "user story implemented"

Merging

Merge = ek ya zyada branches ke changes ko ek single branch me combine karna (taaki final software build ban sake).

$ git checkout master        # pehle master par aao
$ git merge feature1         # feature1 ke changes master me merge

Agar master aage badh chuka ho (koi aur commit aa gaya), toh Git three-way merge karta hai โ€” no conflicts ho toh automatic, warna conflicts manually resolve karke merge karo.

Conflict kab hota hai? ๐Ÿ˜ค

Jab do branches ne same file ki same lines ko alag-alag tarike se change kiya ho. Git confuse ho jata hai ki kaunsa version rakhe โ€” developer ko decide karna padta hai.

Git Rebase

Scenario: Aap feature1 par kaam kar rahe the. Beech me priority issue aaya โ†’ issueP branch banai โ†’ fix karke (function fun1 add kiya) master me merge kar di. Ab feature1 me wapas aaye โ€” but aapko fun1 chahiye jo feature1 me hai hi nahi (kyunki branch purane commit se bani thi)!

$ git checkout feature1
$ git rebase master          # feature1 ko master ke latest commit par le aao
# ab fun1() available hai โ€” use karo, test karo
$ git checkout master
$ git merge feature1         # conflicts rebase me hi resolve ho chuke
YAAD RAKHO

Merge โ€” branches ko combine karta hai. Rebase โ€” aapki branch ka base latest commit par shift kar deta hai, jisse doosri branch ke changes aapko mil jaate hain aur history linear rehti hai.

04

Issue Tracking & Code Review

// GitHub par team collaboration

Issue Tracking kya hai?

Jab developer / tester / user ko koi bug ya problem milti hai, wo ek issue report karta hai. Issue ko fix hone tak save aur track karna zaroori hai โ€” warna bhool jayenge!

GitHub Issues ki features

โœ… Doosre developers issue dekh sakte hain, comments add kar sakte hain
โœ… Issue ko specific developer ko assign kar sakte ho (assign/tag hone par email notification jaata hai)
โœ… Labels laga sakte ho โ€” baad me similar issues filter karke dhoondh sakte ho
โœ… Issue se directly branch ya Pull Request map kar sakte ho (many-to-many mapping bhi possible)
โœ… Fix hone par issue close kar do โ€” filters se open/closed issues list kar sakte ho

Pull Request (PR) + Code Review flow

Issue create ๐Ÿ›โ†’Branch banaoโ†’Fix + Commitโ†’Pull Request ๐Ÿ“ฌโ†’Review ๐Ÿ‘€โ†’Approve โœ…โ†’Mergeโ†’Issue auto-close ๐ŸŽ‰

Issue page se hi branch banao toh PR automatically issue se link ho jaata hai. Reviewer PR kholta hai, "Files changed" tab me changes dekhta hai, specific line numbers par comments add karta hai. Sab comments address hone ke baad reviewer Approve mark karta hai. Fir merge button se changes source branch me merge โ€” aur linked issue apne aap close!

05

Debugging

// bug dhoondo, bug maaro ๐Ÿ”จ๐Ÿ›

3 Key Terms โ€” Error, Failure, Fault

TermMatlab
ErrorActual behavior aur intended behavior me discrepancy (jo hona chahiye tha vs jo hua)
FailureJab error observable ho โ€” galat output value ya exception dikhe
FaultWo line of code jahan ye failure originate hua

Debugging = failure ka cause dhoondhna + code rectify karna taaki failure chala jaye. Steps: Reproduce the problem โ†’ Find the cause โ†’ Investigate & implement the fix โ†’ Test.

Debugging Techniques (tools/tarike) ๐Ÿ› ๏ธ

1. Logging ๐Ÿ–จ๏ธ

Strategic jagahon par print statements daal ke output examine karna. Sabse common technique.

2. Dump & Diff ๐Ÿ“„

Diff tool se do output files compare karo โ€” correct run ka log vs incorrect run ka log. Difference se error location milti hai.

3. Stepping in Debugger ๐Ÿ‘ฃ

Debugger (jaise Python ka pdb) se code line-by-line execute karo, variables ki values dekhte hue.

4. Profiling Tool โฑ๏ธ

Statistics deta hai โ€” program ke kaunse parts kitni baar / kitni der execute hue. Performance issues ke liye best.

Debugging Strategies (approach/soch) ๐Ÿง 

Input Manipulation

Alag-alag inputs do, outputs ke differences observe karo, aur usse fault guess karo. Jaise: minimum-finder program ko different lists dekar dekhna kahan galat aata hai.

Backwards Strategy โฌ…๏ธ

Jo statement galat output generate kar raha hai use dhoondo, phir data & control dependencies ko peeche (backwards) follow karke incorrect line tak pahuncho.

Forwards Strategy โžก๏ธ

Backwards ka ulta โ€” jo event trigger karta hai wahan se shuru karo, aur control flow ko aage (forward) trace karo jab tak incorrect state na mile.

Blackbox Debugging ๐Ÿ“ฆ

Jab external APIs/libraries use kar rahe ho (inner working access nahi) โ€” documentation aur code examples padh ke check karo ki usage sahi hai ya nahi.

Python Debugger (pdb)

# tarika 1: command line se
$ python -m pdb myprogram.py

# tarika 2: code ke andar se
import pdb
pdb.set_trace()   # yahan execution pause hoga

Execution pause hota hai, aap line-by-line step kar sakte ho. Exit karne ke liye Ctrl+Z.

06

Software Metrics

// code quality ko numbers me naapna ๐Ÿ“

Software metrics = code ki complexity ko quantitatively measure karna. Complex code = samajhna mushkil = bugs ka zyada risk. Python me tool: Radon (pip install radon).

A. Cyclomatic Complexity (CC)

Formula ekdum simple: CC = number of decisions + 1

ConstructCC par effect
if+1 (naya decision)
elif (else-if)+1 (naya decision)
else+0 โ€” koi naya decision nahi! โš ๏ธ
for+1 (loop ke start par decision)
while+1 (condition check = decision)
SOLVED EXAMPLE (Assignment Q2)
def lsearch(key, lst):
    for i in lst:        # decision 1
        if i == key:      # decision 2
            print("key found")
            return
    print("key not found")

Decisions = 2 (ek for, ek if) โ†’ CC = 2 + 1 = 3 โœ…

Radon CC Ranks (A se F)

RankCC ScoreMatlab
A1 โ€“ 5Low โ€” simple block ๐Ÿ˜Ž
B6 โ€“ 10Low โ€” well structured, stable
C11 โ€“ 20Moderate โ€” slightly complex
D21 โ€“ 30More than moderate โ€” more complex
E31 โ€“ 40High โ€” complex, alarming
Fโ‰ฅ 41Very high โ€” error-prone, unstable ๐Ÿšจ

CC zyada ho toh solution: function ko chhote functions me split karo (refactoring). Radon command: radon cc -s file.py

B. Raw Metrics

๐Ÿ“ LOC โ€” Total lines of code (code + comments + docstrings + blanks, sab kuch)
๐Ÿง  LLOC โ€” Logical lines of code (asli logical statements)
๐Ÿ“„ SLOC โ€” Source lines of code (jaise likha gaya)
๐Ÿ’ฌ Comments & comment ratio โ€” kitna documented hai code

Difference example: agar ek line ko \ (backslash) se do lines me todo, toh SLOC me 2 lines count hongi but LLOC me sirf 1. Command: radon raw file.py

C. Halstead Metrics

Code ki measurable properties aur unke relationships par based:

ParameterKya count karta hai
h1Distinct operators (jaise +, โˆ’, %, <, ++ ...)
h2Distinct operands
N1Total operators
N2Total operands

In 4 se calculate hote hain: vocabulary, program length, volume, difficulty, effort etc. Command: radon hal file.py

EXAM POINT

Question me "distinct operators + total operators + distinct operands + total operands... vocabulary, difficulty, volume" โ€” ye keywords dikhe toh answer = Halstead's Metrics. (Assignment Q6!)

07

Writing Clean Code

// code smells + refactoring + pylint ๐Ÿงผ

Code smells = code ki problematic/inelegant characteristics. Code kaam toh karta hai, but "kuch sahi nahi lag raha". Reference book: Clean Code by Robert Martin (63 code smells!).

Comments se related smells ๐Ÿ’ฌ

Redundant comments

Jo cheez code padhke obvious hai (# incrementing i by 1) โ€” uska comment mat likho. Comments sirf "why" explain karne ke liye.

Commented-out code

Final codebase me commented-out code NAHI rakhna chahiye โ€” delete karo! Zaroorat padi toh Git history se wapas mil jayega.

Functions se related smells ๐Ÿ”ง

๐Ÿ“Œ Golden rule: ek function ko sirf EK kaam karna chahiye. "Function that does many things" = bad!
๐Ÿ“Œ Functions short hone chahiye โ€” lambe function ka matlab wo ek se zyada kaam kar raha hai
๐Ÿ“Œ Arguments: ideally 0 arguments; practically 3 se zyada nahi. Zyada arguments = function split karo
๐Ÿ“Œ Flag arguments mat use karo โ€” flag ki value ke basis par function alag kaam kare = control coupling = bad. Solution: har flag value ke liye alag function
๐Ÿ“Œ Dead functions (jo kahin call nahi hote) โ€” remove karo

General smells ๐ŸŒ

DRY Principle

Don't Repeat Yourself โ€” identical code baar-baar likhne ke bajaye ek method me daalo aur call karo.

Boundary conditions

Boundaries par code sahi behave kare โ€” "incorrect behavior at the boundaries" ek common smell hai.

Explanatory variables

m jaise cryptic naam mat rakho โ€” thirty_day_months jaisa self-explanatory naam use karo.

Refactoring โ™ป๏ธ

Code ka structure improve karna behavior change kiye bina. Smells hatao, code wahi kaam karta rahe.

Pylint โ€” static code analyzer

$ pip install pylint
$ pylint myfile.py

Pylint source code scan karke batata hai: naming convention issues (Python me snake_case chahiye), missing docstrings, unused variables, unreachable code. Message types: C = convention, W = warning. Doosre tools: flake8, Vulture. Zyaadatar IDEs (VS Code) me linters pre-installed hote hain.

๐ŸŽฏ

Graded Assignment 7 โ€” Q&A

// question par click karo โ†’ answer + explanation khulega
Q1Flask framework ke baare me kaunse statements correct hain? (MSQ)
A. It is a mobile application framework
B. It is a web application framework โœ”
C. It is a micro-framework โœ”
D. It imposes several restrictions (databases, web servers...)
โœ… Answer: B aur C

Flask Python ka web framework hai (mobile nahi), aur "micro" hai kyunki ye minimal core deta hai aur database/server jaisi choices par koi restriction impose nahi karta โ€” isliye D bhi galat.

Q2lsearch function (for loop + if condition wala) ki cyclomatic complexity kya hai?
A. 1
B. 2
C. 3 โœ”
D. 4
โœ… Answer: C (3)

CC = decisions + 1. Code me for (1 decision) + if (1 decision) = 2 decisions โ†’ 2 + 1 = 3. Yaad rakho: return aur print decisions nahi hote.

Q3Kaunsi cyclomatic complexity range error-prone, unstable code represent karti hai?
A. 11โ€“20
B. 21โ€“30
C. 31โ€“40
D. โ‰ฅ 41 โœ”
โœ… Answer: D (>= 41)

Radon ranks me F rank (CC โ‰ฅ 41) = very high risk, error-prone, unstable. Matlab ek hi function me ~41 conditions chal rahi hain โ€” turant refactor karo, functions me split karo!

Q4Developer galat output wala statement dhoondh ke data & control dependencies ko backwards follow karta hai. Kaunsi strategy?
A. Input manipulation
B. Backwards strategy โœ”
C. Forwards strategy
D. Blackbox debugging
โœ… Answer: B

Definition hi question me likhi hai โ€” incorrect output wale statement se peeche ki taraf dependencies trace karna = Backwards strategy. (Forwards me trigger event se aage trace karte hain.)

Q5Code smells se related TRUE statements identify karo. (MSQ)
A. Commented-out code final codebase me retain karna chahiye
B. Function jo bahut saare kaam kare wo better hai
C. Ideally function me koi argument nahi; practically 3 se zyada nahi โœ”
D. Function me flag argument use nahi karna chahiye โœ”
โœ… Answer: C aur D

A galat โ€” commented code delete karo (Git history hai na). B galat โ€” function ko ek hi kaam karna chahiye. C aur D Robert Martin ke exact rules hain: max 3 arguments, aur flag arguments = control coupling = bad.

Q6Kaunsi metric distinct/total operators aur operands se program vocabulary, difficulty, volume measure karti hai?
A. Cyclomatic complexity
B. Raw Metrics
C. Halstead's Metrics โœ”
D. None of the above
โœ… Answer: C

Halstead metrics h1 (distinct operators), h2 (distinct operands), N1, N2 se vocabulary, volume, difficulty, effort calculate karti hai. Cyclomatic = decisions count; Raw = lines of code count.

Q7Minimum-finder program debug karne ke liye developer various inputs deta hai aur output differences observe karta hai. Kaunsi strategy?
A. Input manipulation โœ”
B. Backwards strategy
C. Forwards strategy
D. Blackbox debugging
โœ… Answer: A

"Various inputs + observe output differences" = textbook definition of Input Manipulation. Inputs badal-badal ke pattern samjho, fault guess karo.

Q8git status me file1.py modified hai; untracked me data.txt (4GB, share nahi karna) aur lib/ folder (share karna hai). Best version control strategy?
B. git commit -ma "adding new files..."
C. git add lib โ†’ git commit -m "adding new library..."
D. git add lib file1.py โ†’ git commit -m "adding new files" โœ”
โœ… Answer: D

Share karna hai: file1.py (modified) + lib/ (naya folder). Share nahi karna: data.txt. Toh selectively sirf lib aur file1.py stage karo aur commit karo. Option C me file1.py miss ho jaata; commit -a se untracked data.txt handle hi nahi hota properly + selective control nahi milta.

Q9Java code: PrintDivBy3(n, m) โ€” [n, m] range me 3 se divisible numbers print karta hai (but loop me i < m hai). Kaunsa input-output combination error reveal karta hai?
A. [10, 20] โ†’ 12 15 18
B. [30, 40] โ†’ 30 33 36 39
C. [20, 30] โ†’ 21 24 27 โœ”
D. [10, 25] โ†’ 12 15 18 21 24
โœ… Answer: C

Bug: loop i < m hai, hona chahiye tha i <= m (range [n, m] inclusive hai). Bug tab hi dikhega jab m khud 3 se divisible ho โ€” tab m print hona chahiye tha but nahi hua. C me: [20, 30] โ†’ 30 divisible hai by 3, expected output me 30 aana chahiye tha (21 24 27 30), but sirf "21 24 27" aaya โ†’ error reveal! A me 20, B... wait B me 40 divisible nahi, aur B ka output 30 se start hota hai jo sahi hai (30 included as start) โ€” but 39 tak sahi. D me 25 divisible nahi. Sirf C me boundary value (30) miss hona detect hota hai. Ye input manipulation ka practical example hai!

Q10Match the following โ€” git commands vs actions

I. git init โ†’ 4. Start creating a new project repository
II. git clone โ†’ 3. Download the target repository from the server
III. git push โ†’ 2. Move local repository changes to the remote repository
IV. git add โ†’ 5. Include the modified file/directory to the current commit

โœ… Answer: C (I-4, II-3, III-2, IV-5)

Note: option 1 ("create a new file in current folder") kisi se match nahi hota โ€” git files create nahi karta, versions manage karta hai!

Q11Team standard design patterns follow karna chahti hai aur authentication/routing ke pre-built modules use karna chahti hai. Best tool?
A. Version Control System
B. Compiler
C. Framework โœ”
D. Text Editor
โœ… Answer: C

"Pre-built modules" + "standard design patterns" = Framework ki definition (jaise Flask/Django me authentication, routing built-in structure milta hai). VCS versions ke liye, compiler code convert karne ke liye, editor likhne ke liye.

Q12Developer pehle print statements use karta hai, phir do runs ke logs compare karta hai, phir slow functions locate karne ke liye application profile karta hai. Kaunsi techniques?
A. Logging, Dump & Diff, Profiling tool โœ”
B. Stepping in debugger, Logging, Dump & Diff
C. Profiling tool, Stepping in debugger, Logging
D. Dump & Diff, Profiling tool, Stepping in debugger
โœ… Answer: A

Order match karo: print statements = Logging โ†’ logs compare between two runs = Dump & Diff โ†’ slow functions locate = Profiling tool (kitna time kahan lag raha). Ekdum sequence-wise mapping!

QUICK REVISION CHEAT-SHEET ๐Ÿ“

โ€ข Flask = web + micro framework, no restrictions
โ€ข CC = decisions + 1; else se CC nahi badhta; CC โ‰ฅ 41 = F rank = error-prone
โ€ข Techniques: Logging, Dump&Diff, Debugger stepping, Profiling | Strategies: Input manipulation, Backwards, Forwards, Blackbox
โ€ข Halstead = operators/operands โ†’ vocabulary, volume, difficulty
โ€ข Function: 1 kaam, โ‰ค3 args, no flags, no dead code | DRY | commented code delete
โ€ข git init (naya) / clone (download) / add (stage) / commit (local DB) / push (remote) / pull (fetch+update)
โ€ข Rebase = branch ko latest commit par shift; Issue โ†’ Branch โ†’ PR โ†’ Review โ†’ Approve โ†’ Merge โ†’ auto-close