搜尋

發展原則

Sep 03, 2024 am 11:41 AM

Development Principles

程式設計是一個不斷改進的旅程。隨著經驗的積累,我們會遇到一些實踐和原則,這些實踐和原則可以幫助我們完善我們的技術,從而產生更高品質的程式碼。本文將引導您了解可協助您成為更好的程式設計師的關鍵原則和實踐,包括 SOLID 原則、設計模式和編碼標準。我們將探討這些概念如何改善您的開發流程。

關於範例的說明:我選擇 Go 作為程式碼範例是因為它的簡單性和可讀性 - 它通常被描述為「可執行偽程式碼」。如果你不熟悉 Go,請不要擔心!這是一個很好的學習機會。

當您遇到不熟悉的語法或概念時,請花點時間尋找它們。這個發現的過程可以為您的學習體驗帶來有趣的獎勵。請記住,我們正在討論的原則適用於各種程式語言,因此請專注於理解概念而不是 Go 語法的細節。

1. 擁抱程式設計標準

成為更好的程式設計師的第一步就是遵守程式設計標準。標準提供了命名約定、程式碼組織和文件結構等方面的指南。透過遵循這些約定,您可以確保程式碼一致且易於閱讀,這對於協作和長期維護至關重要。

每種程式語言通常都有自己的一套約定。對於 Go,官方 Go 風格指南中概述了這些內容。學習並接受與您的環境相關的標準非常重要,無論是您團隊的慣例還是特定於語言的指南。

讓我們來看一個範例,了解以下標準如何提高程式碼可讀性:

// Before: Inconsistent styling
func dosomething(x int,y string)string{
if x>10{
return y+"is big"
}else{
return y+"is small"
}}

此程式碼有幾個問題:

  1. 不一致的間距和縮排使程式碼結構難以理解。
  2. 函數名稱不遵循 Go 的駝峰命名約定。
  3. 不必要的 else 子句增加了認知複雜性。

現在,讓我們重構此程式碼以遵循 Go 標準:

// After: Following Go standards
func doSomething(x int, y string) string {
    if x > 10 {
        return y + " is big"
    }
    return y + " is small"
}

在此改良版中:

  1. 正確的縮排可以清楚顯示程式碼的結構。
  2. 函數名稱遵循 Go 的命名約定。
  3. 刪除了 else 子句,簡化了邏輯。

這些變化不僅與美觀有關;也與美觀有關。它們顯著提高了程式碼的可讀性和可維護性。在團隊中工作時,一致應用這些標準可以讓每個人更輕鬆地理解和使用程式碼庫。

即使您的團隊沒有既定標準,您也可以主動遵循程式設計社群中廣泛接受的約定。隨著時間的推移,這種做法將為您的專案帶來更具可讀性、可維護性的程式碼。

2.遵循設計原則

程式設計原則是幫助您編寫更好程式碼的指南。這些原則不僅可以應用於程式碼架構,還可以應用於系統設計,甚至開發過程的某些方面。
設計原則有很多,其中一些在特定情況下更相關。其他的則更通用,例如 KISS(保持簡單、愚蠢)或 YAGNI(你不需要它)。
在這些一般原則中,SOLID 原則是最具影響力的原則之一。讓我們探索每個原則,並專注於它們如何改進您的程式碼。

單一責任原則(SRP)

這項原則鼓勵您設計具有單一、明確定義目的的元件(函數、類別或模組)。當一個元件承擔多重職責時,它會變得更難理解、測試和維護。

讓我們來看一個重構函數以遵守 SRP 的範例:

