Omotola started Ampersand in September 2020 on the Junior Talent Programme. Graduating from the University of Salford with a degree in Software Engineering, she has a keen interest in creating technical content, public speaking and dancing! Omotola also volunteers for tech communities that advocate for empowering equality and diversity in the industry. Follow her on Instagram and LinkedIn to keep up to date with what she’s up to.
1. PSR0-PSR4 php style coding standard (Coding Standards, PSR and Magento)
Coding standards allow for high technical interoperability between shared PHP code, which ensures that the code is easily understood amongst software systems and interfaces, while style guides enhance code readability.
The PSR1 and PSR2 (now PSR12) give us a guide to how we expect devs to write code, removing issues of personal preference of coding styles, which may result in conflicts when pull requests are made. These PSR standards are mostly used for our non-Magento projects, however, for our Magento projects we use a ruleset made by the Magento team.
Following this link, you’ll notice that the rules to follow are tedious to remember, this is where tools come in! We use PHP-CodeSniffer to follow the PSR2 standards and this is compatible with IDEs like, PHPStorm and Vscode. It is also flexible enough to allow custom configuration.
Another tool we use is PHPStan, which is mostly used for our Magento projects. It drastically reduces the amount of bugs in the codebase, improves performance, improves readability and maintainability.
2. Write Descriptive variable/method names
We use camelCase for naming our variables (Ampersand’s preference) and method names (PSR1 Standard). When writing method signatures, it’s useful to give a description of expected return types and indicate the type of arguments, if any are required. It’s also important to not forget to add the PHPDoc (a PHP standard for commenting code).
The example below shows:
- PHPDOC has been added which is autogenerated from the signature of our method
- This function is for handling request to view a product page
- We are expecting an argument of type array that is a productList
- The function returns nothing
3. Dependency Injection is King
Following the SOLID principles is a rule of thumb, in this section we focus on applying Composition over Inheritance. Composition refers to ‘has a’ relationship while Inheritance refers to a ‘is a’ relationship. The former is encouraged because it promotes loose coupling between objects which is helpful for code flexibility, maintainability and scalability.
Dependency Injection relies on Composition through Inversion of control. The concept of DI refers to when an object receives other objects that it relies on. We use dependency injection (DI) because it makes objects easily exchangeable, and it abstracts the job of creating and supplying dependencies needed.
With DI in the example below, we bypass initialising the dependencies (objects) in the dependent class’s constructor. The below image shows: if class A is dependent on class B and C to carry out its functions, class A should be implemented like this.
4. PHP Unit testing is a requirement
We love automated testing and use PHPUnit for it. Test Driven Development is also encouraged because it allows us to ensure that the code is easily maintained (testable); has met the acceptance criteria; prevents exposing methods that are meant to be kept private; hints when a piece of code needs refactoring and generally minimises bugs. It also serves as a great type of documentation for our developers, because writing tests in this way gives knowledge about what types of input are expected and their return values.
All of these best practices take time to become a habit, and with more practise it becomes second nature. At Ampersand we pride ourselves on producing quality code.This notion is adopted across the other Frameworks, Tools and Languages we use. Curious about what other technologies we use, get yourself familiar with Magento, React, Docker, and GraphQL.