# CodeRefinery Trondheim, October 22 - 24
link: https://bit.ly/coderefinery-ntnu
## Modular Code Development
link: https://bit.ly/coderefinery-ntnu
Split in groups after language. Discuss:
* What best practices can you recommend to arrive at well structured, modular code in your favourite programming language?
* What would you recommend your colleague who starts in the same programming language?
* How do you deal with code complexity in your projects
### Python
Discussion on object oriented or functions oriented:
- learn object oriented code in python for project, the structure can more easely be structured
- function oriented code is easier to learn, best place to start
- data is object, can make the code easier
- on larger projects, have the most experient people write the core and the other people write the outside
Other best practises:
- give functions sensible name, readable for humans
- write get variables functions, easier for more complex variables
- keep the processing part and visual part of the code separated
- Make a structure on where to save and document, also train new people
- Train people on how to use the code
- Make examples and a web page for larger projects
- For smaller single person project, make parts of the code (the central part) in a good and documented way
- How to go over your code again?
- start a new file and take over only the functions you use to get an overview over what you use
- make new functions instead of many similar functions
- map interdependence between functions?
- make a module tree
- have staight lines of interdepencense
- make a diagram of your code
- When to start with documentation
- start when you start a project
- at the leatest with a code revision
- comment in the code
- follow PEP8 and actuallt do wirte what the functions do
- Complexity
- keep functions short
### C/++
- C++:
- Define Classs (well structured)
- Use sysmatic name rule for variable, class...
- Use Macros and Template, but with detailed documentation
- Sufficient comments in the code (deal with code complexity)
- Be consistent in writing style (deal with code complexity)
- Wiki page for the code (deal with code complexity)
- Use doxygen for generating documentation
- - C:
-Organize HW peripherals with moduls(.c and .h -files)
-Organize similar functionality toghether in modules
-Macros to abstact away HW-architecture.(Documentation important to ensure similar rules.)
-Clearly described and commented interfaces.
-Clearly described functionality of funcitons and modules.
### Java
- Separate API from the implementation
- Interfaces
- Consitent naming
- Well documented
- Libraries
- Dependency injection
- High emphasis on Design Patterns
- OSGi is all about modularity
- Support for switching the implementation at runtime
- Encapsulation
- Design then implement
- Reduce side effects
---
* What would you recommend your colleague who starts in the same programming language?
- Good understanding of Object Oriented Programing (OOP) concepts
## Test Discussion
link http://bit.ly/coderefinery-ntnu
### Group 1
- tests for NaN for functions that accepts number
- factorial(n)
- 1.5 throws an Error
- -2 throws an Error
- count_word_occurence_in_string(text, word)
- "dog" in "dog,cat" should succeed
- "dog" in "dogs" should fail
- count_word_occurence_in_file(file_name, word)
- check non-existing file
- check normal scenario
- check_reactor_temperature(temperature_celsius)
- absurd high value should return negative status
- Pet
- creat a new object
- assert object has hunger of 0
- go for a walk
- check that hunger of object is 1
- go for a walk n times
- check that hunger of object is n+1
### Group 2
- Define function boundary conditions
### Group 3
- Decide spesifications - make tests accordingly
- factorial: ok not to test for complex numbers. Otherwise test for for small number, big number and negative number (how should the function react to a fraction?)
- count_word_occurence_in_string: A case with zero, one and many target words, unicode, check for different seperators between words
- count_word_occurence_in_file: Check files with different encodings, if the file is a text file, specify in documentation what kind of files are accepted and the like before a case with zero, one and many target words