mowwwalker
mowwwalker

Reputation: 17362

File Data inside of source code?

I'm working on a class for managing STFS files, and some of the data I need is hundreds of bytes long. I'm wondering if there's anything wrong with creating a byte array with that information in the source code instead of reading it from a file. Like doing:

byte[] data = new byte[0x100]{0x23,0x55,0xFF...

I would think that you're creating a byte array no matter what you do, but I've never seen any code like that, so I thought it might be wrong, or not done for readability purposes.

Upvotes: 0

Views: 150

Answers (2)

Alexei Levenkov
Alexei Levenkov

Reputation: 100547

It is ok if data is relatively small and will not ever change (ever = singnificantly less often then source code).

Conisder following when making the decision:

  • do you need to comment data in the source (i.e. magicHeader[3]=42;// the answer).
  • is it relatively small (I'd say 256 entries is max)
  • does it ever change (i.e. signatures for existing file formats GIF - 0x47,0x49, 0x46, 0x38, 0x37, 0x61 are essentially set in stone and will not ever change - see more on magic numbers in http://en.wikipedia.org/wiki/Magic_number_(programming)# )

For large constant blobs embedding into an application as a resource may be better approach.

Upvotes: 1

Matthias
Matthias

Reputation: 16209

If it is about validation (e.g., headers) of files you read, then I think it is okay, to declare the values in a byte array. But be sure, that you have only one instance of that in your whole application.

For instance:

static class STFSValidation
{
     public static readonly byte[] Header = new byte[] { ... };
}

Upvotes: 1

Related Questions