← Back to C Course | Chapter 12: Advanced Topics | Lesson 18 of 20

C की Code Style और Best Practices

Good code style का मतलब है clear names और sensible structure के साथ neat, readable code लिखना, tidy handwriting की तरह। यह programs को सबके समझने में आसान बनाता है।

Clean Indentation

Consistent indentation का इस पर zero असर होता है कि compiler आपका code कैसे interpret करता है, लेकिन इसका एक बड़ा असर होता है कि कोई human — आपका future self समेत — एक नज़र में nested blocks, matching braces, और control flow कितनी जल्दी visually parse कर सकता है।

उदाहरण: Clean Indentation

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Check whether 1
	if (1) {
		// Print formatted text to the screen
		printf("Properly indented block");
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

Meaningful Variable Names

x या ts की बजाय totalScore जैसे descriptive variable names चुनना type करने में लगभग कुछ खर्च नहीं करता लेकिन बाद में real time बचाता है, क्योंकि नाम खुद बिना किसी comment की ज़रूरत के document करता है कि variable क्या रखता है।

Loop counters (i, j, k) जैसे tight, conventional contexts के लिए short single-letter names अभी भी ठीक हैं।

उदाहरण: Meaningful Variable Names

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare totalScore and set it to 95
	int totalScore = 95;
	// Print formatted text to the screen
	printf("%d", totalScore);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Constants को Centralize करना

86400 या 3.14159 जैसे unexplained numeric literals सीधे अपनी logic में बिखेरना code को समझना और safely बदलना मुश्किल बनाता है; हर एक को #define या एक const variable से एक नाम देना एक mystery number को self-documenting code में बदल देता है और मतलब है आपको इसे सिर्फ एक जगह update करना है।

उदाहरण: Centralizing Constants

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Define the macro SECONDS_PER_DAY (replaced by the preprocessor)
#define SECONDS_PER_DAY 86400
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%d", SECONDS_PER_DAY);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Function Modularity

हर function को एक single, well-defined task पर focused रखना इसे test करना आसान, कहीं और reuse करना आसान, और isolation में इसके बारे में reason करना आसान बनाता है, जबकि पांच unrelated काम करने वाला एक विशाल function बढ़ने के साथ safely modify करना progressively मुश्किल हो जाता है।

उदाहरण: Function Modularity

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
int square(int n) {
	// Send n * n back to the caller
	return n * n;
}
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%d", square(4));
	// Return 0 to signal that the program finished successfully
	return 0;
}

Magic Numbers से बचना

एक magic number कोई भी unexplained literal value है जो सीधे आपके code में embedded है जिसका मतलब context से obvious नहीं है, और इसे एक named constant से replace करना (जैसे bare 3 की बजाय MAX_ATTEMPTS) बाद में code पढ़ने वाले किसी को भी उस value की intent clear कर देता है।

उदाहरण: Avoiding Magic Numbers

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Define the macro MAX_ATTEMPTS (replaced by the preprocessor)
#define MAX_ATTEMPTS 3
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%d", MAX_ATTEMPTS);
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. inconsistent indentation इस्तेमाल करना ताकि block structure देखना मुश्किल हो।
  2. important values के लिए x और tmp जैसे vague names इस्तेमाल करना।
  3. const या #define से नाम देने की बजाय कई जगहों पर magic numbers दोहराना।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.