blueshirt Posted May 23, 2015 Share Posted May 23, 2015 how can i do this? Quote Link to comment Share on other sites More sharing options...
fixthissite Posted May 23, 2015 Share Posted May 23, 2015 (edited) I'm pretty sure it's boolean hasLogs = getInventory().getAmount("Logs") == 2; if(hasLogs) { //... } Edited May 23, 2015 by fixthissite Quote Link to comment Share on other sites More sharing options...
Khaleesi Posted May 23, 2015 Share Posted May 23, 2015 I'm pretty sure it's boolean hasLogs = getInventory().getAmount("Logs") == 2; if(hasLogs) { //... } Ya I guess so, but why waste a boolean on it? Just put it straight in the condition? Quote Link to comment Share on other sites More sharing options...
fixthissite Posted May 23, 2015 Share Posted May 23, 2015 (edited) Ya I guess so, but why waste a boolean on it? Just put it straight in the condition? We can look at: if(hasTwoLogs) without needing extra cognition to work out the getInventory().getAmount(...) == 2 in our heads. If we really care about how we determine we have two logs (which there is no reason to, unless there's a bug), we can look at the assignment, but now we aren't forced to; we are instead given an actual name to read. It's a lot more useful when the condition has multiple expressions (|| or &&), but I still prefer it over placing the expression in the if statement. Slightly lowers the amount of thinking needed, as we can ignore the assignment (of the expression) without any confusion about the code. Compilers will inline the value if they see it's effectively final (not modified after initialization), so there's no performance impact; it's just to make things easier to understand when it comes to reading the actual processing part. On top of that, worrying about such small changes should be avoided (micro-optimizations) as they can consume quite a bit of time over the course of a project, while giving no noticable performance boost. You should never pre-optimize anyways: always monitor your program AFTER writing it, to see where optimization is really needed Keep in mind, that expression is a boolean, we are just storing it somewhere and giving it a name. Either way you do it, a single boolean is being allocated to the stack. Edited May 23, 2015 by fixthissite 1 Quote Link to comment Share on other sites More sharing options...