Skip to main content
Code Review

Return to Question

replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

From the related questions here, I've found this this and this this answers, both of which suggest Replacing Conditional with Polymorphism, are probably what I'm looking for. However, I'm not sure if polymorphism would be a good choice for my particular case. Below are the relevant code snippets (sorry they're incomplete).

From the related questions here, I've found this and this answers, both of which suggest Replacing Conditional with Polymorphism, are probably what I'm looking for. However, I'm not sure if polymorphism would be a good choice for my particular case. Below are the relevant code snippets (sorry they're incomplete).

From the related questions here, I've found this and this answers, both of which suggest Replacing Conditional with Polymorphism, are probably what I'm looking for. However, I'm not sure if polymorphism would be a good choice for my particular case. Below are the relevant code snippets (sorry they're incomplete).

Fixed typos
Source Link
int length = data.Length;
switch(code)
{
 case Code.A:
 case Code.E:
 case Code.F:
 case Code.G:
 QR = data[0];
 if (code == Code.F)
 // set another property
 break;
 case Code.B:
 if (length < 13)
 throw new ArgumentException();
 Index = data[0]; 
 break;
 case Code.C:
 int content_length = data[1];
 if (length < content_length + 30)
 throw new ArgumentException();
 Flag = (ContentFlag)data[0];
 ContentLength = data[1];
 if (Flag & ContentFlag.FlagA == ContentFlag.FlagA) 
 {
 Content = new byte[content_length];
 Array.Copy(data, 2, Content, 0, content_length);
 }
 if (Flag & ContentFlag.FlagB == ContentFlag.FlagB) 
 {
 // set property
 }
 // 5 more flags
 break;
 case Code.D:
 int genre = data[2];
 int genre_length = 0;2;
 switch (genre) 
 {
 case Genre.TypeA:
 genre_length += 4;
 break;
 case Genre.TypeB:
 genre_length += 8;
 break;
 case Genre.TypeC:
 genre_length += 1;
 break;
 case Genre.TypeD:
 genre_length += 15;
 break;
 }
 if (length != genre_length)
 throw new ArgumentException();
 Array.Copy(data, 0, Target, 0, 2);
 switch (genre) 
 {
 case Genre.TypeA:
 GenreID = BitConverter.ToUInt32(data, 02);
 break;
 case Genre.TypeB:
 GenreID = BitConverter.ToUInt64(data, 02);
 break;
 case Genre.TypeC:
 GenreID = data[0];data[2];
 break;
 case Genre.TypeD:
 GenreID = ASCIIEncoding.ASCII.GetString(data, 02, 15);
 break;
 }
 break;
default:
 throw new ArgumentException();
int length = data.Length;
switch(code)
{
 case Code.A:
 case Code.E:
 case Code.F:
 case Code.G:
 QR = data[0];
 if (code == Code.F)
 // set another property
 break;
 case Code.B:
 if (length < 13)
 throw new ArgumentException();
 Index = data[0]; 
 break;
 case Code.C:
 int content_length = data[1];
 if (length < content_length + 30)
 throw new ArgumentException();
 Flag = (ContentFlag)data[0];
 ContentLength = data[1];
 if (Flag & ContentFlag.FlagA == ContentFlag.FlagA) 
 {
 Content = new byte[content_length];
 Array.Copy(data, 2, Content, 0, content_length);
 }
 if (Flag & ContentFlag.FlagB == ContentFlag.FlagB) 
 {
 // set property
 }
 // 5 more flags
 break;
 case Code.D:
 int genre = data[2];
 int genre_length = 0;
 switch (genre) 
 {
 case Genre.TypeA:
 genre_length += 4;
 break;
 case Genre.TypeB:
 genre_length += 8;
 break;
 case Genre.TypeC:
 genre_length += 1;
 break;
 case Genre.TypeD:
 genre_length += 15;
 break;
 }
 if (length != genre_length)
 throw new ArgumentException();
 switch (genre) 
 {
 case Genre.TypeA:
 GenreID = BitConverter.ToUInt32(data, 0);
 break;
 case Genre.TypeB:
 GenreID = BitConverter.ToUInt64(data, 0);
 break;
 case Genre.TypeC:
 GenreID = data[0];
 break;
 case Genre.TypeD:
 GenreID = ASCIIEncoding.ASCII.GetString(data, 0, 15);
 break;
 }
 break;
default:
 throw new ArgumentException();
int length = data.Length;
switch(code)
{
 case Code.A:
 case Code.E:
 case Code.F:
 case Code.G:
 QR = data[0];
 if (code == Code.F)
 // set another property
 break;
 case Code.B:
 if (length < 13)
 throw new ArgumentException();
 Index = data[0]; 
 break;
 case Code.C:
 int content_length = data[1];
 if (length < content_length + 30)
 throw new ArgumentException();
 Flag = (ContentFlag)data[0];
 ContentLength = data[1];
 if (Flag & ContentFlag.FlagA == ContentFlag.FlagA) 
 {
 Content = new byte[content_length];
 Array.Copy(data, 2, Content, 0, content_length);
 }
 if (Flag & ContentFlag.FlagB == ContentFlag.FlagB) 
 {
 // set property
 }
 // 5 more flags
 break;
 case Code.D:
 int genre = data[2];
 int genre_length = 2;
 switch (genre) 
 {
 case Genre.TypeA:
 genre_length += 4;
 break;
 case Genre.TypeB:
 genre_length += 8;
 break;
 case Genre.TypeC:
 genre_length += 1;
 break;
 case Genre.TypeD:
 genre_length += 15;
 break;
 }
 if (length != genre_length)
 throw new ArgumentException();
 Array.Copy(data, 0, Target, 0, 2);
 switch (genre) 
 {
 case Genre.TypeA:
 GenreID = BitConverter.ToUInt32(data, 2);
 break;
 case Genre.TypeB:
 GenreID = BitConverter.ToUInt64(data, 2);
 break;
 case Genre.TypeC:
 GenreID = data[2];
 break;
 case Genre.TypeD:
 GenreID = ASCIIEncoding.ASCII.GetString(data, 2, 15);
 break;
 }
 break;
default:
 throw new ArgumentException();
Added more information
Source Link

Basically, I have a class that takes in a byte array and a code. Based on the code, I am setting properties inof the class. There will be more than 20 cases in total, each case may or may not have precondition verifications and may or may not be handled differently, as shown in the snippets. I I feel this is already getting pretty hard to read and maintain, so I might end up with 20 implementation classes if I use polymorphism. What would the best way to refactor this?

Update

I guess my main concern is that I may have too many implementation classes if I use one for each case (except for the common ones which is about 10-15% of all cases), not to mention there'll likely be more codes added with other ways of being handled, and it doesn't make sense to not put them in one namespace...or is this not a problem at all?

Basically, I have a class that takes in a byte array and a code. Based on the code, I am setting properties in the class. There will be more than 20 cases in total, each case may or may not be handled differently, as shown in the snippets. I feel this is already getting pretty hard to read and maintain, so I might end up with 20 implementation classes if I use polymorphism. What would the best way to refactor this?

Basically, I have a class that takes in a byte array and a code. Based on the code, I am setting properties of the class. There will be more than 20 cases in total, each case may or may not have precondition verifications and may or may not be handled differently, as shown in the snippets. I feel this is already getting pretty hard to read and maintain, so I might end up with 20 implementation classes if I use polymorphism. What would the best way to refactor this?

Update

I guess my main concern is that I may have too many implementation classes if I use one for each case (except for the common ones which is about 10-15% of all cases), not to mention there'll likely be more codes added with other ways of being handled, and it doesn't make sense to not put them in one namespace...or is this not a problem at all?

edited title
Link
BCdotWEB
  • 11.4k
  • 2
  • 28
  • 45
Loading
Edit title as suggested.
Link
Loading
Source Link
Loading
lang-cs

AltStyle によって変換されたページ (->オリジナル) /