1

Is there any difference between doing

if (numberOfEntries >= array.length) {do stuff}; // Check if array is full directly

over doing something like

private boolean isArrayFull(){ 
    return numberOfEntries >= array.length;
}

if (isArrayFull()) {do stuff}; // Call a check function

Over large arrays, many iterations and any other environment of execution, is there any difference to these methods other than readability and code duplication, if I need to check if the array is full anywhere else?

Peebl
  • 211
  • 3
  • 13
  • 5
    My impression is that your question aims to *performance*. Beware of *premature optimization* **Never** choose a certain syntax or style for *performance consideration* unless you have **proven by measurement** that you actually *have* a performance problem **and** the code in question is really the bottleneck **and** the alternative *really solves the problem.* – Timothy Truckle Feb 05 '18 at 18:17
  • 2
    Second option is more readable. Also JIT probably will optimize it to first version so both should have same performance (and even if it doesn't difference shouldn't be that big). – Pshemo Feb 05 '18 at 18:17
  • 1
    @Pshemo the second option is not necessarily more readable. Seeing that method call, we only know that we are calling a method. What does it do? Its name gives limited information. How do we define "full"? Supposing we know what "full" means, which array are we referring to? The explicit simple comparison answers all of these questions. (Of course, this is a very simple case. In general, especially with something more complicated, a method call _can_ be more readable - _but only if it is named appropriately and takes appropriately named arguments_.) – DodgyCodeException Feb 05 '18 at 18:26
  • The method makes a lot more sense in the context of it's class, but i see what you're arguing for @DodgyCodeException – Peebl Feb 05 '18 at 18:31
  • @DodgyCodeException the method name describes what happens inside in words that have *business case related* meaning. This is a big value when trying to understand other persons code (and remember that even *your* code is "from another person" in 3 month...) – Timothy Truckle Feb 05 '18 at 18:35
  • @DodgyCodeException That is fair point. My comment wasn't necessary about this particular name of method but general idea of wrapping condition in method. Anyway good method name shouldn't be *too* specific. It just should say *what* method does, not necessarily *how* it does it. – Pshemo Feb 05 '18 at 19:20
  • @Peebl Another reason to wrap such condition in a method is fact that you will most likely use it in many places. But according to DRY (Don't Repeat Yourself) principle code which appears in many places should be put in its own method. This will save you a lot of time if you decide that your class shouldn't collect data in array but lets say in list. So instead of rewriting all occurrences of `numberOfEntries >= array.length` into something like `numberOfEntries >= listMaxSize` you would just need to rewrite it once, in method which wraps this code. – Pshemo Feb 05 '18 at 19:25

3 Answers3

2

Forget about performance. That is negligible.

But if you are doing it many times, util method isArrayFull() makes sense. Because if you are adding more conditions to your check, changing in the function reflects everywhere.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • So there is none or close to no difference in performance then? – Peebl Feb 05 '18 at 18:19
  • 1
    @Peebl To be frank, I never benchmarked this case but there shouldn't be any considerable performance gains. – Suresh Atta Feb 05 '18 at 18:20
  • 1
    It wouldn't be surprising if the method were inlined, especially if it's heavily used. – Kayaman Feb 05 '18 at 18:37
  • @Peebl *"So there is none or close to no difference in performance then?"* even if there was any: it is so few that you already spend more time thinking about it then the the time possibly saved with the "faster" alternative over the life time of the application. – Timothy Truckle Feb 05 '18 at 18:37
1

As said above, first make your design good and then determine performance issues, using some tools. Java has JIT optimisations for inlining, so there is no difference.

The JIT aggressively inlines methods, removing the overhead of method calls

from https://techblug.wordpress.com/2013/08/19/java-jit-compiler-inlining/

nick318
  • 575
  • 4
  • 18
1

Note: The below explanation is not any language specific. It is generic.

The difference comes when you analyze the options at machine level, A function is actually some JMP operations and allot of PUSH/POP operations on the CPU. An IF is usually a single COMP operation which is much cheaper than any what happens during function call.

If your 'IF's usually return false/true then I won't worry about it as the CPU optimizes IFs in a very good way by predicting the result as long as the IFs are "predictable" (usually returns true or false or has some pattern of true/false)

I would go with the IFs in cases where even negligible improvement in performance is a big deal.

In cases like web applications reducing the code redundancy to make the code manageable and readable is way more important than the optimization to save a few instructions at machine level.

Priya Jain
  • 795
  • 5
  • 15
  • 1
    This reasoning is simply wrong in Java (or any other moder language having a Virtual Machine runtime environment). The code gets optimized at runtime in ways you cannot predict by looking at the source code. – Timothy Truckle Feb 05 '18 at 18:40
  • The reasoning is not Java specific. It is just a general explanation. Also javac can optimize code if you give -o option , but people do not prefer that as generating optimized byte code results in making the run time optimization difficult. A nice post https://stackoverflow.com/questions/5981460/optimization-by-java-compiler. I put a note in my answer for clarity – Priya Jain Feb 05 '18 at 18:46
  • 1
    That's not my point. You simply cannot conclude from the byte code to what instructions the CPU will execute at the end. – Timothy Truckle Feb 05 '18 at 18:51
  • Imagine the languages other than Java where where there is no concept of bytecode. There compiler does the optimizations like folding etc. I am not saying from byte code perspective. The simple thing is in any language a function call involves stack and push pop as context switch. I answered from perspective where it can make a difference in performance to use "if vs function call". – Priya Jain Feb 05 '18 at 19:00
  • 1
    @PriyaJain languages other than Java are irrelevant in the context of a question about Java. – Andy Turner Feb 05 '18 at 19:06