if ((args.length == 0) || (args.length == 1))
How about:
if (args.length < 2)
so that we only need one runtime check if args.length == 1?
I like Joes way better. The code will end up the same after being compiled, but Joe's way is more clear to people reading it.
Uhh, wha-?
That might be the case in Java, but *only* for the length property. The thing about some languages such as Java, C, C#, etc. is that by calling methods you could inadvertantly change the class.
For example, if (instead of saying .length), you had to use a function like .getLength(), Java would have no way of knowing that the array object didn't change state and that it could optimize it out. C fortunately offers that option to intelligent programmers:
class CArray {
public:
int getLength() const;
}
with the const modifier on class methods.
The problem is that what the assembler will produce is something like this:
mov eax, [edx+08h] ; current
cmp eax, 0
jz conditional_okay
cmp eax, 1
jnz conditional_false
conditional_okay:
; do stuff inside the conditional scope
conditional_false:
; ...
Whereas with my way, there's only one conditional evaluation:
mov eax, [edx+08h]
cmp eax, 2
jge conditional_false
; do stuff inside the conditional scope
conditional_false:
; else --
Now, let's say that we're calling a method (non-virtual) from a class that's not marked const:
mov edx, [ebx+08h]
call edx
cmp eax, 0
jz conditional_okay
mov edx, [ebx+08h] ; depending on whether edx was overwritten, this instruction may be unnecessary. The linker knows.
call edx
cmp eax, 1
jnz conditional_false
conditional_okay:
; do stuff inside the conditional scope
conditional_false:
; ...
Hopefully you see the problem.
Plus, what if Joe made a "debuffer" in Java? Then, using this line of coding would be something like:
if (pck.getInt32() == 0 || pck.getInt32() == 1)
which would DEFINITELY lead to runtime errors as he read too quickly through a packet where this value did not initially equal zero.
The other nice thing about my method, besides what I've already given you, is that it guards you from some exceptional cases. For example, the .indexOf function returns -1 when it cannot find an instance of the specified string within another string. This is an exceptional case -- it shouldn't require an Exception object, but it should be able to be handled.