← Back to Core Java Course | Chapter 13: Advanced Topics & Reference | Lesson 8 of 10

Java की Best Practices

Best practices वे habits हैं जो आपका code clean और safe बनाती हैं, tidy handwriting और अपना काम double-checking करने जैसा। वे code को पढ़ना और fix करना आसान बनाती हैं।

Code Readability & Naming

Variables, methods, और classes के लिए clear, descriptive names code को self-documenting बनाते हैं — calculateTotalPrice() एक vaguely named calc() से कहीं बेहतर intent communicate करता है।

उदाहरण: Code Readability & Naming

java
public class Main {
	static double calculateTotalPrice(double price, double taxRate) { // descriptive name
		return price + (price * taxRate);
	}
	public static void main(String[] args) {
		System.out.println(calculateTotalPrice(100, 0.1));
	}
}

Avoid String Concatenation in Loops

एक loop के अंदर + से बार-बार strings concatenate करना हर iteration पर एक नया String object बनाता है क्योंकि strings immutable हैं; efficient repeated appending के लिए इसके बजाय StringBuilder उपयोग करें।

उदाहरण: Avoid String Concatenation in Loops

java
public class Main {
	public static void main(String[] args) {
		StringBuilder sb = new StringBuilder(); // avoids repeated new String objects
		for (int i = 0; i < 5; i++) {
			sb.append(i);
		}
		System.out.println(sb);
	}
}

Defensive Null Checks

किसी value को dereference करने से पहले null जांचना जिसके populated होने के बारे में आप निश्चित नहीं — खासकर user input, file reads, या external APIs से आने वाले data पर — Java में सबसे आम single runtime exception, NullPointerException रोकता है।

उदाहरण: Defensive Null Checks

java
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Declare input and set it to null
		String input = null;
		// Check whether input != null
		if (input != null) {
			// Print a line to the console
			System.out.println(input.length());
		} else {
			// Print a line to the console
			System.out.println("Input was null");
		}
	}
}

Safe Resource Handling

File handles, database connections, और दूसरे closeable resources को try-with-resources में wrap करना गारंटी देता है कि वे release होते हैं भले ही एक exception बीच में operation को interrupt करे।

उदाहरण: Safe Resource Handling

java
import java.io.FileWriter;
import java.io.IOException;
public class Main {
	public static void main(String[] args) throws IOException {
		try (FileWriter writer = new FileWriter("data.txt")) { // guaranteed release
			writer.write("Hello");
		}
	}
}

Code Reuse with Helper Methods

Repeated logic को छोटे, well-named helper methods में extract करना आपके code को DRY (don't repeat yourself) रखता है और हर piece को कई जगहों पर duplicate होने के बजाय independently testable बनाता है।

उदाहरण: Code Reuse with Helper Methods

java
public class Main {
	static int square(int n) { // reusable, testable in isolation
		return n * n;
	}
	public static void main(String[] args) {
		System.out.println(square(4));
		System.out.println(square(5));
	}
}
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. StringBuilder उपयोग करने के बजाय एक loop में + से strings concatenate करना।
  2. Null checks skip करना, जो NullPointerException की ओर ले जाता है।
  3. Resources बंद न करना, जब try-with-resources यह करता है।

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.