java - Should you create variables for a if statement? -


i wondering, technically efficient way.

what smarter code with:

    if (args[0].tolowercase().tostring().equals("server")) {         system.out.println("server");     } 

or:

    string host = args[0].tolowercase();      if (host.equals("server")) {         system.out.println("server");     } 

keep in mind, know variable redundant. , argument can contain number, not case of question. it's example.

when should create variable if statement? always? safer? should not, because wasting memory? impact performance @ all?

by means, create variable. single variable never, ever performance bottleneck, never.

always favor readability. performance problems appear in specifics parts of system. when arise, , not before measuring (aka having numbers), treat them case case. never forget: premature optimization root of evil.

in specific case, go further , create variable, explaining intent of comparison:

string host = args[0].tolowercase(); boolean isrunningunderproduction = host.equals("server"); if (isrunningunderproduction) {     system.out.println("server"); } 

much better comment. , explains intent of code within glimpse.


a quote martin fowler's refactoring book:

the interesting thing performance if analyze programs, find waste of time in a small fraction of code. if optimize code equally, end 90 percent of optimizations wasted, because optimizing code isn't run much. time spent making program fast, time lost because of lack of clarity, wasted time.

in other words: if programs easier read, easier fix slow parts, when time comes (if comes).


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -