- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
I am a .NET developer, but I don't see why you would want to kill the application unless the user has kindly asked the app to stop running.
If they're there as a defensive measure, then this becomes TRWTF; Exceptions/Try/Catch/Finally can take care of that.
CAPTCHA: Sagaciter - One who quotes sagas, I would guess...
Admin
Admin
TBH (as many others have sadi before me), I think I'd generally split it into several constants: [code] /* Message = Header (sender, msg ID 10 char) + 100 for body (although most messages will be 50, we support sending 2 Customers in one message) + Footer (for whatever reason the protocol requires two byte tail) */ public final static int MSG_HEADER_LEN = 10; public final static int MAX_MSG_BODY_LEN = 100; public final static int MSG_FOOTER_LEN = 2; public final static int MAX_MESSAGE_LEN = MSG_HEADER_LEN + MAX_MSG_BODY_LEN + MSG_FOOTER_LEN; [code] It costs little or nothing, and is clear why we set message length to a particular value (without the developer having to think about what the specific value is).
Admin
Why wouldn't you rely on the compiler to do the math - you are, after all, (likely) relying on the compiler to correctly create instructions to do other more complicated math.
Sure your name didn't used to be geoffery?
Admin
Admin
If you're a troll, You're Doin' It Right(TM). If you're serious, step away from the computer and never program again.
Admin
Admin
Admin
... or some such buggery. It's neater in some other languages.
Admin
I don't know, but the random generator that will random generate it, good random generator will be!
Admin
What Ol' Frank is saying is that you FIRST test that the specific issue reported is fixed (this might be checking that the issue with 12321232 is fixed). THEN you test to make sure everything else still appears to work - this would include the normal testing of edge cases (0, Max valid value, max valid value+1, etc) as well as (probably seeing as we use Random Numbers) some testing to satisfy yourselves that with Random Numbers things still work (and although I agree with some people who say it's not the Random Number Generation that we're testing, I think at least some testing with the real random Number generation is worthwhile if nothing else to maximise the chance of catching a hitherto unthought of error)
Admin
Admin
Admin
The constant in question is actually defined at method level, which to me was a WTF for sure, especially when you sprinkle in the magic number.
Further, the 199 + 6 magic number was not commented to explain anything about it.
Captcha: erat - e-rat?
Admin
Weather phenomena and the like may be unpredictable because we don't fully understand all the factors that go into causing a particular event, not because there's some higher being rolling 14 sided dice...
Perhaps a clearer example is traffic. Traffic is caused by coincidence, not randomness. A number of people make decisions that result in them being on the same transport infrastructure at the same time. Meanwhile, decisions dictating traffic light sequences (and other factors come into play).
So what if "Random" is just our way of saying "inexplicable with our knowledge"? What if there is no random? What if Chaos Theory is the only truth? Sierpinski's Sieve is certainly an example of how even random events can be used to create predictable patterns. Even lotteries are not random (in fact I think they rarely even have "Random Distributions"), although I guess that movie's already been made....
Short: Pillosophical - What is Random? If Random is simply "unpredictable" then perhaps there comes a time when more is understood and random ceases to exist.
Admin
Dont do that, this will skew the statistics, and will not produce an perfect s-curve.
Admin
No programmer is a sane people
Admin
One of the probelms (as I see it) with Exceptions is that some programmers decide (rather than shutting cleanly, and perhaps displaying a quasi-useful explanation for Tech Staff) they think they should try to recover the irrecoverable.
We recently had an incident where a (fairly critical) database error occured (corrupt data file, I think). Whoever had written our code (thankfully about 12 years before me, so it wasn't I) had a check in place to break out of a loop on a Database (or any) error. Unfortunately, this meant that we exited the loop and assumed life was normal (ie DB transactions had successfully) and a whole lot of mess ensued.
Granted, an Exception in the Java/C# sense might have meant that this was caught rather than ignored, however one way or another the behaviour has to be to do no more processing - even if that means the system doesn't work for the user!! If something is broke, then make it break for the user, don't try to pretend the world is happy...
That said, not all errors/exceptions are (or should be) fatal, but I think there are times when you want them to be.
Admin
A constant in a class is standard procedure. In a non-static context though, well that would be silly.
Admin
I like exceptions and find them useful. As a programming feature, they do make sense. Primarily because it deals with the problem that based on what you're returning, it might be hard to have a consistent way of reporting errors (no error channel). In a strongly typed traditional language it makes the most sense. In a weakly typed language you could return an error object instead of the normal return type ultimately to the same effect.
If you think that it means no more processing can be done or that the program should just crash I have to disagree with that. Some things can be recovered, or more likely ignored. Especially in the context of where you are doing things in parallel and a failure somewhere shouldn't bring down the entire program (as it wouldn't affect anything else). When it comes to programming people are more often at fault than their tools. Don't make the mistake of blaming tools because they can't fight back.
There's really nothing wrong with handling errors, no matter the criticality, as long as you are actually handling them. However it is something that can be horribly abused.
Probably the most common abuse:
An ambiguous abuse:
This isn't always an abuse, remember that throw is like an extra return channel and sometimes you just aren't always interested everything that gets returned. However, sometimes people ignore what is being returned hen you really shouldn't. A more common mistake is being too unspecific about the type of Exception that you want to ignore. To give an example of where you may want to reasonably ignore a throw, when closing a socket.
The most fubar thing I ever saw was where someone had apparently used every error handling system under the sun.
It's been a while now since I was working with that but it was something like the above but probably worse. You could never be too sure in any instant what you had to do to detect an error. Some things would let it be throw up the stack, some not. Being in a high level language made things even worse. I'm sure someone once thought this was a clever pattern but the net result was having to implement error checking at every level from the callee up through every caller when not having to do that is one of the key benefits of throwing exceptions.
I have since decided that I would rather spend the rest of my life on the dole and only program for personal enjoyment.
Admin
I think he means without any explanation. A very common malpractice. Something went wrong? Go with the bare minimum exit strategy.
Considerate programmers have the common decency to in the very least have a print() before exit() or use a procedure to the effect of printThenExit(message).
Admin
It's good to know that you have no concept of what random is.
Admin
All of those results could be randomd (unlikely, perhaps, but possible). If you always got those result I'd start to worry, but still could be random....
I don't knwo whether you're (@kattman not MDB) aware of this, but people do actually win the lottery occasionally. Some people win it multiple times. It would be possible to win it with the integers from 1 to n (where n= the number of numbers required to win).
Admin
The part I was objecting to was:
Which seemed to forbid testing with random numbers and insist on using a fixed set of predetermined numbers
Addendum (2012-05-18 00:50):
The part I was objecting to was:
Which seemed to forbid testing with random numbers and insist on using a fixed set of predetermined numbers. And testing with ACTUAL random numbers should be part of the regression.
Admin
Damn this site code
Admin
I do that sometimes, when the individual numbers have some meaning to them. Although generally I'd leave a comment saying that too.
I guess more correctly you should define the two numbers as constants with meaningful names in their own right, and then define the third as a compound...
Admin
I'm not necessarily seeing a WTF here. While 205 may be just as good as 199+6, it's not as readable - say 6 is a header size, 199 is the data size and such.
As for constants in a class: Why not?
Admin
The main WTF is that nowhere in this class is a message explaining exactly what these numbers actually mean. Unexplained magic numbers are a major WTF in my book, all the worse for being so common, the result of a programmer too lazy or impatient to exercise basic human politeness.
Admin
It should be selected at random.
Admin
(Hint: undecorated integer constants have type int if possible, and int is allowed to be 16 bits in C and C++. The promotion to uint32 is done after the multiplication. Fix it as 60L60L24L, then do something to fix the dumb compiler's loss-of-precision warning on a system with 64-bit longs...)
Admin
Oh wow, didn't think people here would take my sample so literally :P of course I don't name my consts that way in real code. I just named it like that so it would be clear why I used 2 * 60 and not 120. I guess i should've written SOME_RANDOM_CONSTANT_FROM_CODE, maybe then it would be clear.
Admin
Working fine for me, but I'm using IE 8. Time you got a modern browser.
Admin
Wow is this captian douchbag and Fapper Sidkick? Yes these results all the time would not be random, even getting them once should throw a red flag for testing, but that does not mesn I have no idea what random is, it actually makes me think I know it more than you based off of a very small sample set, that set being the last posts by the two of you.
Admin
//sanity dump int ROOM = 101;
Admin
I can agree to that.
In the context of the story, though, when the app 'inexplicably' exits, that sounds to me like evidence of overuse of exiting the application.
I can add that I think when an app enters an unrecoverable state, before it exits it should tell the user that the world is no longer happy and the app cannot continue operating. Inexplicable exits cause the user/customer huge WTF moments, which translate into those irritating 3 in the morning irate calls.
CAPTCHA: Appellatio - the Harry Potter spell for turning naughty coders into apples!
Admin
Learn something about randomness.
With only 65000 randomly-selected 32-bit numbers, you have over 50% probability of a duplicate. The probability of getting all 4 billion different values by selecting only 4 billion samples is quite small.
--Joe
Admin
Learn something about random number generators. The output of a PRNG is a deterministic function of its state vector. Therefore, a PRNG with a 32-bit state vector must generate every value it is capable of generating within 2^32 iterations (possibly far less, if the generator is a crappy one). This is undoubtedly what Lurch was referring to.
On the other hand, Lurch might want to bear in mind that just because a PRNG outputs 32-bit values doesn't mean it has a 32-bit state vector. In fact, a 32-bit state would result in a very poor generator, since it will start failing tests for correlation within a few hundred million iterations, which is well within the range of normal usage.
Admin
Especially if you have a large application that contains bits written in a mix of languages. Having a random generator that can be called with a fixed seed might not be enough. You may run into situations where you need a random generator that behaves identical, regardless of the language a snippet of code was written in. Unfortunately, you can't rely on system libraries across languages to have the same implementation.
Admin
You'd use The One RNG.
Admin
Admin
Admin
Admin
well, no.. but you can do
const int TIMEOUT_SECONDS = 2 * 60;
because your API takes the timeout in seconds, and you want anyone who happens to read the code to know you're setting up 2min without having to do the math.
Personally, I always initialize a double-buffered 32bit screen memory like so:
buf = (byte *)alloc(640 * 480 * 4 * 2);
Admin
Quantum mechanics is probabilistic. You know that there is about a 25% the the electron will be in orbit x, or other such statement about a physical system. Probabilistic isn't the opposite of random but it's still pretty far away.
Admin
Even then you should never ever use System.Environment.Exit directly unless you are dealing with the simplest of all terminal programs. The fact that the user account that is running the program requires SecurityPermissionFlag.UnmanagedCode to call Environment.Exit should tell you plenty about the nature of the problems surrounding it:
Calling Exit does not shut down the process's application domain cleanly. It doesn't terminate processes started from your process, kills managed threads, doesn't call Dispose or run finalizers to take care of unmanaged resources and doesn't run code in finally blocks. It stops everything stone cold and can potentially leak a lot of system resources (Like file handles, window handles, sockets, memory from various types of in-memory stream, etc.)
Admin
Must be ... since that is so much more likely than forgetting what "2*60" represented in that particular case.
Sarcasm aside, there is something to be said for going even further and embracing seemingly over-engineered DSL-ish typesafing stuff. Like, this is a minute, and by definition it has 60 seconds. Rather than just chucking integers about (which is still what you are doing even if you define them as constants). Not something I've had much experience of but seems like it does what the constant-mongers are trying to do, but with more success.
Admin
Or if you are writing production code for helping people with introductory software books...
Admin
Pfft, this one is too obvious. The code is obviously there to check the programmer's sanity. If you're too sane you will be hung up on this line too much.
Admin
Admin