I have this code

if (position!=1&&position!=2){ viewPager.setCurrentItem(position,false); drawerLayout.closeDrawers(); } if (position==1){ Toast.makeText(Home.this, "Акции Π²Ρ€Π΅ΠΌΠ΅Π½Π½ΠΎ Π½Π΅ Ρ€Π°Π±ΠΎΡ‚Π°ΡŽΡ‚", Toast.LENGTH_LONG).show(); drawerLayout.closeDrawers(); } if (position==2){ if (strTotalPrice<500) Toast.makeText(Home.this, "Минимальная сумма Π·Π°ΠΊΠ°Π·Π° 500 Ρ€ΡƒΠ±.", Toast.LENGTH_SHORT).show(); else viewPager.setCurrentItem(position,false); drawerLayout.closeDrawers(); } 

Tell me how you can make this code more readable. And then the beginner code seems to)).

If there is another site for such things. Then give pliz link.

    3 answers 3

     switch(position) { case 1: Toast.makeText(Home.this, "Акции Π²Ρ€Π΅ΠΌΠ΅Π½Π½ΠΎ Π½Π΅ Ρ€Π°Π±ΠΎΡ‚Π°ΡŽΡ‚", Toast.LENGTH_LONG).show(); break; case 2: if (strTotalPrice<500) Toast.makeText(Home.this, "Минимальная сумма Π·Π°ΠΊΠ°Π·Π° 500 Ρ€ΡƒΠ±.", Toast.LENGTH_SHORT).show(); else viewPager.setCurrentItem(position,false); break; default: viewPager.setCurrentItem(position,false); break; } drawerLayout.closeDrawers(); 
    • Thanks Yuri. I changed the code a bit. Can you change the code through the switch again? - Andro
    • @xTIGRx, it's so easy to copy everything ... But in general, changing the TZ after its execution is bad. Well, you seem to be a freelancer, and you do this (( YuriySPb ♦
    • 3
      So tnank you)). To copy this case of schoolchildren, but I want to understand this. Thanks to your code and a bit of walking in the internet. I understood how default works. Thank you very much)) - Andro

    To begin with, to improve the readability of the code, I advise you to refer to the document.

    Code Conventions for the Java TM Programming Language

    There are rules in the convention that should be followed when writing Java libraries, code style rules, Java language rules, etc. These rules should be followed so that your code is concise, understandable and, most importantly, β€œfamiliar” to all programmers. Many companies have their own code-writing conventions, but that’s another story.

    There are many translations of Java Code Conventions into Russian, but it is better, if possible, to still read the original. Moreover, it should be done only once.

    Specifically, in your example, the following violations of the convention:

    • operators must be separated by spaces

       if (position != 1 && position != 2) { 
    • between the parameters is also necessary space

       viewPager.setCurrentItem(position, false); 
    • In Java, it is customary to always use braces, even for "single-line" conditions, each line with a new line

        if (strTotalPrice < 500) { Toast.makeText(Home.this, "Минимальная сумма Π·Π°ΠΊΠ°Π·Π° 500 Ρ€ΡƒΠ±.", Toast.LENGTH_SHORT).show(); } else { viewPager.setCurrentItem(position, false); } 

    I do not pay attention to the structure of the code (enter mutually exclusive if ... else, or go to switch ... case), I made a post about violating the Java convention for writing code, I hope this will be useful

    • Yeah, thanks ... - Andro

    Use switch and get rid of code duplication:

     switch (position) { case 1: Toast.makeText(Home.this, "Акции Π²Ρ€Π΅ΠΌΠ΅Π½Π½ΠΎ Π½Π΅ Ρ€Π°Π±ΠΎΡ‚Π°ΡŽΡ‚", Toast.LENGTH_LONG).show(); break; case 2: if (strTotalPrice<500) { Toast.makeText(Home.this, "Минимальная сумма Π·Π°ΠΊΠ°Π·Π° 500 Ρ€ΡƒΠ±.", Toast.LENGTH_SHORT).show(); break; } /* falls through */ default: viewPager.setCurrentItem(position, false); } drawerLayout.closeDrawers(); 

    It seems ideone does not show errors except for nonexistent classes.

    PS: And if you really want to use mutually exclusive if , then it makes sense to put else .