Coding style & general guidelines

General

  • Ideally, discuss your plans on the forums to see if others want to work with you on it
  • We use GitHub, please get an account there and clone the repositories you want to work on
  • Fixes go directly to master, nevertheless they need to be tested thoroughly.
  • New features are always developed in a branch and only merged to master once they are fully done.
  • Software should work. We only put features into master when they are complete. It’s better to not have a feature instead of having one that works poorly.
  • It is best to start working based on an issue - create one if there is none. You describe what you want to do, ask feedback on the direction you take it and take it from there.
  • When you are finished, use the merge request function on GitHub to create a pull request. The other developers will look at it and give you feedback. You can signify that your PR is ready for review by adding the label “5 - ready for review” to it. You can also post your merge request to the mailing list to let people know. See the code review page for more information
  • It is key to keep changes separate and small. The bigger and more hairy a PR grows, the harder it is to get it in. So split things up where you can in smaller changes - if you need a small improvement like a API addition for a big feature addition, get it in first rather than adding it to the big piece of work!
  • Decisions are made by consensus. We strive for making the best technical decisions and as nobody can know everything, we collaborate. That means a first negative comment might not be the final word, neither is positive feedback an immediate GO. Nextcloud is built out of modular pieces (apps) and maintainers have a strong influence. In case of disagreement we consult other seasoned contributors.

Labels

We assign labels to issues and pull requests to make it easy to find them and to signal what needs to be done. Some of these are assigned by the developers, others by QA, bug triagers, project lead or maintainers and so on. It is not desired that users/reporters of bugs assign labels themselves, unless they are developers/contributors to Nextcloud.

The most important labels and their meaning:

  • #backport-request - the pull requests also needs to be applied to older Nextcloud versions
  • #bug - this issue is a bug
  • #enhancement - this issue is a feature request/idea for improvement of Nextcloud
  • #design - this needs help from the design team or is a design-related issue/pull request
  • #technical debt - this issue or PR is about technical debt
  • #good first issue - these are issues which are relatively easy to solve and ideal for people who want to learn how to code in Nextcloud
  • #needs info - this issue needs further information from the reporter, see ../bugtracker/triaging
  • #high #medium #low signify how important the bug is.
  • Tags showing the state of the issue or PR, numbered 0-4:
    • 0. Needs triage - ready to start development on this
    • 1. to develop - ready to start development on this
    • 2. developing - development in progress
    • 3. to review - ready for review
    • 4. to release - reviewed PR that awaits unfreeze of a branch to get merged
  • Feature tags: #feature: something. These tags indicate the features across apps and components which are impacted by the issue or which the PR is related to

If you want a label not in the list above, please first discuss on the mailing list.

Coding

  • Maximum line-length of 80 characters
  • Use tabs to indent
  • A tab is 4 spaces wide
  • Opening braces of blocks are on the same line as the definition
  • Quotes: ‘ for everything, ” for HTML attributes (<p class=”my_class”>)
  • End of Lines : Unix style (LF / ‘n’) only
  • No global variables or functions
  • Unit tests
  • HTML should be HTML5 compliant
  • Check these database performance tips
  • When you git pull, always git pull --rebase to avoid generating extra commits like: merged master into master

License headers

Nextcloud is licensed under the GNU AGPLv3. From June, 16 2016 on we switched to “GNU AGPLv3 or any later version” for better long-term maintainability. If you create a new file please use this header:

  1. /**
  2. *
  3. * @copyright Copyright (c) <year>, <your name> (<your email address>)
  4. *
  5. * @license GNU AGPL version 3 or any later version
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as
  9. * published by the Free Software Foundation, either version 3 of the
  10. * License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. *
  20. */

If you edit an existing file please add a copyright notice with your name, if you consider your changes substantial enough to claim copyright. As a rule if thumb, this is the case if you contributed more than seven lines of code.

