Parse number String of other languages

You can not directly parse number String of language other then English in Java. To parse number String of other language say Hindi you can use following way,

NumberFormat numberFormat = NumberFormat.getInstance(new Locale("hi", "IN"));
try {
    Number number = numberFormat.parse("३५");
} catch (ParseException e) {

If you use above block of code it will print 35 in console.

You can only parse the number for one of the supported locales of Java.



Does code hurts?

We all programmers have right to implement the code in our own way the only goal is we want it to run as per the requirement. At start I believed that there is nothing like good code or bad code it’s all about working and not working code. But I was wrong, there is such thing like bad smelly code which hurts, hurts you in the eye and hurts service or product for long term.

Let me simply share a very small example of taking wrong decision while writing the code and shipping it without code review which is a most dangerous step actually.

int id = Integer.parseInt(String.valueOf(Long.valueOf(pojo.geLong())));

It will take you a while to understand what above line is trying to do. You may take several minutes to figure out what can be the reason for writing such mysterious line of code.

Some of us will argue that no one can write such code but trust me such people exist on this planet earth. I am not going to blame them only it’s something that was in code since long and none of the people who checked this code tried to fix it.

This line can be fixed easily to avoid the confusion and in reasonable ways as per the code written before this particular line.

How about,

int id = pojo.geLong().intValue();

I might be wrong completely if getLong could return null. Well yes, in that case I would add null check or I’ll make sure that it does not return null for any case.

This line is not a problem who wrote it but for those who are going to understand it in future. I’ve multiple such examples which are really really disturbing and almost all the people in your team will advice you to avoid fixing this as they have fear of raising other problems in the code.

Come on! you can not hide truth for long, it may create problem in future. Good code is one of the standard you have to maintain in your product or service. The time has gone where working program was the only requirement. This is not about optimization but it’s about optimal approach of writing the code.

I’m happy that I usually take steps to fix such things and I may fail but I will get used to with dealing with buggy and smelly code which will help me to grow and I advice you to do the same as well.