Hi. Hello. Hola.
I have been busy with my work that I have not had time to write any technical posts. As a teaser for my next article, it will be about React Native and calling functions in a view layer. Their current suggested approach is setting and managing boolean values; this can lead to both potential errors and dirty code.
In the meantime, I thought I would give my thoughts and opinions on code quality. I would say one of the harder aspects of code quality is we all believe that our code is understandable to everyone because we can understand it. This is not always the case.
Clarity and XKCD
I see clarity as an overarching core tenant of programming. Anyone that comes to the project, code reviews, or pair programs should be able to understand, with minimal effort, the logic that you have implemented. Here are a few markers that will help yourself and others:
Code should be maintainable and efficient
Code should be well documented
Code should be well tested in both happy and error paths
If you have other standards your team utilizes please share them in the comments below.
I like to think the easier you can find your way around the better off you will be. There isn’t one right answer to everything in this scenario² but make sure you converse with your peers and agree upon a set of standards to enforce. In doing this, you will save yourself a lot of headaches. This can include a variety
If I’m being honest this post is really about linters. It is one of the quickest and easiest ways to generate consistency across a project. A couple of examples for customization include:
My personal linter of choice for this is SwiftLint.
The community is very active in support and making sure everyone has the tools they need in their workflow. I won’t go into detail on integrating this into your workflow as their README.md is very detailed. Here is the current set of rules that I utilize.
Objective-C and React Native
In the last six months, I have been working on a React Native project. A different set of challenges and integrations come with this new working environment. I recently stumbled across a package that allows me to integrate clang-format¹ into the work. Let’s look at how we can incorporate this into your work.
2. Add .clang-format into your project root directory
3. Utilize husky for git hooks:
This process looks convoluted however once completed you will never have to think about it again. Pre-commit hooks are a great way to automate code quality while committing to your repo.
The two Objective-C and Swift examples are a starting point there are plenty of linters at everyone’s disposal. Take a look at this Github collection and find one you can integrate into your project.
So let’s say you decided to add your linter late into the project and you don’t want to create a whitespace commit just to force your git hook actions. Here is a quick cli command you can run to lint every .m and .h file inside your current folder:
- Except for linting, always lint your code fam.