0

I was wondering if there is a better way to code this piece of code:

private void getLatitudeAndLongitudeFromZipcode() {
        String zipcode = mSharedPreferences.getString("Zipcode", "");

        try {
            List<Address> address = geocoder.getFromLocationName(zipcode, 1);
            if ((address != null ? address.size() : 0) > 0) {
                Address first = address.get(0);
                mLatitude = first.getLatitude();
                mLongitude = first.getLongitude();
                mCurrentLocationName = getLocationAsName();
                mSharedPreferences.edit().putLong("oldLat", Double.doubleToRawLongBits(mLatitude))
                        .apply();
                mSharedPreferences.edit().putLong("oldLong", Double.doubleToRawLongBits(mLongitude))
                        .apply();
            } else {
                getOldZipcodeLocation();//duplicate method call
            }
        } catch (IOException e) {
            getOldZipcodeLocation();//duplicate method call
            e.printStackTrace();
        }
    } 

Basic idea is that if they don't have internet and an exception is thrown, I want to get the old coordinates from storage. However, I also want to get the old coordinates if they are currently in a place that doesn't give them coordinates. For example, if the geocoder returns null. What bothers me is the duplicate method call in the else block and catch block. Any way to make this code cleaner? I'll take any other tips as well!

  • (1) Get rid of the test for `address` being null or empty. (2) Catch the null pointer exception or index out of range exception in the same `catch` block. (3) lose the "print stacktrace" despite what the average IDE thinks, there's no law saying you have to do this. // It is a matter of opinion whether this is "better"; personally I'd leave it as-is. – user16632363 Aug 22 '21 at 20:43
  • I really don't see the problem if you have 1 duplicate line... what is causing the `IOException`? – Alberto Sinigaglia Aug 22 '21 at 20:47
  • You can throw a `IOException` when `address` is null or empty. –  Aug 22 '21 at 21:12
  • @AlbertoSinigaglia The exceptions can be caused by many things. For example, if there is no internet. – Ely Jacobi Aug 22 '21 at 21:35
  • @user16632363 and saka1029 I'll do just that – Ely Jacobi Aug 22 '21 at 21:36
  • Don't write code like 'if null or empty then throw exception' - just let it happen naturally. – user16632363 Aug 22 '21 at 23:15

1 Answers1

0

Yes you can , 1st get address through IOException separately , then use address in your if..else statement . that's it .

 private void getLatitudeAndLongitudeFromZipcode() {

    String zipcode = mSharedPreferences.getString("Zipcode", "");
    List<Address> address = null;
    try {
        address = new Geocoder(this).getFromLocationName(zipcode, 1);
    } catch (IOException e) {
        e.printStackTrace();
    }

    if ((address != null ? address.size() : 0) > 0) {
        Address first = address.get(0);
        mLatitude = first.getLatitude();
        mLongitude = first.getLongitude();
        mCurrentLocationName = getLocationAsName();
        mSharedPreferences.edit().putLong("oldLat", Double.doubleToRawLongBits(mLatitude))
                .apply();
        mSharedPreferences.edit().putLong("oldLong", Double.doubleToRawLongBits(mLongitude))
                .apply();
    } else {
        getOldZipcodeLocation();

    }
}
Zahid Islam
  • 740
  • 1
  • 5
  • 11