1

I want to know which one is better in programming

for Example I want to have Rijndael & TripleDES Encryptions in my project

I have 2 ways :

1 . Create an encryption class and have 4 methods in this

class Encryption

  • .EncryptWithRijndael()
  • .DecryptWithRijndael()
  • .EncryptWithTripleDES()
  • .DecryptWithTripleDES()

2 . Create 4 Extension Methods for String class

  • "ABC".EncryptWithRijndael()
  • "XYZ".EncryptWithTripleDES()

Encryption is only an example not my problem I want to know When use a new class When use an extension method for same purpose ?

Which one is better in OOP ? (or even both), Why ?

Hamed F
  • 41
  • 2
  • 1
    Make class for it.Because it has it's own logic.It's purpose is ecnryption and decryption – Suren Srapyan Nov 13 '15 at 08:17
  • New class for encryption. Use binary serialization to en/decrypt, like GZipStream is used, or maybe take ideas [from here](http://stackoverflow.com/a/273499/344541) – Kay Zed Nov 13 '15 at 08:32
  • @Kay Zed , I dont have any problem for coding , my issue is choose, When use new class When extension methods ? – Hamed F Nov 13 '15 at 08:35
  • Extension methods are hard to test, so maybe because of that you should implement that logic in its own class. I do use extension methods but I try to use them for conversions only/ – MeTitus Nov 13 '15 at 08:40
  • Use extension methods for the most straightforward things. If you have to configure options, want to reuse options, need other resources (IDisposable things), have intermediate steps etc. (in short, _state_ related) build a class. – Kay Zed Nov 13 '15 at 08:48
  • 1
    I think an extension method should have a strong logical connection to the class. The .net extensions for `IEnumerable` are good examples. `string` is too generic for a strong link to encryption, even in an application with a narrow focus on it. If you define a class which contains a string which holds user input specfically made for encryption though, then yes. – Peter - Reinstate Monica Nov 13 '15 at 08:56

2 Answers2

1

To answer your question it is mostly a matter of taste for the specific programmer.

BUT

If you think that also other programmers need to use the program think about it how easy and self explaining things are to them.

If we take your example there as base. If you add an encrypt/decrypt to string does that look natural? Normally I wouldn't expect the string class to have such a method. Thus no.

Would its own class look natural? In this case yes.

Thus to sum it up: Look at what looks natural. Natural is that all things that are thematically related (stronlgy related as encryption/decryption is only weakly related to text itself) are in a single class. And if these methods extend the functionality of that class while being thematically linked they are candidates for extension methods.

As example lets take int. If you have a addTogether method that has 3 values but is not stronger related to a specific workflow than it is to int then that could be a candidate for an extension class for int. Else it would be in a class named "Calculations" for example and there as a normal method.

Thomas
  • 2,886
  • 3
  • 34
  • 78
1

TLDR:

In OOP, a class is better than an extension method.

The first reason is that a class and its methods are natively "Object Oriented" (OO). You can use inheritance, polymorphism and other OO stuff like that.

An extension method is a static method. A static method is not OO because you cannot use any OO stuff. And it is thread unsafe (many threads could access the same method and return different results, or worse, cause a deadlock).

Moreover, a extension method does not take precedence over instance method. For example, you cannot write a ToString extension method for string type. Well, you can, but it would never be called :

public static string ToString(this string input)
{
    return input + " whatever you want to do...";
}
Dude Pascalou
  • 2,989
  • 4
  • 29
  • 34