- 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 remember doing something like this on my ti 83+ calculator once... IIRC, it was to decode a string to get psuedo-properties on an "object" described by said string (i.e. Dialog, Textbox, Label, etc).
Windows Ti never did make it very far off the ground....
Admin
But since the lookup doesn't check for boundaries, there are lurking substring bugs if the numbers ever roll-over and need another digit...
Admin
TRWTF is anyone seeing a bright side to string-comparisons for numbers. There will be plenty of lurking bugs if anyone ever asks to validate an ID < 10000, or an ID >= 100000 is added to the lists. Or if it ever runs on a machine where the prevailing culture does something weird in ToString(), such as inserting commas every third place. FFS, three hard-coded arrays of ints would involve negligibly more typing and would actually function correctly...
Admin
Another stupid, the method is declared static but not the constant strings.
Admin
It was sarcasm.
Admin
Or if you just search for e.g. "29".
Admin
#Top Ten Ways To Display The Number One! ##Only Discourse
UsersVictims Will Get ThisAdmin
#Top Ten Ways To Display The Number Two! ##Only Discourse
UsersVictims Will Get ThisAdmin
For later expendability, those strings are not consts. That's Doing It RightTM.
But why is metricId.ToString() evaluated three times? Wouldn't it be more effective to do it this way:
And to avoid possible length clashes:
Then tell your intern that integer arrays are more effective, leading to (in C#):
and
Then your intern learns that string concatenations should be done by a StringBuilder for efficiency:
( code omitted for sanity reasons )
Admin
Admin
Only if the compiler is aware of int.ToString() yielding always the same result for the same number. (Should be the case, but you never can tell.)
Maybe I should introduce a variable IFormatProvider fmtProv and call metricId.ToString(fmtProv) ?
Filed under: Psychology : post mortem rationalisation of Doing It WrongTM
Admin
Admin
I know in theory, but it is hard to get used to that if you've learned programming when assembler was still a thing for applications and you had to cram all your binaries and data into maybe 2 KiByte of memory.
Admin
After changing from a string to an array integers, you might end up with good code. Assuming you keep the array sorted (which, of course, is a potential source of bugs), you can then do a binary search across the array, which would be just as fast as a dictionary lookup. I'm not certain what language the WTF was using, but I know that both C and C++ have standard library functions for efficiently searching a sorted array.
Building a dictionary (or STL map in C++) may produce the most efficient code for searching, but there is overhead to building the dictionary in the first place. Depending on how many numbers are in the list and how many times this lookup will be performed during a single run of the process, it might be more efficient to binary-search a static array of integers instead of building a dictionary at startup.
Admin
Without confirming by examining the machine code generated from the IL, I'd really hesitate to assume that.
Admin
I know, and it's not exactly easy to avoid, especially if the sane solution is obvious and shorter.
Admin
Admin
I think swich case can get optimized to a lookup table.
Oh the downside is having to write
case
thousands of times, and depending on code formatting style, thousands of lines.Probably a good thing if you are paid per line or byte of code though.
Admin
It's a tough problem. You know, it's really too bad that we don't have a tool, like some kind of data...base...that we could use to look up things like this.
Admin
The worst thing about the ancient proprietary language our codebase uses is that it's old enough that doing string lookups was faster than using a database when it was designed, and so it never got any tools for dealing with a real database, only it's own pure text database.
It doesn't have lists or arrays either, as far as I know, and everything ever can be null (which is represented as an empty string).
(^(x)>=)
is what a null check looks like, where x is a variable name. We do have tables at least, but those are always part of the database, so you can't just make one to use as a list.So typically to do a lookup, you have to do something like
_BLAH_BLAH2_BLAH3_{_^(x)>_
(Find_x_
in_BLAH_BLAH2_BLAH3_
.Glad to be working on a C# project here now.
Admin
Sure, a database will get the job done, but is it worth the overhead?
If the table is large and can change dynamically while the system is running, then it might be a good idea. But if the table is not too large, and only changes rarely (e.g. when the software itself updates), then that's a lot of overhead.
I don't think I'd want to impose the overhead of something like SQLite for something as trivial as this unless there are other product-level requirements that can justify it.
Admin
Couldn't the construction of a default-allocator, constant (as in declared const) map be basically performed at compile-time? Or are compilers not quite smart enough to do that yet? ;)
Admin
The internet has a few big binary discussions: "which side should the toilet paper hang?", "water on your toothbrush before or after putting the tooth paste?", "does
90.999... equal 1?" etc. The discussion whether IDs should be stored as int or string also seems to split the internet.It's like the Beatles or the Stones, I guess.
Edit: Props to who had seen it :+
Admin
Toilet paper should hang out so that I don't have to touch the wall accidentally, or risk shoving the paper against the wall. Sanitation saves millions of lives. The opposing side, claims aesthetics.
Water your toothbrush before and after for best results.
.9999.... cannot be properly expresses in finite space. Therefore we chose to represent it as 1.
Do you add IDs?
Who are these Stoney Beetles?
Admin
que the flame war...
digs trench to hide in
:smile:
Admin
The 1998 version of C++ doesn't have any kind of static constructor for std::map. Some code must execute to populate it.
The 2011 and 2014 versions of the language allow maps to be constructed from an initializer list, which (I think) can be generated statically. I don't know if the map itself can be statically initialized when such a list is used, however, because it will need to generate the internal data structures (typically an RB-tree, but others are theoretically possible). The code will be syntactically cleaner than calling a function to populate the map, and may be more efficient than inserting nodes individually, but I don't think it can be done entirely at compile time.
Other languages, of course, may have different kinds of containers and may support compile-time initialization.
Admin
Someone should tell your manager that Brainfuck was never intended to be used in production systems.
Admin
I prefer it hanging in front of the roll, because I find it more usable, especially when I'm half asleep.
But if you have pet cats, that can be an invitation for them to unwind the entire roll onto the floor. When it hangs on the wall-side of the roll, a cat pulling on the front of the roll won't end up unwinding anything.
Admin
I can see how that would be a problem in public bathrooms.
[spoiler]Or in homes where the residents don't know how to close doors[/spoiler]
[spoiler]If you have cats that can open doors.... you should look around for some way to profit off of that.[/spoiler]Admin
Well, all men know how painful swollen listicles are...
Admin
The language was designed 30 years ago by a lawyer. We're kind of stuck with it. Interestingly, the whole
^()>
thing just denotes some kind of symbol rather than text. The>
is actually non-printing character that we display as a right-facing triangle, but we have a VS plugin that auto-closes them, so we don't need to worry terribly about them.For a builtin function call, between the
^
and(
, you'd put the name. For a user defined one, you have to use^FUNCTION(FunctionName)>
and procedures have another call. The function name is the name of the function file.Admin
I was given some stupid FORTRAN to maintain once which was to return whether a particular date was a public holiday or not. Every year we had to edit the program and add the string of comma-separated numbers which were the days of that year (can't remember the format) into a new Data statement and then recompile all the code into all the libraries that used this data.
I suggested that it might be better to rewrite it to stick it into a file and get the program to initialise itself from that file. Oh no, far too big an overhead, was the bletheringly stupid answer from the silly old woman who happened to be the boss at the time. No, it was not time critical, it was hardly ever used in fact -- but when it was used it was important.
Sorry but it's far more important in my mind to make sure that such information is easily maintained and managed -- especially so, because only used once a year, the knowledge of how to do it is not at the forefront of the brain.
Utterly stupidly stupid place. I'm glad I left.
Admin
(^(x)>=)
looks way too much like ASCII art. Is this a programmer's version of day-doodling?Admin
Beatles and Stones can lock themselves in a room to fight it out, while real men listen to Motörhead.
Admin
My family had a cat that we're pretty sure figured out how to open doors. One of the bedrooms where we would shut him in if we had visitors had a bureau next to the door, and he figured out that he could get on top if the bureau and reach across to turn the doorknob. He would also try to turn the doorknob on the door from the house into the garage, but luckily he was a couple inches too short to reach it from the floor.
Admin
The
^
is called a hat, and the thing I denote with>
is called a shoe.Admin
and
=)
is the rocket exhaust?Admin
Admin
No, that's an equals sign, a null, and a closing paren. The only strange thing about it is that the null is just not anything. Putting a space there would make it check against space rather than null, and we have no null keyword of any kind. Interestingly, a tab in there would be ignored.
Admin
You know, if you're going to code in something that looks like line noise , you really should just cut your losses and use Brainfuck. Good luck getting that on a requirements doc, though...
Admin
They should of course be stored as doubles. How else are you going to have room for them all?
Admin
Don't be ridiculous. GUIDs or GTFO. :stuck_out_tongue:
Admin
Gee, I don't know, I thought I invented that way to find if an item is in a set. The code could be a bit faster by prepending a "|" so the string search might run a bit faster than looking for a digit.
Admin
That being said, I’m surprised no one has noticed this pattern (here’s my rewrite):I started a rewrite to use integer conditionals (assuming every Id had a range as that seemed mostly apparent from the snippet my browser displayed), but when I copy/pasta'd the code into Notepad++, I got the full strings and saw that pattern did NOT work as anticipated. -_-
Wow, someone actually came up with a good argument for which way to hang the toilet paper! I have been “converted.” LOL. Now I’m glad the only pets I have are birds. ;-)Admin
I'm talking about the compiler basically building the initial RB-tree at compile time...
That would be an interesting optimization question for our resident compiler experts. @dkf?
Admin
I should think this one gets a unanimous "no," but I'm sure there's someone out there who doesn't read very carefully.
<yes, I know what he meant....math gramming!
Admin
If you at any point ever, and i do mean at any point, terminate that repeating sequence of nines, then the answer is absolutely no, if you literally have an uncountably infinite number of nines there after the decimal point..... possibly? if you're dealing with something in the real world then "if you've got enough 9's there it's close enough that within experimental tolerance it's indistinguishable from 1"
..... FICK! i missed where exactly the decimal point is.
:epic nooooooooooooo.png:
Admin
EDIT: Damn, I missed where the decimal point was, too. D'oh!
Admin
Assuming an infinite recurrence of 9s, you can prove that using logarithms
Admin
In my home, most door-handles are lever type. A cat can reach up and attempt to pull the handle down. It sometimes works.
In my previous house, which had more normal knobs, the two cats would work together. One would stand on his hind legs and attempt to turn the knob, while the other pushed the door. We put child-safety covers on the doorknobs to stop that.