Code Readability, Naming Conventions, Refactoring

Code Readability, Naming Conventions, Refactoring

Code readability concept in Java.

In the Java programming language, the code readability concept means developing a Java program with source code that any ordinary Java software developer can easily analyze and understand.

Code Readability, Naming Conventions, Refactoring

Difficult to read code readability concept.

int m;

double n = p * r * t;

if (n > 9999) {

m = 9;

}

The above program does not clearly define what the m, p, r, t, and n data type elements represent in the user-declared program variables.

Easy code readability concept.

double intrstAmount = principal * rate * time;

if (intrstAmount > 9000) {

discountAmount = 9;

}

Now, the purpose and use of each program data type variable element declared in the above program is more clearly defined.

Ways to improve readability in Java source code.

  • Use data type elements with programmatically related names in each program.
  • Always keep the declared data type method small.
  • Avoid unnecessary deeply program loop expressions and nested condition logic.
  • Always use consistent formatting within the current program.
  • Avoid manual comments in every condition expression in an unnecessary program.
  • Avoid the concept of having most programs follow the same source coding style format.

The main idea of ​​the code readability concept.

In the Java programming language, the code readability concept requires that program source code be designed and developed in an easily human-readable format, not just for computer execution output.

Naming convention concept in Java.

In the Java programming language, naming conventions are rules and regulations for naming classes, methods, variables, constants, etc., in which a Java developer must follow a naming convention method when creating a program source code. Every Java developer must use all the naming convention concepts related to the program source code appropriately.

For the Java Naming convention concept.

Classes → CompanyCase

class EmployeeJob {

}

class PaymentMethod {

}

Methods → employeeCase

void TotalSalary() {

}

void methodPayment() {

}

Variables → employeeCase

String employeeName;

int employeeAge;

double salary;

Constants → UPPER_TEST_CASE

static final int MAX_TEST_COUNT = 7;

static final double PI = 3.14;

Bad naming concept in Java.

int t;

String w;

void y() {

}

Good naming concept in Java.

int employee_Salary;

String client_Name;

void methodPayment() {

}

Boolean naming concept in Java.

Declaring a Boolean data type variable in a Java program should normally look like a question.

boolean isActive;

boolean isAllowed;

boolean isRestricted;

Use this in Java user Boolean naming concept instead.

boolean state;

boolean validity;

The main idea of ​​a naming convention.

A good name in a custom Java program source code should indicate to the Java user developer what a program class, method, variable, or constant data type represents, or what it does in the existing program.

Refactoring concept in Java.

In the Java programming language, the concept of refactoring a program source code means customizing or improving the internal structure of the existing program code to suit your current program needs without modifying its external behavior.

Refactoring concept in simple terms.

Same existing source code functionality plus improved program source code internal structure behavior.

Java program before refactoring concept.

class ItemOrder {

void supplyOrder() {

// Here we calculate the total of the ItemOrder value.

double totalOrder = price * quantity;

// Here we apply the required ItemOrder discount method.

if (totalorderl > 5000) {

totalorder = totalorder – (totalorder * 0.30);

}

// Here this will print the invoice billing information in the ItemOrder.

System.out.println(“The Total Order Is -: ” + totalorder);

}

}

In this example, this method performs several functions similar to the refactoring concept method.

Java program after refactoring concept.

class ItemOrder {

void supplyOrder() {

double totalorder = countTotal();

totalorder = provideDiscount(totalorder);

printInvoice(totalorder);

}

double countTotal() {

return price * quantity;

}

double provideDiscount(double totalorder) {

if (totalorderl > 5000) {

return totalorder – (totalorder * 0.30);

}

return totalorder;

}

void printInvoice(double totalorder) {

System.out.println(“The Total Order Is -: ” + totalorder);

}

}

After the refactoring concept, the internal behavior of the existing program remains the same, but the source code of the existing program becomes easier to analyze, understand, and maintain.

Common Java refactoring techniques.

Method Extract Java.

This takes a large chunk of Java user program code and puts it into a separate method.

// First source code

void execution() {

// 10 lines of task source code

// 28 lines of job method

}

↓

void execution() {

task();

job();

}

Rename existing program variables.

int p;

↓

int employee_Total;

Remove duplicate program source code.

calculateIncomeTax();

storeWithDatabase();

calculateIncomeTax();

storeWithDatabase();

Here, extract common functionality in this program into a single method.

Replace a magic number in the existing program.

if (emp_age >= 21) {

Improved source code.

final int MINIMUM_EMPLOYEE_AGE = 21;

if (emp_age >= MINIMUM_EMPLOYEE_AGE) {

Main Difference Between Code Readability, Naming Conventions, Refactoring

#Each Method/ConceptDetail Purpose of Code Readability, Naming Conventions, Refactoring
1.Code ReadabilityCode readability concept helps us to make design develop code easy to understand for all anyone
2.Naming ConventionsJava naming conventions method helps us to give meaningful and consistent program variable method data type names
3.RefactoringRefactoring java concept enables us to improve existing program source code internal structure without changing its external behavior

Conclusion of Code Readability, Naming Conventions, Refactoring.

  • Code Readability → Can I and a typical Java user understand a program’s source code?
  • Naming Conventions → Do the names of classes, methods, variables, constants, etc. in an existing program indicate what task or action these objects perform in the current program?
  • Refactoring → Can I and other Java users improve the program’s source code without modifying it?

Leave a Reply