Programmalıq támiynat qurılması hám evolyuciyası / 5-ameliy_PTQE
.pdf
5-ámeliy jumıs. Quramalılıqtı jeńiw usıllarına mısallar.
Jumıstıń maqseti: Programmalıq támiynattaǵı "Kútilmegen quramalılıqtı" (Accidental Complexity) hám "Zárúr quramalılıqtı" (Essential Complexity) ajıratıw. Quramalılıqtı jeńiw usılları bolǵan: Refactoring (Qayta qurıw), Design Patterns (Proektlestiriw úlgileri) hám Standartlastırıw (Coding Standards) usılların ámeliyatta qollanıw. "Spagetti kod"tı (Spaghetti Code) "Taza kod"qa (Clean Code) aylandırıw procesin C++ tilinde ámelge asırıw.
Teoriyalıq bólim: Fred Bruks ("Mifikalıq adam-ay") quramalılıqtı eki túrge bóledi:
1.Essential Complexity (Zárúr): Máseleniń óz tábiyatınan kelip shıǵadı (mısalı, salıq esaplawdıń qıyınlıǵı). Bunı joq etiw múmkin emes.
2.Accidental Complexity (Kútilmegen/Qosımsha): Biziń qáte sheshimlerimizden kelip shıǵadı (mısalı, jaman kod, qáte qural). Bunı joq etiw kerek.
Jeńiw usılları:
Refactoring: Kodtıń sırtqı minez-qulqın ózgertpey, ishki strukturasın jaqsılaw.
Design Patterns: Singleton, Factory, Strategy sıyaqlı tayın úlgilerdi qollanıw.
Separation of Concerns: Juwapkershilikti bóliw (MVC).
ÁMELIY BÓLIM:
"Spagetti kod"tan baslap, onı basqıshpa-basqısh "Taza kod"qa aylandıramız.
1-mısal: Mashqala (Spaghetti Code)
Másele: Paydalanıwshınıń buyırtpasın (Order) qayta islew.
Kod (Jaman):
void processOrder(string item, int qty, int price, string userType) { if (qty > 0) {
if (price > 0) {
double total = qty * price; if (userType == "VIP") {
total = total * 0.9; // 10% jenillik
cout << "VIP Jeńillik qollanıldı." << endl;
} else if (userType == "Regular") { if (total > 100) {
total = total * 0.95; // 5% jenillik cout << "Bonus Jeńillik." << endl;
}
}
cout << "Tovar: " << item << endl; cout << "Juwmaq: " << total << endl;
// Bazaǵa jazıw (Simulyaciya)
cout << "Bazaǵa jazıldı." << endl;
} else {
cout << "Qáte baha." << endl;
}
} else {
cout << "Qáte muǵdar." << endl;
}
}
Analiz: Dumalaq if-else, logika aralasqan (esaplaw, shıǵarıw, baza).
2-mısal: Refactoring - 1 (Guard Clauses)
Dáslep, ishine-ishi kirgen if-lerdi "Guard Clause" (Qorǵawshı shárt) arqalı tegisleymiz.
Kod:
void processOrder(string item, int qty, int price, string userType) { // 1. Validaciya (Teksiriw)
if (qty <= 0) {
cout << "Qáte muǵdar." << endl; return;
}
if (price <= 0) {
cout << "Qáte baha." << endl; return;
}
// 2. Esaplaw
double total = qty * price; if (userType == "VIP") {
total *= 0.9;
cout << "VIP Jeńillik." << endl;
} else if (userType == "Regular" && total > 100) { total *= 0.95;
cout << "Bonus Jeńillik." << endl;
}
// 3. Nátiyje
cout << "Tovar: " << item << ", Juwmaq: " << total << endl; cout << "Bazaǵa jazıldı." << endl;
}
Analiz: Kod oqılıwǵa ańsatlastı.
3-mısal: Refactoring - 2 (Separation of Concerns)
Logikanı 3 bólek funkciyaǵa bólemiz: Validaciya, Esaplaw, Saqlaw.
Kod:
bool isValid(int qty, int price) { if (qty <= 0 || price <= 0) {
cout << "Qáte maǵlıwmat." << endl; return false;
}
return true;
}
double calculateTotal(double baseTotal, string userType) { if (userType == "VIP") return baseTotal * 0.9;
if (userType == "Regular" && baseTotal > 100) return baseTotal * 0.95;
return baseTotal;
}
void saveToDB(string item, double total) {
cout << "Baza: " << item << " - " << total << endl;
}
void processOrder(string item, int qty, int price, string userType) { if (!isValid(qty, price)) return;
double base = qty * price;
double finalPrice = calculateTotal(base, userType);
saveToDB(item, finalPrice);
}
Analiz: Hárbir funkciya tek bir jumıs qıladı (Single Responsibility).
4-mısal: Design Pattern (Strategy)
Jeńillik logikası ózgeriwsheń. Onı "Strategy" patterni arqalı klasslarǵa bólemiz.
Kod:
// Interfeys
class DiscountStrategy { public:
virtual double applyDiscount(double total) = 0;
};
// VIP Strategiya
class VIPDiscount : public DiscountStrategy { public:
double applyDiscount(double total) override { return total * 0.9;
}
};
// Regular Strategiya
class RegularDiscount : public DiscountStrategy { public:
double applyDiscount(double total) override { if (total > 100) return total * 0.95; return total;
}
};
// Context
class OrderProcessor { private:
DiscountStrategy* strategy; public:
OrderProcessor(DiscountStrategy* s) : strategy(s) {}
void process(double amount) {
double final = strategy->applyDiscount(amount); cout << "Tólem: " << final << endl;
}
};
int main() { VIPDiscount vip;
OrderProcessor p1(&vip); p1.process(200); // 180
RegularDiscount reg; OrderProcessor p2(®); p2.process(200); // 190 return 0;
}
Analiz: Jeńillik qaǵıydaların `if-else` siz, klasslar arqalı basqardıq.
ÁMELIY TAPSÍRMALAR
1-tapsırma: Refactoring (Validaciya).
`registerUser(string user, string pass, int age)` funkciyasın "Guard Clause" usılında qayta jazıń.
Shártler: User bos bolmasın, Pass > 6 belgi, Age >= 18.
2-tapsırma: Extract Class (Klassqa shıǵarıw).
`Employee` strukturasında `name`, `street`, `city`, `zipCode` bar.
Adreske tiyisli 3 maydandı (`street, city, zip`) bólek `Address` klasına shıǵarıń hám `Employee` ishinde qollanıń.
3-tapsırma: Strategy Pattern (Tólem).
`Payment` interfeysin jaratıń (`pay(amount)`).
Eki strategiya jazıń: `CardPayment` ("Karta menen tólendi") hám `CashPayment`
("Naq aqsha tólendi").
`main` de ekewin almastırıp qollanıń.
4-tapsırma: Singleton (Jalǵız obyekt).
`Logger` klasın jaratıń. Ol programmanıń ishinde tek bir dana bolıwı kerek
(Singleton Pattern).
`log(msg)` metodı bolsın.
5-tapsırma: Factory Pattern (Fabrika).
`ShapeFactory` klasın jaratıń. `createShape(type)` metodı bolsın. Eger type="Circle" bolsa, `new Circle()` qaytarsın.
Eger type="Rect" bolsa, `new Rectangle()` qaytarsın.
