Prateek no rules
Prateek no rules

Reputation: 39

Is it better to change visibility of a view after checking if the same view already has the desirable visibility or is it better to directly change it

I am creating an Android app where I need to change the visibility of a view depending on what selection is made by the user is Radio button section.

Let's consider a scenario where if 'A' is selected, the view 'v' need to be visible but if 'B' or 'C' or 'D' is selected, 'v' needs to be gone. I could simply just change visibility of 'v' to Visible when 'A' is selected and change it to gone when either of 'B'/'C'/'D' is selected but doing this means if user has already selected 'B' then visibility of 'v' is gone, if he switches to 'C', again the 'v' is set to gone (even though it already was already set to gone).

After several repetitions like this, we are just setting the visibility of the view to what it already was. I found this as bad practice and decided to create a function to make sure the value is only changed if it needs to be, it looks like this :

'''

 private fun changeViewVisibility(
        visibilityValue: Int,
        changeViewVisibility: View
    ) {
        if (visibilityValue == View.VISIBLE) {
            if (changeViewVisibility.visibility == View.GONE)
                changeViewVisibility.visibility = View.VISIBLE
        } else {
            if (changeViewVisibility.visibility == View.VISIBLE)
                changeViewVisibility.visibility = View.GONE
        }
    }
'''

I am wondering if this conditional check is better than changing the value of visibility every single time. What is faster or better and why?

Upvotes: 1

Views: 131

Answers (0)

Related Questions