関数型プログラミング、手続き型プログラミング、オブジェクト指向プログラミングを比較してください。

オラクル株式会社がJava SE 8でいくつかの機能構造を導入したことから、最近ではほとんどの面接官が関数型プログラミングについての質問をすることに興味を持っています。Java/Groovy/Scala開発者や他の関数型プログラミング開発者として、面接での質問をクリアするために以下の質問と回答を学ぶべきです。

    1. 関数型プログラミングとは何ですか?

 

    1. 関数型プログラミングの「利点」と「欠点」は何ですか?

 

    1. 関数型プログラミングと手続き型プログラミングの違いは何ですか?

 

    1. 関数型プログラミングとオブジェクト指向プログラミングの違いは何ですか?

 

    1. 関数型プログラミングと手続き型プログラミングとオブジェクト指向プログラミングの主な違いは何ですか?

 

    1. 関数型プログラミングの主な利点は、手続き型プログラミングやオブジェクト指向プログラミングに比べて何ですか?

 

    1. いつ関数型プログラミングを使うべきですか?

 

    1. いつオブジェクト指向プログラミングを使うべきですか?

 

    1. オブジェクト指向プログラミングの欠点は何ですか?

 

    1. オブジェクト指向プログラミングの継承の欠点は何ですか?

 

    …などなど

この投稿では、順番に以下の3つの人気のあるプログラミングパラダイムについて説明し、上記の質問にも答えます。

    1. 関数型プログラミング(FP)

 

    1. 命令型プログラミング(IP)

 

    オブジェクト指向プログラミング(OOP)

関数型プログラミングは何ですか?

単純に言えば、関数型プログラミング(FP)は、状態の変更やデータの変異なしで、数学的な関数のような計算を行う、人気のあるプログラミングパラダイムの一つです。関数型プログラミングでは、関数は第一級の候補とされます。私たちは関数と不変のデータのセットを定義してプログラムを書きます。Scala、Haskell、Erlangなどは人気のある関数型プログラミング言語の例です。Java SE 8にもいくつかの関数型構造があります(詳細はJava 8の投稿を参照してください)。

FPの特徴は何ですか?

Scalaなどの関数型プログラミング言語には、以下のような特徴があります。

    1. 国家は存在しない。

 

    1. FPプログラムには状態が存在しません。つまり、すべてのデータはイミュータブルなデータであり、関数は状態を変更することはありません。

 

    1. 2. 実行順序の重要性が低い

 

    1. FP言語では、独立した関数のセットでプログラムを記述します。関数には一連のステートメントが含まれます。FPでは、関数の実行順序は重要ではありません。なぜなら、関数は状態を持たず、すべての関数が独立して動作するからです。実行順序を変更しても同じ結果が得られます。

 

    1. 3. 状態を持たないプログラミングモデル

 

    1. すべてのFPプログラムはイミュータブルなデータと関数を使用し、そのデータを変更することはできません。これは、FP言語が状態を持たないプログラミングモデルをサポートしていることを意味します。

 

    1. 4. 関数は第一級の要素です

 

    1. FP言語では、関数は第一級のオブジェクトです。関数は独立した単位であり、任意の順序で実行することができます。

 

    1. 5. 主要な操作単位

 

    1. FP言語では、主要な操作単位は関数とデータ構造です。すべてのプログラムはこれらの単位を使用して作成されます。

 

    1. 6. モジュラープログラミング

 

    1. FP言語では、状態を持たないプログラミングモデルをサポートするために、より小さく独立した単位である純粋関数を作成する必要があります。これは、FPがOOPよりも優れたモジュラリティをサポートしていることを意味します。

 

    1. 7. 高階関数と遅延評価

 

    1. 関数型プログラミング言語は、高階関数と遅延評価の機能をサポートする必要があります。

 

    1. 8. 主要な制御フロー

 

    FP言語では、For…ループ、Do…Whileループ、While…ループなどの制御フローを使用せず、また、If…ElseやSwitch文などの条件文も使用しません。すべてのFP言語は以下のものを使用してプログラムを書きます:
  • Functions
  • Function calls
  • Function calls with Recursion
    1. 抽象化、カプセル化、継承、ポリモーフィズム

 

    オブジェクト指向プログラミング(OOP)のように、関数型プログラミング(FP)言語も4つの概念、すなわち抽象化、カプセル化、継承、ポリモーフィズムをサポートしています。FP言語では、型クラスまたはインプリシットを使用して継承をサポートしています。また、ジェネリックスの助けを借りてポリモーフィズムをサポートしています。これはパラメトリックポリモーフィズムとも呼ばれています。

FPの主な焦点は何ですか? (FP no omona shōten wa nan desu ka?)

OOP言語とは異なり、全てのFP言語のプログラムは主に「あなたが何をしているか」または「何を行うべきか」に焦点を当てています。主に以下の点に重点を置いています。

  • What Information is desired that is Inputs.
  • What Transformations are required that is Actual Logic.

これは、FPは主に「何をすべきか」に焦点を当てていることを意味しています。「どのようにすべきか」にはあまり焦点を当てていません。そのため、我々は関数型プログラミングを問題領域の説明のような形で書くことができます。そのため、開発者だけでなく、他の人々もFPのコードを非常に簡単に理解することができます。今回は関数型プログラミングの「利点と欠点」について議論します。

関数型プログラミングの利点は何ですか? (Kansūgata puroguramingu no riten wa nan desu ka?)

関数型プログラミング言語には、以下の利点があります。

  • Bugs-Free Code
    As FP Languages do not support state, they don’t raise any side-effects that means we can write Error-free code or Bugs-free code or Less Error-prone Code.- Efficient Parallel Programming
    As FP Languages have NO Mutable state, they don’t raise any state-change issues. That means they use only Immutable Data. They use Independent Units to write programs that is “Functions”. We can write very efficient Parallel or Concurrent Programming because they run independently without changing state.- Better Performance
    As FP Programs compose with all Independent units, They can run Parallel or Concurrently. Because of this reason, FP Applications gain better performance.- Better Encaspulation
    Unlike OOP, FP supports better Encaspulation with Pure Functions. Pure functions means without side-effects.- Supports Nested Functions
    Nested Functions means composing functions with-in other functions to solve problems. FP supports Nested Functions.- Increase Reusability
    As FP programs are made up of Independent Units that is “Functions”, we can reuse them very easily.- Better Modularity
    In FP Languages, we need to write smaller and independent units, called Pure Functions to support Stateless Programming model. That means FP supports better Modularity than OOP.- Easy Lazy Evaluation
    In FP Languages, it is very easy to write Lazy Evaluation. They support Lazy Functional Constructs like Lazy Lists, Lazy Maps etc.- Increase Readability and Maintainability
    Functional Programming (FP) also improves Readability and Maintainability because they work Independently and they don’t change state.- Increase Testability
    As we write our FP programs using Independent Units that is “Functions”, we can unit test them very easily.- Supports Abstraction over Behavior
    Unlike OOP, FP supports both “Abstraction over Data” and “Abstraction over Behavior”. Because Real-world contains both.- Support for BigData
    As FP supports Parallel programming and better performance, FP is very good for developing BigData Applications.- Robust and Reliable Code
    As FP uses Immutable Data, we can easily develop Robust and Reliable Code using FP.

関数型プログラミングの欠点は何ですか?

素晴らしい利点に加えて、関数型プログラミング言語にはごくわずかな欠点しかありません。以下のデメリットのみが存在します。

  • Requires Lot of Memory
    FP don’t have state. They always creates new Objects to perform actions instead of modifying existing objects. Because of this, FP Applications takes lot of memory.- Does NOT concentrate on Liskov Substitution

関数型プログラミングの主要な概念は何ですか?

以下のコンセプトは、関数型プログラミングの主要かつ重要な概念です。

  • First-Class Functions.
  • Lazy Evaluation.
  • Higher-Order Functions.
  • Immutability(Non-Mutable Data).
  • Modularity.
  • No Side-effects.
  • Lazy Evaluation.
  • Recursive Function-Calls.

「インパラティブプログラミング」とは何ですか?

命令型プログラミング(IP)は、ある順序で一連の手順/命令/文を実行する人気のあるプログラミングパラダイムの一つです。IP言語の例:Java、C、C++など。

命令形プログラミングの主な特徴は何ですか?

どんな命令形プログラミング言語(IP)でも、以下の特徴を含むことができます。

  • Sequence of Statements.
  • Order of execution of Statements is very important.
  • They contain state.
  • They use both Immutable and Mutable Data.
  • They can change state.
  • They may have Side-effects.
  • Stateful Programming Model.
  • They directly change the state of Program.
  • They represent state with Data Fields.

オブジェクト指向プログラミングとは何ですか?

オブジェクト指向プログラミングは、別の種類のプログラミングパラダイムです。すべてをオブジェクトとして表現します。各オブジェクトにはデータフィールドとメソッドが含まれています。すべてのオブジェクト指向プログラムには状態があります。変更可能なデータとデータ構造を使用します。関数型プログラミングのように、イミュータブルなデータを使用して完全なプログラミングができますが、このルールは強制されません。オブジェクト指向プログラミング(OOP)は命令型プログラミングのスーパーセットです。IPのすべての特徴を備え、いくつかの追加機能も持っています。

  • Everything is an Object.
  • Each Object contains Some Data Fields and Methods.
  • OOPs Concepts: Abstraction,Encaspulation,Inheritance and Polymorphism

関数型プログラミング言語とは異なり、オブジェクト指向言語は主に「どのように行われるか」に焦点を当てています。つまり、開発者としては「あなたがどのように行っているか」に注目します。さらに、オブジェクト指向は「あなたが何をしているか」と「どのように行っているか」の両方を結びつけています。そのため、簡潔で読みやすいコードを書くことができません。コードを理解することができるのは開発者だけであり、他の人はアプリケーションのコードを理解する際に混乱を招くことがあります。彼らは理解できないでしょう。

オブジェクト指向プログラミング(OOP)のデメリットは何ですか?

OOPは多くのリアルタイムの問題を解決するものの、以下のような欠点もあります(FPと比較した場合)。

  • It does not support full Reusability.
  • It is not fully Modularity.
  • It breaks Encaspulation concept.
  • Inheritance has lot of drawbacks.

継承の主なデメリット:

  • Breaks Encapsulation principle
  • When Inheritance levels increases, it is very tough and hard to maintain and create objects.

関数型プログラミングはいつ使うべきですか?

次のシナリオでは、関数型プログラミング(FP)を選択すべきです。

  • When we are going to be performing lots of different operations on the data that has fixed.
  • In other words, when we have few things with more operations.

オブジェクト指向プログラミングはいつ利用するのですか? (Obujekuto shikō puroguramingu wa itsu riyou suru nodesu ka?)

以下のシナリオでは、オブジェクト指向プログラミング(OOP)を選択すべきです。

  • When we are going to be performing few operations on lots of different variants which have common behavior.
  • In other words, when we have more things with few operations.

以下の内容を日本語で言い換えます。オプションは1つだけ必要です。
注意:ここでの「Things」とは実際のオブジェクトを指し、「operations」とは実際の行動を意味します。例えば、Javaではこれらの実際のオブジェクトを「クラス」とし、実際の行動を「メソッド(操作)」として表現します。

FP(関数型プログラミング)とOOP(オブジェクト指向プログラミング)(IP)の違いは何ですか?

Functional Programming OOP
Does not exist State Exists State
Uses Immutable data Uses Mutable data
It follows Declarative Programming Model It follows Imperative Programming Model
Stateless Programming Model Stateful Programming Model
Main Fcous on: “What you are doing” Main focus on “How you are doing”
Good for Parallel (Concurrency) Programming Poor for Parallel (Concurrency) Programming
Good for BigData processing and analysis NOT Good for BigData processing and analysis
Supports pure Encaspulation It breaks Encaspulation concept
Functions with No-Side Effects Methods with Side Effects
Functions are first-class citizens Objects are first-class citizens
Primary Manipulation Unit is “Function” Primary Manipulation Unit is Objects(Instances of Classes)
Flow Controls: Function calls, Function Calls with Recursion Flow Controls: Loops, Conditional Statements
It uses “Recursion” concept to iterate Collection Data. It uses “Loop” concept to iterate Collection Data. For example:-For-each loop in Java
Order of execution is less importance. Order of execution is must and very important.
Supports both “Abstraction over Data” and “Abstraction over Behavior”. Supports only “Abstraction over Data”.
We use FP when we have few Things with more operations. We use OOP when we have few Operations with more Things. For example: Things are classes and Operations are Methods in Java.

これは、3つの人気のあるプログラミングパラダイムについてのすべてです。注意:私は基本的にOOP出身ですが、FPについては1年前から取り組み始めました。したがって、関数型プログラミングの専門家がこの投稿に間違いを見つけた場合は、貴重なご意見をいただきたいです。もし私の投稿が気に入った場合や疑問や提案がある場合は、コメントをお願いします。

コメントを残す 0

Your email address will not be published. Required fields are marked *