Monday, December 16, 2019

Do you know Precision and Scale of a Number ?




You might be thinking what is this stupidity blog, I know it. 
Can you please fill the blank in this table :


Number
Precision
Scale
0.0
?
?
567.234
?
?
-56.3457
6
?
50
?
0


Ok, it is for those who know it but forget.


I was not clear about the Precision and Scale of a Number perfectly !!!
My boss looked at me surprisingly and asked me, is It? Honestly, I told yes.
Then instead of explaining to me, he told me to read it again and explain to him.
I did a wrong explanation knowingly and he agreed. i.e. he has also forgotten the same !!!


So are you with me too? This blog will help to refresh the bean which we have eaten during
our schooling !!!


 What is precision?

Precision refers to the amount of information that is conveyed by a number in terms of its digits.
i.e Precision is the total number of significant digits in a number.


What is Scale?

 - Scale is the number of digits to the right of the decimal point.


So now the above table will be like :


Number
Precision
Scale
0.0
1
1
567.234
6
3
-56.3457
6
4
50
2
0

Where is it Used?
It is used in most of the calculations even in Database too. As a Java developer,
if you are performing any operations using BigDecimal,
you might have seen the use cases such as MathContext  & add method of BigDecimal.


In the MySql database, Decimals can be added by using the DECIMAL(M, D) data type. This requires 2 arguments.
  • M is the maximum number of digits, ranging from 1 to 65.
  • D is the number of decimal places available, ranging from 0 to 30.
Note that with D digits reserved for decimal places, there can be at most M-D digits available for the integer part. So if we use DECIMAL(6,4), the number 45.8239 would be valid, but 456.12 would not.

Wednesday, November 27, 2019

Monetary calculations in Java Using float or double !!!

Monetary calculations in Java :

Money plays an important factor in life and a Software engineer has to deal with money calculations like monetary arithmetic a lot during software development. If it is an e-commerce or banking application, then most of the calculations are on money only. But sometimes 0.01 value also makes the whole monetary arithmetic wrong.  That time we trigger our mind button and keeps on debugging, calculations manually bla bla bla !!! But in the end we sometimes weren't able to find the solutions; but actually, the solutions are dancing in-front, but we are not able to catch it.

When you deal with “money” value, always a question arises – Should I use double or float datatype to represent the monetary values?
Answer: Neither double nor float. Oh, boss!!!!! Why and what should I use ??

Why not double/float :

Let's take an example :

double amount1 =2.15;
double amount2 =1.10;
while( (amount1-amount2) !=1.05){
   System.out.println("Oh!!! Stil not true... I am getting stuck infinite lopp...but why...");
}

Output:
Infinite loop. . . .......................
............................

This code will result in an infinite loop because the result of subtraction of amount1 and amount 2 will not be 1.5 instead it would be "1.xxxxxxxxxxxxx" which make boolean condition always true.

System.out.println(2.00-1.1); // check the o/p

System.out.println(42/85); //check the o/p


Solution :

This is one of the common mistakes Java programmers make until they are familiar with BigDecimal class. We can avoid the above mistakes by using BigDecimal class as follows :


BigDecimal amount1 =new BigDecimal("2.15");
BigDecimal amount2 =new BigDecimal("1.10");
while( ! new BigDecimal((amount1.subtract(amount2).equals(new BigDecimal("1.05"))))
   System.out.println("Oh!!!  I am safe now. . .);
}

Output:
The difference of two amount is 1.05 and as the condition evaluates to false, no o/p will come.

Using Incorrect BigDecimal constructor

Another mistake Java Programmers make is using the wrong constructor of BigDecmialBigDecimal has an overloaded constructor and if you use the one which accepts double as an argument you will get the same result as you do while operating with double. So always use BigDecimal with String constructor. here is an example of using BigDecmial constructed with double values:

//Creating BigDecimal from double values
BigDecimal amount3 = new BigDecimal(2.15);
BigDecimal amount4 = new BigDecimal(1.10) ;
System.out.println("difference between 2.15 and 1.0 using BigDecmial is: " + (amount3.subtract(amount4)));

Output:
difference between 2.15 and 1.0 using double is: 1.0499999999999998
difference between 2.15 and 1.0 using BigDecmial is: 1.049999999999999822364316059974953532218933105468750

Conclusion :

  • Don’t use float and double on monetary calculation.
  • Use BigDecimal, long or int for monetary calculation.
  • Use BigDecimal with String constructor and avoid double one.
  • Use Money API (JSR-354

Note :

The performance of BigDecimal operations is slower than the performance of primitive types such as double, float . 

Friday, August 9, 2019

What is Code Review ?

What Is Code Review?

It is the act of potential flows of source code, intended to
detect the consistency with the overall design, estimating
the code quality and adherence to the coding standards.
It is especially productive to detect security vulnerabilities,
potential troubles such as memorye leak, race conditions,
boilerplate detection.  

Who shouldn’t do the code review :


During this above 60 months of journey in this software orbit,
I have found that sometimes the comments from the reviewer
is really violating the aim of “Code review”
due to their lack of knowledge in the specified domain. 
E.g. If you will review your JAVA 8 / Spring Boot written code
from a ‘C’ developer who is unaware about the features
of the technology you have used, he might not be able to
provide productive code review comments.
He might sought that “Lambda” is not providing code
readability, use a normal loop instead. 
So before doing the code review, the reviewer should be more
experienced than you on the specific
technology at least and must have the functional requirement
understanding about the story/task as well as
should be good enough on the specific programming
language.
I always try to get my code review by someone with more
experienced on the specific language/domain
so that I can capture any domain-specific scenario which
has been missed during think through the process.



Checklist for code review

Below are some checklists points which should be taken
care during the code review:
a) Clean code
The code is the heart of your software. If the heart will be not clean,
there might be a chance of early death. 
So always the code should be care like a “baby”.

There are lots of area to achieve the code as cleaner.
Below are the some points:
  • Must follow the naming conventions(e.g. OOPs principle). 
  • A method definition must not be more than 15-20 lines. If required, use modularity. 
  • Never user abbreviations . 
  • Use the features of provided language
b) Code Coverage
This is the most important points I have figured out .
Sometimes the developers develop the number
of codes but used never.
This is like we cook foods without planning the events
which causes waste and environment can be polluted.
There are lots of tools available to checks the
code smell and code coverage.
We should use this before approving the code.
    1. Cobertura
    2. JaCoCo 
    3. NCover
c) Exception Handling
Prefer Custom Exception handling with proper message/status code.

d) Performance & Memory 
Creating the instances is a heavy cost-effective for
any programming language.
It is better to use Design patterns where ever you can.

e) Reusability of code snippets

f) Security

g) Commenting and formatting

h) Unit test


Common code review approaches

Over-the-Shoulder
This is a very older technique, but the easiest way to
engage in peer code review.
Once your code is ready with your unit testing, find a
qualified colleague to your workstation
and do the code review by explaining the features you
have implanted. 
(Bring a notebook and  collect the review comments)
Using Tools
There are lots of 3-rd party open sources available such as
1. Collaborator.
2. Review Assistant.
3. Codebrag.
4. Gerrit.
5. Codestriker.
6. Rhodecode.
7. Phabricator.
8. Crucible.
9. Veracode.
10.Review Board.
The email threads
The author sends an email of the code to the reviewers for code review.
This technique is preferred by open source projects.