| 6 |
| 7 | This directory is meant to house common utilities that can be shared across the |
| 8 | whole Chromium codebase. //base is similar, but due to the way //base grew over |
| 9 | time, it has not been well organized to allow for fine-grained ownership. Also, |
| 10 | there is a mixture of commonly useful utility code and extremely subtle code |
| 11 | with performance and security implications. The combination of the two lead to |
| 12 | a small number of //base OWNERS enforcing a very high quality bar over a |
| 13 | diverse bag of code. The goal of //base/util is to avoid both these issues. |
| 14 | Over time, functionality in //base proper will sorted into //base/util and |
| 15 | one of two things will occur: |
| 16 |
| 17 | 1. //base becomes empty and subdirs in //base/util get moved up to //base |
| 18 | 2. A distinct "core" //base module is distilled out. |
| 19 |
| 20 | We will iterate on this purpose as we add more functionality into this |
| 21 | directory. |
| 22 |
| 23 | ## Structure of //base/util: |
| 24 |
| 25 | * No files other than README.md, and OWNERS are allowed in the top-level |
| 26 | directory. |
| 27 | * Subdirectories should be named for a class of functionality; it should |
| 28 | be relatively easy to browse the directory and find a utility. |
| 29 | * There is no top-level BUILD.gn. Subdirectories will have their own and |
| 30 | are allowed to reference other subdirectories. |
| 31 |
| 32 | ## Responsibilities of //base/util/OWNERS |
| 33 |
| 34 | * Creating new categories. |
| 35 | * Helping dedupe and organize code being added. |
| 36 | * Ensuring the directory is somewhat browseable. |
| 37 |
| 38 | It is specifically NOT the OWNERS job to gatekeep what is a "good pattern" |
| 39 | for Chromium code. Obviously they can still object, but the objection |
| 40 | should be considered similar to any other Chromium dev objection. |
| 41 |
| 42 | There will be cases when a utility is deemed to be more negative than |
| 43 | positive after it has been landed. The //base/util/OWNERS may aide in |
| 44 | coordinating and tracking removal, but responsibility for actually |
| 45 | deleting the code and its uses falls to the *category* OWNERS. |
| 46 |
| 47 | ## Guidelines for adding code to //base/util/{category} |
| 48 |
| 49 | * You will be added to an OWNERS file under //base/util/category and be |
| 50 | responsible for maintaining your addition. |
| 51 | * A //base/util/OWNER must approve of the location of your code. |
| 52 | * Code must be needed in at least 2 places in Chrome and have no "higher |
| 53 | layered" directory (eg "//ui/base, //media/base, etc.), that could |
| 54 | facilitate sharing. |
| 55 | * Code must have unittests, and coverage should be > 95%. |
| 56 | * Code must have clear usage documentation for all the APIs. |
| 57 | * Public APIs must be in `::util` namespace. All implementation details |
| 58 | should be in `::util::internal`. Macros, which are not namespaceable, |
| 59 | are permitted but should be used sparingly and cause a small pang of guilt. |
| 60 | * Implementation and expected usage must be understandable by another OWNER |
| 61 | in your subdirectory; if creating a new subdirectory you must find a |
| 62 | co-OWNER. |
| 63 | * New subdirectories should have their own BUILD.gn files. |
| 64 |
| 65 |
| 66 | ## Why not just put new utilities in //base/FooDir? |
| 67 |
| 68 | At some point, //base/util directories could get moved back into //base. |
| 69 | Until then, //base/util will |
| 70 |
| 71 | 1. make a distinct separation between "common useful utility code" from the |
| 72 | "extremely subtle code with performance and security implications" that //base |
| 73 | also houses. |
| 74 | 2. remove //base/OWNERS as a bottleneck. |
| 75 |
| 76 | The boundary is still a work-in-progress, but should clear itself up with time |
| 77 | after some of the more obviously "utility-esque" classes are moved. |
| 78 |
| 79 |
| 80 | ## How does this differ from //components |
| 81 | Both //components and //base/util contain subdirectories that are (a) intended |
| 82 | for reuse. In addition, //components imposes no global layering in Chromium, so |
| 83 | a subdirectory placed in //components can be used from most-to-all layers in the |
| 84 | codebase, subject to the dependencies that that subdirectory itself holds. |
| 85 |
| 86 | In spite of these similarities, there are *conceptual* differences: //components |
| 87 | contains things are closer to full features or subsystems (eg autofill, heap |
| 88 | profiler, cloud devices, visited link tracker) that are not really intended for |
| 89 | large scale reuse. |
| 90 |
| 91 | There is some overlap and at some point it will become a judgment call, but |
| 92 | in general, //components are a better fit if the code in question is a feature, |
| 93 | module, or subsystem. //base/util is better if it is a more narrow construct |
| 94 | such as a data structure, coding primitive, etc. |
| 95 |
| 96 |
| 97 | ## Why not the "Rule-of-3?" |
| 98 |
| 99 | The [Rule-of-3](https://en.wikipedia.org/wiki/Rule_of_three_%28computer_programming%29) |
| 100 | is a simple guidance on when it makes sense to extract common functionality out |
| 101 | versus duplicating it. It has commonly been used in //base as a way of measuring |
| 102 | "how general" is this functionality. |
| 103 |
| 104 | Unfortunately, there are reasons for wanting to share code beyond just |
| 105 | cleanliness. For example, if you need to guarantee exact behavior across |
| 106 | two modules, duplication is not proper even if there will ever only be 2 |
| 107 | users. |
| 108 |
| 109 | Furthermore, there is a chicken-and-egg problem that prevents incremental |
| 110 | adoption of a utility. For example, someone introduces ThingerDoer in |
| 111 | //foo/bar. Later, ThingerDoer is wanted in //foo/qux, but this still fails |
| 112 | the rule-of-3 test, MyDoer is created. When //foo/waldo wants it, it's |
| 113 | completely a game of chance whether or not the CL author manages to find |
| 114 | both classes, wants to spend the time to determine if they've diverged, |
| 115 | and then tries to abstract it. |
| 116 |
| 117 | As such, the rule-of-3 runs contrary to the goal of sharing that |
| 118 | //base/util is designed to facilitate. |
| 119 |
| 120 | ## Tips |
| 121 | * if doing a mass-move of code, look at `//tools/git/mass-rename.py` |
| 122 |