// Before: A function doing too much
func processOrder(order Order) error {
    // Validate order
    if order.Total 



<p>函數正在執行多個不相關的任務:驗證訂單、將其保存到資料庫、發送電子郵件和更新庫存。讓我們將其分解為單獨的函數,每個函數都有一個職責:<br>
</p>

<pre class="brush:php;toolbar:false">// After: Breaking it down into single responsibilities
func processOrder(order Order) error {
    if err := validateOrder(order); err != nil {
        return err
    }
    if err := saveOrder(order); err != nil {
        return err
    }
    if err := sendOrderConfirmation(order); err != nil {
        return err
    }
    return updateInventoryForOrder(order)
}

現在,讓我們實現以下每個功能:

func validateOrder(order Order) error {
    if order.Total 



<p>在此重構版本:</p>

  • 每個職能都有單一、明確的職責。
  • 程式碼更加模組化,也更容易測試。
  • 各個組件可以重複使用或修改,而不會影響其他組件。

相信我,未來的你會感謝你的這種程度的組織。

開閉原則(OCP)

開閉原則建議軟體實體應該對擴充開放,對修改關閉。這意味著您應該能夠在不更改現有程式碼的情況下新增功能。

Liskov Substitution Principle (LSP)

LSP states that objects of a superclass should be replaceable with objects of its subclasses without affecting the correctness of the program. This ensures that inheritance hierarchies are well-designed and maintainable.

Interface Segregation Principle (ISP)

ISP suggests that no code should be forced to depend on methods it does not use. In practice, this often means creating smaller, more focused interfaces.This modularity makes your code easier to manage and test.

Here's an example demonstrating ISP:

// Before: A large interface that many structs only partially implement
type Worker interface {
    DoWork()
    TakeBreak()
    GetPaid()
    FileTicket()
}

This large interface forces implementers to define methods they might not need. Let's break it down into smaller, more focused interfaces:

// After: Smaller, more focused interfaces
type Worker interface {
    DoWork()
}

type BreakTaker interface {
    TakeBreak()
}

type Payable interface {
    GetPaid()
}

type TicketFiler interface {
    FileTicket()
}

Now, structs can implement only the interfaces they need:

type Developer struct{}

func (d Developer) DoWork() {
    fmt.Println("Writing code")
}

func (d Developer) TakeBreak() {
    fmt.Println("Browsing Reddit")
}

func (d Developer) FileTicket() {
    fmt.Println("Creating a Jira ticket")
}

type Contractor struct{}

func (c Contractor) DoWork() {
    fmt.Println("Completing assigned task")
}

func (c Contractor) GetPaid() {
    fmt.Println("Invoicing for work done")
}

This modularity makes your code easier to manage and test. In Go, interfaces are implicitly implemented, which makes this principle particularly easy to apply.

Dependency Inversion Principle (DIP)

DIP promotes the use of abstractions rather than concrete implementations. By depending on interfaces or abstract classes, you decouple your code, making it more flexible and easier to maintain. This also facilitates easier testing by allowing mock implementations.


Applying the SOLID (see what I did there?) principles leads to decoupled, modular code that is easier to maintain, scale, reuse, and test. I’ve found that these principles, while sometimes challenging to apply at first, have consistently led to more robust and flexible codebases.

While these principles are valuable, remember that they are guidelines, not strict rules.There will always be exceptions to the rules, but it’s important to remember that following these principles is a continuous process and not a one-time event. It will take time and effort to develop good habits, but the rewards are well worth it.

3. Utilize Design Patterns

Design patterns provide reusable solutions to common programming problems. They are not rigid implementations but rather templates that can be adapted to fit specific needs. Many design patterns are related to SOLID principles, often aiming to uphold one or more of these principles in their design.

Design patterns are typically categorized into three types:

Creational Patterns

These patterns deal with object creation mechanisms. An example is the Factory Method pattern, which creates objects based on a set of criteria while abstracting the instantiation logic.

Let's look at a simple Factory Method example in Go:

type PaymentMethod interface {
    Pay(amount float64) string
}

type CashPayment struct{}

func (c CashPayment) Pay(amount float64) string {
    return fmt.Sprintf("Paid %.2f using cash", amount)
}

type CreditCardPayment struct{}

func (cc CreditCardPayment) Pay(amount float64) string {
    return fmt.Sprintf("Paid %.2f using credit card", amount)
}

Here, we define a PaymentMethod interface and two concrete implementations. Now, let's create a factory function:

func GetPaymentMethod(method string) (PaymentMethod, error) {
    switch method {
    case "cash":
        return CashPayment{}, nil
    case "credit":
        return CreditCardPayment{}, nil
    default:
        return nil, fmt.Errorf("Payment method %s not supported", method)
    }
}

This factory function creates the appropriate payment method based on the input string. Here's how you might use it:

method, err := GetPaymentMethod("cash")
if err != nil {
    fmt.Println(err)
    return
}
fmt.Println(method.Pay(42.42))

This pattern allows for easy extension of payment methods without modifying existing code.

Structural Patterns

Structural patterns deal with object composition, promoting better interaction between classes. The Adapter pattern, for example, allows incompatible interfaces to work together.

Behavioral Patterns

Behavioral patterns focus on communication between objects. The Observer pattern is a common behavioral pattern that facilitates a publish-subscribe model, enabling objects to react to events.


It's important to note that there are many more design patterns, and some are more relevant in specific contexts. For example, game development might heavily use the Object Pool pattern, while it's less common in web development.

Design patterns help solve recurring problems and create a universal vocabulary among developers. However, don't feel pressured to learn all patterns at once. Instead, familiarize yourself with the concepts, and when facing a new problem, consider reviewing relevant patterns that might offer a solution. Over time, you'll naturally incorporate these patterns into your design process.

4. Practice Good Naming Conventions

Clear naming conventions are crucial for writing readable and maintainable code. This practice is closely related to programming standards and deserves special attention.

Use Descriptive Names

Choose names that clearly describe the purpose of the variable, function, or class. Avoid unnecessary encodings or cryptic abbreviations.

Consider this poorly named function:

// Bad
func calc(a, b int) int {
    return a + b
}

Now, let's improve it with more descriptive names:

// Good
func calculateSum(firstNumber, secondNumber int) int {
    return firstNumber + secondNumber
}

However, be cautious not to go to the extreme with overly long names:

// Too verbose
func calculateSumOfTwoIntegersAndReturnTheResult(firstInteger, secondInteger int) int {
    return firstInteger + secondInteger
}

Balance Clarity and Conciseness

Aim for names that are clear but not overly verbose. Good naming practices make your code self-documenting, reducing the need for excessive comments.

Prefer Good Names to Comments

Often, the need for comments arises from poorly named elements in your code. If you find yourself writing a comment to explain what a piece of code does, consider whether you could rename variables or functions to make the code self-explanatory.

Replace Magic Values with Named Constants

Using named constants instead of hard-coded values clarifies their meaning and helps keep your code consistent.

// Bad
if user.Age >= 18 {
    // Allow access
}

// Good
const LegalAge = 18
if user.Age >= LegalAge {
    // Allow access
}

By following these naming conventions, you'll create code that's easier to read, understand, and maintain.

5. Prioritize Testing

Testing is an essential practice for ensuring that your code behaves as expected. While there are many established opinions on testing methodologies, it's okay to develop an approach that works best for you and your team.

Unit Testing

Unit tests focus on individual modules in isolation. They provide quick feedback on whether specific parts of your code are functioning correctly.

Here's a simple example of a unit test in Go:

func TestCalculateSum(t *testing.T) {
    result := calculateSum(3, 4)
    expected := 7
    if result != expected {
        t.Errorf("calculateSum(3, 4) = %d; want %d", result, expected)
    }
}

Integration Testing

Integration tests examine how different modules work together. They help identify issues that might arise from interactions between various parts of the code.

End-to-End Testing

End-to-end tests simulate user interactions with the entire application. They validate that the system works as a whole, providing a user-centric view of functionality.

Remember, well-tested code is not only more reliable but also easier to refactor and maintain over time. The SOLID principles we discussed earlier can make testing easier by encouraging modular, decoupled code that is simpler to isolate and validate.

6. Take Your Time to Plan and Execute

While it might be tempting to rush through projects, especially when deadlines are tight, thoughtful planning is crucial for long-term success. Rushing can lead to technical debt and future maintenance challenges.

Take the time to carefully consider architectural decisions and plan your approach. Building a solid foundation early on will save time and effort in the long run. However, be cautious not to over-plan—find a balance that works for your project's needs.

Different projects may require varying levels of planning. A small prototype might need minimal upfront design, while a large, complex system could benefit from more extensive planning. The key is to be flexible and adjust your approach based on the project's requirements and constraints.

Conclusion

By following these principles and practices, you can become a better programmer. Focus on writing code that is clear, maintainable, and thoughtfully structured. Remember the words of Martin Fowler: "Any fool can write code that a computer can understand. Good programmers write code that humans can understand."

Becoming a better programmer is a continuous process. Don't be discouraged if you don't get everything right immediately. Keep learning, keep practicing, and most importantly, keep coding. With time and dedication, you'll see significant improvements in your skills and the quality of your work.

Here are some resources you might find helpful for further learning:

  • For general programming principles: Programming Principles
  • For understanding the importance of basics: The Basics
  • For a deep dive into software design: The Philosophy of Software Design and its discussion
  • For practical programming advice: The Pragmatic Programmer
  • For improving existing code: Refactoring

Now, armed with these principles, start applying them to your projects. Consider creating a small web API that goes beyond simple CRUD operations, or develop a tool that solves a problem you face in your daily work. Remember, the best way to learn is by doing. Happy coding!

以上是發展原則的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
Golang:Go編程語言解釋了Golang:Go編程語言解釋了Apr 10, 2025 am 11:18 AM

Go語言的核心特性包括垃圾回收、靜態鏈接和並發支持。 1.Go語言的並發模型通過goroutine和channel實現高效並發編程。 2.接口和多態性通過實現接口方法,使得不同類型可以統一處理。 3.基本用法展示了函數定義和調用的高效性。 4.高級用法中,切片提供了動態調整大小的強大功能。 5.常見錯誤如競態條件可以通過gotest-race檢測並解決。 6.性能優化通過sync.Pool重用對象,減少垃圾回收壓力。

Golang的目的:建立高效且可擴展的系統Golang的目的:建立高效且可擴展的系統Apr 09, 2025 pm 05:17 PM

Go語言在構建高效且可擴展的系統中表現出色,其優勢包括:1.高性能:編譯成機器碼,運行速度快;2.並發編程:通過goroutines和channels簡化多任務處理;3.簡潔性:語法簡潔,降低學習和維護成本;4.跨平台:支持跨平台編譯,方便部署。

SQL排序中ORDER BY語句結果為何有時看似隨機?SQL排序中ORDER BY語句結果為何有時看似隨機?Apr 02, 2025 pm 05:24 PM

關於SQL查詢結果排序的疑惑學習SQL的過程中,常常會遇到一些令人困惑的問題。最近,筆者在閱讀《MICK-SQL基礎�...

技術棧收斂是否僅僅是技術棧選型的過程?技術棧收斂是否僅僅是技術棧選型的過程?Apr 02, 2025 pm 05:21 PM

技術棧收斂與技術選型的關係在軟件開發中,技術棧的選擇和管理是一個非常關鍵的問題。最近,有讀者提出了...

如何在Go語言中使用反射對比並處理三個結構體的差異?如何在Go語言中使用反射對比並處理三個結構體的差異?Apr 02, 2025 pm 05:15 PM

Go語言中如何對比並處理三個結構體在Go語言編程中,有時需要對比兩個結構體的差異,並將這些差異應用到第�...

在Go語言中如何查看全局安裝的包?在Go語言中如何查看全局安裝的包?Apr 02, 2025 pm 05:12 PM

在Go語言中如何查看全局安裝的包?在使用Go語言開發過程中,經常會使用go...

GoLand中自定義結構體標籤不顯示怎麼辦?GoLand中自定義結構體標籤不顯示怎麼辦?Apr 02, 2025 pm 05:09 PM

GoLand中自定義結構體標籤不顯示怎麼辦?在使用GoLand進行Go語言開發時,很多開發者會遇到自定義結構體標籤在�...

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
3 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )專業的PHP整合開發工具

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境