-
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. -
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. Therefor be extremely careful regarding usage of the latter approach. -
You can negate boolean values with
!
, and you can negate 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. -
See for example the GeoGebra developer forum's list of gotchas