How to write clean Java code (tutorial)

Writing clean and simple Java code is very important for test automation.

Because the Java code should not just work.

Other people should understand what it does and be able to make changes to it without a lot of effort.



Writing code that works is easy. 
Writing code that is easy to understand and maintain is difficult.

Assume that you have a bunch of lines of code that implement a specific problem.

The code makes sense to you because you wrote it.

But it is not clear at all for other people.

And it is quite difficult to change.



How do you write code that works, is clean, easy to understand and maintain? 


You can do it with code refactoring and unit testing.

Code refactoring means that, as soon as you have working code, you make changes to the code to simplify it.

The code refactoring changes may mean
  • breaking methods into more methods
  • simplifying how the code works
  • eliminating variables
  • reducing the number of parameters for a method
  • using an object instead of a few variables
  • creating new classes
  • merging methods
  • moving code from one class to another
  • changing variables and methods' names
  • and much more

These code changes may impact how the existing code works.

To ensure that the new changes are safe, we write unit tests that verify the functionality of existing code.

Then, after each future code change. we run the unit tests.


If the unit tests pass, the new code changes are safe.
If the unit tests fail, the new code changes are not good yet.

These 2 phases (code refactoring and executing unit tests) may need to be done repeatedly until the code becomes clean and easy to understand.



Code Refactoring Example

To understand how this process works, we will use the BINARY GAP exercise given sometimes in test automation job interviews.

BINARY GAP


Find longest sequence of zeros in binary representation of an integer.
A binary gap within a positive integer N is any maximal sequence of consecutive zeros that is surrounded by ones at both ends in the binary representation of N.
For example,
  • 9 has binary representation 1001 and contains a binary gap of length 2 
  • 529 has binary representation 1000010001 and contains two binary gaps: one of length 4 and one of length 3
  • 20 has binary representation 10100 and contains one binary gap of length 1
  • 15 has binary representation 1111 and has no binary gaps 
Write code that, given a positive integer N, returns the length of its longest binary gap.
The function should return 0 if N doesn't contain a binary gap.
For example, given N = 1041, the function should return 5, because N has binary representation 10000010001 and so its longest binary gap is of length 5.
Assume that N is an integer within the range [1..2,147,483,647].

Multiple iterations will be needed for this exercise, each iteration adding more changes to the results of the previous iteration.


Decompose the problem in smaller pieces

We need to find the binary gap for a binary representation of a number.

The binary representation of a number will be determined first.

Next, we will look into a way of verifying that the binary representation is correct.

Last, we will work on determining the binary gap for the binary representation.



INTERESTED IN THE FULL JAVA CODE FOR THIS PROBLEM?

It is just USD 5.

Please use the following link for the transfer:


PayPal.Me/AlexandruSiminiuc



I will email you the link as soon as the transfer is complete.


Alex


0 Comment to "How to write clean Java code (tutorial)"

Post a Comment