Comment On Sanity Check

"Lucky me," Ryan wrote, "I got assigned to work on Legacy, an application whose name accurately describes itself. I'm pretty sure that this system manages to have a WTF/line ratio greater than 1.0, especially if we include the 'minor' ones, like the System.Environment.Exit calls peppered throughout library code that causes the app to inexplicably exit." [expand full text]
« PrevPage 1 | Page 2 | Page 3 | Page 4Next »

Re: Sanity Check

2012-05-17 09:02 • by Crazy Dutch Bastard (unregistered)
I must be insane.

Captcha: secundum - comes after fristum

Re: Sanity Check

2012-05-17 09:09 • by Vamp (unregistered)
I have done that with constants before. For example timeouts or similar:

const int TWO_MINUTE_CONSTANT = 2 * 60;

Makes it easier to read for me later. Maybe it was something similar for those 2 values :)

Re: Sanity Check

2012-05-17 09:13 • by Turd Blossom (unregistered)
const maxVolume = 10 + 1;

Re: Sanity Check

2012-05-17 09:16 • by Christian (unregistered)
in my opinion it makes perfect sense to encapsulate something like a random-number generator in your own class, this way you can replace this component with one that returns a "random" number which you can control in your unit-tests.

Re: Sanity Check

2012-05-17 09:17 • by portablejim
At least you have the magic values in one place instead of spread out inside the class.

Re: Sanity Check

2012-05-17 09:19 • by Other Nagesh (unregistered)
381327 in reply to 381325
Christian:
in my opinion it makes perfect sense to encapsulate something like a random-number generator in your own class, this way you can replace this component with one that returns a "random" number which you can control in your unit-tests.


Or you could use multiple random number generators and randomly select the one to use for enhanced super-randomness.

Re: Sanity Check

2012-05-17 09:21 • by AGray (unregistered)
Constants in a class may or may not be a WTF - I'd honestly rather deal with a constant defined at the class level (preferably, in a Constants region) where I can easily find, identify, and maintain it, than have to sift through a bunch of code to find a method-level constant.

The value of that constant though...I think that's TRWTF.

Captcha: enim - must be Eminem's little brother.

Re: Sanity Check

2012-05-17 09:23 • by Severity One
381329 in reply to 381325
Christian:
in my opinion it makes perfect sense to encapsulate something like a random-number generator in your own class, this way you can replace this component with one that returns a "random" number which you can control in your unit-tests.
Would you mind giving an example where a non-random random number makes sense?

Re: Sanity Check

2012-05-17 09:25 • by CT (unregistered)
probably some serious backstory to that 199 + 6. I have some humorous (to me anyway) code like that for managers who are decidedly NOT developers and have weird ideas about what is right.

Re: Sanity Check

2012-05-17 09:25 • by Testing Troll (unregistered)
381331 in reply to 381325
Or... you could do the sane thing and seed the PRNG with a constant when you're unit testing, and let it be seeded with the system time for normal operations...


CAPTCHA: conventio... if you follow the conventio you won't produce WTF-ridden code.

Re: Sanity Check

2012-05-17 09:26 • by Andrew (unregistered)
"I don't even want to know why a constant is declared inside a class...
Because that's the way it's done in Java? Yes, I know that this is probably .NET/C#, but the guy might not be thinking that way.

Captcha: veniam. That's the way we coded in Ve'niam.

Re: Sanity Check

2012-05-17 09:31 • by zelmak
381333 in reply to 381329
Severity One:
Christian:
in my opinion it makes perfect sense to encapsulate something like a random-number generator in your own class, this way you can replace this component with one that returns a "random" number which you can control in your unit-tests.
Would you mind giving an example where a non-random random number makes sense?

Similar to this method for having a fake clock.

Re: Sanity Check

2012-05-17 09:31 • by Tony (unregistered)
381334 in reply to 381329
Here's a couple:

1) Using random numbers for a short wait time so a server doesn't get hit with a lot of requests (ala ethernet collsion algorithm) - You might want to have a non random number to see how the server behaves when it does get hit simultaneously

2) Boundary checking - you might expect random numbers to be in a certain range, but for testing purposes, you could simulate numbers at the boundary or across the boundary

