0

Simply put, I have an if statement with multiple different outcomes, but a lot of code in common.
I want to make sure that the following Code is the proper way to do what I'm trying to do
(ex: chaining Method calls, as shown).. is this correct?

SharedPreferences getPrefs =
    PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

final String notifType = getPrefs.getString
    (PREF_NOTIFICATION_TYPE, NOTIFICATION_TYPE_SOUND_AND_VIBRATION);

if (notifType != null && notifType.equals
    (NOTIFICATION_TYPE_SOUND_AND_VIBRATION)) {
        // Call the appropriate Methods, depending on the Preference..
        notificationVibration();
        playNotificationTone();
        showReminderNotification();

} else if (notifType !=null && notifType.equals
    (NOTIFIATION_TYPE_VIBRATION_ONLY)) {
        // Calling alot of the same code, but minus the Sound..
        notificationVibration();
        showReminderNotification();

} else if (notifType !=null && notifType.equals
    (NOTIFIATION_TYPE_SILENT_REMINDER)) {
        // Again re-using common code..
        showReminderNotification(); 
}

public void notificationVibration() {
    // Vibration code here
}

public void playNotificationTone() {
    // Sound code here
}

public void showReminderNotification() {
    // Notification code here
}

SO - is this the correct way to go about this?
Can I chain Method calls (as shown) and have them all fire at the same time?
If not, what is the correct way to efficiently execute this?
Feedback greatly appreciated! Thanks.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Studio2bDesigns
  • 578
  • 5
  • 12
  • You can extract the null check to check it once then use a switch/case (set the compiler to 1.7). Usually when you want to check against multiple cases you should think switch/case. – m0skit0 Feb 24 '19 at 15:20
  • Method calls for chaining usually return the same object. See if you can return "this" after each method. Also look at https://stackoverflow.com/questions/21180269/how-to-achieve-method-chaining-in-java – Mallik Kumar Feb 24 '19 at 16:36
  • So if I'm understanding the answers correctly, 1) Yes my Code will work, but 2) I can make it more efficient by using `Switch/Case`, is this correct? – Studio2bDesigns Feb 25 '19 at 04:24

2 Answers2

1

You can use a if block with switch inside:

if (notifType != null) {
    switch(notifType) {
        case NOTIFICATION_TYPE_SOUND_AND_VIBRATION:
            ....
        break;
        case NOTIFIATION_TYPE_VIBRATION_ONLY:
            ....
        break;
        case NOTIFIATION_TYPE_SILENT_REMINDER:
            ....
    }
}

You can also use multiple if statements instead of switch. The only way use can make the code more efficient is by using if (notifType != null) once.

Susmit Agrawal
  • 3,649
  • 2
  • 13
  • 29
  • Thank you! I'm assuming what you're saying is that YES my code WILL work, BUT that it would be more efficient to use a Case/Switch statement, correct? My only question is: would I need to include a `default` case at the bottom of the `Switch` stataement, that correlates to the default state of the Preference? and if I run the statement you posted from within my main code (not its own Method), do I need to use a `return` at the bottom? – Studio2bDesigns Feb 25 '19 at 06:56
  • That's right. The `default` corresponds to a final `else` block without a condition. – Susmit Agrawal Feb 25 '19 at 08:17
  • You can have a different `return` for every `case`, and/or a `return` after the whole `switch` block. – Susmit Agrawal Feb 25 '19 at 08:36
  • What I meant is - after all my cases, must I add a return so it goes back to my original Code after it executes the Code within my Switch statement, OR, will it automatically execute the proper code within the statement, and then go back to where the Method was originally called from? – Studio2bDesigns Feb 25 '19 at 08:44
  • `return` is not required; the flow is exactly the same as an if-else-if ladder. – Susmit Agrawal Feb 25 '19 at 08:48
0

Assuming the code itself is inside a method you could do something like this.

private void methodName() {

    if (notifType != null) 
        return

    switch(notifType) {
        case NOTIFICATION_TYPE_SOUND_AND_VIBRATION:
            ....
        break;
        case NOTIFIATION_TYPE_VIBRATION_ONLY:
            ....
        break;
        case NOTIFIATION_TYPE_SILENT_REMINDER:
            ....
    }
}
David
  • 2,602
  • 1
  • 18
  • 32
Oshan Madusanka
  • 327
  • 3
  • 9