CodingC# .NET. Which makes more sense?

 

Press Ctrl+Enter to quickly submit your post
Quick Reply  
 
 
  
 From:  Mikee  
 To:  ALL
35502.1 
Two pretty simple bits of code. The top one is mine, the bottom one is a version that someone was actually pedantic enough to email me with to tell me to correct my work.

What angered me the most is probably that his doesn't even seem to be logical.. The setter seems redundant. Am I missing something??

c# code:
 
private bool? _isDataFeed;
public bool isDataFeed
{
    get
    {
	if (_isDataFeed == null)
	{
	    string header = Request.Headers["X-Requested-With"];
            _isDataFeed = (!String.IsNullOrEmpty(header) && header.ToLower() == "xmlhttprequest");
 
	}
	return (bool)_isDataFeed;
    }
    set
    {
	_isDataFeed = value;
    }
}
 



c# code:
 
private bool _isDataFeed;
public bool isDataFeed
{
    get
    {
	string header = Request.Headers["X-Requested-With"];
	_isDataFeed = (!String.IsNullOrEmpty(header) && header.ToLower() == "xmlhttprequest");
	return _isDataFeed;
    }
    set
    {
	_isDataFeed = value;
    }
}
 
0/0
 Reply   Quote More 

 From:  Rowan  
 To:  Mikee     
35502.2 In reply to 35502.1 

Assuming that Request.Headers["X-Requested-With"] can change (and I would assume it can), the two are functionally quite different.

 

And, yes, the setter is redundant in the second one.

0/0
 Reply   Quote More 

 From:  THERE IS NO GOD BUT (RENDLE)  
 To:  Mikee     
35502.3 In reply to 35502.1 
Yes, they're different. Yours makes more sense if you only want to check once. His makes sense if you need to check every time, although obviously the setter and private member are redundant.

To get pedantic with yours:

c# code:
private bool? _isDataFeed;
public bool isDataFeed
{
    get
    {
	if (!_isDataFeed.HasValue)
	{
	    string header = Request.Headers["X-Requested-With"];
            _isDataFeed = ((!String.IsNullOrEmpty(header)) && header.Equals("xmlhttprequest", StringComparison.InvariantCultureIgnoreCase));
 
	}
	return _isDataFeed.Value;
    }
    set
    {
	_isDataFeed = value;
    }
}


Notes: Parentheses around !String.IsNullOrEmpty check, otherwise the bang negates the entire condition; Using Nullable<T>.Value and HasValue properties saves unnecessary boxing and unboxing; using String.Equals for case-insensitive comparison saves creating string instances (which is expensive because String is a class, not a struct).

0/0
 Reply   Quote More 

 From:  Rowan  
 To:  THERE IS NO GOD BUT (RENDLE)     
35502.4 In reply to 35502.3 

My, someone's been reading Effective C#. Or, in fact, probably More Effective C#.

 

Good books. We have them in our lounge, somewhere.

0/0
 Reply   Quote More 

 From:  THERE IS NO GOD BUT (RENDLE)  
 To:  Rowan     
35502.5 In reply to 35502.4 
No, I'm just great.

0/0
 Reply   Quote More 

 From:  Rowan  
 To:  THERE IS NO GOD BUT (RENDLE)     
35502.6 In reply to 35502.5 
Well, that's handy for you, I suppose. Me too, as it happens, but I like to pretend I get it from books, so I don't look like a nob. Each to their own, though.
0/0
 Reply   Quote More 

Reply to All    
 

1–6

Rate my interest:

Adjust text size : Smaller 10 Larger

Beehive Forum 1.5.2 |  FAQ |  Docs |  Support |  Donate! ©2002 - 2024 Project Beehive Forum

Forum Stats