-
Notifications
You must be signed in to change notification settings - Fork 0
Development Gotchas
While developing this project, we noticed some weird behaviour, that we want to inform you about. Besides these irregularities, we list some general Java syntax that may not be commonly known or is very specific to Java.
In no particular order, here are some of the things, that you might not know beforehand. Some of them are general for Java, while others are specific to WPILib.
-
Use British/American characters only; even though Java has full UTF-8 support, the WPILib does sadly not. So do not use Æ,Ø,Å,é and so forth - not even in comments!
-
We use the ternary operator (the inline if statement) quite often to reduce clutter. It has the syntax
condition ? then : else
, and if you stack/nest or&&
,||
them together, be sure to use parentheses; otherwise you may get some unexpected results. -
We write in Java 8, so you might see lambda expressions here and there. If some class is implementing a functional interface, you can use the arrow
->
instead of initialising an anonymous inner class. -
Rather use too many sanity checks than too few. As this code is running on a (partly autonomous) robot, we do not want any possible crashes.
-
The
&&
and||
performs conditional test on two boolean operators. These are short-circuited, meaning they will not perform any redundant tests. This is especially useful when checking for instances that may benull
. If we letobjA = null
, then thisif(objA == null || objA.someProperty == b) return;
works flawlessly, whileif(objA == null | objA.someProperty == b) return;
will cast an NullPointerException. Therefore, be extremely careful regarding usage of the latter approach. -
You can negate boolean values with
!
, and you can negate (bitwise complement) any other primitive type (short, int, long, byte...) with~
, but you can not do the opposite (eg negate a boolean value with~
and a primitive type with!
). -
someBool &= someOtherBool
,someBool |= someOtherBool
andsomeBool ^= someOtherBool
are all perfectly valid syntaxes. They do what you might expect; the first equalssomeBool = someBool && someOtherBool
, and so forth. -
The
^
(hat) is XOR - the EXclusive OR, which is surprisingly useful for inverting values with flags;boolean shouldPerformAction = isWithinRange() ^ !shouldBeWithinRange
. -
1/2
does not work as most often intended! That snippet performs an integer division, giving the result of 0. Use instead1/2.0
or1/2d
. Likewise, use10_000_000L
for explicitly telling that you are using a long number. -
Math.signum(double d)
is surprisingly useful! It returns the sign of the number; ifd
is negative, it returns-1
; ifd
is positive, it returns 1, and ifd
is 0, it returns 0. -
See for example the GeoGebra developer forum's list of gotchas