1

I am fetching a database row inside my c# code. Row contains 3 different flags (3 columns with true or false values). Only one of these columns will be true and that will determine the type of that object. How can i determine the type of that object in one line of code. If all three flags are false then i need to have a default type.

var myObject = this.unitOfWork.myRepository.GetMeObject();

 var objectType = myObject .IsA == true
                              ? "A"
                              : myObject .IsB == true
                                    ? "B"
                                    : myObject .IsC == true
                                          ? "C"
                                          : "D";

If none of the condition is valid then ObjectType should be D

Any suggestion will be much appreciated.

Thanks

3 Answers3

5

Personally, I would be tempted to create an extension method for your database object. Something like this:

public static string GetObjectTypeOrDefault(this MyObject myObject)
{
    if(myObject.IsA) return "A";
    if(myObject.IsB) return "B";
    if(myObject.IsC) return "C";
    return "D";
}

Then you would use it like this:

MyObject myObject = this.unitOfWork.myRepository.GetMeObject();
string objectType = myObject.GetObjectTypeOrDefault();

You can even check for null objects with this method


If you still just want a single line, then you original attempt will work fine, but could be cleaned up a bit:

var objectType = myObject.IsA ? "A" : myObject.IsB ? "B" : myObject.IsC ? "C" : "D";
musefan
  • 47,875
  • 21
  • 135
  • 185
  • it definitely *is* good practice, what would you do instead when you need a split in your code execution? – Sayse Jul 12 '13 at 10:12
  • @Sayse see this http://stackoverflow.com/questions/12685675/is-it-good-practice-use-more-that-one-return-statement-in-a-method – sangram parmar Jul 12 '13 at 10:16
  • 1
    @sangramparmar: The accepted answer that you posted specifically says it is fine in short examples. Also, this is one person's opinion - where is the evidence as to why it is bad practice? – musefan Jul 12 '13 at 10:18
  • @musefan - He is suggesting that you should assign a variable and then return that variable.. Half understand that. But at the same time, I believe your code here to be more efficient as you are performing multiple checks that will be redundant if object `IsA`. I prefer efficiency, if developers are going to get confused, you can add a comment to your method. – Sayse Jul 12 '13 at 10:21
  • ok see this http://stackoverflow.com/questions/708675/why-is-it-good-practice-to-return-at-the-end-of-a-method – sangram parmar Jul 12 '13 at 10:26
  • 1
    @sangramparmar: Again, the most upvoted answer says they don't support the view and will use which ever approach is right for the job. Fact is, I don't live my life by blindy following one rule I read somewhere back in the day. I will evaluate each problem for what it is, and choose what I believe to be the best solution. I know what you are suggesting to fix my method, and I would use a "result" variable in many case where the code flow is more complex. But in this instance my approach is much simpler. Can you honestly tell me one disadvantage of what I have done here? – musefan Jul 12 '13 at 10:29
  • 1
    @sangramparmar: Also, 489 voters agree with [this answer](http://stackoverflow.com/a/36714/838807). In fact, most of the answers on that thread say how acceptable multiple return points are. Including [this example](http://stackoverflow.com/a/36839/838807) of why blindly following such a silly rule can make code hard to read – musefan Jul 12 '13 at 10:33
  • @musefan - The only thing I found to support his point was [this](http://programmers.stackexchange.com/a/118717) but this does not apply to C#, SEME pattern for me – Sayse Jul 12 '13 at 10:42
  • @Sayse: While I agree that is an example of poor use of an early return. I would say the rule there is more "make sure you clean up" rather than "early returns are bad". An example in C# where this might occur is if we open a DB connection, and return before closing it. But that is just bad decision making on the coders part. Of course we can overcome this with the `using` block. However, I would never return early while I have a connection, or stream, or something else open. I think the overall point is, the so called rule has been created to reduce the amount of bugs that bad coders create – musefan Jul 12 '13 at 10:56
  • @musefan - I agree, that article just points out that programming languages can evolve faster than the programmer – Sayse Jul 12 '13 at 11:03
0

Not quite the same, as I'm aiming here for readability rather than efficiency; this may be easier to follow for future developers of your system:

var myObject = this.unitOfWork.myRepository.GetMeObject();

string objectType = (myObject.IsA ? "A" : "") +
                    (myObject.IsB ? "B" : "") +
                    (myObject.IsC ? "C" : "");

objectType = (objectType == "") ? "D" : objectType;

Or, as a slight less readable single-line version,

var myObject = this.unitOfWork.myRepository.GetMeObject();

string objectType = (myObject.IsA ? "A" : "") +
                    (myObject.IsB ? "B" : "") +
                    (myObject.IsC ? "C" : "") +
                    ((myObject.IsA || myObject.IsB || myObject.IsC) ? "" : "D");
Adrian Wragg
  • 7,311
  • 3
  • 26
  • 50
-1

try this

            var objectType = ((myObject.IsA)
                                    ? "A"
                                    : ((myObject.IsB)
                                                    ? "B"
                                                    : ((myObject.IsC)
                                                                ? "C"
                                                                : "D")));
sangram parmar
  • 8,462
  • 2
  • 23
  • 47