Good practices for Developers
-
Upload
piotr-miazga -
Category
Software
-
view
106 -
download
0
Transcript of Good practices for Developers
How to rest assured
couple of good practices
by @polishdeveloper
About me
@PolishDeveloper
Almost 10 years
CleanCode advocate
Racing is my love
When no one is looking I play Minecraft
Twitter:polishdeveloper
Skype:polishdeveloper
GitHub:polishdeveloper
M@ail: [email protected]
Agenda
about me
why
be lazy
unit testing
don’t trust
use tools
estimations
Q\A
Why
Helping each other
It’s good to give
Giving a lecture makes you a better person,
really!
Be Lazy
"if something — anything — requires more than 90 seconds of his
time, he writes a script to automate that.”
- late at work
- automatic customer email
- send email to wife
- make coffee
http://www.businessinsider.com/programmer-automates-his-job-2015-11
Be Lazy
• If something requires more than one command or command has more than two params, write a script.
• If you do something often, automate it
• If you can tell computer to do something for you, do it (Hooks)
• Don’t ask me to think - consistent dictionary across team
• Don’t ask me to think 2 - charts and logs
• Create WIKI
Be lazy - npm scripts
"scripts": {
"postinstall": "./node_modules/.bin/typings install",
"build": "cross-env NODE_ENV=production webpack --config webpack/webpack-
production.config.js --progress --profile --colors",
"build:dev": "node webpack/dev-server.js",
"start": "concurrently \"npm run build\" \"node server/production\"",
"start:dev": "concurrently \"npm run build:dev\" \"node server/development\"",
"test": "tsc && mocha --require test-setup --recursive ./dist/**/__spec__/**/*-spec.js"
},
Be lazy - git hook
#!/usr/bin/env ruby
message_file = ARGV[0]
message = File.read(message_file)
$regex = /\[ref: (\d+)\]/
if !$regex.match(message)
puts "[POLICY] Your message is not formatted correctly"
exit 1
end
Be lazy but not too lazy
“There are two sides to every story”
JS won’t keep you lazy
console.log(1 + 1); // 2
console.log(1 + '1'); // '11'
console.log('1' + 1); // '11'
console.log('1' + '1'); // '11'
JS won’t keep you lazy
console.log(1 + 1); // 2
console.log(1 + '1'); // '11'
console.log('1' + 1); // '11'
console.log('1' + '1'); // '11'
console.log(1 - 1); // 0
console.log(1 - '1'); // 0
console.log('1' - 1); // 0
console.log('1' - '1'); // 0
JS won’t keep you lazy
console.log(1 + 1); // 2
console.log(1 + '1'); // '11'
console.log('1' + 1); // '11'
console.log('1' + '1'); // '11'
console.log(1 - 1); // 0
console.log(1 - '1'); // 0
console.log('1' - 1); // 0
console.log('1' - '1'); // 0
console.log('2' - '1' + '1'); // ??
console.log('2' - ('1' + '1')); // ??
JS won’t keep you lazy
console.log(1 + 1); // 2
console.log(1 + '1'); // '11'
console.log('1' + 1); // '11'
console.log('1' + '1'); // '11'
console.log(1 - 1); // 0
console.log(1 - '1'); // 0
console.log('1' - 1); // 0
console.log('1' - '1'); // 0
console.log('2' - '1' + '1'); // 11
console.log('2' - ('1' + '1')); // -9
JS is amazing
var x = {
empty1: '',
empty2: 0,
empty3: null,
empty4: undefined
};
console.debug(x.empty1 != false); // false
console.debug(x.empty2 != false); // false
console.debug(x.empty3 != false); // true
console.debug(x.empty4 != false); // true
console.debug(x.empty5 != false); // true
console.debug(x.hasOwnProperty('empty4')
&& x.empty4 != false);
Don’t trust - defensive programming
- Verify input
- Do not modify input
- Encapsulation everywhere
- Use const
It’s easy to make a typo
export class APIConnector {
public static OK = 'ok';
public static isSuccess(status:string) {
return status === APIConnector.OK;
}
}
Code should look like poem
export class Demo {
public static HAS_MORE_ITEMS = true;
public getMore(response:IArticlesResponse) {
return response.more == Demo.HAS_MORE_ITEMS
? this.fetchMore(response)
: [];
}
}
Unit test
const assert = require(‘chai').assert;
describe('Array', function() {
describe('#indexOf()', function() {
it('should return -1 when the value is not present’,
function() {
assert.equal(-1, [1,2,3].indexOf(5));
assert.equal(-1, [1,2,3].indexOf(0));
}
);
});
});
Don’t trust
if (_.get(x, 'schools.clasess[0].notebook', false) == false) {
// do something
}
Lazy people don’t trust(plus lazy people don’t want to think)
var hasNotebook = function(student) {
//assert that is proper student and has notebook
if (!_.has(student,“schools”) {
throw new Error("Schools property not found");
}
if (_.isEmpty(student.schools.classes)) {
throw new Error("User is not signed up to any class");
}
return _.first(student.schools.classes).notebook == false;
};
if (hasNotebook(student)) {
// do something
}
Keep things consistent
- Same dictionary
- Locked versions
- Docker/Vagrant as development container
Thing to remember
First write for humans,
then write for computers.
Tools
Tools - console has more to offer
console.log('test');
console.group('There are more');
console.debug('test');
console.info('test');
console.warn('test');
console.error('test');
console.groupEnd();
console.table([1,2,3]);
console.count();
console.trace('method');
Tools - console
Makefile is amazing
# development tasks, should be executed in your local env:
$ make run # runs the API via Docker Compose (for local dev)
$ make stop # stops the running containers
$ make image # builds a distribution Docker images
$ make push # pushes the Docker image to Docker Hub
$ make shell # Opens Docker container shell
# other tasks, should be executed in context of API container (see $ make
shell)
$ make install # installs all dependencies and prepares for development
$ make update # updates all dependencies and prepares for development
$ make migrate # migrates database to match the current mappings
$ make fixtures # loads fixtures into the database
$ make qa # runs QA tasks
$ make report # runs tests and generates code coverage report
3rd party APIs - MixPanel
<script type=“text/javascript">
mixpanel.track("Video played", {
"Video length": 213,
"id": "hY7gQr0"
});
</script>
3rd party APIs - TrackJS
<script type="text/javascript">
window._trackJs = { token: 'YOUR_TOKEN_HERE' };
</script>
<script
src=“//cdn.trackjs.com/releases/current/tracker.js">
</script>
Charts - why ?
Successful logins
Charts - why ?
Successful logins
Estimations
Create a WIKI
Q/A
Questions ???
Hugs time
Thank you!
\o/
Twitter:polishdeveloper
Skype:polishdeveloper
GitHub:polishdeveloper