Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this redundant code?

I'm upgrading a system and am going through another developers code (ASP.NET in C#).

I came across this:

private ReferralSearchFilterResults ReferralsMatched
{
    get
    {
        if (Session[SESSION_REFERRAL_SEARCHFILTERRESULTS] == null || Session[SESSION_REFERRAL_SEARCHFILTERRESULTS].GetType() != typeof(ReferralSearchFilterResults))
            return null;
        else
            return (ReferralSearchFilterResults)Session[SESSION_REFERRAL_SEARCHFILTERRESULTS];
    }
    set
    {
        if (value == null)
        {
            Session[SESSION_REFERRAL_SEARCHFILTERRESULTS] = value;
        }
        else if (value.GetType() == typeof(ReferralSearchFilterResults))
        {
            Session[SESSION_REFERRAL_SEARCHFILTERRESULTS] = value;
        }
    }

}

Is checking the type on the setter unnecessary? Surely, if I set the property to something other than a ReferralSearchFilterResults object, the code wouldn't even compile? Am I missing something or am I right to think this can be achieved just by using:

set
{
    Session[SESSION_REFERRAL_SEARCHFILTERRESULTS] = value;
}
like image 921
Jamie Avatar asked Nov 25 '10 14:11

Jamie


People also ask

What is a redundant code?

Code redundancy occurs when a character or group of characters in a code word can be partially or completely deduced from the remaining characters of the code word.

How do I remove a redundant code?

Go to the cleanup profiles settings page: Code Editing | Code Cleanup | Profiles. Create a new profile as described in the Create a new custom cleanup profile section. In the Selected profile settings section for the new profile, tick the Remove code redundancies checkbox.

What is redundancy in line coding?

A good redundancy detects faults by having two independent paths to the same result. A failure in one path does not affect the other, so the discrepancy in result indicates an error.

Is functions reduce redundant code?

Besides simplifying long sections of code, functions are also regularly used to reduce redundancy in code, similar to loops. Using functions, we can take code that is repeated in multiple locations, and keep it in one centralized location.


2 Answers

The original code prevents any subclasses of ReferralSearchFilterResults from being set or get to or from the property. This is because value.GetType() will return the actual Type of the object referenced by value. If that Type is a subclass of ReferralSearchFilterResults, then it will not equals typeof(ReferralSearchFilterResults).

I'm not sure of your context here, so I can't tell you whether that's correct behaviour or not. If it's intended behaviour, it does smell a bit dirty as it will silently ignore any assignments of subclasses. But I can't really judge without more context.

like image 110
Daniel Chambers Avatar answered Oct 08 '22 23:10

Daniel Chambers


I think you're right - the setter shouldn't compile if provided with something of that cannot be implicitly cast to a ReferralSearchFilterResults.

like image 45
Jackson Pope Avatar answered Oct 08 '22 21:10

Jackson Pope