- 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
Any consultant getting more than about $20 for this code would count as highly paid.
Admin
The point of coding standards and things like that is to reduce the little things you have to think about to leave more brain power for the parts that are actually hard.
Admin
So, magic number to avoid typing errors ? Well, add a CRC on a comment, at the end of line. This works also for other kind of constants, like strings, and even for variable names.
Maurizio
Admin
You can also use exponential format, at least in C it works without hassle.
1 billion 1e9 1 million 1e6 1 hundred thousand 1e5
Admin
Admin
I do believe I will be 0, not 1. It discards all but the last expression.
Admin
I regularly define constants for common numbers, even for things like 1000.
K = 1000 M = 1000K G = 1000M T = 1000*G
SECOND = 1 MINUTE = 60SECOND HOUR = 60MINUTE DAY = 24*HOUR
delta = this_date - last_date if delta > 4HOUR and file.st_size < 2M {...}
Beats the heck out of mentally dividing by 60, 3600, or 86400; or counting up the zeros. (Yes, I also define things like "FILE_MAX_AGE = 4*HOUR", then use "if delta > FILE_MAX_AGE". It's a simplified example. Don't hassle me about best practices.)
Admin
makes I 1.
makes J 0.
Admin
Admin
Admin
If you think that having difficulty counting zeroes and inconsistent naming schemes are on par with each other, for the love of all things holy, please find another industry.
Admin
I don't write Java, but doing a quick search shows that the substring method on String takes two indices, not a start and length. So, someString.substring(0,29) returns the first 30 characters of said string (since we start counting at 0). So while their code was obfuscated and redundant, I believe it was correct.
http://docs.oracle.com/javase/7/docs/api/java/lang/String.html
Admin
Works without cast in most cases thanks to int promotion rules (at least in C, C++ I don't give a s...). 1e9 is converted to long. Of course depends a bit on the context.
Admin
But the fact that it's infrequent that this situation arises doesn't mean that when it does you shouldn't take steps to try to improve things. If it was hard to do or something like that that'd be one thing, but even if you defined a special "BILLION" then used that you're talking one extra line of code. The improved-readability to effort-expended ratio is what matters, and in this case I think is very favorable.
Admin
Oh, so the assignment has a higher precedence than the comma. Did not know that.
Admin
http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#substring(int, int)
Admin
I think we'll just have to agree to disagree. I don't see why anything more than a comment is necessary.
Admin
RCA closed down their computer division. But RCA didn't close down their computer division because it was losing money. They knew they were losing money trying to compete with IBM, and they decided they would try anyway. Losing money wasn't the reason they gave up.
RCA closed down their computer division because they miscalculated the AMOUNT of money they were losing. When they looked at the amount of money they thought they were losing, they decided they couldn't afford to compete any more. They didn't know that they were actually losing an affordable amount.
RCA's computations had made an off-by-zero error.
Admin
Admin
Actually that's incorrect plus it's worse than that. Literal expressions have the possiblity to be evaluated at compile time. A gcc snippet best shows the overhead for a simple "printf("%d\n",1+3)" expression..
..whereas if you declare a couple on integers ONE,TWO "printf("%d\n",ONE+TWO)" now becomes..
Admin
And then I get to use the blame feature. :D
Admin
Admin
Admin
The real WTF is how the article tried to explain the WTF.
Admin
And to be fair, it's not unique to my style:
You can screw up any of the listed methods quite easily.
Admin
This is true but even if you use -O3 for..
int main () {static const int ONE=1,TWO=2; printf("%d\n",ONE+TWO); return 0; }
..you still get a difference..
I'll be the first to admit I know bugger all about x86 asm & I suspect the xorl may be faster that the movl. I also loathe java so I'm glad to see someone who knows their stuff!
I hope you see my original point, very few people these days understand the consequences of what they're writing. Compiler optimization saves them from having to learn.
Admin
Admin
That's not quite possible. If a ring R contains (a subring isomorphic to) Z where R's arithmetic is an extension of Z's, 0_R will equal 0_Z, since it is equal to (x - x) for any x \in Z, and the subtraction takes place in R and Z simultaneously.
OTOH if you take 1_Z to be an idempotent element of a commutative ring R then the additive subgroup of R generated by 1_Z will be a subring of R isomorphic to the integers or the integers modulo something. As long as the idempotent isn't equal to 0_R or 1_R and generates an infinite subgroup, this will do the trick.
Admin
why should it be different for programmers? If you don' know the answer, you should not be managing programmers!
Admin
Admin
Man... if only there were an existing function in the framework that would return the first n characters of a string...
Admin
Man... what do I have to do to become a Highly Paid Consultant? Sounds like my kind of job. While my colleagues fight some stupid strdup function, I can read the rest of the Internet and/or contribute some open source code after writing my working and tested solution that I'll present at the end of the day first.