0

I'm trying to avoid the statement reaching FCut class twice.

if(qty<FCut.fc[0][16]&&mq!=FCut.fc[0][16]){

}
  • 3
    Sure. Just store `FCut.fc[0][16]` in a variable and use that variable in both places in your `if` clause. – JustAnotherDeveloper Aug 04 '21 at 09:41
  • That means I have to make one new variable? Hmm... tough choice but thank you for the answer – Phillip Us Aug 04 '21 at 09:43
  • @PhillipUs Are you trying to [code golf](https://codegolf.stackexchange.com/questions/tagged/code-golf)? – MC Emperor Aug 04 '21 at 09:54
  • I don't know what code golf means. But I guess that means trying to minimize the code flow, and yes. I try to minimize the usage of variable & codes flow as much as I can. – Phillip Us Aug 04 '21 at 09:59
  • @PhillipUs *"Code-golf is a competition to solve a particular problem in the fewest bytes of source code."* That's what it looks like you are trying to do. But don't do that. Avoiding to create an extra variable because you think it harms the performance, then this is an example of [premature optimization](https://stackoverflow.com/questions/385506/when-is-optimisation-premature). Instead, *the most important function of computer code is to communicate the programmer's intent to a human reader* ([source](https://stackoverflow.com/a/385529/507738)). – MC Emperor Aug 04 '21 at 10:03
  • @MCEmperor Thank you so much for this information. I really appreciate it. – Phillip Us Aug 04 '21 at 10:09
  • @MCEmperor Btw, may I ask your opinion? I'm doing it because I have weak warning says "Method DEV1234 is too complex to analyze by data flow algorithm". – Phillip Us Aug 04 '21 at 10:15
  • That warning won't disappear if you create less variables. Probably your code is long and convoluted, having many conditional branches and loops. The way to get rid of the warning is to refactor the method, usually converting it into several smaller, simpler methods. – JustAnotherDeveloper Aug 04 '21 at 11:03
  • @PhillipUs Exactly what JustAnotherDeveloper said. Less variables won't remedy the problem. You should split up your method into multiple smaller ones, each of which should have exactly one purpose. – MC Emperor Aug 04 '21 at 11:19
  • Having many conditional branches is true, but I believe not many loop. 288 line for that method (2nd biggest) the only one getting that warning. First biggest is 291 (no warning). The rest of 145 methods under 200 lines. Maybe I should follow your instruction which is splitting to some methods, even none of the conditions has double result. I was about placing those codes to a new question, but just found out only 1 question allowed per 2 days (understandable). Thank you so much for the advice, and thank you so much also to MC Emperor for all the guidance. I really appreciate it. – Phillip Us Aug 04 '21 at 12:49
  • @JustAnotherDeveloper I have found the most fairly way among all suggestion ^^ No new variable, but reaching FCut class once. – Phillip Us Aug 05 '21 at 01:40

1 Answers1

0

I've found it!

private void Spin(){
    if(vl>0)SP.IN(prd,vl,vt,0,0,bahan,gr,bbs,bled,0,0,0);
    else SP.IN(prd,hl,ht,0,0,bahan,gr,bbs,bled,0,0,0);
    Qdot(FCut.fc[0][16]);//<== Only once ^^
}
void Qdot(int fc16){
    String fc=Integer.toString(fc16);
    if(qty<fc16&&mq!=fc16){
        if(qin.getText().length()>0){qin.setText(null);qblk.setText(null);}
    }else{//<==next todo

    }
    if(fc.length()>3){
        switch(fc.length()){
            case 4:fc=fc.charAt(0)+"."+fc.substring(1,3);break;
            case 5:fc=fc.substring(0,2)+"."+fc.substring(2,4);break;
            case 6:fc=fc.substring(0,3)+"."+fc.substring(3,5);break;
            case 7:fc=fc.charAt(0)+"."+fc.substring(1,3)+"."+fc.substring(4,6);break;
            default:fc=fc.substring(0,2)+"."+fc.substring(2,4)+"."+fc.substring(5,7);
        }
    }
    mqt.setText((R.string.minq+fc));
    Say();
}
  • so finally you decide to define a variable as a param of method? – LunaVulpo Aug 05 '21 at 07:23
  • @LunaVulpo yes, because the next flow must go to another method anyway, so I put them together. – Phillip Us Aug 05 '21 at 07:26
  • As an aside, I would *strongly* encourage you to embrace whitespace (both horizontal and vertical) as a way of making your code more readable, along with braces for all `if` statements, and meaningful variable names. – Jon Skeet Aug 05 '21 at 08:07
  • @JonSkeet Thank you so much for the advice. I will give a white space to all after finished. (Because I need a fresh mind to remember where the shortcut to do white space for the entire project). lol. – Phillip Us Aug 05 '21 at 15:54
  • @JonSkeet So sorry I was late to say "it's an honor to get an advice from you sir". I was struggling with Android Studio yesterday. It tricked me with index number logic in that substring. – Phillip Us Aug 06 '21 at 05:02