- Swift Functional Programming(Second Edition)
- Dr. Fatih Nayebi
- 221字
- 2021-07-02 23:54:31
Best practices in function definition
There are proven best practices for function and method definition provided by amazing software engineering resources, such as Clean Code: A Handbook of Agile Software Craftsmanship, by Robert C. Martin, Code Complete: A Practical Handbook of Software Construction, Second Edition, by Steve McConnell, and Coding Horror (https://blog.codinghorror.com/code-smells/), that we can summarize as follows:
- Try not to exceed 8-10 lines of code in each function as shorter functions or methods are easier to read, understand, and maintain.
- Keep the number of parameters minimal because the more parameters a function has, the more complex it is.
- Functions should have at least one parameter and at least one return value.
- Avoid using type names in function names since they are going to be redundant.
- Aim for one and only one functionality in a function.
- Name a function or method in a way that it describes its functionality properly and is easy to understand.
- Name functions and methods consistently. For instance, if we have a connect function, we can have a disconnect one.
- Write functions to solve the current problem and generalize it when needed. Try to avoid what-if scenarios as probably You Aren't Going to Need It (YAGNI).
It is important to follow these best practices. After all, we are talking about FP and you know, functions are important!
推薦閱讀
- 計算機組成原理與接口技術:基于MIPS架構實驗教程(第2版)
- 數據存儲架構與技術
- 數據庫應用實戰
- 算法競賽入門經典:習題與解答
- Libgdx Cross/platform Game Development Cookbook
- 大數據時代下的智能轉型進程精選(套裝共10冊)
- Sybase數據庫在UNIX、Windows上的實施和管理
- 計算機應用基礎教程上機指導與習題集(微課版)
- TextMate How-to
- Instant Autodesk AutoCAD 2014 Customization with .NET
- Hadoop集群與安全
- 區塊鏈技術應用與實踐案例
- 從實踐中學習sqlmap數據庫注入測試
- Hands-On System Programming with C++
- 商業智能工具應用與數據可視化