Clean Coding Techniques
Clean Coding Techniques
208
Bhavanam Venkata Suresh Reddy
But then, every programming language and paradigm present their own
set of nuances, which mandates us to adopt befitting practices.
209
Bhavanam Venkata Suresh Reddy
Martin Fowler
210
Bhavanam Venkata Suresh Reddy
Maintainable Codebase
Easier Troubleshooting
Faster Onboarding
211
Bhavanam Venkata Suresh Reddy
Codebase
update and maintain software over time, ensuring its usefulness
and longevity.
Easier Fixing problems in software becomes easier and faster when
Troubleshooting
Code
212
Bhavanam Venkata Suresh Reddy
This leads to a lower total cost of ownership for the software lifecycle.
Focused code should be written to solve a specific problem
Characteristics of Clean
213
Bhavanam Venkata Suresh Reddy
214
Bhavanam Venkata Suresh Reddy
Project Structure
Clean Coding Techniques Naming Convention
Source File Structure
Whitespaces
Indentation
Method Parameters
Hardcoding
Code Comments
Logging
SOLID Principles
DRY & KISS
TDD
215
Bhavanam Venkata Suresh Reddy
Project Structure
While Java doesn’t enforce any project structure, it’s always useful to
follow a consistent pattern to organize our source files, tests,
configurations, data, and other code artifacts.
Maven, a popular build tool for Java, prescribes a particular project structure.
While we may not use Maven, it’s always nice to stick to a convention.
216
Bhavanam Venkata Suresh Reddy
217
Bhavanam Venkata Suresh Reddy
218
Bhavanam Venkata Suresh Reddy
Naming Convention
Naming conventions can go a long way in making our code readable and
hence, maintainable.
219
Bhavanam Venkata Suresh Reddy
Naming Convention
Classes
Variables
Methods
220
Bhavanam Venkata Suresh Reddy
Naming Convention
221
Bhavanam Venkata Suresh Reddy
Naming Convention
Classes : Class in terms of Syntax : public class Customer {
object-oriented concepts is a
blueprint for objects which
}
often represent real-world
objects. Hence it’s meaningful
to use nouns to name classes
describing them sufficiently:
222
Bhavanam Venkata Suresh Reddy
Naming Convention
Variables:Variables in Java Syntax : public class Customer {
capture the state of the private String customerName;
object created from a class.
}
The name of the variable
should describe the intent
of the variable clearly:
223
Bhavanam Venkata Suresh Reddy
Naming Convention
Methods: Methods in Java Syntax : public class Customer {
are always part of classes and private String customerName;
hence generally represent an
public String getCustomerName( ) {
action on the state of the
object created from the class. return this.customerName;
224
Bhavanam Venkata Suresh Reddy
225
Bhavanam Venkata Suresh Reddy
226
Bhavanam Venkata Suresh Reddy
227
Bhavanam Venkata Suresh Reddy
228
Bhavanam Venkata Suresh Reddy
229
Bhavanam Venkata Suresh Reddy
230
Bhavanam Venkata Suresh Reddy
Whitespaces
We all know that it is easier to read and understand short paragraphs
compared to a large block of text. It is not very different when it comes to
reading code as well. Well-placed and consistent whitespaces and blank lines
can enhance the readability of the code.
The idea here is to introduce logical groupings in the code which can help
organize thought processes while trying to read it through. There is no one
single rule to adopt here but a general set of guidelines and an inherent
intention to keep readability at the center of it:
231
Bhavanam Venkata Suresh Reddy
Whitespaces
Two blank lines before starting static blocks, fields, constructors and inner
classes
One blank line after a method signature that is multiline
A single space separating reserved keywords like if, for, catch from an
open parentheses
A single space separating reserved keywords like else, catch from a closing
parentheses.
232
Bhavanam Venkata Suresh Reddy
Indentation
A well-indented code is much easier to read and understand.
Normally, there should be a cap over the line length, but this can be set
higher than traditional 80 owing to larger screens developers use today.
233
Bhavanam Venkata Suresh Reddy
Indentation
Lastly, since many expressions will not fit into a single line, we must break
them consistently:
234
Bhavanam Venkata Suresh Reddy
Indentation
Syntax :
.collect(Collectors.toCollection(ArrayList::new));
235
Bhavanam Venkata Suresh Reddy
Method Parameters
Parameters are essential for methods to work as per specification.
236
Bhavanam Venkata Suresh Reddy
Method Parameters
Restrict the number of parameters a method accepts, three parameters
can be one good choice
237
Bhavanam Venkata Suresh Reddy
Method Parameters
public boolean setCustomerAddress(String firstName, String lastName,
String streetAddress, String city, String zipCode, String state, String
country, String phoneNumber) {
}
// This can be refactored as below to increase readability
public boolean setCustomerAddress(Address address) {
238
Bhavanam Venkata Suresh Reddy
Hardcoding
Hardcoding values in code can lead to duplication, which makes
change more difficult. It can often lead to undesirable behavior if the
values need to be dynamic.
239
Bhavanam Venkata Suresh Reddy
Hardcoding
In most of the cases, hardcoded values can be refactored in one of the
following ways:
240
Bhavanam Venkata Suresh Reddy
Hardcoding
Syntax :
241
Bhavanam Venkata Suresh Reddy
Code Comments
Code comments can be beneficial while reading code to
understand the non-trivial aspects. At the same time, care should be
taken to not include obvious things in the comments. This can bloat
comments making it difficult to read the relevant parts.
242
Bhavanam Venkata Suresh Reddy
Code Comments
Java allows two types of comments:
• Implementation comments
• documentation comments.
243
Bhavanam Venkata Suresh Reddy
Code Comments
Documentation/JavaDoc Comments
244
Bhavanam Venkata Suresh Reddy
Code Comments
Implementation/Block Comments
245
Bhavanam Venkata Suresh Reddy
Code Comments
So, how should we optimally use them so that they are useful and
contextual?
246
Bhavanam Venkata Suresh Reddy
Code Comments
So, how should we optimally use them so that they are useful and
contextual?
247
Bhavanam Venkata Suresh Reddy
Code Comments
Syntax :
/**
* This method is intended to add a new address for the customer. However
*do note that it only allows a single address per zip code. Hence, this will
*override any previous address with the same postal code.
*/
248
Bhavanam Venkata Suresh Reddy
Code Comments
Syntax :
/*
* This method makes use of the custom implementation of equals
* method to avoid duplication of an address with same zip code.
*/
public addCustomerAddress(Address address) {
}
249
Bhavanam Venkata Suresh Reddy
Logging
logger.info(String.format("A new customer has been created with customer
Id: %s", id));
250
Bhavanam Venkata Suresh Reddy
SOLID
SOLID is a mnemonic acronym that draws from the five principles it sets
forth for writing understandable and maintainable software:
• Open-Closed Principle
251
Bhavanam Venkata Suresh Reddy
Single Responsibility Each interface, class, or method we create should have a clear purpose,
Principle ideally performing one task well, resulting in smaller, more testable
code.
Open-Closed Code should be designed so that we can add new functionality without
Principle changing the existing code, promoting extension through inheritance or
composition.
Liskov Substitution Every derived class should be able to replace its parent class without
Principle affecting the program's correctness, promoting code flexibility and
reusability.
Interface Implementing an interface allows a class to have specific behavior, but
Segregation Principle it's important to create smaller interfaces so that classes don't have to
implement unnecessary methods.
Dependency Classes should rely on general concepts, not specific details, and should
Inversion Principle not create their own dependencies but instead have them provided
from outside.
252
Bhavanam Venkata Suresh Reddy
we should be careful in adopting this rather too literally. Some duplication
can actually improve code readability and maintainability.
253
Bhavanam Venkata Suresh Reddy
254
Bhavanam Venkata Suresh Reddy
255