Skip to content

A summary of small things a php programmer can do to improve code and the readability of code

License

Notifications You must be signed in to change notification settings

freshp/php-the-small-things

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 

Repository files navigation

php the small things

A summary of small things a php programmer can do to improve the code and the readability of code.

estimated reading time (without links): 15 minutes

There are so brilliant programming concepts and paradigms in the PHP community. I don't want to chew them again and again. But I think everybody needs some easy small things we can improve in our everyday's work. There are so many books, conferences, videos and so on, where you can learn great new or old architectures. But most of the time you have to start from scratch or read thick books before you can start. That is fine, but IMO there are so many small things you can improve and start now. I saw many developer doing great work in general, but no one understands the goal of their software or the way to the goal. There will be the day they dont support their fancy tools any more. And a new developer starts from scratch, because he or she do not understands the code at all. Everybody can produce legacy code. (legacy code is code that works) So IMO the best code is code that everybody can understand by reading it. This repository should help to see, that there are so many small things we can improve from now on.

I am convinced that in a complex situation the small things makes the difference.

  1. PHP programming
    1. Meta theses
    2. PHP programming theses
  2. Learning the basics
  3. Blogs
  4. Tools
    1. Use web services for default tasks
    2. Free online tools
  5. IDE
  6. Important composer packages
    1. Global composer packages
  7. Code reviews
    1. Code review thesis
  8. Documentation

PHP programming

Meta theses

Every thesis can be an advise.

  • Every advice has its own context and the context beats every advice.
  • Please use common sense for every advice, because they are no rules.
  • Every advice which does not bring any benefit should be skipped.
  • Never create advices from subjective perspectives.
  • A advice should ever have a real benefit.
  • A advice should apply generally.
  • You don't have to follow them all, but you should know that they exist.
    • comment: You should not miss out any advice, if you dont know the reason.

PHP programming theses

These are some theses I developed in more than 10 years of programming in teams. It is just a numbered list to get in discussion about it. New thesis will be placed at the end of the list. Feel free to add some of them. The given list is farley no complete, but these points I was talking about too often and it maybe help other too.

My context has always been to develop products that have to be adapted and extended over a long period of time. Defects in these products result in direct financial loss or a blocker of work for teams.

  1. What cannot be tested should not be programmed.

    • comment: There are many tools for automated testing, but start with PHPUnit.
  2. Use type declarations for all properties, functions (return types and parameters) and variables in your code.

    • comment: They didn't come in for nothing.
  3. Immutability is king.

  4. Always check clauses (e.g. in IFs) with the most accurate possible value.

    • comment: Return types can change, but the clause should not. (core methods can also be changed.)
  5. Avoid else and try early returns.

    • comment: Read some why's.
  6. Do not use arrays across class (or function) boundaries.

    • comment: They are not typed.
  7. String concatenation always with sprintf.

    • comment: It is easier to extend and it prevents magic.
  8. Use yoda comparison.

    • comment: Explanation. You can look at the same position for every clause and you prevent to assign a variable.
  9. Use declare(strict_types=1); at the top of every PHP script

  10. Use final for your objects.

    • comment: Our brain is not designed to understand inheritance (like exponential function).
  11. The default visibility private applies to properties and functions.

    • comment: You have to take care about every public function or property like a public api.
  12. As long as annotations are not a language construct in PHP use them sparingly and only for inevitable process control or external packages where the maintainers know what they are doing.

    • comment: Writing your own annotations is for pro's or use PHP8 attributes.
  13. Do not use full namespaces in the code, use even if only \ has to be specified

    • comment: You can see how many external dependencies your class have.
  14. Traits should never use traits. I think you should prevent traits at all.

    • comment: It is nearly impossible to get out of this inheritance tree. And think at exponential functions.
  15. Strings should be consts.

    • Try to prevent strings from your code besides output and use constants instead. They are reusable and you reduce copy-paste-errors.
  16. Do not check in commented or unused code.

    • comment: You have a version control system, use it.
  17. No abbreviations in the code.

    • comment: You use an IDE, use it.
  18. Use dev-dependency-tools as phar files.

    • comment: Your requirement-tree will be thankful, there are less conflicts and its faster.
  19. Do not use a synchronous programming language like a asynchronous.

    • comment: I know there are good approaches, but both types are different ways of thinking. Maybe it is my fault, but IMO you can make one way very good, or two a little bit.

Learning the basics

This is just a small snapshot I can recommend.

  • Principles of object oriented design (web)
  • The clean coder (book | web)
  • Extremely defensive PHP (video | slides)
  • Design pattern (web)
  • PHP standards recommendations PSR
  • Semantic versioning (web)
  • Refactoring (book)
  • Principles of package design (book)
  • A philosophy of software design (book)
  • Pro git (ebook)

Blogs

There are many more interesting blogs, but these selection cross up my way multiple times.

Tools

  • use package managers for your local requirements
    • for example in MacOS install brew
  • install php over brew on MacOS
    • comment: the big advantage is to have multiple versions linked by brew directly from the cellar
  • install git over brew on MacOS
  • install Oh My Zsh bash on MacOS
  • install composer globally for dependency management in PHP
  • install xdebug for debugging, tracing, profiling and the codecoverage

Use web services for default tasks

  • use a automation server like jenkins for continuous integration, continuous deployment or any other automation
  • use a remote repository server like gitlab or bitbucket or at least github
  • use a private package repository like satis
  • use a error tracker like sentry

Free online tools

  • if you need regex, than use this page
  • you want to schedule a cronjob, than look at this page
  • you are not sure with your version constraint, than look at this page

IDE

Important composer packages

The list of packages is not complete, but you can start projects from this stack.

Global composer packages

There are some packages you can install and use as global dependency.

  • Run Composer1 installs parallel with prestissimo.
    • Composer2 is very fast on its own. Uninstall this plugin and update the Composer itself.

Code reviews

Start making code reviews. If you dont know how? Read this article

Code review theses

You are not your Code!

  • code reviewer do not comment you, they comment your code

If you find code you do not understand, ask.

  • It is not the question if you should know, but when.

Documentation

I only saw one approach, which seems to be suitable for software projects and this is arc42.

For further diagrams or graphs you can use draw.io.

About

A summary of small things a php programmer can do to improve code and the readability of code

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published