-
Notifications
You must be signed in to change notification settings - Fork 37
Programming Style Conventions
As in any programming, adopting a consistent style makes many things far easier:
- collaboration
- debugging
- development
to name a few. iDynoMiCS is predominantly Java-based, and so we try as much as possible to stick to conventional Java style. Useful references include Oracle and Eclipse's in-built formatter. The Practice of Programming by Kernighan and Pike is a good read for anyone looking to improve their general programming technique. Occasionally it may make more sense to adapt elements of this style to our particular needs. The relative merits of exact details are of secondary importance - the crucial thing is to be consistent. The purpose of this page is to lay out general guidelines for how iDynoMiCS should be written. Please try to stick to these as closely as possible, and if you notice any significant deviations then either recode them (if you feel comfortable doing so) or flag them as a problem in the Issues tab.
This may seem a very minor issue, but it certainly improves readability in situations where there are several layers of nesting. Eclipse has an in-built tool: Source > Correct indentation.
The standard form of iterating through a for list in Java is for (initialisation; condition; update) { statement; }
. For example, when counting through the integers 0 to 9, for (int i=0; i<10; i++;) { statement; }
, but when working through a predefined list, something like for (Element aChild : xmlRoot.getChildren("kineticFactor")) { statement; }
is more appropriate.
while loops require only a condition and may not execute the statements they contain. They should not be confused with do-while loops, which always execute the statements at least once. Infinite loops, broken once a condition is met, are used on occasion in iDynoMiCS: while (true) { statement; break-statement; }
is recommended as it is clearer than while (1) {...}
or for (;;) {...}
.