SlideShare a Scribd company logo
7 GOLDEN RULES OF
SCRUMT CODE
Simple, Clean, Readable, Understanable, Maintainable
(Testable) Code
Slides by , presented
by Sumit Chhetri.
Geshan Manandhar
WHY WRITE CLEAN CODE
Code for humans not machines
Think that the next person who reads your code is a chainsaw
maniac
If you don't write clean code, you know your fate.
Rule 1: Follow a consistent coding
standard
If you are doing PHP follow
Other languages like Java, Python, Javascript also have
coding standards
Use a linter or CS fixer to follow the standard. Integrate
build tools in Sublime or use default PHPStrom code
refactor, to automate this
Makes it easy for everyone - including yourself - in the
team, to read the code
PSR-2
Rule 2: Name things properly, long
variable and function/method names
are allowed
Stick to one convention. ($someVariable or
$SomeVariable or $some_variable)
Don't use abbreviations, may not be understandable
to the person, who reads your code
Naming is difficult, do it well. Name classes, variables
and methods that make sense
Do not be redundant.
Example
Rule 3: Be expressive, write code as you
speak and be optimally verbose
Focus on API rather than patterns.
First, write down the API for perfect scenario, observe
how it feels, then jump to coding and make it work.
Tell, don't ask.
Example
Rule 4: Max indent per method should
be 2, in case of exceptions 3
Avoid the use of else.
Extract the logic to other readable method.
Return early
Throw exception
Example
6 levels of indentation (in our public code) source
public function settingsAction()
{
//0
if ($_POST) {
//1
try {
//2
...
} else {
...
//3
if ($reportingOrgNew != $reportingOrgOld) {
//4
if ($save == "ok") {
...
//5
if ($activityPublish) {
//6
Rule 5: Avoid creating   and
long methods
god object
One class should do one thing not everything like
StatusChangeror StatusManagernot StatusGod
Avoid using andin method names like
validateAndSave, one method needs to do one
thing and one thing well
Keep methods small, a 50 line method is a problem
Keep classes small, a 1000 line class is a pain
Keep the instance variables to as low as 6 or 7.
Rule 6: Keep the method in one place,
inject the class and call it, DRY
In MVC keep controllers slim, business logic belongs to
services or repositories, check
for controller 5-10-20
If you access property of a property a->b->c->d() there
is something wrong, you can use a wrapper or proxy
function.
Use language constructs like interfaces, traits to make
code more expressive and reusable
If you find yourself copying the same code several
times, extract that code into its own method.
Refactor your code, every once in a while.
symfony best practice
Example
Rule 7: Avoid in-line comments
(comment with code), put comments in
the method doc
Comment is a code smell (anti pattern), like
If you need to write comments to explain your code,
means you need to put it in a new method.
here
Example
public function addActivity($data , $default)
{
$this->defaults = $default;
$identity = Zend_Auth::getInstance()->getIdentity();
//var_dump($data);exit;
$model = $this->model;
$modelActivity = new Model_Activity();
$activitiesId = $model->getIdByField('iati_activities', 'account_id', $ident
//Create activity and its defaults
$iatiIdentifier['activity_identifier'] = $data['identifier'];
Other considerations
Avoid working with just arrays for large data sets, use
class and type hint
Rather than starting to write something on your own
spend 5 minutes to read the framework/library
documentation to know if its already provided
Extract out methods and rename variables as
necessary
Read about cyclomatic complexity and N-Path
complexity, here
Recap 1
Rule 1: Follow a consistent coding standard
Rule 2: Name things properly, long variable and
function names are allowed
Rule 3: Be expressive, write code as you speak and be
optimally verbose
Rule 4: Max indent per method should be 2, in case of
exceptions 3
Recap 2
Rule 5: Avoid creating and long methods
Rule 6: Keep the method in one place, inject the class
and call it,
Rule 7: Avoid in-line comments (comment with code),
put comments in the method doc
Dont forget the consideration and reading
god object
DRY
PS: you can print the recap slides and mug them up.
References
Most code examples are taken from YIPL open source
code on Github.
Rules for simpler code - laracast
Maintainable Code
Readable Code
Object Calisthenics
Your code sucks - let fix it
Thanks
Hope we can step by step minimize code complexity and
write SCRUMT code.
Remember, simple, understandable, readable and eventually
maintainable code is greater than any pattern ever invented
(be practical).

More Related Content

What's hot

Clean Code I - Best Practices
Clean Code I - Best PracticesClean Code I - Best Practices
Clean Code I - Best Practices
Theo Jungeblut
 

What's hot (20)

Clean code slide
Clean code slideClean code slide
Clean code slide
 
Coding standard
Coding standardCoding standard
Coding standard
 
Refactoring Tips by Martin Fowler
Refactoring Tips by Martin FowlerRefactoring Tips by Martin Fowler
Refactoring Tips by Martin Fowler
 
Code smells and remedies
Code smells and remediesCode smells and remedies
Code smells and remedies
 
Refactoring
RefactoringRefactoring
Refactoring
 
Unit Testing with Python
Unit Testing with PythonUnit Testing with Python
Unit Testing with Python
 
Clean Code
Clean CodeClean Code
Clean Code
 
Coding Best Practices
Coding Best PracticesCoding Best Practices
Coding Best Practices
 
Code Refactoring
Code RefactoringCode Refactoring
Code Refactoring
 
Refactoring 101
Refactoring 101Refactoring 101
Refactoring 101
 
Clean code and Code Smells
Clean code and Code SmellsClean code and Code Smells
Clean code and Code Smells
 
Why TypeScript?
Why TypeScript?Why TypeScript?
Why TypeScript?
 
C# conventions & good practices
C# conventions & good practicesC# conventions & good practices
C# conventions & good practices
 
Clean Code Principles
Clean Code PrinciplesClean Code Principles
Clean Code Principles
 
Clean Code
Clean CodeClean Code
Clean Code
 
Kotlin
KotlinKotlin
Kotlin
 
Typescript ppt
Typescript pptTypescript ppt
Typescript ppt
 
Coding standards and guidelines
Coding standards and guidelinesCoding standards and guidelines
Coding standards and guidelines
 
Clean Code I - Best Practices
Clean Code I - Best PracticesClean Code I - Best Practices
Clean Code I - Best Practices
 
TypeScript - An Introduction
TypeScript - An IntroductionTypeScript - An Introduction
TypeScript - An Introduction
 

Viewers also liked

Clean Code III - Software Craftsmanship
Clean Code III - Software CraftsmanshipClean Code III - Software Craftsmanship
Clean Code III - Software Craftsmanship
Theo Jungeblut
 
Clean Code II - Dependency Injection
Clean Code II - Dependency InjectionClean Code II - Dependency Injection
Clean Code II - Dependency Injection
Theo Jungeblut
 

Viewers also liked (7)

Clean Code III - Software Craftsmanship
Clean Code III - Software CraftsmanshipClean Code III - Software Craftsmanship
Clean Code III - Software Craftsmanship
 
Embrace chatOps, stop installing deployment software
Embrace chatOps, stop installing deployment softwareEmbrace chatOps, stop installing deployment software
Embrace chatOps, stop installing deployment software
 
Do You Git Your Code? Follow Simplified Gitflow Branching Model to Improve Pr...
Do You Git Your Code? Follow Simplified Gitflow Branching Model to Improve Pr...Do You Git Your Code? Follow Simplified Gitflow Branching Model to Improve Pr...
Do You Git Your Code? Follow Simplified Gitflow Branching Model to Improve Pr...
 
Things i wished i knew as a junior developer
Things i wished i knew as a junior developerThings i wished i knew as a junior developer
Things i wished i knew as a junior developer
 
Embrace chatops, stop installing deployment software - Laracon EU 2016
Embrace chatops, stop installing deployment software - Laracon EU 2016Embrace chatops, stop installing deployment software - Laracon EU 2016
Embrace chatops, stop installing deployment software - Laracon EU 2016
 
Clean Code II - Dependency Injection
Clean Code II - Dependency InjectionClean Code II - Dependency Injection
Clean Code II - Dependency Injection
 
Clean Code - Design Patterns and Best Practices at Silicon Valley Code Camp
Clean Code - Design Patterns and Best Practices at Silicon Valley Code CampClean Code - Design Patterns and Best Practices at Silicon Valley Code Camp
Clean Code - Design Patterns and Best Practices at Silicon Valley Code Camp
 

Similar to 7 rules of simple and maintainable code

Writing Good Code
Writing Good CodeWriting Good Code
Writing Good Code
Leo Liang
 
Save time by applying clean code principles
Save time by applying clean code principlesSave time by applying clean code principles
Save time by applying clean code principles
Edorian
 
Best practices in enterprise applications
Best practices in enterprise applicationsBest practices in enterprise applications
Best practices in enterprise applications
Chandra Sekhar Saripaka
 
Code Review Looking for a vulnerable code. Vlad Savitsky.
Code Review Looking for a vulnerable code. Vlad Savitsky.Code Review Looking for a vulnerable code. Vlad Savitsky.
Code Review Looking for a vulnerable code. Vlad Savitsky.
DrupalCampDN
 

Similar to 7 rules of simple and maintainable code (20)

In-Depth Guide On WordPress Coding Standards For PHP & HTML
In-Depth Guide On WordPress Coding Standards For PHP & HTMLIn-Depth Guide On WordPress Coding Standards For PHP & HTML
In-Depth Guide On WordPress Coding Standards For PHP & HTML
 
Clean code
Clean codeClean code
Clean code
 
[DevDay2018] Let’s all get along. Clean Code please! - By: Christophe K. Ngo,...
[DevDay2018] Let’s all get along. Clean Code please! - By: Christophe K. Ngo,...[DevDay2018] Let’s all get along. Clean Code please! - By: Christophe K. Ngo,...
[DevDay2018] Let’s all get along. Clean Code please! - By: Christophe K. Ngo,...
 
Writing Good Code
Writing Good CodeWriting Good Code
Writing Good Code
 
Writing Readable Code
Writing Readable CodeWriting Readable Code
Writing Readable Code
 
Raya code quality guidelines - enhancing readability
Raya code quality guidelines - enhancing readabilityRaya code quality guidelines - enhancing readability
Raya code quality guidelines - enhancing readability
 
Save time by applying clean code principles
Save time by applying clean code principlesSave time by applying clean code principles
Save time by applying clean code principles
 
AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLE
AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLEAN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLE
AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLE
 
AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLE - CFObjective() 2017
AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLE - CFObjective() 2017AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLE - CFObjective() 2017
AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLE - CFObjective() 2017
 
Back-2-Basics: .NET Coding Standards For The Real World
Back-2-Basics: .NET Coding Standards For The Real WorldBack-2-Basics: .NET Coding Standards For The Real World
Back-2-Basics: .NET Coding Standards For The Real World
 
Back-2-Basics: .NET Coding Standards For The Real World
Back-2-Basics: .NET Coding Standards For The Real WorldBack-2-Basics: .NET Coding Standards For The Real World
Back-2-Basics: .NET Coding Standards For The Real World
 
Writing High Quality Code in C#
Writing High Quality Code in C#Writing High Quality Code in C#
Writing High Quality Code in C#
 
Clean Code V2
Clean Code V2Clean Code V2
Clean Code V2
 
Best practices in enterprise applications
Best practices in enterprise applicationsBest practices in enterprise applications
Best practices in enterprise applications
 
Code Review Looking for a vulnerable code. Vlad Savitsky.
Code Review Looking for a vulnerable code. Vlad Savitsky.Code Review Looking for a vulnerable code. Vlad Savitsky.
Code Review Looking for a vulnerable code. Vlad Savitsky.
 
Designing Modules in Python
Designing Modules in PythonDesigning Modules in Python
Designing Modules in Python
 
Php Best Practices
Php Best PracticesPhp Best Practices
Php Best Practices
 
Php Best Practices
Php Best PracticesPhp Best Practices
Php Best Practices
 
Good Coding Practices with JavaScript
Good Coding Practices with JavaScriptGood Coding Practices with JavaScript
Good Coding Practices with JavaScript
 
Tips to Comment the Code
Tips to Comment the CodeTips to Comment the Code
Tips to Comment the Code
 

More from Geshan Manandhar

Drupal 7 basic setup and contrib modules for a brochure website
Drupal 7 basic setup and contrib modules for a brochure websiteDrupal 7 basic setup and contrib modules for a brochure website
Drupal 7 basic setup and contrib modules for a brochure website
Geshan Manandhar
 
Drupal 7 install with modules and themes
Drupal 7 install with modules and themesDrupal 7 install with modules and themes
Drupal 7 install with modules and themes
Geshan Manandhar
 
Business Rules Logical Experssion and SBVR
Business Rules Logical Experssion and SBVRBusiness Rules Logical Experssion and SBVR
Business Rules Logical Experssion and SBVR
Geshan Manandhar
 

More from Geshan Manandhar (20)

Are logs a software engineer’s best friend? Yes -- follow these best practices
Are logs a software engineer’s best friend? Yes -- follow these best practicesAre logs a software engineer’s best friend? Yes -- follow these best practices
Are logs a software engineer’s best friend? Yes -- follow these best practices
 
We lost $ 20.5K in one day and how we could have saved it… hint: better autom...
We lost $ 20.5K in one day and how we could have saved it… hint: better autom...We lost $ 20.5K in one day and how we could have saved it… hint: better autom...
We lost $ 20.5K in one day and how we could have saved it… hint: better autom...
 
Moving from A and B to 150 microservices, the journey, and learnings
Moving from A and B to 150 microservices, the journey, and learningsMoving from A and B to 150 microservices, the journey, and learnings
Moving from A and B to 150 microservices, the journey, and learnings
 
Adopt a painless continuous delivery culture, add more business value
Adopt a painless continuous delivery culture, add more business valueAdopt a painless continuous delivery culture, add more business value
Adopt a painless continuous delivery culture, add more business value
 
Software engineering In Nepal mid 2015 part 01
Software engineering In Nepal mid 2015 part 01Software engineering In Nepal mid 2015 part 01
Software engineering In Nepal mid 2015 part 01
 
A simplified Gitflow
A simplified GitflowA simplified Gitflow
A simplified Gitflow
 
How to become a better software company technically
How to become a better software company technicallyHow to become a better software company technically
How to become a better software company technically
 
Things I wished I knew while doing my tech bachelor / undergraduate
Things I wished I knew while doing my tech bachelor / undergraduateThings I wished I knew while doing my tech bachelor / undergraduate
Things I wished I knew while doing my tech bachelor / undergraduate
 
Message Queues a basic overview
Message Queues a basic overviewMessage Queues a basic overview
Message Queues a basic overview
 
Most popular brands, people on facebook in nepal as of 2013 q4
Most popular brands, people on facebook in nepal as of 2013 q4Most popular brands, people on facebook in nepal as of 2013 q4
Most popular brands, people on facebook in nepal as of 2013 q4
 
Drupal 7 basic setup and contrib modules for a brochure website
Drupal 7 basic setup and contrib modules for a brochure websiteDrupal 7 basic setup and contrib modules for a brochure website
Drupal 7 basic setup and contrib modules for a brochure website
 
Git intro hands on windows with msysgit
Git intro hands on windows with msysgitGit intro hands on windows with msysgit
Git intro hands on windows with msysgit
 
Drupal 7 install with modules and themes
Drupal 7 install with modules and themesDrupal 7 install with modules and themes
Drupal 7 install with modules and themes
 
Drupal introduction
Drupal introductionDrupal introduction
Drupal introduction
 
No sql
No sqlNo sql
No sql
 
Business Rules Logical Experssion and SBVR
Business Rules Logical Experssion and SBVRBusiness Rules Logical Experssion and SBVR
Business Rules Logical Experssion and SBVR
 
Drupal A non technical Introduction
Drupal A non technical IntroductionDrupal A non technical Introduction
Drupal A non technical Introduction
 
01 Php Introduction
01 Php Introduction01 Php Introduction
01 Php Introduction
 
09 Oo Php Register
09 Oo Php Register09 Oo Php Register
09 Oo Php Register
 
10 Email Etc
10 Email Etc10 Email Etc
10 Email Etc
 

Recently uploaded

Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
Safe Software
 

Recently uploaded (20)

UiPath Test Automation using UiPath Test Suite series, part 1
UiPath Test Automation using UiPath Test Suite series, part 1UiPath Test Automation using UiPath Test Suite series, part 1
UiPath Test Automation using UiPath Test Suite series, part 1
 
Powerful Start- the Key to Project Success, Barbara Laskowska
Powerful Start- the Key to Project Success, Barbara LaskowskaPowerful Start- the Key to Project Success, Barbara Laskowska
Powerful Start- the Key to Project Success, Barbara Laskowska
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
 
UiPath Test Automation using UiPath Test Suite series, part 2
UiPath Test Automation using UiPath Test Suite series, part 2UiPath Test Automation using UiPath Test Suite series, part 2
UiPath Test Automation using UiPath Test Suite series, part 2
 
IESVE for Early Stage Design and Planning
IESVE for Early Stage Design and PlanningIESVE for Early Stage Design and Planning
IESVE for Early Stage Design and Planning
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptxUnpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
 
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
 
In-Depth Performance Testing Guide for IT Professionals
In-Depth Performance Testing Guide for IT ProfessionalsIn-Depth Performance Testing Guide for IT Professionals
In-Depth Performance Testing Guide for IT Professionals
 
WSO2CONMay2024OpenSourceConferenceDebrief.pptx
WSO2CONMay2024OpenSourceConferenceDebrief.pptxWSO2CONMay2024OpenSourceConferenceDebrief.pptx
WSO2CONMay2024OpenSourceConferenceDebrief.pptx
 
"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi
 

7 rules of simple and maintainable code