Clean Code: Stop wasting my time

31
CLEAN CODE: STOP WASTEING MY TIME

description

Let us spend less time writing trivial stuff and more time focusing on great code that doesn't need documentation to be understood. Save other peoples time by writing less !

Transcript of Clean Code: Stop wasting my time

Page 1: Clean Code: Stop wasting my time

CLEAN CODE: STOP WASTEING MY

TIME

Page 2: Clean Code: Stop wasting my time

About me

Volker Dusch / @__edorian

Doing PHP for ~7 Years now

I‘m sorry for the bullet points, i don‘t know any better.

(There will be code, and cake, maybe)

Feel free to interrupt me at any time !

Page 3: Clean Code: Stop wasting my time

About you

You seem to be smarty people

I guess you are motivated

And I'm just going to assume you work with smart people you respect

Page 4: Clean Code: Stop wasting my time

This is about time

About your time

About my time

About the next guys time

Page 5: Clean Code: Stop wasting my time

So stop wasting time

Take more time writing code, you are only doing it once

Make everyone waste less time reading code, that happens quite a lot

Disclaimer: When saying ‘documentation‘ i never talk about @phpdoc for apis (@param, @return)

Page 6: Clean Code: Stop wasting my time

Oh yeah by that way

I won’t be talking about ’design’ very much

Bad design hurts… but you’ll get over it … or at least used to it

Bad code hurts every day because it sucks differently every time

Page 7: Clean Code: Stop wasting my time

Samples are next

Stuff I’d say you all know about:SCM

Unit testing

Dependency Injection

That Singletons / Statics / Globals are evil

Other design mumbo jumbo

Page 8: Clean Code: Stop wasting my time

Yay ! Samples

class myClass {

/**

* Constructor

*/

public function __construct() {

}

// methods...

}

Page 9: Clean Code: Stop wasting my time

Yay ! Samples

class myClass {

/**

* Create an instance of ‘myClass’

*/

public function __construct() {

}

// methods...

}

Page 10: Clean Code: Stop wasting my time

Yay ! Samples

class myClass {

/**

* @return myClass

*/

public function __construct() {

}

// methods...

}

Page 11: Clean Code: Stop wasting my time

Yay ! Samples

class myClass {

public function __construct() {

}

// methods...

}

Page 12: Clean Code: Stop wasting my time

Yay ! Samples

class myClass {

// methods...

}

Page 13: Clean Code: Stop wasting my time

So…

But everything has to have a docblock ! It‘s in the guidelines !

But i might need it later !

That's just because it‘s in the template and i didn‘t delete it

Page 14: Clean Code: Stop wasting my time

DOCUMENT EVERYTHING !!1

That's at least what they told me

Provocation incoming:

Good code is hard document

Bad code is easy to document

Page 15: Clean Code: Stop wasting my time

‘Bad‘ Code

class user {

public function getId() {…}

public function getName() {…}

/** Calculate Body-Mass-Index @link … */

public function getBMI() {…}

/** @param float $kg Weight in Kilogramm */

public function setWeight($weightInKg) {…}

Page 16: Clean Code: Stop wasting my time

‘Good‘ Code

class user {

public function getUserId() {…}

public function getFirst/Last/DisplayName() {…}

/** @link … */

public function getBodyMassIndex() {…}

/** @param float $kilogramm */

public function setWeight($kilogramm) {…}

Page 17: Clean Code: Stop wasting my time

Again

A short, undescriptive, function name like ‘calc‘ always make it easy to write documentation

Sadly people will need to read it (again and again ..)

Page 18: Clean Code: Stop wasting my time

Another little example

class foo {

/**

* Setter for property x

* @param string $x

*/

public function setX($x) {

$this->x = $x;

}

}

Why ?

• Generating API Docs ?

• Consistency ?• Improved

readability through uniformity ?

Page 19: Clean Code: Stop wasting my time

But I NEEEED that

We are programmers, trivial stuff is for the computer

If you need trivial stuff for E_UGLY_REQUIRENMENT_X than automate it

Spent time once putting that in your build system

Page 20: Clean Code: Stop wasting my time

This is PHP_CodeSniffer valid Well… after our Doc Prepare

<?php

class myClass {

} Yes that breaks your IDEs sniffs

Page 21: Clean Code: Stop wasting my time

Output<?php

/**

* @package module_x

* @subpackage y_z

*/

/**

* @package module_x

* @subpackage y_z

* @since creation_date

* @version 1.2.3.4

*/

class myClass { }

Page 22: Clean Code: Stop wasting my time

Inline Comments

$i++ // Increment $i by oneYeah.. we don‘t need to talk about that

Can be great, especially when they tell you ‘WHY‘ something was done

Most time aren‘t that great

Page 23: Clean Code: Stop wasting my time

Inline Sample

public function generateReport() {

// get the db connection

$reg = GlobalStorage::get(“db“);

// auth

if(!$reg->getOne(“SELECT VIEW_REPORT FROM USER ….“)) {…}

$id = $reg->getOne(“select … „); // template

// render

new ReportTemplate($id); // ….

Page 24: Clean Code: Stop wasting my time

Inline Sample

public function generateReport() {

$this->checkAuthentication();

$template = this->createReportTemplate();

$this->renderReport($template);

}

That's not perfect but the ‘// next block‘ comments are gone

Page 25: Clean Code: Stop wasting my time

No docs are not the answer

I‘m not saying BE GONE all documentation

Let‘s remove useless comments !

Let‘s (maybe ?) agree upon that sometimes there is no USEFULL comment.

Know who you write docs for

Page 26: Clean Code: Stop wasting my time

It‘s not ONLY about the code Commit messages matter !

Commit message are like book covers, they raise expectations. The diff should tell a matching story

Don’t repeat the obvious, tell why you did it and then show me how in the diff

Page 27: Clean Code: Stop wasting my time

Commits

Yes, this highly depends on your team

Fixes #5232 Fixes #4523 with the last release the

database structure changed Reworked the Authentication to account

for the new SingleSignOn Fixed some problems Tidy | phpdoc | cleanup | etc.

Page 28: Clean Code: Stop wasting my time

One Commit – One Purpose Stuff you can do:

Fix a bugImplement a featureRefractor somethingTidy up some code

Stuff I’d like you not to do:More than one of the above at once

Page 29: Clean Code: Stop wasting my time

To sum up

Don’t write documentation you think has no use

See if you can substitute documentation with more descriptive naming

Always: Do what your team has agreed upon and if you don’t like it try to change it if there is a benefit others see too.

Page 30: Clean Code: Stop wasting my time

ANY MORE QUESTIONS OR

COMMENTS ?

Page 31: Clean Code: Stop wasting my time

THANK YOU FOR YOUR TIME