├── .gitignore ├── .editorconfig ├── .travis.yml ├── LICENSE ├── CODE_OF_CONDUCT.md ├── jetbrains_code_style.xml ├── README.md └── checkstyle.xml /.gitignore: -------------------------------------------------------------------------------- 1 | # IntelliJ 2 | .idea 3 | *.iml -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_size = 2 5 | indent_style = space 6 | trim_trailing_whitespace = true 7 | insert_final_newline = false 8 | max_line_length = 80 9 | charset = utf-8 10 | end_of_line = lf 11 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | 2 | dist: bionic 3 | language: bash 4 | 5 | addons: 6 | apt: 7 | update: true 8 | packages: 9 | - lmodern 10 | - texlive-latex-extra 11 | - texlive-generic-recommended 12 | - texlive-fonts-recommended 13 | - pandoc 14 | 15 | jobs: 16 | include: 17 | - script: pandoc -t latex README.md -o style-guide.pdf 18 | 19 | deploy: 20 | provider: releases 21 | file: style-guide.pdf 22 | skip_cleanup: true 23 | api_key: 24 | secure: h3XruwaedAsLyIk8uO+3OYWyixdiGwqo4NTk+njL/wYwwA6SCDtvhGVTc2YaEjJk7V9KsapGURr0uTQp6gU01lk8Q9JnRi4KOSG9Bn1EduYu8OGzHAk0G3DBFnw6lqw41kNFhsR7MTJiShMQDAmPsORh7DD4f34lagEuKwH51upvx92iBuU9y8AlUWwbnibTbM5Q0/P/hs+yaNdZKXmrqWtljovwUPUih2QbeyqeEARd0eYHDXdpHmXglI91wS055v1mhMK7XQ9R5x5w3SkCagJMA1z8sPiMQi5wl+1GXC6L0veNXIOCEDGJ/33Tou/GKB77FkykXk1ZSW5lJu7ezUrNyUBd93AXqdIu1MM5Gf3/eQP2lgp9oKWbEE4KAuw4D1qMDJWRUnjoLNCd0be+8Nx7VK7Uujiep8WuWsgMHBFSWNdG6B2RrpXnsegLRIv5RCFnYsHwn0dQzmB30dl1EoY0qpXjRekNOiu+IkRjNiSxUxvLYuDn4rPJ4pZ00rof4IdLFun/M0Zqkq452rZocNft3hobqaGq0/bHXdTagYBVcfjbuK8nj6zsMroDw00SvJYIr+xM6w5JHM1hcovYVkxjVfWNAqWzdO4OIx6KbvUWvKvg6No91SFbtFBbVVADATD6g7ClPpFrn16Hiio1udHYS0PGFckq2hJJEbbsBg8= 25 | on: 26 | tags: true 27 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | This is free and unencumbered software released into the public domain. 2 | 3 | Anyone is free to copy, modify, publish, use, compile, sell, or 4 | distribute this software, either in source code form or as a compiled 5 | binary, for any purpose, commercial or non-commercial, and by any 6 | means. 7 | 8 | In jurisdictions that recognize copyright laws, the author or authors 9 | of this software dedicate any and all copyright interest in the 10 | software to the public domain. We make this dedication for the benefit 11 | of the public at large and to the detriment of our heirs and 12 | successors. We intend this dedication to be an overt act of 13 | relinquishment in perpetuity of all present and future rights to this 14 | software under copyright law. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | OTHER DEALINGS IN THE SOFTWARE. 23 | 24 | For more information, please refer to 25 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as 6 | contributors and maintainers pledge to making participation in our project and 7 | our community a harassment-free experience for everyone, regardless of age, body 8 | size, disability, ethnicity, sex characteristics, gender identity and expression, 9 | level of experience, education, socio-economic status, nationality, personal 10 | appearance, race, religion, or sexual identity and orientation. 11 | 12 | ## Our Standards 13 | 14 | Examples of behavior that contributes to creating a positive environment 15 | include: 16 | 17 | * Using welcoming and inclusive language 18 | * Being respectful of differing viewpoints and experiences 19 | * Gracefully accepting constructive criticism 20 | * Focusing on what is best for the community 21 | * Showing empathy towards other community members 22 | 23 | Examples of unacceptable behavior by participants include: 24 | 25 | * The use of sexualized language or imagery and unwelcome sexual attention or 26 | advances 27 | * Trolling, insulting/derogatory comments, and personal or political attacks 28 | * Public or private harassment 29 | * Publishing others' private information, such as a physical or electronic 30 | address, without explicit permission 31 | * Other conduct which could reasonably be considered inappropriate in a 32 | professional setting 33 | 34 | ## Our Responsibilities 35 | 36 | Project maintainers are responsible for clarifying the standards of acceptable 37 | behavior and are expected to take appropriate and fair corrective action in 38 | response to any instances of unacceptable behavior. 39 | 40 | Project maintainers have the right and responsibility to remove, edit, or 41 | reject comments, commits, code, wiki edits, issues, and other contributions 42 | that are not aligned to this Code of Conduct, or to ban temporarily or 43 | permanently any contributor for other behaviors that they deem inappropriate, 44 | threatening, offensive, or harmful. 45 | 46 | ## Scope 47 | 48 | This Code of Conduct applies both within project spaces and in public spaces 49 | when an individual is representing the project or its community. Examples of 50 | representing a project or community include using an official project e-mail 51 | address, posting via an official social media account, or acting as an appointed 52 | representative at an online or offline event. Representation of a project may be 53 | further defined and clarified by project maintainers. 54 | 55 | ## Enforcement 56 | 57 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 58 | reported by contacting the project team at info@vicuna.io. All 59 | complaints will be reviewed and investigated and will result in a response that 60 | is deemed necessary and appropriate to the circumstances. The project team is 61 | obligated to maintain confidentiality with regard to the reporter of an incident. 62 | Further details of specific enforcement policies may be posted separately. 63 | 64 | Project maintainers who do not follow or enforce the Code of Conduct in good 65 | faith may face temporary or permanent repercussions as determined by other 66 | members of the project's leadership. 67 | 68 | ## Attribution 69 | 70 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, 71 | available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html 72 | 73 | [homepage]: https://www.contributor-covenant.org 74 | 75 | For answers to common questions about this code of conduct, see 76 | https://www.contributor-covenant.org/faq 77 | -------------------------------------------------------------------------------- /jetbrains_code_style.xml: -------------------------------------------------------------------------------- 1 | 2 | 347 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # vicuna.io Style Guide 2 | 3 | ## Formatting 4 | 5 | Indent using 2 Spaces (including continuous indents) 6 | 7 | ### Use "One True Brace Style" aka. [1TBS](https://en.wikipedia.org/wiki/Indent_style#Variant:_1TBS) 8 | Since we want a consistent code base and reduce the vertical size of our code. 9 | 10 | ```java 11 | // BAD 12 | void attack(Creature target) 13 | { 14 | for (Warrior warrior : warriors) { 15 | warrior.attack(target); 16 | } 17 | } 18 | 19 | // GOOD 20 | void attack(Creature target) { 21 | for (Warrior warrior : warriors) { 22 | warrior.attack(target); 23 | } 24 | } 25 | ``` 26 | 27 | ### Use Blocks whenever applicable 28 | To improve clarity and prevent bugs like [Apple's goto bug](https://dwheeler.com/essays/apple-goto-fail.html#targetText=On%202014%2D02%2D21%20Apple,fail%20goto%20fail%E2%80%9D%20vulnerability). 29 | 30 | ```java 31 | // BAD 32 | void defeatDragon(Dragon dragon) { 33 | while (dragon.isAlive()) attack(dragon); 34 | } 35 | 36 | // GOOD 37 | void defeatDragon(Dragon dragon) { 38 | while (dragon.isAlive()) { 39 | attack(dragon); 40 | } 41 | } 42 | ``` 43 | 44 | ### Max line length 45 | Prefer to keep the amount of characters below `80` and hard wrap lines at `90` characters. 46 | 47 | ### Blank Lines 48 | Avoid using blank line at the beginning and at the end of class and method definitions to reduce the code's vertical size. 49 | 50 | ```java 51 | // BAD 52 | public final class Person { 53 | 54 | private final String name; 55 | 56 | public String getName() { 57 | 58 | return name; 59 | } 60 | 61 | } 62 | 63 | // GOOD 64 | public final class Person { 65 | private final String name; 66 | 67 | public String getName() { 68 | return name; 69 | } 70 | } 71 | ``` 72 | 73 | ### Whitespaces 74 | Don't put whitespaces after parentheses and generally use them sparingly. Horizontal length is one of the most important properties of clean formatting. You should not waste your horizontal space with whitespaces. They can also make code harder to read. 75 | 76 | You should however put whitespaces after control structure keywords like `if` and `for`. 77 | 78 | ```java 79 | // BAD 80 | Sandwich prepareSandwich( Ingredients ingredients, SandwichRecipe recipe ) { 81 | synchronized ( kitchen ) { 82 | return recipe.prepare( ingredients ); 83 | } 84 | } 85 | 86 | // GOOD 87 | Sandwich prepareSandwich(Ingredients ingredients, SandwichRecipe recipe) { 88 | synchronized (kitchen) { 89 | return recipe.prepare(ingredients); 90 | } 91 | } 92 | ``` 93 | 94 | ## Naming 95 | 96 | ### Outdated Screaming Snake Case 97 | While Screaming Snake Case had their right to existence in constants and enum names, they are simply not needed these days. They mainly disturb and stand out 98 | in the otherwise clean written camelCase or UpperCamelCase language. With modern IDEs it is easy to identify any variable as an enum, constant, class name or anything else without having to change the formatting. Because of that, camelCase constants or UpperCamelCase enums improve the natuaral flow of written language. 99 | 100 | ```java 101 | // BAD 102 | public enum FallbackPolicy { 103 | THROW, 104 | LAST_ENTRY, 105 | EXPLICIT 106 | } 107 | 108 | // GOD 109 | public enum FallbackPolicy { 110 | Throw, 111 | LastEntry, 112 | Explicit 113 | } 114 | ``` 115 | 116 | ```java 117 | // BAD 118 | private static final String FALLBACK_FORMAT = "unknown property: %s"; 119 | 120 | // GOOD 121 | private static final String fallbackFormat = "unknown property: %s"; 122 | ``` 123 | 124 | 125 | ## Forbidden Language Constructs 126 | 127 | ### Double brace initialization 128 | Double brace initialization creates an anonymous class with an initializer. This increases the possibility of memory leaks and adds a useless class definition. Furthermore this construct can only be applied to non-final classes. 129 | 130 | ```java 131 | // BAD 132 | new HashMap() {{ 133 | put("one", 1); 134 | put("two", 2); 135 | }}; 136 | ``` 137 | 138 | ### Public fields 139 | While public fields are an easy way to provide exported properties of a data structure, they can not be protected against illegal access and may not be removed from a class without breaking its API. Prefer accessors to public fields. 140 | 141 | ```java 142 | // BAD 143 | public final class Person { 144 | public String name; 145 | } 146 | 147 | // GOOD 148 | public final class Person { 149 | private String name; 150 | 151 | public String getName() { 152 | return name; 153 | } 154 | } 155 | ``` 156 | 157 | ### Public constructors 158 | If there is no strong reason for a public constructor (like framework constraints), 159 | don not create one. There are several reasons not to use public constructors, 160 | Josh Bloch lists the following in his Book Effective Java (Item #1): 161 | 162 | [Medium - Effective Java Item 1](https://medium.com/@biratkirat/learning-effective-java-item-1-57f85b93c254) 163 | 164 | Create private constructors and use one of the following creators: 165 | 166 | - Static Factory Method - *When having a few (1-3) fields* 167 | - Builder Class - *When having a more fields* 168 | - Factory Class - *When the construction of a class is complicated or requires reused dependencies.* 169 | 170 | ```java 171 | // BAD 172 | public final class Address { 173 | private static final int DEFAULT_PORT = 8080; 174 | private static final String DEFAULT_HOST = "localhost"; 175 | 176 | private final int port; 177 | private final String host; 178 | 179 | public Address(int port) { 180 | this(DEFAULT_HOST, port); 181 | } 182 | 183 | public Address(String host, int port) { 184 | this.host = host; 185 | this.port = port; 186 | } 187 | } 188 | 189 | // GOOD 190 | public final class Address { 191 | public static Address create(String host, int port) { 192 | Objects.requireNonNull(host); 193 | validatePort(port); 194 | return new Address(host, port); 195 | } 196 | 197 | public static Address createLocalhost(int port) { 198 | validatePort(port); 199 | return new Address("localhost", port); 200 | } 201 | 202 | private static void validatePort(int port) { ... } 203 | 204 | private final int port; 205 | private final String host; 206 | 207 | private Address(String host, int port) { 208 | this.host = host; 209 | this.port = port; 210 | } 211 | } 212 | ``` 213 | 214 | ### Don't use null 215 | Using null to represent absent values is a bad design decision and leads to maintainability and safety problems. Instead of using null, favour returning `Optional` or throwing an exception if an error caused the value to be absent. 216 | 217 | Instead of taking null parameters, provide an extra method or overload. 218 | 219 | Exception to this rule are internal/private methods. 220 | When allowing nullable parameters or returning nullable values, use the `@javax.annotation.Nullable`-Annotation. 221 | 222 | ### Don't declare boolean parameters or fields 223 | Boolean fields and parameters should be used sparingly, as they are not self-documenting. 224 | 225 | Instead of having a field of type boolean, declare an Enum. 226 | 227 | ```java 228 | // BAD 229 | public final class Session { 230 | private boolean active; 231 | } 232 | 233 | // GOOD 234 | public final class Session { 235 | // Must not be public 236 | enum State { 237 | ACTIVE, 238 | CLOSED; 239 | } 240 | 241 | private State state; 242 | } 243 | ``` 244 | 245 | Instead of a method having a boolean parameter, split it up into two methods and mark their difference by descriptive names. 246 | 247 | ```java 248 | // BAD 249 | void accept(Visitor visitor, boolean recursive); 250 | 251 | // GOOD 252 | void accept(Visitor visitor); 253 | void acceptRecursive(Visitor visitor); 254 | ``` 255 | 256 | Using the method from the bad example, one would write `accept(visitor, true)` and the reader, who may not know the signature of accept, can not infer what `true` causes. 257 | 258 | When using the method from the good example, the same code would be written as `acceptRecursive(visitor)`, which can be understood without looking at the methods documentation. 259 | 260 | ## Object Oriented Programming Anti-Patterns to avoid 261 | 262 | ### God Object 263 | God objects know too much and are known to well. They often store 'global variables' or 264 | dependencies and are referenced to access them in other classes. Sometimes they also 265 | contain methods that can be thought of as procedures (procedural programming). Those 266 | objects result in code being harder to test, extend and maintain. Suddenly every class 267 | will be coupled to this God object. 268 | 269 | Instead use dependency injection and apply the [information expert principle](https://en.wikipedia.org/wiki/GRASP_(object-oriented_design)#Information_expert). 270 | 271 | ### Job Names (Anemic Domain Model) 272 | There are those class names that one comes across in almost every ~~object oriented~~ codebase, some examples are: 273 | 274 | - `UserManager` 275 | - `JsonParser` 276 | - `RequestHandler` 277 | 278 | A lot of these classes have one single exported method like: 279 | - `parseJson` 280 | - `handleRequest` 281 | 282 | Whilst others have so many responsibilities, that their name becomes very generic 283 | like `Manager` or `Service`. But they have something in common, they consist of procedures, 284 | operating on data, rather than objects exposing behaviour. And some just have a badly chosen name. 285 | 286 | The real problem is not their name but their design. In order to avoid such classes, 287 | move stop exposing data, move methods closer to the data they operate on and start 288 | thinking in objects rather than procedures. 289 | 290 | ## Other bad practices 291 | 292 | ### Anonymous classes instead of lambdas 293 | Some developers think using anonymous classes instead of lambdas would give them some performance benefits. This isn't true, at least not for the OpenJDK. While linking time may be affected by lambdas, their implementation doesn't really differ from that of anonymous classes. They actually define an anonymous class at link-time. One difference is, that lambdas, defined in instance methods, won't capture the instance variable unless required. Anonymous classes always capture it, resulting in a higher chance of memory leaks. Lambdas and especially stateless lambdas can be optimized quite well by the JVM. 294 | 295 | One major difference is, that lambdas are much easier to read and don't consume the amount of space that anonymous classes do. 296 | 297 | ### Lambdas with blocks 298 | Lambdas should be small. Try not to define lambdas with more than one statement. 299 | Instead create a new method and call it from within the lambda. 300 | 301 | ```java 302 | // BAD 303 | shoppingCart.removeIf(item -> { 304 | var owned = inventory.lookup(item.id()); 305 | return owned != null && !owned.isBroken(); 306 | }); 307 | 308 | // BETTER 309 | shoppingCart.removeIf(item -> isAlreadyOwned(item)); 310 | 311 | // BEST 312 | shoppingCart.removeIf(this::isAlreadyOwned) 313 | ``` 314 | 315 | ### Magic numbers 316 | When looking at someone else's code, one often comes across magic numbers. They have a meaning which is often only known by the author. Instead of using magic numbers, use number constants. 317 | 318 | The only number literals allowed in code are `-1`, `0`, and `1`. Which doesn't mean that you shouldn't put them into constants. 319 | 320 | ```java 321 | // BAD 322 | int reward = ((60 / time) * 100) + (kills * 2); 323 | 324 | // GOOD 325 | int reward = ((TIME_LIMIT / time) * TIME_REWARD) + (kills * KILL_REWARD)); 326 | ``` 327 | 328 | ## Principles to follow 329 | - Design By Contract 330 | - Law Of Demeter 331 | - Law of Least Astonishment 332 | - Command Query Separation (if applicable) 333 | 334 | ## Checkstyle 335 | 336 | We created a simple [checkstyle config](checkstyle.xml) that enforces major rules 337 | of our style guide. The config is based on the 338 | [google config](https://github.com/checkstyle/checkstyle/blob/master/src/main/resources/google_checks.xml) 339 | and should be used as strict as possible (warnings = errors). 340 | 341 | ### Editorconfig 342 | 343 | We created a simple [.editorconfig](.editorconfig) that will help you formatting your source code. 344 | -------------------------------------------------------------------------------- /checkstyle.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 36 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 51 | 52 | 53 | 54 | 55 | 56 | 59 | 60 | 61 | 62 | 63 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 75 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 117 | 118 | 119 | 121 | 122 | 123 | 124 | 126 | 127 | 128 | 129 | 131 | 132 | 133 | 134 | 136 | 137 | 138 | 139 | 141 | 142 | 143 | 144 | 145 | 147 | 148 | 149 | 150 | 152 | 153 | 154 | 155 | 157 | 158 | 159 | 160 | 162 | 163 | 164 | 165 | 167 | 169 | 171 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | 197 | 198 | 199 | 200 | 201 | 202 | 205 | 206 | 207 | 208 | 210 | 211 | 212 | 213 | 214 | 215 | 216 | 217 | 218 | 219 | 220 | 222 | 223 | 224 | 225 | 226 | 228 | 229 | 230 | 231 | 232 | 233 | 234 | 235 | 236 | 237 | 238 | 239 | 240 | 241 | 242 | 243 | 244 | 246 | 247 | 248 | 249 | 250 | 251 | 252 | 253 | 254 | 255 | 256 | --------------------------------------------------------------------------------