開発原則

Sep 03, 2024 am 11:41 AM

Development Principles

プログラミングは継続的な改善の旅です。経験を積むにつれて、技術を磨き、より高品質なコードを生み出すのに役立つ実践や原則に出会うことができます。この記事では、SOLID 原則、デザイン パターン、コーディング標準など、より優れたプログラマーになるために役立つ主要な原則と実践方法について説明します。これらの概念が開発プロセスをどのように改善できるかを探っていきます。

例に関するメモ: コード例として Go を選択したのは、そのシンプルさと読みやすさのためです。Go は「実行可能な疑似コード」とよく言われます。 Go に詳しくなくても、心配しないでください。これは学ぶ絶好の機会です。

馴染みのない構文や概念に遭遇した場合は、時間をかけて調べてください。この発見のプロセスは、学習体験に楽しいボーナスとなる可能性があります。ここで説明する原則はさまざまなプログラミング言語に適用されるため、Go 構文の詳細ではなく概念を理解することに重点を置いてください。

1. プログラミング標準を受け入れる

より優れたプログラマーになるための最初のステップの 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 (Keep It Simple、Stupid) や YAGNI (You Ain't Gonna Need It) のような、より一般的なものもあります。
これらの一般原則の中で、SOLID 原則は最も影響力のある原則の 1 つです。コードをどのように改善できるかに焦点を当てて、それぞれの原則を見ていきましょう。

単一責任原則 (SRP)

この原則は、明確に定義された単一の目的を持ってコンポーネント (関数、クラス、またはモジュール) を設計することを奨励します。コンポーネントに複数の責任がある場合、理解、テスト、保守が難しくなります。

SRP に準拠するように関数をリファクタリングする例を見てみましょう。

// Before: A function doing too much
func processOrder(order Order) error {
    // Validate order
    if order.Total <= 0 {
        return errors.New("invalid order total")
    }
    // Save to database
    db.Save(order)
    // Send confirmation email
    sendEmail(order.CustomerEmail, "Order Confirmation", orderDetails(order))
    // Update inventory
    for _, item := range order.Items {
        updateInventory(item.ID, item.Quantity)
    }
    return nil
}
ログイン後にコピー

この関数は、注文の検証、データベースへの保存、電子メールの送信、在庫の更新など、無関係な複数のタスクを実行します。これを、それぞれが 1 つの責任を持つ個別の関数に分割してみましょう。

// 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 <= 0 {
        return errors.New("invalid order total")
    }
    return nil
}

func saveOrder(order Order) error {
    return db.Save(order)
}

func sendOrderConfirmation(order Order) error {
    return sendEmail(order.CustomerEmail, "Order Confirmation", orderDetails(order))
}

func updateInventoryForOrder(order Order) error {
    for _, item := range order.Items {
        if err := updateInventory(item.ID, item.Quantity); err != nil {
            return err
        }
    }
    return nil
}
ログイン後にコピー

このリファクタリングされたバージョンでは:

  • 各機能には単一の明確な責任があります。
  • コードはよりモジュール化されており、テストが簡単です。
  • 個々のコンポーネントは、他のコンポーネントに影響を与えることなく再利用または変更できます。

信じてください、将来のあなたはこのレベルの組織に感謝するでしょう。

オープンクローズド原則 (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 中国語 Web サイトの他の関連記事を参照してください。

このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

Video Face Swap

Video Face Swap

完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

ホットツール

メモ帳++7.3.1

メモ帳++7.3.1

使いやすく無料のコードエディター

SublimeText3 中国語版

SublimeText3 中国語版

中国語版、とても使いやすい

ゼンドスタジオ 13.0.1

ゼンドスタジオ 13.0.1

強力な PHP 統合開発環境

ドリームウィーバー CS6

ドリームウィーバー CS6

ビジュアル Web 開発ツール

SublimeText3 Mac版

SublimeText3 Mac版

神レベルのコード編集ソフト(SublimeText3)

Debian OpenSSLの脆弱性は何ですか Debian OpenSSLの脆弱性は何ですか Apr 02, 2025 am 07:30 AM

OpenSSLは、安全な通信で広く使用されているオープンソースライブラリとして、暗号化アルゴリズム、キー、証明書管理機能を提供します。ただし、その歴史的バージョンにはいくつかの既知のセキュリティの脆弱性があり、その一部は非常に有害です。この記事では、Debian SystemsのOpenSSLの共通の脆弱性と対応測定に焦点を当てます。 Debianopensslの既知の脆弱性:OpenSSLは、次のようないくつかの深刻な脆弱性を経験しています。攻撃者は、この脆弱性を、暗号化キーなどを含む、サーバー上の不正な読み取りの敏感な情報に使用できます。

フロントエンドからバックエンドの開発に変身すると、JavaやGolangを学ぶことはより有望ですか? フロントエンドからバックエンドの開発に変身すると、JavaやGolangを学ぶことはより有望ですか? Apr 02, 2025 am 09:12 AM

バックエンド学習パス:フロントエンドからバックエンドへの探査の旅は、フロントエンド開発から変わるバックエンド初心者として、すでにNodeJSの基盤を持っています...

Beego ormのモデルに関連付けられているデータベースを指定する方法は? Beego ormのモデルに関連付けられているデータベースを指定する方法は? Apr 02, 2025 pm 03:54 PM

Beegoormフレームワークでは、モデルに関連付けられているデータベースを指定する方法は?多くのBEEGOプロジェクトでは、複数のデータベースを同時に操作する必要があります。 Beegoを使用する場合...

GOの浮動小数点番号操作に使用されるライブラリは何ですか? GOの浮動小数点番号操作に使用されるライブラリは何ですか? Apr 02, 2025 pm 02:06 PM

GO言語の浮動小数点数操作に使用されるライブラリは、精度を確保する方法を紹介します...

Go's Crawler Collyのキュースレッドの問題は何ですか? Go's Crawler Collyのキュースレッドの問題は何ですか? Apr 02, 2025 pm 02:09 PM

Go Crawler Collyのキュースレッドの問題は、Go言語でColly Crawler Libraryを使用する問題を調査します。 �...

Golandのカスタム構造ラベルが表示されない場合はどうすればよいですか? Golandのカスタム構造ラベルが表示されない場合はどうすればよいですか? Apr 02, 2025 pm 05:09 PM

Golandのカスタム構造ラベルが表示されない場合はどうすればよいですか?ゴーランドを使用するためにGolandを使用する場合、多くの開発者はカスタム構造タグに遭遇します...

Redisストリームを使用してGO言語でメッセージキューを実装する場合、user_idタイプの変換の問題を解決する方法は? Redisストリームを使用してGO言語でメッセージキューを実装する場合、user_idタイプの変換の問題を解決する方法は? Apr 02, 2025 pm 04:54 PM

redisstreamを使用してGo言語でメッセージキューを実装する問題は、GO言語とRedisを使用することです...

DebianでMongoDB自動拡張を構成する方法 DebianでMongoDB自動拡張を構成する方法 Apr 02, 2025 am 07:36 AM

この記事では、自動拡張を実現するためにDebianシステムでMongodbを構成する方法を紹介します。主な手順には、Mongodbレプリカセットとディスクスペース監視のセットアップが含まれます。 1。MongoDBのインストール最初に、MongoDBがDebianシステムにインストールされていることを確認してください。次のコマンドを使用してインストールします。sudoaptupdatesudoaptinstinstall-yymongodb-org2。mongodbレプリカセットMongodbレプリカセットの構成により、自動容量拡張を達成するための基礎となる高可用性とデータ冗長性が保証されます。 Mongodbサービスを開始:Sudosystemctlstartmongodsudosys

See all articles