Re: Sanity Check

2012-05-17 09:37 • by Fool (unregistered)
I do things like 199+6 isntead of 205 on occasion.

2 reasons:
1) I am often doing math where I might look at the code one day and say, why the heck did I put 205 there? But looking around I see the numbers 195 and 6 somewhere else in the code.

2) 199+6 compiles to 205 so it should be equivilent to 205 in the binary file that is generated.

Re: Sanity Check

2012-05-17 09:37 • by Mike (unregistered)
381336 in reply to 381327
Other Nagesh:

Or you could use multiple random number generators and randomly select the one to use for enhanced super-randomness.


But which random number generator would you use to select which random number generator to use for your super random number generator?

Re: Sanity Check

2012-05-17 09:38 • by DonaldK (unregistered)
CT:
probably some serious backstory to that 199 + 6


If you walk the version control commit history might get something like:


[Source change 382]
const int expected_length = 199;
[Comment block]
Should be fine for all cases


[Source change 383]
const int expected_length = 199 + 1;
[Comment block]
I'm sure it was some zero based issue


[Source change 384]
const int expected_length = 199 + 2;
[Comment block]
Wierd. +1 should have worked


[Source change 385]
const int expected_length = 199 + 3;
[Comment block]
Makes sense now... it's 2 UNICODE char's -1 (since it's zero based)


[Source change 386]
const int expected_length = 199 + 4;
[Comment block]
Still didn't work... +4 should be OK I'm sure


[Source change 387]
const int expected_length = 199 + 5;
[Comment block]
Still crashing! We really need to look at this logic


[Source change 388]
const int expected_length = 199 + 6;
[Comment block]
Praying this will be enough for all cases

Re: Sanity Check

2012-05-17 09:41 • by Nagesh (unregistered)
In java, ain't declared quite the seme. Writen like this:

public static final int EXPECTED_LENGTH = 199 + 5;


Constant declared in class for purpose of scope. Concretion values being used for purpose of showing how result is being found. Unfortunate after meking us wating a day, Alex provide code semples that is not being WTF. :(

Re: Sanity Check

2012-05-17 09:42 • by Tyler (unregistered)
Looks like a normal thing to do. He has a fixed length string of 199 characters that has 6 chars appended to the end.

As for "within a class" sounds good, better than global, I've even done such things as:

//Tightly-Scope
{
int bPtr=0,ePtr=0;
...
}
(No if statement/while block etc. to set off the block).

Re: Sanity Check

2012-05-17 09:42 • by Nick (unregistered)
199 - clearly they are zero based arrays, and the max line length is 200 chars.

Now if that line is sent elsewhere, then it is going to be bundled into a buffer. You will need an overhead for the message type, and the length of the text being sent. I would suggest that is 2 bytes for the message, and 4 bytes for the length. Or 4 and 2.

Hence the 6.

Would be better if that was a constant in its own right.

e.g

max_line = 199
protocol_size = 6
message_size = max_line + protocolsize

Re: Sanity Check

2012-05-17 09:51 • by C-Octothorpe
// We need to give 110% here...

const double MAX_EFFORT = 1D + 0.1D;

Re: Sanity Check

2012-05-17 09:53 • by Gazzonyx
TRWTF is that there isn't a comment on the magic numbers there. Not that there is anything wrong with something like this, but it should be documented. For instance, this is one of my commits to Samba :

[...]
+ /* domain/username%password */
+ const int max = MAX_DOMAIN_SIZE +
+ MAX_USERNAME_SIZE +
+ MOUNT_PASSWD_SIZE + 2;
[...]

Re: Sanity Check

2012-05-17 09:54 • by snoofle
381343 in reply to 381329
Severity One:
Christian:
in my opinion it makes perfect sense to encapsulate something like a random-number generator in your own class, this way you can replace this component with one that returns a "random" number which you can control in your unit-tests.
Would you mind giving an example where a non-random random number makes sense?
If you want to write a test where you need predictable results, then you'll need to know the "random" inputs in advance.

Re: Sanity Check

2012-05-17 10:01 • by Nagesh (unregistered)
381344 in reply to 381339
Tyler:
As for "within a class" sounds good, better than global

Agreed. TOS ain't understanding scope is TRWTF.

Re: Sanity Check

2012-05-17 10:12 • by Matthew (unregistered)
381345 in reply to 381329
Repeatability testing.

If I'm testing a simulation program, I may want to run the simulation under the same set of constraints repeatedly for testing, but use random numbers in production.

Re: Sanity Check

2012-05-17 10:15 • by ZPedro
You don't get it. This is like an encounter in a Cthulhu mythos RPG, where you have to roll a dice to know if you are staying sane. Same here, the original coder put an unfathomable expression to force any future reader of the code into insanity if he wasn't sane enough already, thus avoiding borderline cases from going any further - and he helpfully documented he was doing this. Think of it as an assert on the maintenance programmer, forcing him to exit with insanity if he fails the check.

Re: Sanity Check

2012-05-17 10:20 • by concerned citizen (unregistered)
Yeah, I gotta say, I don't see a WTF here except for maybe not having a comment explaining where the 199 and 6 came from. And like hell if I'm going to throw stones in that glass house!! :p

Re: Sanity Check

2012-05-17 10:25 • by Lockwood (unregistered)
[Insert xkcd random number thing here.]

199+6? Simple explanation time?
Expected length used to be 199. There was some reason deep in the logic or spec that said "We need it to be 6 longer".
They did 199 + 6 rather than 205, expecting it to be respecced and it being easier that way.

(I've got code that is now sadly barely maintainable because the spec and calculation to use was being changed hourly, so it has loads of commented versions littered all over the place, "Change it to this. It should be *2 not *3. Change it back to the original. *3 again. No, wait. *2)

Re: Sanity Check

2012-05-17 10:32 • by Dave (unregistered)
381349 in reply to 381325
Christian:
in my opinion it makes perfect sense to encapsulate something like a random-number generator in your own class, this way you can replace this component with one that returns a "random" number which you can control in your unit-tests.


I tend to agree, but that's a very different case to that mentioned in the article:

The Article:
solely to duplicate the behavior

Re: Sanity Check

2012-05-17 10:34 • by Xeromal (unregistered)
381350 in reply to 381337
University all over again. :(

Cogo, I had fun in the Cogo line today.

Re: Sanity Check

2012-05-17 10:46 • by Not sure if Fry or just Philip (unregistered)
Really guys, use the fucking comments if you want to explain magic numbers.

// length is 199 (because of foo) + 6 (because of bar)
public final static or whatever length = 205;

Even better, use javadoc.

Re: Sanity Check

2012-05-17 10:55 • by airdrik
381352 in reply to 381351
Not sure if Fry or just Philip:
Really guys, use the fucking comments if you want to explain magic numbers.

// length is 199 (because of foo) + 6 (because of bar)
public final static or whatever length = 205;

Even better, use javadoc.


Oh, now that makes perfect sense, thanks!

Re: Sanity Check

2012-05-17 10:57 • by Playoffs? (unregistered)
381354 in reply to 381351
Not sure if Fry or just Philip:
Really guys, use the fucking comments if you want to explain magic numbers.

// length is 199 (because of foo) + 6 (because of bar)
public final static or whatever length = 205;

Even better, use javadoc.


Magic Number = T - W - L

where
T is the total number of games in the season
A is the number of games won by the team for which you are trying to calculate the magic number.
B is the number of games lost by the team for which you are trying to calculate the magic number against.

Source 1
Source 2

That being said, it's way to early to start talking playoffs

Re: Sanity Check

2012-05-17 10:59 • by operagost
I am not a real programmer, so I don't work in C/C++ and didn't realize that this was legal in any languages. The last time I tried this it wouldn't compile. It was probably something esoteric like DEC BASIC.

Re: Sanity Check

2012-05-17 11:01 • by WhiskeyJack
381357 in reply to 381340
Nick:
199 - clearly they are zero based arrays, and the max line length is 200 chars.

Now if that line is sent elsewhere, then it is going to be bundled into a buffer. You will need an overhead for the message type, and the length of the text being sent. I would suggest that is 2 bytes for the message, and 4 bytes for the length. Or 4 and 2.

Hence the 6.

Would be better if that was a constant in its own right.

e.g

max_line = 199
protocol_size = 6
message_size = max_line + protocolsize


You beat me to it, I was just thinking that's probably how I would see it appear in my code if it were to appear at all.

But a better approach would be to declare a struct for the particular message type including the header block (message type, message length) and then do a sizeof() to calculate the size instead of hard-coding it to 205.

Re: Sanity Check

2012-05-17 11:07 • by cellocgw
381358 in reply to 381351
Not sure if Fry or just Philip:
Really guys, use the fucking comments if you want to explain magic numbers.

// length is 199 (because of foo) + 6 (because of bar)
public final static or whatever length = 205;


Which reminds me of a MatLab WTF: everybody, including me, uses _foo_ and _bar_ as throwaway variables. Well, just try that in MatLab, and then try to call their function to draw a bar plot, which is named, you guessed it, "bar" . Hilarious subscript errors ensue.

Re: Sanity Check

2012-05-17 11:12 • by Karl (unregistered)
C'mon guys it is staring you right in the face! How come nobody got it yet?

// Sanity check

If you are a sane person, when you look at the next line of code you will immediately delete it. Nobody has yet, therefore you and all your peers are insane.

Re: Sanity Check

2012-05-17 11:20 • by Frank (unregistered)
381360 in reply to 381343
snoofle:
Severity One:
Christian:
in my opinion it makes perfect sense to encapsulate something like a random-number generator in your own class, this way you can replace this component with one that returns a "random" number which you can control in your unit-tests.
Would you mind giving an example where a non-random random number makes sense?
If you want to write a test where you need predictable results, then you'll need to know the "random" inputs in advance.
Almost got there snoofle.

Everyone should know the answer to this question. If you don't, you're Doing It Wrong. (Or never worked with random numbers.)

Think about your workflow. You write software. You test it. It goes live. Someone finds a bug. You fix it. You test it again.

What is the goal of that last test?

Hmmm?

Are you sure?

If you answered "to confirm the bug is fixed" you lose. Well you get half points, which is 50%, which is an F in most classes.

The answer is "to confirm the bug is fixed and to ensure no new bug is introduced, or other old bug came back".

How do you do that? You save the results of the "before fix" test and compare them with the "after fix" test. Only one thing should have changed. The badness should now be goodness. No other new badness.

Now, if you have random numbers in the mix, most likely every run will be different. So how can you compare the output? Huh??

See, if you'd ever done proper testing against random numbers, you'd know you have to generate a known repeatable random number (or sequence) during this type of testing.

Re: Sanity Check

2012-05-17 11:38 • by DaveK
381361 in reply to 381342
Gazzonyx:
TRWTF is that there isn't a comment on the magic numbers there. Not that there is anything wrong with something like this, but it should be documented. For instance, this is one of my commits to Samba :
Good stuff. Here's your next one:

[...]
/* domain/username%password */
const int max = MAX_DOMAIN_SIZE +
MAX_USERNAME_SIZE +
- MOUNT_PASSWD_SIZE + 2;
+ /* Oops. CVE2012-xxx-yyy */
+ MOUNT_PASSWD_SIZE + 3;
[...]

Re: Sanity Check

2012-05-17 11:42 • by DaveK
381362 in reply to 381361
DaveK:
Gazzonyx:
TRWTF is that there isn't a comment on the magic numbers there. Not that there is anything wrong with something like this, but it should be documented. For instance, this is one of my commits to Samba :
Good stuff. Here's your next one:

[...]
/* domain/username%password */
const int max = MAX_DOMAIN_SIZE +
MAX_USERNAME_SIZE +
- MOUNT_PASSWD_SIZE + 2;
+ /* Oops. CVE2012-xxx-yyy */
+ MOUNT_PASSWD_SIZE + 3;
[...]
Oh, don't bother, I see someone already caught it for you. Heh.

(I also see it's not directly used to size a buffer anyway.)

Re: Sanity Check

2012-05-17 11:45 • by Loren Pechtel
381363 in reply to 381328
AGray:
Constants in a class may or may not be a WTF - I'd honestly rather deal with a constant defined at the class level (preferably, in a Constants region) where I can easily find, identify, and maintain it, than have to sift through a bunch of code to find a method-level constant.

The value of that constant though...I think that's TRWTF.

Captcha: enim - must be Eminem's little brother.


Yup. I've declared plenty of constants in classes. They're not applicable outside the class but a constant is much better than a magic number in the code.

Nick:
199 - clearly they are zero based arrays, and the max line length is 200 chars.

Now if that line is sent elsewhere, then it is going to be bundled into a buffer. You will need an overhead for the message type, and the length of the text being sent. I would suggest that is 2 bytes for the message, and 4 bytes for the length. Or 4 and 2.

Hence the 6.

Would be better if that was a constant in its own right.

e.g

max_line = 199
protocol_size = 6
message_size = max_line + protocolsize


Agreed. If the reason for the math isn't self-evident you should do something like this.

Re: Sanity Check

2012-05-17 11:56 • by Not sure if Fry or just Philip (unregistered)
381364 in reply to 381352
airdrik:
Not sure if Fry or just Philip:
Really guys, use the fucking comments if you want to explain magic numbers.

// length is 199 (because of foo) + 6 (because of bar)
public final static or whatever length = 205;

Even better, use javadoc.


Oh, now that makes perfect sense, thanks!
Since there seems to be a misunderstanding, here's my point: Don't rely on the compiler to do your math. Show what you meant in the comment, and store the calculated result in the const.
On the other hand, generic variables like "foo" and "bar" are the most evil variable names ever. While they're perfectly acceptable in short and meaningless examples, they should never, ever appear in production code. Except when they mean something, like spacebar or meter bar.

Re: Sanity Check

2012-05-17 12:08 • by WordCrawler (unregistered)
381365 in reply to 381364
Not sure if Fry or just Philip:
airdrik:
Not sure if Fry or just Philip:
Really guys, use the fucking comments if you want to explain magic numbers.

// length is 199 (because of foo) + 6 (because of bar)
public final static or whatever length = 205;

Even better, use javadoc.


Oh, now that makes perfect sense, thanks!
Since there seems to be a misunderstanding, here's my point: Don't rely on the compiler to do your math. Show what you meant in the comment, and store the calculated result in the const.
On the other hand, generic variables like "foo" and "bar" are the most evil variable names ever. While they're perfectly acceptable in short and meaningless examples, they should never, ever appear in production code. Except when they mean something, like spacebar or meter bar.


Oh yeah, and don't forget to maintain that comment when you change the number from 205 to 206!

CAPTCHA: damnum = damn number

Re: Sanity Check

2012-05-17 12:10 • by Jack (unregistered)
381366 in reply to 381349
Dave:
Christian:
in my opinion it makes perfect sense to encapsulate something like a random-number generator in your own class, this way you can replace this component with one that returns a "random" number which you can control in your unit-tests.


I tend to agree, but that's a very different case to that mentioned in the article:

The Article:
solely to duplicate the behavior, but with tons of bugs


FIFY (if history is any indicator)

Re: Sanity Check

2012-05-17 12:10 • by Calli Arcale (unregistered)
381367 in reply to 381360
Frank:
The answer is "to confirm the bug is fixed and to ensure no new bug is introduced, or other old bug came back".


Well, that's what you *hope* to do. More often, you'll find that although the bug was fixed, it has now broken something else. Or it is only fixed in condition A, but in condition B it remains broken. Or the bug didn't break anything else at all, but you've stumbled upon something broken that nobody had noticed yet, possibly because the bug you fixed was concealing it.

I actually get a little worried when regression testing always passes. I find myself wondering whether or not the testing is adequate.

Re: Sanity Check

2012-05-17 12:12 • by Dan Minjoc (unregistered)
381368 in reply to 381364
Not sure if Fry or just Philip:
airdrik:
Not sure if Fry or just Philip:
Really guys, use the fucking comments if you want to explain magic numbers.

// length is 199 (because of foo) + 6 (because of bar)
public final static or whatever length = 205;

Even better, use javadoc.


Oh, now that makes perfect sense, thanks!
Since there seems to be a misunderstanding, here's my point: Don't rely on the compiler to do your math. Show what you meant in the comment, and store the calculated result in the const.
On the other hand, generic variables like "foo" and "bar" are the most evil variable names ever. While they're perfectly acceptable in short and meaningless examples, they should never, ever appear in production code. Except when they mean something, like spacebar or meter bar.


Disagree. The compiler will automatically evaluate the expressions. Why decrease code readability for no benefit?

Which would you rather have?

const uint32 NUM_SEC_IN_DAY = 60 * 60 * 24;
const unit32 NUM_SEC_IN_DAY = 86400;

Both are correct, but the first one shows exactly where the calculation is coming from.

Re: Sanity Check

2012-05-17 12:13 • by Jack (unregistered)
381369 in reply to 381364
Not sure if Fry or just Philip:
Since there seems to be a misunderstanding, here's my point: Don't rely on the compiler to do your math. Show what you meant in the comment, and store the calculated result in the const.


I tend to disagree. I trust the compiler's math way more than most developers'.

Re: Sanity Check

2012-05-17 12:16 • by Harrow (unregistered)
381370 in reply to 381355
operagost:
I am not a real programmer, so I don't work in C/C++ and didn't realize that this was legal in any languages. The last time I tried this it wouldn't compile. It was probably something esoteric like DEC BASIC.
Ah yes, I recall whiling away many happy hours writing DEC BASIC. In fact, I give DEC BASIC full credit for making me the person I have become today. I would explain more but Doctor says I'm not supposed to even use the computer at all while I'm in here.

-Harrow.

Re: Sanity Check

2012-05-17 12:18 • by C-Octothorpe
381371 in reply to 381360
Frank (Captain Obvious):
snoofle:
Severity One:
Christian:
in my opinion it makes perfect sense to encapsulate something like a random-number generator in your own class, this way you can replace this component with one that returns a "random" number which you can control in your unit-tests.
Would you mind giving an example where a non-random random number makes sense?
If you want to write a test where you need predictable results, then you'll need to know the "random" inputs in advance.
Almost got there snoofle.

Everyone should know the answer to this question. If you don't, you're Doing It Wrong. (Or never worked with random numbers.)

Think about your workflow. You write software. You test it. It goes live. Someone finds a bug. You fix it. You test it again.

What is the goal of that last test?

Hmmm?

Are you sure?

If you answered "to confirm the bug is fixed" you lose. Well you get half points, which is 50%, which is an F in most classes.

The answer is "to confirm the bug is fixed and to ensure no new bug is introduced, or other old bug came back".

How do you do that? You save the results of the "before fix" test and compare them with the "after fix" test. Only one thing should have changed. The badness should now be goodness. No other new badness.

Now, if you have random numbers in the mix, most likely every run will be different. So how can you compare the output? Huh??

See, if you'd ever done proper testing against random numbers, you'd know you have to generate a known repeatable random number (or sequence) during this type of testing.
Wow, ground-breaking stuff you got there... So, you're telling me that when I fix a bug, I need to make sure I don't break everything else? Thank goodness you were here to tell everyone in such a condescending tone...

Re: Sanity Check

2012-05-17 12:28 • by Joe (unregistered)
381372 in reply to 381360
Frank:
Now, if you have random numbers in the mix, most likely every run will be different. So how can you compare the output? Huh??

See, if you'd ever done proper testing against random numbers, you'd know you have to generate a known repeatable random number (or sequence) during this type of testing.


You run the test 10 billion times so that you know you're testing every random 32-bit number (to within 5%) that might come up in production.

This also gives the advantage of never finding another problem during your career, and/or inflating the "number of tests passed" metric that you get a bonus on.

--Joe

Re: Sanity Check

2012-05-17 12:28 • by Zylon
381373 in reply to 381323
Vamp:
I have done that with constants before. For example timeouts or similar:

const int TWO_MINUTE_CONSTANT = 2 * 60;

Makes it easier to read for me later.

So, in case at some point in the future you forget what "TWO MINUTES" means?
« PrevPage 1 | Page 2 | Page 3 | Page 4Next »

Add Comment