C++ Coding Best Practices
Coding best practices क्या हैं?
एक C++ program compile हो सकता है, चल सकता है, सही output भी दे सकता है — और फिर भी पढ़ने में मुश्किल, आसानी से टूटने वाला और छिपे bugs से भरा हो सकता है। Best practices वे आदतें हैं जिन्हें अनुभवी programmers अपनाते हैं ताकि code सही, पढ़ने-योग्य और बदलने में आसान रहे। यह कोई नई syntax नहीं है; यह हर line लिखते समय लिए जाने वाले सही फ़ैसले हैं।
इस lesson में students और beginners के लिए सबसे ज़रूरी practices हैं, हर एक के साथ "ऐसा न करें" और "ऐसा करें" का छोटा उदाहरण।
1. ऐसे नाम जो खुद अपना मतलब बताएँ
अच्छा नाम comment की ज़रूरत ख़त्म कर देता है। Variables के लिए संज्ञा (noun), functions के लिए क्रिया (verb) वाले नाम रखें, और loop counter के अलावा एक अक्षर वाले नामों से बचें।
// ऐसा न करें
int a, b, c;
int f(int x, int y) { return x * y / 100; }
// ऐसा करें
int totalMarks, maxMarks, percentage;
int calculatePercentage(int obtained, int maximum) {
return obtained * 100 / maximum;
}एक style चुनें और उसी पर टिके रहें: variables/functions के लिए camelCase और classes के लिए PascalCase आम है। Constants अक्सर MAX_STUDENTS या kMaxStudents की तरह लिखे जाते हैं।
2. एक जैसा indentation और braces
हर block को बराबर जगह से indent करें (आमतौर पर 4 spaces) और एक statement होने पर भी braces लगाएँ। Braces भूलना bugs का पुराना कारण है:
// ऐसा न करें — if के अंदर सिर्फ पहली line है
if (marks >= 33)
cout << "Pass\n";
passCount++; // यह सबके लिए चलेगा!
// ऐसा करें
if (marks >= 33) {
cout << "Pass\n";
passCount++;
}ज़्यादातर editors (VS Code, Code::Blocks, Dev-C++) file को अपने-आप format कर सकते हैं — इसका इस्तेमाल करें।
3. हर variable को initialise करें
जो local variable declare तो हुआ पर उसे value नहीं मिली, उसमें garbage value होती है। उसे पढ़ना undefined behaviour है।
int sum; // ऐसा न करें: garbage value
int sum = 0; // ऐसा करें
int count{}; // ऐसा करें: brace से 0 पर initialise4. जो value नहीं बदलनी, उसे const बनाएँ
const आपका इरादा साफ़ करता है और compiler गलती से होने वाले बदलाव रोक देता है। इसे fixed values, सिर्फ पढ़े जाने वाले parameters और object न बदलने वाले member functions के लिए इस्तेमाल करें।
const double PI = 3.14159;
double area(const double radius) { // यहाँ radius सिर्फ पढ़ा जा सकता है
return PI * radius * radius;
}
void printReport(const std::string& name) { // न copy, न बदलाव
std::cout << name << '\n';
}5. Functions छोटे और एक काम वाले रखें
एक function एक ही काम करे। अगर main() 200 lines का है, तो उसे बाँटें: readMarks(), calculateResult(), printReport()। छोटे functions को test, reuse और debug करना आसान होता है। काम का नियम: अगर किसी block को समझाने के लिए comment चाहिए, तो वह block शायद अलग function बनना चाहता है।
6. Magic numbers नहीं
// ऐसा न करें
if (attendance < 75) { ... }
// ऐसा करें
const int MIN_ATTENDANCE_PERCENT = 75;
if (attendance < MIN_ATTENDANCE_PERCENT) { ... }अब नियम का एक नाम है, और बाद में बदलना हो तो सिर्फ एक line बदलनी होगी।
7. Raw arrays की जगह std::string और std::vector
Raw C arrays को अपना size नहीं पता और वे bounds check नहीं करते। std::vector ज़रूरत के हिसाब से बढ़ता है, size() जानता है, और at() index check करता है।
#include <vector>
std::vector<int> marks = {78, 91, 66};
marks.push_back(84);
for (int m : marks) std::cout << m << ' ';इसी तरह char name[20] की जगह std::string लें — buffer overflow नहीं, और == से आसान तुलना।
8. Memory objects को संभालने दें (RAII)
सीधे new/delete से बचें। Heap object चाहिए तो smart pointer लें; scope ख़त्म होते ही वह memory अपने-आप free कर देता है।
#include <memory>
// ऐसा न करें
Student* s = new Student("Aman");
// ... बीच में return हुआ तो memory leak
delete s;
// ऐसा करें
auto s = std::make_unique<Student>("Aman"); // अपने-आप freeविस्तार के लिए देखें smart pointers और dynamic memory।
9. बड़े objects को const reference से भेजें
std::string या std::vector को by value भेजने पर उसकी copy बनती है। सिर्फ पढ़ना हो तो const& से, और बदलना हो तो & से भेजें।
double average(const std::vector<int>& marks); // सिर्फ पढ़ना, copy नहीं void addBonus(std::vector<int>& marks, int bonus); // caller का data बदलता है
10. Warnings on करें और उन्हें ठीक करें
g++ -std=c++17 -Wall -Wextra -pedantic main.cpp -o main
"unused variable", "comparison of signed and unsigned" या "may be used uninitialised" जैसी warnings अक्सर असली bug की ओर इशारा करती हैं। Testing के समय -g -fsanitize=address,undefined जोड़ें ताकि range के बाहर access run time पर पकड़ में आए।
11–15. और ज़रूरी आदतें
- Comment में "क्यों" लिखें, "क्या" नहीं।
i++; // i बढ़ाओबेकार है;// header row छोड़ोइरादा बताता है। - Variable वहीं declare करें जहाँ पहली बार चाहिए, और सबसे छोटे scope में।
- Input check करें।
if (!(cin >> age)) { ... }उस स्थिति को संभालता है जब user number की जगह अक्षर लिख दे। - **Header files में
using namespace std;न लिखें।** छोटी.cpppractice file में यह ठीक है। - Code दोहराएँ नहीं। कोई block तीन बार copy-paste हो रहा है तो उसे function या loop बना दें।
उदाहरण: बिखरा code vs साफ़ code
नीचे के दोनों programs student का percentage निकालते हैं। पहला चलता है, पर दूसरा वह है जो reviewer — या examiner — देखना चाहता है।
#include<iostream>
using namespace std;
int main(){int a,b;cin>>a>>b;
cout<<a*100/b;}#include <iostream>
int calculatePercentage(int obtained, int maximum) {
if (maximum <= 0) {
return 0; // zero से भाग से बचाव
}
return obtained * 100 / maximum;
}
int main() {
int obtainedMarks = 0;
int maximumMarks = 0;
std::cout << "Obtained marks: ";
std::cin >> obtainedMarks;
std::cout << "Maximum marks: ";
std::cin >> maximumMarks;
std::cout << "Percentage: "
<< calculatePercentage(obtainedMarks, maximumMarks) << "%\n";
return 0;
}साफ़ version में हर चीज़ का नाम है, zero से भाग का बचाव है, calculation और input/output अलग हैं, और layout ऐसा है कि हर step साफ़ दिखे।
आम गलतियाँ
- पूरा program
main()के अंदर लिख देना। - Compiler warnings को "चल तो रहा है" सोचकर अनदेखा करना।
new/deleteके साथ manual error handling — और error वाले रास्ते पर memory leak।- Loops में
intकी तुलनाvector.size()(unsigned) से करना और warning पाना;std::size_tया range-basedforलें। - Submit किए गए code में debug वाली
coutlines छोड़ देना।
पिछले महीने लिखा कोई भी program लें। हर variable को अर्थपूर्ण नाम दें, main() को कम से कम तीन functions में बाँटें, जहाँ हो सके const लगाएँ और -Wall -Wextra से तब तक compile करें जब तक एक भी warning न बचे।
सारांश
- साफ़ नाम और एक जैसी formatting से code पढ़ने-योग्य बनता है।
- Variables initialise करें,
constलगाएँ, magic numbers से बचें। - Functions छोटे रखें; बड़े objects
const&से भेजें। - Raw arrays और
new/deleteकी जगहstd::vector,std::stringऔर smart pointers लें। - Warnings on करके compile करें और उन्हें bug मानें।
संबंधित: C++ program की संरचना, C++ में comments, C++ interview questions।
अक्सर पूछे जाने वाले प्रश्न (FAQ)
C++ में beginners के लिए सबसे ज़रूरी coding best practices कौन-सी हैं?
const बनाएँ, हर variable initialise करें, raw arrays/pointers की जगह std::vector/std::string लें, और warnings on करके compile करें (-Wall -Wextra)। इन्हीं आदतों से beginners की ज़्यादातर गलतियाँ रुक जाती हैं।क्या C++ में using namespace std लिखना चाहिए?
.cpp file के अंदर यह ठीक है। Header file में कभी न लिखें, क्योंकि जो भी file उस header को include करेगी उसमें यह पहुँच जाएगा और नामों का टकराव हो सकता है। बड़े projects में std:: लिखें या using std::cout; जैसी सीमित declarations इस्तेमाल करें।Modern C++ में raw new और delete से क्यों बचना चाहिए?
new के लिए ठीक एक delete चाहिए, और बीच में return या exception आने पर वह आसानी से छूट जाता है — memory leak। std::vector जैसे containers और std::unique_ptr जैसे smart pointers scope खत्म होते ही memory अपने-आप free कर देते हैं (RAII), इसलिए leak हो ही नहीं पाता।C++ में गलतियाँ पकड़ने के लिए कौन-से compiler flags काम आते हैं?
-Wall -Wextra -pedantic से आम warnings दिखती हैं, और testing के समय -g के साथ -fsanitize=address,undefined लगाने से array के बाहर access और undefined behaviour run time पर पकड़ में आ जाते हैं। Warnings को शोर नहीं, ठीक करने लायक errors मानें।