This page documents some Stork coding guidelines.
- Test-Driven Development
- Documentation
- Dead code
- Go Style
- TypeScript Style
- HTML & CSS Style
- General
- User Interface (UI) Guidelines
- Imported Code
- Guidelines Adopted by Other Projects
Refer also to the Kea coding guidelines and BIND best practices. They should be used where they do not conflict with the guidelines on this page. This is because we expect some ISC developers work on both versions of code, and in that case it's easier to maintain the code if the styles are as compatible as possible. Some of the styles derived from BIND 9 that are often forgotten or misunderstood are explicitly mentioned below. Having said that, Stork is a project with radically different environment. We don't need to stick to old rules invented for C code in the 1990s.
Test-Driven Development
Kea project proved beyond any doubt that TDD works and improves the code quality. We absolutely want to repeat the exercise in Stork. However, there are some lessons learned in Kea project. In general, unit-tests should be developed alongside the production code (before the code if possible) and included in the same MRs. Having said that, Kea project sometimes went a bit overboard with this and there were cases when people were wasting time implementing unit-tests for trivial getters and setters. That's not the goal here.
The intention is to make sure that the most complex or tricky parts of the code should be testable and we should have reasonable confidence in them being correct. There's another essential aspect here. If you feel that it's not possible to write unit-tests for your new code, perhaps your new code is structured badly? This is one of two major reasons why the tests cannot be written letter. The other is that there's never good time to come back and implement tests...
Finally, Stork is currently approved for 6 months. We don't want to have few features with super extensive tests. This could kill the project if the only thing we can show after 6 months is well written tests. On the other hand, we don't want to have tons of buggy features that don't work. We need to take a middle ground. Please use common sense. If in doubt, please ask @tomek for suggestions.
Documentation
Testing/Documentation addresses and prefixes
Use 192.0.2.0/24 (see RFC5737 and 2001:db8::/32 (see RFC3849 for purposes like addresses used in test cases or examples in documentation. Likewise, use reserved example domain names such as example.com, .test, .example, etc for domain names used in these cases (see RFC2606). They are reserved by specifications and should be the safest in terms of collision avoidance.
TODO Comments
We sprinkle comments in code with keywords to indicate pending work.
In Stork, TODO
is preferred. If there is a corresponding ticket, feel free to specify its number in the comment. Unless other wise specified, issue #1234 means a ticket in the Gitlab, available at http://gitlab.isc.org/isc-projects/stork.
Function/method comments
Every non-trivial (trivial means one or two liner, such as getter or setter) function and method MUST be documented. While it's appreciated, the documentation doesn't have to be very thorough. Well expressed sentence is often enough. Make sure the description is as useful as possible. "Returns URL string" is bad. "Returns agent's contact point URL as a string" is much better.
User's guide
The User's guide should be updated when a new feature is added. Again, the description doesn't have to be complex or extensive. A paragraph of text briefly announcing new feature or capability will help users (and other developers) a lot.
Dead code
Dead code is bad; it suffers from code rot, and it looks unclean. There are some circumstances where there is a reason to keep a bit of unused code around for a while, but these should be the exception rather than the rule, and it should be very clear why it is there, and on what conditions and when it will be re-enabled or removed completely.
Any dead code (both files that are unused and blocks of commented-out code) should in principle be removed. If there is a very good reason to keep it around for a while, it must be accompanied by a comment explaining why it is still there, and when it will be removed or enabled again. This comment should point to a ticket so that we do not forget about it.
Go Style
Used by backend server and agent. We use the syntax recommended by go fmt
command. Please make sure your code adheres before you submit it for review.
TypeScript Style
Used by UI. Details TBD
HTML & CSS Style
Naming
ID of HTML elements and CSS class names should obey English grammar. Words should be separated by dash (-). At the end of the name of ID there should be object. We do not need any specific prefix in the names.
Examples:
-
served-scopes-help-box
- ID of a help box for server scopes -
stork-version-tooltip
- ID of a tooltip for stork version element -
action-button
- class name for action button -
dhcp-services-table
- class name for a table with DHCP services -
section-heading
- class name for section heading -
green-colored-panel
- class name for green panel
Examples from PrimeNG:
-
ui-tooltip-arrow
- class name for an arrow in tooltip -
ui-panel-titlebar
- class name for a titlebar in a panel -
ui-widget-content
- class name of widget content
General
Use all all-lowercase characters for file names. Use dash as a separator (e.g. stork-agent.go). This is consistent with the current practice in kea. Not mixing lower/upper cases will also help avoid name conflicts in a case insensitive file system, such as MacOS.
Ordering Imports in Go
We include first packages from standard library, then 3rd party packages and at the end our own project packages.
Line length
The project kicked off in 2019. FullHD is ubiquitous with large 4K displays are getting common. In the general case, the code should have no more than 128 columns. This is let developers display two panels side by side. In some exceptional cases (such as URL), the code may be extended to 160 columns, but this should be rare occurrence.
Tabs & Indentation
Do not use hard tabs.
Indentation at each level is 4 spaces for C++, other languages should use what is "usual and expected."
Naming
Don't start things with underscores.
Class names are '''LikeThis
''', methods are '''likeThis()
''', variables are '''like_this
''', and constants are '''LIKE_THIS
'''. Data class members are '''like_this_
'''.
Comments
Multiline comments can be written in C++ or C style (that is, with // or /* */ marks).
/*
* This is a comment. It is important probably.
*/
//
// This is a comment. It is important probably.
//
/* This is also ok. */
// As is this.
Comments at the end of lines should usually be C++ style:
class Foo {
int bar_length; // The length of the bar in millimeters.
};
Doxygen Comment Style
When writing a Doxygen special comment block there are several possible styles:
http://www.stack.nl/~dimitri/doxygen/docblocks.html
Doxygen keywords should be prepended with @, not with a backslash. The reason to prefer @ is that backslash may confuse scripts that would go over the code. There is some inconsistency in this regard. There are large parts of older code that still use backslash. It is a matter of personal taste to keep consistency with what is in the file vs. strictly sticking with this principle.
We use the C++ style of 3-slashes:
/// A lot of examples are called foo().
///
/// @param baz foo() usually takes an argument
void
foo(Bar baz) {
...
}
Make sure inserting a blank line between two function/method declarations or definitions:
class Bad {
/// @brief Short description for bad1
void bad1();
/// @brief Short description for bad2, which may also look for bad1().
void bad2();
};
class Good {
/// @brief Short description for good1
void good1();
/// @brief Short description for good2, which should be much clearer.
void good2();
};
Explicit @brief for Doxygen
If you don't use @brief as the first thing in your doxygen comment, then doxygen will turn the first paragraph into a @brief description anyway. However, we include it anyway so that everybody understands that this is the @brief description.
Methods and Functions
Exception-safe getters and string-production methods
Unless there's a compelling reason to do so neither member value getter methods nor and string-production methods, such as toString(), should throw exceptions. Normally a class member is prevented from ever having an invalid value so there is arguably a getter never has a reason to throw, and string-production methods should always be safe to invoke once a class has been instantiated. Both types of methods are commonly used as log statement arguments where one should not have to worry about catching exceptions.
Log Statement Safety
It is extremely important to examine all arguments passed into a log statement to ensure they will produce safe values at runtime:
- Can the argument (or any part of it) be NULL? If so is this taken into account?
- If the argument invokes any functions, are they exception safe?
- If it involves indirection, does this always resolve into a usable value?
- If it raises an exception, is the exception caught? This includes double errors, i.e., log statements in an exception handler.
Log statements are less than helpful if they cause the program to segfault or throw.
User Interface (UI) Guidelines
- The minimum resolution needed to reasonably use Stork UI is 720p (1280x720).
- Other aspects are TBD
Imported Code
If you import code from another project, try to continue the style of the imported project if changes need to be made. This is for two reasons, one is to make merging future versions easier. The other is the encouragement of submitting changes upstream.
Guidelines Adopted by Other Projects
Other projects have their own coding guidelines. Here're some examples of such guidelines. These are reference purposes only; unless explicitly stated we also adopt some part of other guidelines, they are not part of the Stork's coding guidelines.