快速编写自己的算法的5个技巧

Recently, a great developer enrolled in my weekly iOS Computer Science Lab 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.

[R ecently,一个伟大的开发者参加了我的每周iOS的计算机科学实验室向我提出了关于如何写在斯威夫特流行的算法的链接。 由于我们在课堂上复习了类似的概念,因此我很感兴趣并着手检查了细节。 即使这篇文章收到了很多意见(赞),但由于我意识到部分内容不足,沟通不畅以及技术上不完整,令人失望。 为了支持我的假设,作者无法回答听众提出的问题。

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.

通常,项目经理或利益相关者会要求开发人员“编纂”一些新流程或业务规则。 通过预定义的SDK框架功能或第三方库,可以实现许多解决方案。 其他时候,我们不得不袖手旁观,思考系统或流程的工作方式。 从本质上讲,这就是我们为业务或项目带来的价值。 结果,这是编写自己的算法时要考虑的五个技巧。 这些建议可以在技术面试期间或计划下一个项目时应用。

了解过程 (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:

在编写算法时,应将重点放在理解您要捕获的系统或过程上。 您对流程数据流入和流出的了解越多,您就越会做出良好的设计和实现选择。 作为此过程的一部分,请确保在将任何内容提交代码之前,您可以充分描述业务流程(这在白板上很方便)。 在编写新算法或方法时,可能没有先例或已定义的标准。 因此,收集大量细节来完善实现始终是一种好习惯。 考虑以下挑战:

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.

软件开发的酷方面ØNE是运用不同的技术来解决问题。 例如,可以通过编写排序函数来解决此问题。 虽然对数组进行排序无疑会显示出最大的价值,但由于即使是复杂的排序解决方案也可以在O(n log n)时执行,因此也会显着影响性能。 但是,如果我们重新讨论该问题,请注意,它不需要我们对所有值进行排序。

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:

作为开发人员,我们经常听到“最佳实践”这一短语。 尽管几乎陈词滥调,但遵循公认的标准为交流思想奠定了良好的基础。 作为iOS开发人员,我们还拥有我们希望看到的标准。 作为作者和教育家,我使用以下一些标准:

  1. Remove the declaration and use of implicitly unwrapped optionals

    删除声明并使用隐式展开的可选

  2. Ensure the use of return values declared by function/method signatures

    确保使用函数/方法签名声明的返回值
  3. Refactor custom data structures to support generic types

    重构自定义数据结构以支持通用类型
  4. Rewrite recursive algorithms as iterative constructs

    将递归算法重写为迭代构造
  5. Provide code comments and documentation for complex algorithms

    提供复杂算法的代码注释和文档
  6. 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.

除了Swift可选控件之外,这些最佳实践活动在很大程度上与语言无关,这意味着它们可以应用于不同的项目甚至其他编程语言。 尽管您的准则可能有所不同,但是当您的代码最终被提升到生产环境或进行重构时,对细节的关注会带来好处。

了解复杂性 (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.

与其他人进行技术面试的准备时,重点应放在候选人解决​​问题的方法上。 正如讨论的那样,在代码开发中很少有一种千篇一律的模型,因此我们经常面临选择最佳方法的问题。 在这里,您可以很好地理解Big-O表示法 。通过能够根据解决方案的空间或性能效率客观地比较和对比解决方案,您将更熟练地识别可以简化以符合要求的代码。

预期代码重构 (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.

作为软件开发人员,我们对不断变化的业务需求和技术非常熟悉。 作为其中的一部分,我们应该致力于编写可扩展的代码,并且可以在不造成干扰的情况下对其进行更改。 用Swift的话来说,很多可以通过泛型和重构代码来实现,以支持面向协议的方法。 众所周知,Swift协议不仅定义了规则(一致性),而且还可以通过协议扩展的应用来实现行为。 可以通过可比,平等和可哈希化的要领来举例说明。

验证您的工作(与他人一起) (Validate Your Work (with others))

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:

如今,与同事合作的好处比以往任何时候都更加受重视。 即使在我的工作中,能够与其他开发人员建立联系也使我能够:

  1. Gain a fresh perspective on code challenge solutions

    对代码挑战解决方案有了新的认识
  2. Create new code as well as learning materials based on questions asked

    根据提出的问题创建新代码以及学习材料
  3. Identify bugs or missed edge cases in existing source code

    识别现有源代码中的错误或遗漏的情况
  4. 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

    了解解决方案的Big-O符号时间和空间复杂性
  • Plan for your code to be refactored

    计划重构代码
  • Validate your work with fellow development peers and colleagues

    与其他开发同行和同事一起验证您的工作

翻译自: https://medium.com/swift-algorithms-data-structures/5-tips-for-writing-your-own-algorithms-in-swift-827b7e932ad1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值