Tutorial Playlist
191 Lessons1. Introduction to Java
2. What is Java?
3. History of Java
4. Java Tutorial for Beginners
5. How Do Java Programs Work?
6. JDK in Java
7. C++ Vs Java
8. Java vs. Python
9. Java vs. JavaScript
10. From Java Source Code to Executable
11. How to Install Java in Linux
12. How to Install Java in Windows 10
13. Java Hello World Program
14. Structure of Java Program and Java Syntax
15. Operators in Java
16. Java If-else
17. Switch Case In Java
18. Loops in Java
19. Infinite loop in Java
20. For Loop in Java
21. For Each Loop in Java
22. Constructor in Java
23. Constructor Overloading in Java
24. Copy Constructor in Java
25. Default Constructor in Java
26. Parameterized Constructors in Java
27. Constructor Chaining In Java
28. Finalize Method in Java
29. Static Method in Java
30. Equals Method in Java
31. Abstract Method in Java
32. toString() Method in Java
33. Difference between equals method in Java
34. Inheritance in Java
35. Multiple Inheritance in Java
36. Hierarchical Inheritance in Java
37. Java Classes and Objects
38. Scanner Class in java
39. All classes in java are inherited from which class
40. What is Nested Class in Java
41. POJO Class in Java
42. Anonymous Class in Java
43. Final Class in Java
44. Object Class in Java
45. Packages in Java
46. Access Modifiers in Java
47. Static Keyword In Java
48. Final Keyword in Java
49. Checked and Unchecked Exceptions in Java
50. User Defined Exception in Java
51. Error vs. Exception in Java
52. Java Collection
53. Collections in Java
54. Garbage Collection in Java
55. Generics In Java
56. Java Interfaces
57. Functional Interface in Java
58. Marker Interface in Java
59. Streams in Java
60. Byte stream in java
61. File Handling in Java
62. Thread in Java
63. Thread Lifecycle In Java
64. Daemon Thread in Java
65. Thread Priority in Java
66. Deadlock in Java
67. String Pool in Java
68. Java Database Connectivity(JDBC)
69. Design Patterns in Java
70. Functional Programming in Java
71. OOP vs Functional vs Procedural
72. Heap Memory and Stack Memory in Java
73. Applet in Java
74. Java Swing
75. Java Frameworks
76. Hibernate Framework
77. JUnit Testing
78. How to Install Eclipse IDE for Java?
79. Command line arguments in Java
80. Jar file in Java
81. Java Clean Code
Now Reading
82. OOPs Concepts in Java
83. Java OOPs Concepts
84. Overloading vs Overriding in Java
85. Java 8 features
86. String in Java
87. String to int in Java
88. Why String Is Immutable in Java?
89. Primitive Data Types in Java
90. Non-Primitive Data Types in Java
91. This and Super Keyword in Java
92. HashMap in Java
93. Comparable And Comparator in Java
94. Type Casting in Java
95. Arrays Sort in Java with Examples
96. Variable Hiding and Variable Shadowing in Java
97. Enum in Java
98. Substring in Java
99. Pattern Programs in Java
100. Hashcode in Java
101. What is ByteCode in Java?
102. How To Take Input From User in Java
103. GCD of Two Numbers in Java
104. Linked List in Java
105. Arithmetic Operators in Java
106. Conditional Operators in Java
107. Stack and Queue in Java
108. Array Length in Java
109. Number Pattern Program in Java
110. Split in java
111. Map In Java
112. Difference Between Throw and Throws in Java
113. Difference Between Data Hiding and Abstraction
114. HashSet in Java
115. String Length in Java
116. Factorial Using Recursion in Java
117. DateFormat in Java
118. StringBuilder Class in java
119. Instance variables in Java
120. Java List Size
121. Java APIs
122. Reverse an Array in Java
123. StringBuffer and StringBuilder Difference in Java
124. Java Program to Add Two Numbers
125. String to Array in Java
126. Regular Expressions in Java
127. Identifiers in Java
128. Data Structures in Java
129. Set in Java
130. Pass By Value and Call By Reference in Java
131. Try Catch in Java
132. Bubble Sort in Java
133. Caesar Cipher Program in Java
134. Queue in Java
135. Object Creation in Java
136. Multidimensional Array in Java
137. How to Read a File in Java
138. String Comparison in Java
139. Volatile Keyword in Java
140. Control Statements in Java
141. Jagged Array in Java
142. Two-Dimensional Array in Java
143. Java String Format
144. Replace in Java
145. charAt() in Java
146. CompareTo in Java
147. Matrix Multiplication in Java
148. Static Variable in Java
149. Event Handling in Java
150. parseInt in Java
151. Java ArrayList forEach
152. Abstraction in Java
153. String Input in Java
154. Logical Operators in Java
155. instanceof in Java
156. Math Floor in Java
157. Selection Sort Java
158. int to char in Java
159. Stringtokenizer in java
160. Implementing and Manipulating Abs in Java
161. Char array to string in java
162. Convert Double To String In Java
163. Deque in Java
164. Converting a List to an Array in Java
165. The Max function in java
166. Removing whitespace from string in java
167. String arrays in Java
168. Strings in Java Vs Strings in Cpp
169. Sum of digits of a number in Java
170. Art of Graphical User Interfaces
171. Trim in Java
172. RxJava
173. Recursion in Java
174. HashSet Java
175. Difference Between Java and Python
176. Square Root in Java
177. Reverse A String in Java
178. Even Odd Program in Java
179. Fibonacci Series in Java
180. Prime Number Program in Java
181. Java Program to Print Prime Numbers in a Given Range
182. Java Leap Year Program
183. Swapping of Two Numbers in Java
184. LCM of Two Numbers in Java
185. Math.sqrt() Function in Java
186. Area of Triangle in Java
187. Sort a String In Java
188. Factorial Program in Java
189. Javafx
190. Lambda expression in java
191. Setup Java Home and IDE on macOS
Java clean code is one of the fundamental concepts in software development. The idea emphasizes writing code that is “clean” in the sense that it is easy to read, compile, and maintain. Focusing on the idea of clean code in Java is all about making the code elegant and efficient.
Practicing clean code in Java is to go beyond just the functionality—it focuses on readable, flexible, robust, and extensible coding. Java clean code is a crucial aspect of programming that, although isn’t compulsory to master, brings in a lot of benefits to the developer once they decide to make it a part of their syntax.
Java is an inherently complex programming interface and can undertake large-scale projects. Writing clean Java code involves following principles and best practices that promote clarity, maintainability, and simplicity.
Following clean code principles, developers aim to reduce technical debt, enhance collaborative projects, and improve their programming skills. This tutorial on Java clean code aims to equip aspiring and professional developers with a concise and practical guide to writing clean and robust code. It focuses on the essential principles, best practices, and certain clean code examples Java to go about writing Java clean code.
Here are some guidelines and examples that will help you write clean code in Java:
Use descriptive names for variables, methods, and classes that accurately represent their purpose and functionality.
Example:
// Bad example
int a = 5;
// Good example
int age = 5;
Follow consistent indentation and formatting conventions to enhance code readability and organization.
Example:
// Bad example
public void printNumbers(int[] numbers) {
for(int i=0;i<numbers.length;i++) {
System.out.println(numbers[i]);
}
}
// Good example
public void printNumbers(int[] numbers) {
for (int i = 0; i < numbers.length; i++) {
System.out.println(numbers[i]);
}
}
Break down complex tasks into smaller, more manageable methods to improve code modularity and understandability. This also helps in maintaining and testing code.
Example:
// Bad example
public void processUserData(User user) {
// Many lines of code...
}
// Good example
public void processUserData(User user) {
validateUserData(user);
saveUserData(user);
notifyUser(user);
}
The Liskov Substitution Principle states that subtypes must be substitutable for their base types.
In the example below, Rectangle and Circle inherit from the Shape base class and override the calculateArea() method with their specific implementations. Clients of the Shape class should be able to use instances of Rectangle or Circle interchangeably without affecting the correctness of the program.
Code:
public class upGradTutorials {
public static void main(String[] args) {
Shape rectangle = new Rectangle(5, 10);
double rectangleArea = rectangle.calculateArea();
System.out.println("Rectangle Area: " + rectangleArea);
Shape circle = new Circle(7);
double circleArea = circle.calculateArea();
System.out.println("Circle Area: " + circleArea);
}
}
class Shape {
public double calculateArea() {
return 0;
}
}
class Rectangle extends Shape {
private double width;
private double height;
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
@Override
public double calculateArea() {
return width * height;
}
}
class Circle extends Shape {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
@Override
public double calculateArea() {
return Math.PI * radius * radius;
}
}
Include comments to explain the purpose and logic behind code sections, making it easier for other developers to understand and maintain the code.
Example:
// Bad example
int result = a + b; // Adding a and b
// Good example
int result = a + b; // Calculate the sum of a and b
Repeated code segments should be refactored into reusable methods or classes to eliminate redundancy and improve maintainability.
// Bad example
public void calculateTotalPrice() {
// Calculation logic here
}
public void calculateTax() {
// Calculation logic here (similar to calculateTotalPrice)
}
// Good example
private double calculateTax(double price) {
// Calculation logic here
}
public void calculateTotalPrice() {
// Calculation logic here (using calculateTax)
}
Implement proper exception handling to handle errors gracefully and provide meaningful error messages for troubleshooting.
// Bad example
public void processFile(String filePath) {
try {
// File processing code
} catch (Exception e) {
// Catching generic exception
// Error handling code
}
}
// Good example
public void processFile(String filePath) throws IOException {
// File processing code
}
Logging is crucial for monitoring and debugging applications. Java provides the java.util.logging package and various third-party libraries like Log4j, SLF4J, and Logback for logging purposes.
Here's an example of logging using SLF4J and Logback:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class upGradTutorials {
private static final Logger logger = LoggerFactory.getLogger(upGradTutorials.class);
public void doSomething() {
logger.info("Doing something...");
try {
// Code execution
} catch (Exception e) {
logger.error("An error occurred: {}", e.getMessage());
}
}
}
In this example, we declare a logger using the LoggerFactory, and then use various logging methods (e.g., info(), error()) to record relevant messages or exceptions. Proper logging helps in troubleshooting and understanding the application's behavior.
Write unit tests to verify the functionality of individual code components and ensure that changes do not introduce unintended consequences.
Each class and method should have a single responsibility. If a class or method does too many things, consider refactoring it into smaller, focused components.
Example:
// Bad example
public void processUserDataAndSendEmail(User user) {
// ...
}
// Good example
public void processUserData(User user) {
// ...
}
public void sendEmail(User user) {
// ...
}
Writing clean code in Java is a task that incorporates even the smallest requirements, such as naming the class, methods, and variables, to more complex tasks, such as efficient error handling. It is a set of principles encompassing every way to better the code before it is compiled.
Caring about clean code is essential for several reasons, including but not limited to preparing for Java clean code interview questions.
Before we look at Java clean code example, let us get to know some characteristics of clean code in Java:
Signs of bad code, or unclean code, can be identified through several indicators:
Poor Readability: Bad code lacks clear and descriptive naming conventions, making it difficult to understand the purpose and functionality of the code.
Complexity: Unclean code often exhibits excessive complexity, including long methods or functions, deeply nested conditions, and convoluted logic, which makes it challenging to maintain and debug.
Code Duplication: Bad code includes redundant or duplicated code segments that violate the DRY (Don't Repeat Yourself) principle and make updates or bug fixes cumbersome.
Lack of Modularity: Unclean code lacks proper modularization and separation of concerns, leading to tightly coupled components and difficulties in reusing or testing individual parts.
Inadequate Error Handling: Bad code fails to handle errors properly, resulting in unhandled exceptions, inadequate error messages, or improper use of exception-handling mechanisms.
Absence of Documentation: Unclean code lacks sufficient comments or documentation, leaving other developers unaware of the code's purpose, assumptions, or potential pitfalls.
Poor Performance: Bad code may have inefficient algorithms, excessive resource consumption, or unnecessary computations, leading to suboptimal performance.
This example below demonstrates some of the key principles of clean code, promoting readability, maintainability, and simplicity.
The User class represents a user and provides a method to greet the user. The greet method is simple and focuses on generating and printing a greeting message.
The generateGreeting method handles the logic for generating the appropriate greeting based on the user's age. The logic for generating the greeting is encapsulated in a separate private method, generateGreeting, to avoid duplication.
Finally, the main method creates a User object named "Aritra Bandyopadhyay" and age 25. It then calls the greet method to generate and print the greeting for the user.
Code:
public class User {
private String name;
private int age;
public User(String name, int age) {
this.name = name;
this.age = age;
}
public void greet() {
String greeting = generateGreeting();
System.out.println(greeting);
}
private String generateGreeting() {
if (age < 18) {
return "Hi, " + name + "! You are underage.";
} else {
return "Hello, " + name + "! Welcome.";
}
}
public static void main(String[] args) {
User user = new User("John Doe", 25);
user.greet();
}
}
Low coupling and high cohesion are important characteristics of well-designed software. Low coupling reduces the dependencies between components, making them more independent and easier to maintain. It promotes flexibility and modularity while allowing for easier changes without impacting other parts of the codebase.
Meanwhile, high cohesion ensures that each component has a clear and focused responsibility, with closely related functionalities grouped together. This improves code readability, understandability, and maintainability. Components with high cohesion are easier to test, reuse, and understand.
There are several tools available that can assist developers with best coding practices in Java to improve performance:
Tools like SonarQube, Checkstyle, and PMD analyze the codebase and highlight potential issues, such as code smells, coding style violations, and potential bugs.
Tools like TestNG, Selenium, JUnit, and Mockito allow the creation of unit tests, facilitating developers to validate the behavior of their code and ensure its correctness and maintainability.
Modern IDEs like AWS Cloud9, IntelliJ IDEA, Visual Studio, and XCode offer built-in code inspections, refactoring tools, and automatic code formatting features that help enforce clean coding practices.
Tools like Crucible, Gerrit, and GitHub Pull Requests provide collaborative code review capabilities, enabling team members to review code for adherence to clean coding practices and suggest improvements.
Tools like Doxygen, Javadoc, and Swagger assist in generating documentation from code annotations, making it easier to document classes, Javadoc, methods, and their usage.
Understanding the need for and practicing clean code in Java is crucial for all developers, and writing efficient code must be an involuntary practice. Coding or programming is more than just about who frames the perfect solution to the problem—it is also about developing high-quality, readable, and maintainable software.
By prioritizing the readability and robustness of the code, developers can enhance collaboration, reduce technical debt, and improve the overall efficiency of their Java projects.
1. Is clean code hard?
Although clean code promotes easy readability, writing it is hard work, especially for developers just learning to explore the domain. However, with improving skill sets, it gradually becomes a practice, and developers no longer need to incorporate it voluntarily.
2. Is clean code a design pattern?
Writing clean code involves using a clear design pattern, but it is not a design pattern. It is more of a reader-centric approach to coding that promotes the code's efficiency, readability, and robustness.
3. What is dirty code?
Dirty code is software that is painful to read, comprehend, and modify due to conflicting syntax styles, repetition, and other bad practices that are starkly opposite to clean coding.
PAVAN VADAPALLI
Director of Engineering
Director of Engineering @ upGrad. Motivated to leverage technology to solve problems. Seasoned leader for startups and fast moving orgs. Working …Read More
Popular
Talk to our experts. We’re available 24/7.
Indian Nationals
1800 210 2020
Foreign Nationals
+918045604032
upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enrolling. upGrad does not make any representations regarding the recognition or equivalence of the credits or credentials awarded, unless otherwise expressly stated. Success depends on individual qualifications, experience, and efforts in seeking employment.
upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enr...