Java की Best Practices
In this page:
Code Readability & Naming
Variables, methods, और classes के लिए clear, descriptive names code को self-documenting बनाते हैं — calculateTotalPrice() एक vaguely named calc() से कहीं बेहतर intent communicate करता है।
उदाहरण: Code Readability & Naming
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));
}
}
Login to try C/C++/Java/PHP code in the editor
Avoid String Concatenation in Loops
एक loop के अंदर + से बार-बार strings concatenate करना हर iteration पर एक नया String object बनाता है क्योंकि strings immutable हैं; efficient repeated appending के लिए इसके बजाय StringBuilder उपयोग करें।
उदाहरण: Avoid String Concatenation in Loops
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);
}
}
Login to try C/C++/Java/PHP code in the editor
Defensive Null Checks
किसी value को dereference करने से पहले null जांचना जिसके populated होने के बारे में आप निश्चित नहीं — खासकर user input, file reads, या external APIs से आने वाले data पर — Java में सबसे आम single runtime exception, NullPointerException रोकता है।
उदाहरण: Defensive Null Checks
// 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");
}
}
}
Login to try C/C++/Java/PHP code in the editor
Safe Resource Handling
File handles, database connections, और दूसरे closeable resources को try-with-resources में wrap करना गारंटी देता है कि वे release होते हैं भले ही एक exception बीच में operation को interrupt करे।
उदाहरण: Safe Resource Handling
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");
}
}
}
Login to try C/C++/Java/PHP code in the editor
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
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));
}
}
Login to try C/C++/Java/PHP code in the editor
StringBuilderउपयोग करने के बजाय एक loop में+से strings concatenate करना।- Null checks skip करना, जो
NullPointerExceptionकी ओर ले जाता है। - Resources बंद न करना, जब try-with-resources यह करता है।
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: