开发者

Why is the software world full of status codes?

Why did programmers ever start using status codes? I mean, I guess I could imagine this might be useful back in the days when a text string was an expensive resource. WAYYY back then. But even after we had megabytes of memory to work with, we continued to use them. What 开发者_如何学编程possible advantage could there be for obfuscating the meaning of an error message or status message behind a status code?


It's easy to provide different translations of a status code. Having to look up a string to find the translation in another language is a little silly.

Besides, status codes are often used in code and typing:

var result = OpenFile(...);
if (result == "File not fond") {
    ...
}

Cannot be detected as a mistake by the compiler, where as,

var result = OpenFile(...);
if (result == FILE_NOT_FOND) {
    ...
}

Will be.


It allows for localization and changes to the text of an error message.


It's the same reason as ever. Numbers are cheap, strings are expensive, even in today's mega/gigabyte world.


I don't think status codes constitute obfuscation; it's simply an abstraction of a state.

A great use of integer status codes is in a Finite-State Machine. Having the states be integers allow for an efficient switch statement to jump to the correct code.

Integers also allow for more efficient use of bandwidth, which is still a very important issue in mobile applications.

Yet another example of integer codes over strings is for comparison. If you have similar statuses grouped together (say status 10000-10999) performing range comparisons to know the type of status is a major win. Could you imaging doing string comparisons just to know if an error code is fatal or just a warning, eww.


Numbers can be easily compared, including by another program (e.g. was there a failure). Human readable strings cannot.

Consider some of the things you might include in a string comparison, and sometimes might not:

  • Encoding options
  • Range of supported characters (compare ASCII and Unicode)
  • Case Sensitivity
  • Accent Sensitivity
  • Accent encoding (decomposed or composed unicode forms).

And that is before allowing for the majority of humans who don't speak English.


404 is universal on the web. If there were no status codes, imagine if every different piece of server software had its own error string?

  • "File not found"
  • "No file exists"
  • "Couldn't access file"
  • "Error presenting file to user"
  • "Rendering of file failed"
  • "Could not send file to client"
  • etc...

Even when we disregard data length, it's still better to have standard numeric representations that are universally understood, even when accompanied by actual error messages.


Computers are still binary machines and numeric operations are still cheaper and faster than string operations.


Integer representation in memory is a far more consistent thing than string representation. To begin with just think about all those null-terminated and Pascal strings. Than you can think of ASCII and the characters from 128 to 255 that were different according to different codepages and end up with Unicode characters and all of their little endian big endians, UTF-8 etc.

As it comes, returning an integer and having a separate resource stating how to interpret those integers is a far more universal approach.


Well when talking to a customer over the telephone a Number is much better then a string, and string can be in many different languages a number can't, try googeling some error text in lets say Swedish and then try googling it in English guess where you get the best hit.


Because not everyone speaks English. It's easier to translate error codes to multiple languages than to litter your code base with strings.

It's also easier for machines to understand codes as you can assign classes of errors to a range of numbers. E.g 1-10 are I/o issues, 11-20 are db, etc


Status codes are unique, whereas strings may be not. There is just one status code for example "213", but there may be many interpretation of for example "file not found", like "File not found", "File not found!", "Datei nicht gefunden", "File does not exist"....

Thus, status codes keep the information as simple as possible!


How about backwards compatibility with 30+ years of software libraries? After all, some code is still written in C ...

Also ... having megabytes of memory available is no justification for using them. And that's assuming you're not programming an embedded device.

And ... it's just pointless busy work for the CPU. If a computer is blindingly fast at processing strings, imagine the speed boost from efficient coding techniques.


I work on mainframes, and there it's common for applications to have every message prepended by a code (usually 3-4 letters by product, 4-5 numbers by specific message, and then a letter indicating severity of the message). And I wish this would be a standard practice on PC too.

There are several advantages aside from translation (as mentioned by others) to this:

  1. It's easy to find the message in the manual; usually, the software are accompanied with the message manual explaining all the messages (and possible solution, etc.).

  2. It's possible for automation software to react in the specific messages in the log in a specific way.

  3. It's easy to find the source of the message in the source code. You can have further error codes per specific message; in that case, this is again helpful in debugging.


For all practical purposes numbers are the best representation for statuses, even today, and I imagine would be so for a while.

The most important thing about status codes is probably conciseness and acceptance. Two different systems can talk to each other all they want using numbers but if they don't agree on what the numbers mean, it's going to be a mess. Conciseness is another important aspect as the status code must not be more verbose than the meaning it's trying to convey. The world might agree on using

The resource that you asked for in the HTTP request does not exist on this server

as a status code in lieu of 404, but that's just plain nuisance.

So why do we still use numbers, specifically the English numerals? Because that is the most concise form of representation today and all computers are built upon these.

There might come a day when we start using images, or even videos, or something totally unimaginable for representing status codes in a highly abstracted form, but we are not there yet. Not even for strings.


Make it easier for an end user to understand what is happening when things go wrong.


It helps to have a very basic method of giving statuses clearly and universally. Whereas strings can easily be typed differently depending on dialect and can also have grammatical changes, Numerals do not have grammatical formatting and do not change with dialect. There is also the storage and transfer issue, a string is larger and thus takes longer to transfer over a network and store (even if it is a few thousandths of a millisecond). Because of this, we can assign numbers as universal identifiers for statuses, for they can transfer quicker and are more punctual, and for the programmes that read them can identify them however they wish (Being multilingual).

Plus, it is easier to read computationally:

switch($status) {
case '404':
echo 'File not found!';
break;
case '500':
echo 'Broken server!';
break;
}

etc.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