Development Tools & Frameworks
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.
Version Control Systems (Git)
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 VCS | Distributed VCS (Git) โญ |
|---|---|
| Ek central server par repo hota hai | Poora repo (full history ke saath) har developer ke paas locally mirrored hota hai |
| Server down = kaam ruk gaya | Server par complete dependency nahi โ sab apni local copy ke owner hain |
| Purana approach | Aaj 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! ๐
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
| Command | Kya karta hai |
|---|---|
git init | Current folder me naya repo initialize (naya project shuru karna) |
git clone url | Server se target repo download / mirror karta hai locally |
git add file | Modified 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 push | Local commits ko remote repository par bhejta hai |
git pull | Remote ke naye changes fetch karke local repo update karta hai |
git status | Working directory + staging area ka status dikhata hai |
git diff | Commits / working tree ke beech differences dikhata hai |
git reset HEAD file | File ko unstage karta hai |
git checkout file | Committed-but-not-pushed changes undo |
git log | Commit history dikhata hai |
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.
Branching, Merging & Rebase
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
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.
Issue Tracking & Code Review
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 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!
Debugging
3 Key Terms โ Error, Failure, Fault
| Term | Matlab |
|---|---|
| Error | Actual behavior aur intended behavior me discrepancy (jo hona chahiye tha vs jo hua) |
| Failure | Jab error observable ho โ galat output value ya exception dikhe |
| Fault | Wo 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.
Software Metrics
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
| Construct | CC 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) |
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)
| Rank | CC Score | Matlab |
|---|---|---|
| A | 1 โ 5 | Low โ simple block ๐ |
| B | 6 โ 10 | Low โ well structured, stable |
| C | 11 โ 20 | Moderate โ slightly complex |
| D | 21 โ 30 | More than moderate โ more complex |
| E | 31 โ 40 | High โ complex, alarming |
| F | โฅ 41 | Very 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:
| Parameter | Kya count karta hai |
|---|---|
| h1 | Distinct operators (jaise +, โ, %, <, ++ ...) |
| h2 | Distinct operands |
| N1 | Total operators |
| N2 | Total operands |
In 4 se calculate hote hain: vocabulary, program length, volume, difficulty, effort etc. Command: radon hal file.py
Question me "distinct operators + total operators + distinct operands + total operands... vocabulary, difficulty, volume" โ ye keywords dikhe toh answer = Halstead's Metrics. (Assignment Q6!)
Writing Clean Code
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
Q1Flask framework ke baare me kaunse statements correct hain? (MSQ)
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?
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?
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?
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 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?
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?
"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?
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?
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
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?
"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?
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!
โข 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