| 3 | | = BOINC coding style = |
| | 3 | = Software and information architecture = |
| | 4 | |
| | 5 | Since I first wrote it in 2002, BOINC has evolved continuously and fairly smoothly. |
| | 6 | It's now an extremely powerful system, |
| | 7 | but its code size is fairly small, |
| | 8 | and the code is malleable (easy to change and extend). |
| | 9 | I don't see any barriers to BOINC's continued evolution. |
| | 10 | |
| | 11 | This success is due in large part to BOINC's architecture. |
| | 12 | I'll try to articulate some of the principles behind this architecture. |
| | 13 | |
| | 14 | == Simplicity and brevity == |
| | 15 | |
| | 16 | The most important principle is: make the code as simple as possible. |
| | 17 | |
| | 18 | To a large extent, shorter is simpler. So: |
| | 19 | |
| | 20 | * Given two possible implementation, use the shorter one. |
| | 21 | * If you're writing something new, and it needs a function |
| | 22 | that might be of general utility, |
| | 23 | check whether it already exists (in lib/ or html/inc) |
| | 24 | rather than writing it yourself. |
| | 25 | * If code is repeated, factor it out and make it into a function. |
| | 26 | Don't copy and paste code. |
| | 27 | |
| | 28 | Related principles: |
| | 29 | |
| | 30 | * If you're adding a new mechanism, |
| | 31 | try to make it more general than the immediate need, |
| | 32 | without making it more complicated. |
| | 33 | * If a function becomes longer than 100 lines or so, split it up. |
| | 34 | * If a file is becoming too big, |
| | 35 | or has a bunch of unrelated stuff, split it up. |
| | 36 | |
| | 37 | == Uniformity == |
| | 38 | |
| | 39 | Imitate the style and structure of the code that's already there, |
| | 40 | even if you don't like it. |
| | 41 | |
| | 42 | == Data architecture == |
| | 43 | |
| | 44 | BOINC stores information in various forms. |
| | 45 | Guidelines: |
| | 46 | |
| | 47 | * '''MySQL database''': |
| | 48 | The DB server is a potential performance bottleneck, |
| | 49 | and there is a code overhead for DB tables |
| | 50 | (you need to write interface functions in C++ and/or PHP, |
| | 51 | and you need to write web pages or scripts for editing data). |
| | 52 | So use the DB only when it's necessary |
| | 53 | (e.g. because you need indices for large data) and avoid creating new tables. |
| | 54 | Don't use referential constraints; instead, check for lookups returning null. |
| | 55 | * '''XML fields in DB tables''': |
| | 56 | Simplify DB design by storing information in XML in DB fields |
| | 57 | (for example, computing preferences and job file lists). |
| | 58 | This can eliminate the need for separate tables. |
| | 59 | * '''XML files''': |
| | 60 | BOINC uses XML format both for internal state (e.g. client_state.xml) |
| | 61 | and configuration files (cc_config.xml on the client, project.xml on the server). |
| | 62 | Sometimes we use enclosing tags for multiple items |
| | 63 | (e.g. <tasks> in project.xml); sometimes we don't. |
| | 64 | Either is fine - imitate what's already there. |
| | 65 | * '''PHP and/or C++ code''': |
| | 66 | If data is used only in PHP, put it in a PHP data structure. |
| | 67 | You can assume that project admins are able to edit these |
| | 68 | (e.g. html/project/project.inc). |
| | 69 | |
| | 70 | == Avoid over-engineering == |
| | 71 | |
| | 72 | If you make a big change to solve a problem that never actually arises, |
| | 73 | that's over-engineering. |
| | 74 | Avoid it. |
| | 75 | |
| | 76 | There are various things (HTTP headers, message boards, XML parsing) |
| | 77 | where we've had to choose between using someone else's library or doing it ourselves. |
| | 78 | There are always tradeoffs. |
| | 79 | If we have something that works and is stable (e.g. XML parsing), leave it. |
| | 80 | |
| | 81 | In general, big changes should be undertaken only if it's certain |
| | 82 | that there's going to be a big reward. |
| | 83 | Big changes introduce bugs, and end up costing more than you think. |
| | 84 | One exception: occasionally I'll make a change whose only effect |
| | 85 | is to shorten or simplify the code. |
| | 86 | |
| | 87 | = Coding style = |