Recently, a great developer enrolled in my weekly class approached me with a link on how to write a popular algorithm in Swift. Since we review similar concepts in class, I was intrigued and proceeded to check out the details. Even though the essay had received many views (likes), disappointment set in as I realized portions were lacking, were poorly communicated and technically incomplete. To support my assumptions, the author was unable to answer questions asked by audience members.
It’s common for a developer to be asked by a project manager or stakeholder to “codify” some new process or business rule. Many solutions can be fulfilled through predefined SDK Framework functions or third-party libraries. Other times we have to roll up our sleeves and think about how a system or process could work. In essence, this is the value we bring to a business or project. As a result, here are five tips to consider when writing your own algorithms. These suggestions can be applied during a technical interview or when planning your next project.
Understand The Process
As you write your algorithms, considerable focus should go towards understanding the system or process you’re trying to capture. The more you know the process data inflows and outflows, the more you’ll make good design and implementation choices. As part of this, ensure you can fully describe the business process before committing anything to code (this is where the whiteboard comes in handy). When coding a new algorithm or method, there may be no precedent or defined standard. As a result, it’s always good practice to gather lots of details to refine your implementation. Consider the following challenge:
Like this article? Get more content like this when you sign-up for the weekly iOS Computer Science Lab.
let sequence = [8, 2, 9, 10, 6, 7] //challenge: write an algorithm to return the largest collection value
One of the cool aspects of software development is applying different techniques to solve a problem. For example, one could solve this question by writing a sorting function. While sorting the Array will undoubtedly reveal the largest value, it will also significantly impact performance since even complex sorting solutions perform at O(n log n) time. However, if we revisit the question, note that it does not require that we sort all values.
As an alternative, we could use the details provided to streamline our algorithm. For example, this could apply a more efficient form of memoization or perhaps reinterpret the collection as a tree-based structure.
Adhere To Best-Practices
As developers, we hear the phrase “best-practices” used frequently. While almost cliche, following an accepted standard provides an excellent foundation for communicating ideas. As iOS developers, we also have standards of what we like to see. As an author and educator, here are some criteria I use:
Remove the declaration and use of implicitly unwrapped optionals
Ensure the use of return values declared by function/method signatures
Refactor custom data structures to support generic types
Rewrite recursive algorithms as iterative constructs
Provide code comments and documentation for complex algorithms
Write supporting unit test cases when applicable
Except for Swift optionals, these best-practice activities are largely language-agnostic, meaning they can be applied to different projects or even other programming languages. While your guidelines may differ, attention to small details will pay dividends when your code is eventually promoted to a production environment or is refactored.
Understand the Complexity
When working with others preparing for a technical interview, a lot of emphasis is placed on the candidate’s approach to solving a problem. As discussed, there is rarely a one-size-fits-all model in code development, so we are often faced with choosing the best method. This is where a good understanding of Big-O Notation comes in. By being able to objectively compare and contrast solutions based on their space or performance efficiencies, you’ll be better skilled to identify code that could be streamlined to meet requirements.
Anticipate Code Refactoring
As software developers, we are quite familiar with changing business requirements as well as technologies. As part of this, we should aim to write code that is extendable and can be changed with little disruption. In Swift parlance, much of this can be achieved through Generics and refactoring code to support a protocol-oriented approach. As most of us know, Swift protocols not only define rules (conformance) but can also implement behavior through the application of protocol extensions. Examples of this can be seen through the essentials of Comparable, Equatable and Hashable.
Validate Your Work
These days, the benefits of collaborating with colleagues is more valued than ever. Even in my work, being able to connect with fellow developers allows me to:
Gain a fresh perspective on code challenge solutions
Create new code as well as learning materials based on questions asked
Identify bugs or missed edge cases in existing source code
Learn something new!
In Summary
Understand the system or process you’re trying to document
Write code that is easy to understand, adheres to accepted best practices and employs relatively straightforward coding syntax
Understand your solution’s Big-O Notation time and space complexity
Plan for your code to be refactored
Validate your work with fellow development peers and colleagues