1

I have a method which gets called every 100 ms to save some data to the file. The input to this method is byte array, it's a message and message has a type.

private FileStream _fStream;
public void SaveData(byte[] data)
{
    try
    {
        int type = GetTypeOfData(data);
        switch (type)
        {
            case 0:
                // do something
                break;
            case 2:
                SaveDataToFile(data);
                break;
            case 1:
                _fStream = File.Create(fileName);
                break;
        }
    }
    catch (Exception ex)
    {
        // log error
    }
}

private void SaveDataToFile(byte[] data)
{
    if (_fStream != null && _fStream.CanWrite)
    {
         _fStream.Write(data, 0, data.Length);
    }
}

The question is do I need to check whether file stream is null or not or whether it can write or not every time if (_fStream != null && _fStream.CanWrite), I have a try/catch already in SaveData method ? What are the performance issues with that check ?

9
  • 3
    Why not just measure performance with profiler? Also in my opinion _fStream != null && _fStream.CanWrite is fast enough Commented Aug 28, 2013 at 9:25
  • 1
    checking for null has a negligible performance issue, but try { } catch { } on the other hand, is quite expensive. If you can avoid a try { } catch { } with a o != null, then do it. Commented Aug 28, 2013 at 9:25
  • I totally agree with Nikolay. Commented Aug 28, 2013 at 9:26
  • 1
    Are you experiencing some kind of a performance issue? Commented Aug 28, 2013 at 9:26
  • 1
    The Write() call is at least two orders of magnitude more expensive than the two bool checks. You'll never notice the difference if you remove them. You should nevertheless remove them, they are a serious code smell. You never want to hide a serious protocol violation or a completely borken file system. Commented Aug 28, 2013 at 10:01

2 Answers 2

1

if (_fStream != null && _fStream.CanWrite), I have a try/catch already in SaveData method? What are the performance issues with that check ?

As a rule of thumb, you should always try to handle the normal flows of the program. Exception handling should be done only for exceptional situations. And you should note that Exception throwing is a costly operation.

If you compare the performance of check and the cost of throwing the exception than definitely it is better to implement the check.

Though, if it is very rare that _fstream is null or _fstream cannot write than the performance of try catch can be compensated. But still it isn't a good way to program.

Sign up to request clarification or add additional context in comments.

4 Comments

is it still costly even if the exception is not thrown? Or does it only become costly once an exception is thrown? What if the chance of the exception to occur is like 1 out of 10000? Would your point still hold valid?
people work with odds, computers don't. Even is the chance is 1 in 10 billion, you should always write the check. It's not a big effort and makes your code safer. When I write code using GUIDs, I even check for equality even if the chances are more remote than our solar system being wiped out by a rogue supernova shockwave :P Point is, never compromise on code safety.
I have a thorough Python background where this is frowned upon, so was just wondering how the 'C# people' look at this matter :-)
I agree with No One! If your method lives in framework upon which other apps are written then either you shouldn't handle exception at all or at least you should rethrow it to upper levels. Consumer of your app should know that some exception is occured. Else, you should handle exception.
0

You should refactor your code so that is impossible to call SaveData with type == 2 if the stream has not been successfully created and _fStream assigned. Thus you will not have to worry about _fStream being null. You will still need a try...catch around SaveDataToFile, because file writes can fail for all sorts of reasons (out of disk space, other locks on the file etc).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.