No more dead kittens - Clean Code
-
Upload
yves-hoppe -
Category
Software
-
view
204 -
download
1
Transcript of No more dead kittens - Clean Code
No more dead kittens@yveshoppe
DisclaimerThe following presentation contains violent scenes and maybe not suitable for children and kitten lovers.
The issue
During time the technical debt in projects is rising and preventing progress and new features.
Start 2nd 3rd End
Why?
No RefactoringShortcutsHacksCopy & PasteLack of time// … tbc
The result:
How do you realize technical debt?
Something easy gets painful
The fun is missing
At the beginning 10 minutes, now 10 hours
5 Samples
5 Sins
5 Dead Kittens
Thank you Guido for your contribution!
Code-Style and Readability
More samples? Be prepared, it’s worse than these samples!
https://github.com/JoomTools/plg_content_jtcsv2html/blob/master/jtcsv2html.php
Rating
Readability
https://github.com/JoomTools
Communication is the key
Consistency
Clear rules for developers
Mistakes are easier noticed
Thank you Guido for your contribution!
Testability
protected function _validateField($field){
$index = $this->uParams['index'];$data = $this->form[$this->uParams['theme'] . $index]->getData()->toArray();$rule = false;$value = '';$showon = (string) $fie
if ($showon){
$_showon_value = explode(':', $showon);$_showon_value[1] = JText::_($_showon_value[1]);$showon_value = $this->form[$this->uParams['theme'] . $index]->getField($_showon_value[0])->value;if ($_showon_value[1] != $showon_value){
$showField = false;$valid = true;$this->form[$this->uParams['theme'] . $index]->setValue($name, null, '');if ($type == 'spacer'){
$this->form[$this->uParams['theme'] . $index]->setFieldAttribute($name, 'label', '');}
}}if (isset($data[$name])){
$value = $data[$name];}if ($required && !$value){
if (!$showField){
$validateField = false;}
}if ($validateField && $showField){
if ($type == 'file'){
$jinput = JFactory::getApplication()->input;$submitFiles = $jinput->files->get($this->uParams['theme'] . $index);$issetFiles = false;if (!empty($submitFiles[$name][0]['name'])){
$issetFiles = true;$files = $submitFiles[$name];
}elseif (!empty($submitFiles[$name]['name'])){
$issetFiles = true;$files = array($submitFiles[$name]);
}if ($issetFiles){
$value = $files;$this->submitFiles[$name] = $files;$this->fileFields[] = $name;
}}if ($field->option){
$oCount = count($field->option);for ($i = 0; $i < $oCount; $i++){
$_val = (string) $field->option[$i]->attributes()->value;if ($_val){
if (is_array($value)){
$val = in_array(JText::_($_val), $value) ? JText::_($_val) : $_val;}else{
$val = $value == JText::_($_val) ? $value : $_val;}$field->option[$i]->attributes()->value = $val;
}}
}if ($type == 'email'){
$field->addAttribute('tld', 'tld');}if ($validate){
$rule = JFormHelper::loadRuleType($validate);}else{
$rule = JFormHelper::loadRuleType($type);}if ($rule && !empty($value)){
if ($type == 'captcha'){
$valid = $rule->test($field, $value, null, null, $this->form[$this->uParams['theme'] . $index]);if ($valid !== true){
$this->validCaptcha = $valid;$this->issetCaptcha = $name;$valid = false;
}}else{
$valid = $rule->test($field, $value);}
}else{
$valid = $validateField;}
}if (!$valid && $type != 'captcha'){
$this->_invalidField($name);}
Rating
Testability
https://github.com/JoomTools
Avoid methods which are longer then the screen (30-40 lines ++)
Complexity is rising exponentional
Not testable
Not reusable
Thank you Anonymous for your contribution!
Magic Numbers
$allpicsrandom = $params->get('allpicsrandom'); $allpics = $params->get('allpics'); $this->setImageProperties($images, $links_info); if($allpicsrandom == 2 AND $allpics == 1) { $image = $images[0]; unset($images[0]); } else //….
What the f*** is 2?
Rating
Magic Numbers
https://github.com/...
Code should explain itself
and not hide between numbers
It’s magic! or 42
const DESCRIPTION = 2
Thank you Guido for contribution!
Commented and unused Code
<?php endif;$countFieldsets++;if ($countFieldsets % 2 || $countFieldsets >
$sumFieldsets) : ?><!--</div>-->
<?php endif;endforeach;if ($submitSet === false) : ?>
?!
Rating
Unused / commented Code
https://github.com/...
Makes reading hard
Bloats up software
For that we have VCS (like Git)!
phpmd helps you finding unused code!
Thank you Anonymous for your contribution!
Copy & Paste Code
$a = 23;if ($a == 42)
for ($i = 0; $i < $a; $i++)doSomething;
elseif (isArray($a))for each ($a as $b)
doSame; else
// .. doSame
Rating
Copy & Paste
https://github.com/...
A kitten dies!
Changes need to happen on multiple places
Error-proneness!
You don’t think! Coding is thinking!
phpcpd
The solution
Clean codeIt is not enoughfor the code to work(Robert C. Martin)
Avoiding technical debt
RefactoringContinuous refactoring and reviewing.Maintain your code base!
Tests and TDD(Automated) Tests help us writing better and reusable code
PhilosophyIt’s not about code, it’s about thinking and honouring your own work!
Clear dependencies
Why?
Dependencies have to be clearly defined (Testability)
Dependencies should not be created in methods, but in the constructor. So they can easily exchanged against Mocks and Stubs
Single Responsibility
Test Driven development / BDD
TDD / BDDTDD / BDD help you thinking more on
your application and your code!
Business experience first
Gherkin
Clear definition of Requirements
Automated Testing
Putting teststogether to a bigautomatic safety net!
Why automated testing?
Repeatable and scalable
Detect issues for every pull request
Saves time and helps you trusting your own code
Multiple PHP Versions, Browsers
Software used
1000 things more
KISSDRYCCD WerteFurpsSMART// …
Call for volunteers!
Joomla! is looking for new members, which want to make Joomla! Better!
Joomla! Is more than just an CMS, it’s the love, the spirit 1000s of people have put into it.
Questions?Thank you for listening!
@yveshoppe