User interface

  • Software should get out of the way. Do things automatically instead of offering configuration options.
  • Software should be easy to use. Show only the most important elements. Secondary elements only on hover or via Advanced function.
  • User data is sacred. Provide undo instead of asking for confirmation - which might be dismissed
  • The state of the application should be clear. If something loads, provide feedback.
  • Do not adapt broken concepts (for example design of desktop apps) just for the sake of consistency. We aim to provide a better interface, so let’s find out how to do that!
  • Regularly reset your installation to see how the first-run experience is like. And improve it.
  • Ideally do usability testing to know how people use the software.
  • For further UX principles, read Alex Faaborg from Mozilla.

PHP

Starting with Nextcloud 19 there is a shared PHP Coding Standards Fixer configuration you can use to automatically format your app’s source code. For full details see the repository on Github.

Always use:

  1. <?php

at the start of your php code. The final closing:

  1. ?>

should not be used at the end of the file due to the possible issue of sending white spaces.

Comments

All API methods need to be marked with PHPDoc markup. An example would be:

  1. <?php
  2. /**
  3. * Description what method does
  4. * @param Controller $controller the controller that will be transformed
  5. * @param API $api an instance of the API class
  6. * @throws APIException if the api is broken
  7. * @since 4.5
  8. * @return string a name of a user
  9. */
  10. public function myMethod(Controller $controller, API $api) {
  11. // ...
  12. }

Objects, functions, arrays & variables

Use UpperCamelCase for Objects, lowerCamelCase for functions and variables. If you set a default function/method parameter, do not use spaces. Do not prepend private class members with underscores.

  1. class MyClass {
  2. }
  3. function myFunction($default=null) {
  4. }
  5. $myVariable = 'blue';
  6. $someArray = array(
  7. 'foo' => 'bar',
  8. 'spam' => 'ham',
  9. );
  10. ?>

Operators

Use \=== and !== instead of \== and !=.

Here’s why:

  1. <?php
  2. var_dump(0 == "a"); // 0 == 0 -> true
  3. var_dump("1" == "01"); // 1 == 1 -> true
  4. var_dump("10" == "1e1"); // 10 == 10 -> true
  5. var_dump(100 == "1e2"); // 100 == 100 -> true
  6. ?>

Control structures

  • Always use { } for one line ifs
  • Split long ifs into multiple lines
  • Always use break in switch statements and prevent a default block with warnings if it shouldn’t be accessed
  1. <?php
  2. // single line if
  3. if ($myVar === 'hi') {
  4. $myVar = 'ho';
  5. } else {
  6. $myVar = 'bye';
  7. }
  8. // long ifs
  9. if ( $something === 'something'
  10. || $condition2
  11. && $condition3
  12. ) {
  13. // your code
  14. }
  15. // for loop
  16. for ($i = 0; $i < 4; $i++) {
  17. // your code
  18. }
  19. switch ($condition) {
  20. case 1:
  21. // action1
  22. break;
  23. case 2:
  24. // action2;
  25. break;
  26. default:
  27. // defaultaction;
  28. break;
  29. }
  30. ?>

Unit tests

Unit tests must always extend the \Test\TestCase class, which takes care of cleaning up the installation after the test.

If a test is run with multiple different values, a data provider must be used. The name of the data provider method must not start with test and must end with Data.

  1. <?php
  2. namespace Test;
  3. class Dummy extends \Test\TestCase {
  4. public function dummyData() {
  5. return array(
  6. array(1, true),
  7. array(2, false),
  8. );
  9. }
  10. /**
  11. * @dataProvider dummyData
  12. */
  13. public function testDummy($input, $expected) {
  14. $this->assertEquals($expected, \Dummy::method($input));
  15. }
  16. }

JavaScript

There is a shared configuration for eslint that you can use to automatically format your Nextcloud apps’s JavaScript code. It consists of two parts: a config package that contains the formatting preferences and a plugin to detect deprecated and removed APIs in your code. See their readmes for instructions.

  • Use a js/main.js or js/app.js where your program is started
  • Use const or let to limit variable to local scope
  • Use JavaScript strict mode
  • Use a global namespace object where you bind publicly used functions and objects to

DO:

  1. // set up namespace for sharing across multiple files
  2. var MyApp = MyApp || {};
  3. (function(window, $, exports, undefined) {
  4. 'use strict';
  5. // if this function or object should be global, attach it to the namespace
  6. exports.myGlobalFunction = function(params) {
  7. return params;
  8. };
  9. })(window, jQuery, MyApp);

DONT (Seriously):

  1. // This does not only make everything global but you're programming
  2. // JavaScript like C functions with namespaces
  3. MyApp = {
  4. myFunction:function(params) {
  5. return params;
  6. },
  7. ...
  8. };

Objects & inheritance

Try to use OOP in your JavaScript to make your code reusable and flexible.

This is how you’d do inheritance in JavaScript:

  1. // create parent object and bind methods to it
  2. var ParentObject = function(name) {
  3. this.name = name;
  4. };
  5. ParentObject.prototype.sayHello = function() {
  6. console.log(this.name);
  7. }
  8. // create childobject, call parents constructor and inherit methods
  9. var ChildObject = function(name, age) {
  10. ParentObject.call(this, name);
  11. this.age = age;
  12. };
  13. ChildObject.prototype = Object.create(ParentObject.prototype);
  14. // overwrite parent method
  15. ChildObject.prototype.sayHello = function() {
  16. // call parent method if you want to
  17. ParentObject.prototype.sayHello.call(this);
  18. console.log('childobject');
  19. };
  20. var child = new ChildObject('toni', 23);
  21. // prints:
  22. // toni
  23. // childobject
  24. child.sayHello();

Objects, functions & variables

Use UpperCamelCase for Objects, lowerCamelCase for functions and variables.

  1. var MyObject = function() {
  2. this.attr = "hi";
  3. };
  4. var myFunction = function() {
  5. return true;
  6. };
  7. var myVariable = 'blue';
  8. var objectLiteral = {
  9. value1: 'somevalue'
  10. };

Operators

Use \=== and !== instead of \== and !=.

Here’s why:

  1. '' == '0' // false
  2. 0 == '' // true
  3. 0 == '0' // true
  4. false == 'false' // false
  5. false == '0' // true
  6. false == undefined // false
  7. false == null // false
  8. null == undefined // true
  9. ' \t\r\n ' == 0 // true

Control structures

  • Always use { } for one line ifs
  • Split long ifs into multiple lines
  • Always use break in switch statements and prevent a default block with warnings if it shouldn’t be accessed

DO:

  1. // single line if
  2. if (myVar === 'hi') {
  3. myVar = 'ho';
  4. } else {
  5. myVar = 'bye';
  6. }
  7. // long ifs
  8. if ( something === 'something'
  9. || condition2
  10. && condition3
  11. ) {
  12. // your code
  13. }
  14. // for loop
  15. for (var i = 0; i < 4; i++) {
  16. // your code
  17. }
  18. // switch
  19. switch (value) {
  20. case 'hi':
  21. // yourcode
  22. break;
  23. default:
  24. console.warn('Entered undefined default block in switch');
  25. break;
  26. }

CSS

Take a look at the Writing Tactical CSS & HTML video on YouTube.

Don’t bind your CSS too much to your HTML structure and try to avoid IDs. Also try to make your CSS reusable by grouping common attributes into classes.

DO:

  1. .list {
  2. list-style-type: none;
  3. }
  4. .list > .list_item {
  5. display: inline-block;
  6. }
  7. .important_list_item {
  8. color: red;
  9. }

DON’T:

  1. #content .myHeader ul {
  2. list-style-type: none;
  3. }
  4. #content .myHeader ul li.list_item {
  5. color: red;
  6. display: inline-block;
  7. }

TBD