| « Speed up ORDER BY queries in MySQL | cjpeg - Targa (tga) to JPEG (jpg) - simpler, faster but not dirty - the quality is excellent ! » |
Having to deal with a medium application and up to a huge application will increase your need for error coding. I'm convinced that error messages are not enough. For my former job I've wrote an error coding strategy.
The error code should be simple enough to be forwarded by any well intentioned user. That means at most 10 characters, not fancy hex stuff like '0x26F34A34D5' because this scares any user... I opted to use only numbers. For this I've stopped at 8 digits.
CLASS METHOD CONTEXT
xyz klm ab
The first 3 digits will indicate the class the error/exception occurred. The next 3 digits will indicate the method where the error/exception occurred. The last 2 digits the error/exception within the method starting from 01 or 00... adjust by preference. (If you need to return/throw more than 100 errors/exceptions in a method then... there is definitely something wrong with your method.)
How for the first 2 groups of 3 digits each...
Follow up:
Assuming that all code is written in English, I've took an English dictionary and imported it into a MySQL table. Then issued against that table a query like:
SELECT LENGTH(word) AS WordLength, COUNT(*) AS Freq FROM SampleDict GROUP BY LENGTH(word) ORDER BY Freq
I have removed the small frequency values... 20% from the total number of rows. Then I issued a query like this:
SELECT LEFT(word, 1) AS Letter, COUNT(*) AS Freq FROM SampleDict GROUP BY LEFT(word, 1) ORDER BY Letter
Turn the frequency in percentages and for each letter distribute enough error codes for each letter. Here is what came up:
+--------+-------------+---------------+-------------------+ | Letter | 1 Char Code | 2 Char Code | Full 3 char code | +--------+-------------+---------------+-------------------+ | A | 1 | 10 -> 14 | 100 -> 149 | | B | 1 | 15 -> 19 | 150 -> 199 | +--------+-------------+---------------+-------------------+ | C | 2 | 20 -> 26 | 200 -> 269 | | D | 2 | 27 -> 29 | 270 -> 299 | +--------+-------------+---------------+-------------------+ | E | 3 | 30 -> 32 | 300 -> 329 | | F | 3 | 33 -> 35 | 330 -> 359 | | G | 3 | 36 -> 39 | 360 -> 399 | +--------+-------------+---------------+-------------------+ | H | 4 | 40 -> 42 | 400 -> 429 | | I | 4 | 43 | 430 -> 439 | | J | 4 | 44 | 440 -> 449 | | K | 4 | 45 | 450 -> 459 | | L | 4 | 46 -> 49 | 460 -> 499 | +--------+-------------+---------------+-------------------+ | M | 5 | 50 -> 54 | 500 -> 549 | | N | 5 | 55 -> 56 | 550 -> 569 | | O | 5 | 57 -> 59 | 570 -> 599 | +--------+-------------+---------------+-------------------+ | P | 6 | 60 -> 65 | 600 -> 659 | | Q | 6 | 66 | 660 -> 669 | | R | 6 | 67 -> 69 | 670 -> 699 | +--------+-------------+---------------+-------------------+ | S | 7 | 70 -> 79 | 700 -> 790 | +--------+-------------+---------------+-------------------+ | T | 8 | 80 -> 82 | 800 -> 829 | | U | 8 | 83 -> 84 | 830 -> 849 | | V | 8 | 85 -> 86 | 850 -> 869 | | W | 8 | 87 -> 89 | 870 -> 899 | +--------+-------------+---------------+-------------------+ | X | 9 | 90 -> 92 | 900 -> 929 | | Y | 9 | 93 -> 95 | 930 -> 959 | | Z | 9 | 96 -> 98 | 960 -> 989 | +--------+-------------+---------------+-------------------+ | | 990 - __construct | | | 991 - __get | | | 992 - __set | | Reserved | ................... | | | 998 - __clone | | | 999 - __destruct | +----------------------+-----------------------------------+
Now for a class that starts with letter A you will choose a code between 100 and 149. As seen codes are not distributed equally.
In H, I, J, K, L section:
- I, J, K are less frequent so they only get 10 error codes,
- H is less frequent that L so it get's 30 error codes (between 400 and 429),
- L is the most frequent so it get's 40 error codes (between 460 and 499).
One disadvantage is that you need to keep a list of assigned error codes... and a lot of discipline from all programmers in the project... but that is true for any standard.
Trackback address for this post
Trackback URL (right click and copy shortcut/link location)
Recent comments