0% found this document useful (0 votes)
7 views30 pages

Simple Maintainable Code

The document discusses the principles of writing simple, maintainable, and clean code, emphasizing readability, proper naming conventions, and adherence to coding standards. It outlines several rules for achieving clean code, such as writing expressive code, avoiding long methods and classes, and ensuring code is self-explanatory without excessive comments. The author advocates for writing code that is understandable by humans, highlighting the importance of organization and clarity in programming.

Uploaded by

Bijaya Prasad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views30 pages

Simple Maintainable Code

The document discusses the principles of writing simple, maintainable, and clean code, emphasizing readability, proper naming conventions, and adherence to coding standards. It outlines several rules for achieving clean code, such as writing expressive code, avoiding long methods and classes, and ensuring code is self-explanatory without excessive comments. The author advocates for writing code that is understandable by humans, highlighting the importance of organization and clarity in programming.

Uploaded by

Bijaya Prasad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

Simple Maintainable &

Clean Code
Bijaya Prasad Kuikel
Wash Your Code … make it clean
What makes code to be simple & clean?

- Code which is easy to read


- Code which is easy to understand
- Code which is easy to write (update / maintain)
Why to write clean code?

- Code for Humans not for computers


- “Any fool can write code that a computer can understand.
Good programmers write code that humans can
understand.” — Martin Fowler
After reading
someone’s
dirty and
unmanaged
code
“Writing Computer software is one of the purest creative
activities in the history of the human race. Programmers
aren’t bound by practical limitations such as the laws of
physics; we can create exciting virtual worlds with
behaviours that could never exist in the real world.
Programming doesn’t require great physical skill or
coordination like Ballet or Football.
All programming requires is a creative mind and the
ability to organize your thoughts . If you can visualize a
system, you can probably implement it in a computer
program.”
A PHILOSOPHY OF SOFTWARE DESIGN – JOHN OUSTERHOUT
Traits of Clean Code

Readable Well Structured Extensible Testable


Rules to make your code clean simple & maintainable
Rule #1: Focus on proper nomenclature

- Give things properly a clear name


- It should describe what the variable is for (its purpose, what
it represents, etc.),
- what a method does, what a class does, and what it helps
to convey or understand.
- A method doing something other than what it’s name
describes is a surprise
Remember
Sheetal?
Contd...

- Long variable / method names are allowed


- Naming is difficult, do it well.
- Do not be redundant.
- Follow naming convention consistently (eg. $someVariable
or $SomeVariable or $some_variable)
Rule #2: Follow consistent coding standard

- If you are doing PHP follow PSR-12 (Other languages have their
standard too)
- Use a linter or CS fixer to follow the standard. Integrate build tools in
Sublime or use default PHPStorm code refactor, to automate this.
- Makes it easy for everyone - including yourself - in the team, to read
the code
- Share the same settings with the team
- Make habit of pre commit hooks that cleans up the code upto the
same standard
Rule #3: Write Expressive Code

- Be expressive, write code as you speak


- Tell, don’t ask
- Focus on API rather than patterns.
- Frist, write down the API for perfect scenario, observe how it feels,
then jump to coding and make it work.
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
Rule #5: Distribute the responsibility wisely

- Avoid creating GOD object & long methods


- One class should do one thing not everything like StatusChanger or
StatusManager not StatusGod
- Avoid using and in 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.
GOD Objects be like 😂😂
Rule #6: Avoid inline comments & add clear
documentation
- Code should be self expressive
- Comment is a code smell (anti pattern)
- If you need to write comments to explain your code, it means you
need to put it in a new method.
- Provide clear documentation
Rule #7: Write Reusable code, don’t repeat yourself

- If you find yourself copying the same code several times, extract
that code into its own method.
- Use language constructs like interfaces, traits to make code more
expressive and reusable
Other Considerations

- 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
Any Questions?

You might also like