Key takeaways from ‘Clean Code’ as an iOS Developer

Uplift iOS Interview

The Guide is for YOU if
  • You are preparing for an iOS interview and want to improve your skills and knowledge and looking to level up your interview game and land your dream job.
  • You want to gain confidence and ease during iOS interviews by learning expert tips and curated strategies.
  • You want access to a comprehensive list of iOS interview QA to practice and prepare.

Confession time – I’m not the greatest reader. When it comes to technical books, I tend to take my sweet time to really digest everything. That’s why when I was knee-deep in my M.Sc. studies, it took me a while to plow through “Clean code – A handbook of Agile software craftmanship” by Robert C. Martin.

But boy, am I glad I did. It’s been years since I read it, but I still credit this book as the biggest influence on my code-writing style. Sure, it was originally geared towards Java developers, but as an iOS whiz, I still found it incredibly insightful.

One thing that really stood out to me about this book was how down-to-earth and practical it was. It wasn’t just a bunch of theoretical mumbo-jumbo – the author included plenty of real-world examples to really drive his points home. And unlike some other books, it acknowledged that writing code isn’t always a smooth process. We can’t expect to write perfect, clean code on the first try – it’s a journey that takes time and effort. But with the right mindset and approach, we can get there eventually.

As an iOS developer, I soaked up a ton of wisdom from “Clean Code – A Handbook of Agile Software Craftsmanship” by Robert C. Martin. Here are the top takeaways that have helped me elevate my code game:

Chapter 1: Clean Code

  • Code should be easy to read and understand, even by someone who didn’t write it.
  • Good code has good names, is concise, and doesn’t have unnecessary complexity.
  • Key takeaways as an iOS developer, it’s crucial to avoid unnecessary complexity and focus on developer-friendly code, my code should be understandable by interns and juniors. I should avoid making code tricky and complex otherwise I need to invest more time to explain the code to my junior colleagues.

Chapter 2: Meaningful Names

  • Names should reveal intent and be descriptive.
  • Avoid using single letter variable names or abbreviations that are unclear.
  • Key takeaways an iOS developer, When naming variables, functions, or classes in iOS development, it’s crucial to choose names that accurately reflect their purpose. Using clear and concise names can help other developers understand the codebase more quickly, making it easier to maintain and improve over time. Use descriptive names for view controllers, views, and their associated outlets and actions. I should follow Apple API design guidelines and add SwiftLint to my codebase.

Chapter 3: Functions

  • Functions should be short and focused on one task.
  • Functions should have a clear intent, and their name should reflect it.
  • Key takeaways as an iOS developer, it’s essential to break down complex functions into smaller, more manageable ones. This makes it easier to test, understand, and reuse code, resulting in a more maintainable codebase. Functions should be used to handle specific UI interactions or data manipulation, such as updating the user interface or handling data storage. I must not mix those two functionalities in one function. While working on SwiftUI, I also need to break down my views based on functionalities.

Chapter 4: Comments

  • Comments should be used sparingly and only when necessary to explain why something is being done.
  • Key takeaways as an iOS developer, Comments should never be used to explain what the code is doing – the code itself should be clear enough to understand. If it is highly needed, I should add comments but I should rethink the function name or variable name to make it more understandable.

Chapter 5: Formatting

  • Consistent formatting makes code easier to read and understand.
  • Use whitespace and indentation to make code more readable.
  • Key takeaways as an iOS developer, I must use consistent indentation and whitespace to make the code easier to read and understand with help of Xcode preferences and SwiftLint.

Chapter 6: Objects and Data Structures

  • Objects hide their implementation details and expose behavior through interfaces.
  • Data structures expose their implementation details and have no behavior.
  • Key takeaways as an iOS developer, I should framework and abstraction through protocol to hide implementation details.

Chapter 7: Error Handling

  • Write code to handle exceptions and be prepared to handle them at every level of the call stack.
  • Key takeaways as an iOS developer, Use try-catch blocks and NSError to handle errors in a consistent and understandable way. I should try async-await instead of callback to make the error handling efficient.

Chapter 8: Boundaries

  • Use adapters to convert incompatible interfaces.
  • Don’t expose implementation details of external code.
  • Key takeaways as an iOS developer, I must use manager classes as middleware while using frameworks such as Alamofire to make network calls and Core Location to handle location data. I must not call Alamofire functions directly from my viewModel/interector. I must keep in mind, I would probably replace Alamofire in future which should not affect my viewModel/interector. I should only touch my manager classes when I want to remove Alamofire.

Chapter 9: Unit Tests

  • Write tests for your code to ensure it works as expected.
  • Write tests before writing the code to ensure the code is testable.
  • Key takeaways as an iOS developer, I must use XCTest to write unit tests for my code to catch bugs before they reach production.

Chapter 10: Classes

  • Classes should have a single responsibility.
  • They should be open for extension but closed for modification.
  • Key takeaways as an iOS developer, focus on SRP!

Chapter 11: Systems

  • A system is made up of many smaller components.
  • Keep the components separate and independent.
  • Key takeaways as an iOS developer, I will keep my model, view, and controller layers separate and independent, and use dependency injection.

Chapter 12: Emergence

  • Good design emerges from good practices.
  • Key takeaways as an iOS developer, I will keep my code clean and well-organized, and the good design will follow.

Chapter 13: Concurrency

  • Concurrency is complex and can introduce subtle bugs.
  • Key takeaways as an iOS developer, Use concurrency/GCD/Task wisely to make it easier to write correct and bug-free code.

Chapter 14: Successive Refinement

  • Writing clean code is a process, not a one-time event.
  • Key takeaways as an iOS developer, I must continuously improve my code by refactoring it over time.

In conclusion, applying the principles outlined in “Clean Code” is crucial for any iOS developer looking to write high-quality code. By focusing on clean code principles like meaningful names, small functions, consistent formatting, and robust error handling, we can create iOS applications that are more maintainable, reliable, and easy to use. So, let’s roll up our sleeves and start writing some clean code!

✍️ Written by Ishtiak Ahmed

👉 Follow me on XLinkedIn

Get Ready to Shine: Mastering the iOS Interview

Enjoying the articles? Get the inside scoop by subscribing to my newsletter.

Get access to exclusive iOS development tips, tricks, and insights when you subscribe to my newsletter. You'll also receive links to new articles, app development ideas, and an interview preparation mini book.

If you know someone who would benefit from reading this article, please share it with them.