|
| 1 | +# Example guidelines |
| 2 | + |
| 3 | +Main guiding principles |
| 4 | + |
| 5 | +- focus on explaining a use case. |
| 6 | +- the code structure should be easy to understand. |
| 7 | +- the examples should aim at being short. |
| 8 | + |
| 9 | +## Guiding examples |
| 10 | + |
| 11 | +This repository has many ACAP application examples which makes it important to |
| 12 | +keep a common structure for both maintenance and familiarity for users. |
| 13 | + |
| 14 | +New examples must follow the general example format. Most examples are aligned, |
| 15 | +but not all, therefore it's recommended to follow one of these guiding examples: |
| 16 | + |
| 17 | +- [axparameter](./axparameter/) |
| 18 | + <!-- textlint-disable terminology --> |
| 19 | +- [vapix](./vapix/) |
| 20 | + <!-- textlint-enable --> |
| 21 | + |
| 22 | +## Example files and structure |
| 23 | + |
| 24 | +The minimum example structure |
| 25 | + |
| 26 | +```sh |
| 27 | +example-name |
| 28 | +├── app |
| 29 | +│ ├── .c / .cpp / shell script |
| 30 | +│ ├── LICENSE |
| 31 | +│ ├── Makefile (*) |
| 32 | +│ └── manifest.json |
| 33 | +├── Dockerfile |
| 34 | +└── README.md |
| 35 | + |
| 36 | +(*) Makefile is not necessary when architecture equals `all`. |
| 37 | +``` |
| 38 | + |
| 39 | +- **Don't use build scripts like `build.sh`** |
| 40 | + - Build instructions are seen directly from the example README and it's easy |
| 41 | + to make custom builds from command-line by adding e.g. a build flag to the |
| 42 | + `docker` command. This setup is clean and easy to automate for different |
| 43 | + users' needs. |
| 44 | +- **Use general names, avoid versions** |
| 45 | + - Don't use versions in text that will need future updates, making |
| 46 | + maintenance hard. |
| 47 | + - For example, instead of ACAP Native SDK 12, use ACAP Native SDK. |
| 48 | +- **Add copyright license** |
| 49 | + - The standard license is Apache 2.0 |
| 50 | + - Copy LICENSE file and year in line `Copyright 2025 Axis Communications`. |
| 51 | + - All source code should have a copyright header. |
| 52 | +- **Dockerfile** |
| 53 | + - Don't add proxy variables like `http_proxy` and `https_proxy` in the |
| 54 | + example. See the [Proxy](https://developer.axis.com/acap/develop/proxy) |
| 55 | + section for how to work with proxy in build time. |
| 56 | + |
| 57 | +### README |
| 58 | + |
| 59 | +A key to make a good example is to put effort in making the example name, title |
| 60 | +and introduction as clear and concise as possible. |
| 61 | + |
| 62 | +- Describe the use case. |
| 63 | +- An image, graph or code snippet might be helpful in some cases. |
| 64 | + |
| 65 | +After the introduction |
| 66 | + |
| 67 | +- Reference the APIs used and point to API documentation. |
| 68 | +- For larger examples it's useful to make an outline of what the program does |
| 69 | + to help the reader. |
| 70 | + |
| 71 | +### Makefile |
| 72 | + |
| 73 | +For C and C++ files, use as minimum the same amount of GCC warning options as |
| 74 | +the other examples and `-Werror` to make them into build errors. |
| 75 | + |
| 76 | +### C or C++ |
| 77 | + |
| 78 | +- C is preferred, it tends to make examples clearer, especially shorter ones. |
| 79 | +- C++ could be used in larger more advanced examples. |
| 80 | +- Use the language that makes the shortest and easiest to read example |
| 81 | + |
| 82 | +#### C-code style |
| 83 | + |
| 84 | +All C and C++ files are linted by clang and should also be autoformatted by |
| 85 | +clang, see [LINT.md](./LINT.md) for more details. |
| 86 | + |
| 87 | +- **General** |
| 88 | + - Assume knowledge in C, examples should not explain basic functionality. |
| 89 | +- **libc or glib?** |
| 90 | + - Use standard libc as default. |
| 91 | + - Use glib when it saves time, lines, is safer or easier to read. |
| 92 | +- **Functions** |
| 93 | + - The function name is very important, it should explain what is done. |
| 94 | + - Functions should strive to do *one* thing. |
| 95 | + - Don't use function descriptions. |
| 96 | + - Avoid functions with many arguments. Use `structs` instead for such cases. |
| 97 | +- **Function prototypes** |
| 98 | + - Try to avoid them to make examples shorter. |
| 99 | + - Place main function last. |
| 100 | + Place functions without dependencies at the top |
| 101 | + If possible, no forward declarations within the file |
| 102 | +- **Comments** |
| 103 | + - Don't repeat the code in comments. |
| 104 | + - Comments should explain **why** and the code show **how**. |
| 105 | + - Use `//` as in `// An explaining comment` for all comments. |
| 106 | +- **Error handling** |
| 107 | + <!-- textlint-disable terminology --> |
| 108 | + - At errors, use function `panic()` as in the [vapix](./vapix/) example. This |
| 109 | + function logs to syslog and exit with 1 directly without cleanup. |
| 110 | + <!-- textlint-enable --> |
| 111 | + - The examples should not focus on error handling via error propagation. |
| 112 | + By crashing early errors are caught on first occurrence and avoids lengthy |
| 113 | + code not suitable for example code. |
0 commit comments