Skip to content

Commit

Permalink
Add best practices doc page
Browse files Browse the repository at this point in the history
  • Loading branch information
bocekm committed Dec 5, 2018
1 parent cdcc6ac commit 162e5f3
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions docs/best-practices.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Best practices for writing actors


### Follow the Python Coding Guidelines

See the [Python Coding Guidelines](CONTRIBUTING.md#coding-guidelines).

### Avoid running the code on a module level

Actors are completely written in Python, however, they are loaded beforehand to get all the meta-information from the actors.
To avoid slow downs we ask actor writers not to execute any code that is not absolutely necessary on a module level.

### Avoid global non Leapp imports

Avoid importing system or bundled libraries on a module level. This again has to do with slow down, and also to avoid
unnecessary complications for our tests automation which needs to inspect the actors beforehand.

### Use the snactor tool

Using the snactor tool provides you with all minimally necessary layouts of the repository, and creates
things like topics and tags.

It helps you with debugging, and it is able to execute individual actors. The snactor tool has also the ability to
discover all the entities in your current project repository, such as actors, models, tags, topics, and workflows.

### Avoid writing generic actors

Generic actors that are too abstract are usually a sign for code that should be part of the
[Leapp Standard Library](https://github.com/oamg/leapp/tree/master/leapp/libraries/stdlib/) instead. Standard libraries
help to reduce the complexity of the system, and the amount of actors that have to be scheduled and run.

### Allow for unit testing
Write all the actor’s logic in the actor’s library in order to be able to write unit tests for your functions separately.
It is not currently possible to unit test any method or function in the actor.py. The only line in the actor.process()
method then should be ideally just calling an entry point to the library. To create actor’s library, create new folder
libraries in the actor’s folder and there create an arbitrarily named python file, e.g. library.py.

myactor/actor.py:
```
from leapp.libraries.actor.library import do_the_actor_thingy
class MyActor(Actor):
...
def process(self):
do_the_actor_thingy(self)
```

myactor/libraries/library.py:
```
def do_the_actor_thingy(actor):
actor.log.debug("All the actor’s logic shall be outside actor.py")
```

0 comments on commit 162e5f3

Please sign in to comment.