0
\$\begingroup\$

How can I optimize this code of adapter's overriden method getItemCount() into simpler form?

@Override
    public int getItemCount() {
        return super.getItemCount() == 0 && data != null ? 1 : super.getItemCount();
    }
\$\endgroup\$

closed as off-topic by 200_success, yuri, Toby Speight, Graipher, pacmaninbw Jun 6 at 12:48

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – 200_success, yuri, Toby Speight, Graipher, pacmaninbw
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    \$\begingroup\$ Could you provide some context? What is data? \$\endgroup\$ – dfhwze Jun 5 at 11:23
  • \$\begingroup\$ Also, what do you mean with 'simpler form'? You want to beatify or optimize performance? \$\endgroup\$ – dfhwze Jun 5 at 11:26
  • \$\begingroup\$ @dfzwze I want to beautify \$\endgroup\$ – Android Developer Jun 5 at 11:27
  • \$\begingroup\$ Beauty is in the eye of the beholder. It has nothing to do with code reviewing. \$\endgroup\$ – TorbenPutkonen Jun 5 at 11:46
  • \$\begingroup\$ @TorbenPutkonen I find the help center unclear about reviewing code layout. The reason I decided to answer this question anyway is because there is a potential performance optimization as well. \$\endgroup\$ – dfhwze Jun 5 at 11:56
4
\$\begingroup\$

Code Beautification is suggestive. This is how I would write your code. There is also a possible performance optimization by only calling super.getItemCount() once.

  • be consistent with parentheses (I'm using a different rule for methods and conditions, but feel free to use your own preference as long as you are consistent)
  • create one variable to store the item count
  • get the item count you would like to return from the base class
  • determine predicate from the perspective of either
    • (A) the edge case value of item count, when itemCount == 0; an inner condition is used to determine the new value
    • (B) the combined condition that yields a different result itemCount == 0 && data != null

snippet A

@Override
    public int getItemCount() {
        int itemCount = super.getItemCount();
        if (itemCount == 0) 
        {
            itemCount = data != null ? 1 : 0;
        }
        return itemCount;
    }

snippet B

@Override
    public int getItemCount() {
        int itemCount = super.getItemCount();
        if (itemCount == 0 && data != null) 
        {
            itemCount = 1;
        }
        return itemCount;
    }
\$\endgroup\$
  • \$\begingroup\$ Please don't just post code. Explain also the benefits of your chosen code layout. Now, the method naming suggests that itemCount clashes with super class member name. I would use actualItemCount, adjustedItemCount or better yet, some other form that describes the purpose of the overriding implementation (which we unfortunately don't know). I find the latter form better, as it groups the conditions that affect itemCount neatly into the same if-statement. The reader doesn't have to combine statements from multiple lines to figure out what happens. \$\endgroup\$ – TorbenPutkonen Jun 5 at 14:06
  • \$\begingroup\$ @TorbenPutkonen (A) Fair enough, it was lacking some description (B) This is something we, unfortunately don't know because of the lack of information from the OP \$\endgroup\$ – dfhwze Jun 5 at 15:17

Not the answer you're looking for? Browse other questions tagged or ask your own question.