JNode coding DOs and DONTs
This page lists some tips on how to write good JNode code.
Please add other tips as required.
- Avoid using System.in, System.out, System.err
- Where possible, avoid using these three variables. The problem is that these variables are potentially global, and do not necessarily connected to the place that you want them to be.
In a user-level command, you should used use the 'in', 'out' and 'err' streams provided by the Command API; e.g. the 'execute' methods. Device drivers, services and so on that do not have access to these streams should use log4j logging.
- Avoid cluttering up the console with obscure or unnecessary logging
- If a message is important enough to be written to the console, it should be self explanatory. If it is unimportant, it should be logged using log4j at an appropriate level. We really do not need to see console messages left over from someone's attempts to debug something 12 months ago ...
- Avoid using Unsafe.debug(...) methods
- The org.jnode.vm.Unsafe.debug(...) methods write to the VGA screen and (when kernel debug is enabled) to the serial port. This is ugly, and should be reserved for important early boot sequence logging, VM and GC debugging, and other situations where log4j logging cannot be used.
- Don't call 'Throwable.printStackTrace' and friends
- Commands should allow unexpected exceptions to propagate to the shell level where they are handled according to the user's setting of the 'jnode.debug' property. Services, etc should make appropriate log4j calls, passing the offending Throwable as an argument.
- Do use JNode's Command and syntax.* APIs for commands
- Commands that are implemented using the Command and syntax.* APIs support completion and help, and are more likely to behave "normally"; e.g. with respect to stream redirection. There are lots of examples in the codebase.
At the moment, we are still including a legacy "public static void main(String[])" entry point in each JNode command, though this is only used by the old "default" command invoker.
If the APIs don't do what you want, have a word with Stephen Crawley.
