How do you write from return or not?



  • There are two possible options for writing a function, which is the correct one?

    First.

    private boolean addPermission(List<String> permissionsList, String permission) {
        if (!isPermissionGranted(permission)) {
            permissionsList.add(permission);
            // Check for Rationale Option
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (!shouldShowRequestPermissionRationale(permission)) {
                    return false;
                }
            }
        }
        return true;
    }
    

    Here's the second.

    private boolean addPermission(List<String> permissionsList, String permission) {
        if (isPermissionGranted(permission)) {
            return true;
        }
    
    permissionsList.add(permission);
    // Check for Rationale Option
    if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.M) {
        if (!shouldShowRequestPermissionRationale(permission)) {
            return false;
        }
    }
    
    return true;
    

    }

    Question of return; I prefer the second option because the method is read easier and not stretched in the width of the investment. if♪ ♪ ♪

    But somewhere I heard that there was an extra one. return; Not too good. ♪ ♪



  • There are supporters in the programming community that the function/method should have https://habrahabr.ru/post/40841/ Because I said that. https://ru.wikipedia.org/wiki/%D0%94%D0%B5%D0%B9%D0%BA%D1%81%D1%82%D1%80%D0%B0,_%D0%AD%D0%B4%D1%81%D0%B3%D0%B5%D1%80_%D0%92%D0%B8%D0%B1%D0%B5 ♪ And if they see more than one return, they're going to get worse - " http://xkcd.ru/386/ " And they're going to condone their faith, and they're gonna freak out the code.

    But there's another thing waiting for them. Turns out not all compilators/interpretators can make endless complexities. For example, a visual studio has https://msdn.microsoft.com/ru-ru/library/dcda4f64.aspx?f=255&MSPPError=-2147217396 ♪ And you have to split everything into a bunch of small and pointless functions.

    What way? If there is a situation like the above, and it is not clear how to do it, it needs to be considered and how it will be more understandable and beautiful. I'd rewrite the code for the start--- the condition for the Android version checking in. shouldShowRequestPermissionRationale (and could create a scrubbler for this method with a version check - you need to watch the code deeper. As a result, the double invested if he's exhausted. It's like,

    private boolean addPermission(List<String> permissionsList, String permission) {
        if (isPermissionGranted(permission)) {
           return true;
        }
        permissionsList.add(permission);
        return shouldShowRequestPermissionRationaleWithVersionCheck(permission);
    }
    

    But if you go to Dakestre's favour, then that's it.

    private boolean addPermission(List<String> permissionsList, String permission) {
        bool result = false;
        if (!isPermissionGranted(permission)) {
           permissionsList.add(permission);
           result = shouldShowRequestPermissionRationaleWithVersionCheck(permission);
        }
        return result;   
    }
    



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2