- 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
There are many levels of programming ability. You've got some way to go, young grasshopper.
Admin
Admin
If you want to access your data by index, you should store it in an ArrayList.
Unless you are using 1.x which doesn't have generics, your data should be stored in a generic List. It requires less memory for primitives, and is type safe.
Admin
this is the remove method of System.Collections.HashTable reflected ...
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] public virtual void Remove(object key) { uint num; uint num2; Hashtable.bucket bucket; if (key == null) { throw new ArgumentNullException("key", Environment.GetResourceString("ArgumentNull_Key")); } uint num3 = this.InitHash(key, this.buckets.Length, out num, out num2); int num4 = 0; int index = (int) (num % this.buckets.Length); Label_003A: bucket = this.buckets[index]; if (((bucket.hash_coll & 0x7fffffff) == num3) && this.KeyEquals(bucket.key, key)) { Thread.BeginCriticalRegion(); this.isWriterInProgress = true; this.buckets[index].hash_coll &= -2147483648; if (this.buckets[index].hash_coll != 0) { this.buckets[index].key = this.buckets; } else { this.buckets[index].key = null; } this.buckets[index].val = null; this.count--; this.UpdateVersion(); this.isWriterInProgress = false; Thread.EndCriticalRegion(); } else { index = (int) ((index + num2) % ((ulong) this.buckets.Length)); if ((bucket.hash_coll < 0) && (++num4 < this.buckets.Length)) { goto Label_003A; } } }
Admin
wow, just wow, i guess someone had a hangover and missed their "data structures theory" lectures. I'd be thinking about getting in some code auditors to check over some of this guys old code too.
I know there are some instances where you implement some data structures yourself(ie. first year CS classes) but rarely
I'm not sure how you could actually have worked as a programmer without understanding basic things like hashmaps, as well as the concern with the number of methods, i know i said it at the start but WOW.
Admin
If you're going to implement your own sort, why bother with Bubble Sort when there are others that are more efficient and just easy to implement.
What's the point? It has no advantages ... other than the fact that it's the first sorting algorithm you typically encounter in your average "how do I use this programming language" textbook.
Admin
OK, I'll bite.
Which standard sorting methods are just as easy to implement as bubble sort? (You used plural)
There are none simpler that work on unordered data.
And when I'm prototyping an idea, I'm focused on the idea, not the elegance of that piece of it... hell, I might even inline the sorting code rather than shove it in a function... just until I can confirm that my concept works.
Admin
It's often the case. Being a good programmer has much more to do with what might be called "motivation to learn" than it does with "formal training."
People with the former attribute continue to learn better ways to do things, while the latter forget everything they were formally taught and just get worse and worse ...
Admin
I take it you're on a quantum computer. (Lucky you.) The rest of us have to live with computers where this is O((n-1)*(n!)).
Admin
Mine has only 1 function, it should be the most efficient one:
DoIt(Action action,params arg[]) { switch(action) { } }//Action is an enum
Admin
The problem with C is that often your foot is actually a pointer to your head.
(note: looong time C/C++ programmer, now programming in C# ... so, no religious issues here, just the facts, ma'am)
Admin
Because some programmers don't understand the heap or pointers. Those people shouldn't be allowed to program in C/C++ or anything without garbage collection.
Now, that's not the end of it. If you're programming in Jython/Ruby/PHP/Perl/Whatever and you don't understand the heap and pointers, don't understand how you're language works with pass by reference / value, function lookup etc., you're going to write a bunch of crap that will cause your employer to rue the day they hired you. So you can (barely) get away with being an idiot with the right language. With the wrong one, you're dead.
Admin
did Yakir fire him on the spot?
Admin
"That's why VBScript is better"
We will all assume you are joking.
"...knows how costly function calls are..."
IE6? Firefox? Opera? Safari? Compared to what? In what context? For what purpose? Intense graphics for Trident? Yes. Request queuing in Firefox? No? Statistical calculations? That should be in compiled code anyhow.
Admin
You know, I thought you needed to get a college degree for these kind of jobs.... And I thought a college degree would preclude some complete stupidity?
Admin
uglyduckling says: "ARRRGHHHH ... Now i have seen EVERYTHING"
puts gun to james's head bang bang
evil smile
puts gun to own head bang
Admin
Admin
Meh. I've seen as many misfires from self-taught as from formally trained so I wouldn't be so categorical about it. The nature of the misfires are different though. Most self-taught misfires are due to gaping holes in their basic knowledge because they haven't been dragged through all the boring stuff. Most formally-trained misfires are due to intellectual laziness and arrogance. This WTF looks like a self-taught dud. Mind you, there's nothing stopping self-taught from being insufferably arrogant and formally trained from having gaping holes (amnesia sometimes sets in 5 seconds after final exams). This is why every job interview should involve writing (pseudo-)code. You just can't judge people on their papers.
CAPTCHA: pirates - gallow birds the lot of them. Yarr.
Admin
I said "in such a way", referring to James' linear key/value array. ;) Of course there are plenty of cases where you'd want to extend/replace library classes. The point was just that recognising and pointing out the extent of James' incompetence might make one seem arrogant. Which probably goes a long way towards explaining why James isn't flipping burgers for a living.
Admin
Random sort and.. what's it called.. repeatedly searching for and extracting the element with the lowest key, and building a sorted array out of that, I think it has a name. Insert sort is also fairly simple.
Well, actually bubblesort is simpler still. But "fast enough"? No way. For example quicksort is really simple to implement, like a slightly warped, recursive bubblesort, and my experience is it starts to outperform bubblesort at 10 or so elements. The extra overhead means nothing next to the difference in time complexity.
1000 elements is certainly well beyond the useful scope of an O(n^2) algorithm when quicksort is so simple. Even, I would say, for prototyping purposes.
Admin
Please do, that could be interesting :)
Admin
Must... resist... the urge... to reinvent... the wheel...
Admin
Admin
The real question should be why would anyone (well, anyone using Java or C# at least, apart from as College/University exercises) actually implement any sort algorithm? The sorting algorithm used by the Java Collections class is a modified mergesort with n log(n) performance, it's stable, it's damn fast and it's been extensively tested (both by Sun and indirectly by every developer that ever used it). I can think of no good reason to implement any other sort method in Java and I would be amazed to learn that C# (and other modern languages) don't have similar standard methods for sorting. As for C and C++, well most of these algorithms have been around for about 50 years if you can't find a "standard" version I'd recommend Robert Sedgewicks Algorithms in C/Algorithms in C++.
Admin
Confession: I stand guilty of having once implemented quicksort in Javascript (copied straight out of chapter 4 of K&R, and translated from C to Javascript) because I hadn't been working with the language long enough to know it had a built-in sort. As soon as someone told me, I recanted my ways.
Admin
Drinking and posting again, R_A?
Admin
So we started out to laugh as someone with weaker capabilities than us, thereby making us feel superior. However, in wading through the pages of self congratulation, I seem to see that most of us are debating extremely complicated aspects of this "simple problem"... So are we still superior or are we the dummies?
Admin
Yes. Or lookup the algorithm on Wikipedia, implement it once in an open-ended way and you're done. And yes, sorting in Java and C# is as simple as using the built-in functions, unless you're doing something really special-purpose (e.g. something that calls for radix sort).
Admin
Wow! I think this comment is the real WTF of the day.
As if Java or C# would protect you from serious bugs or security issues! This is just plain ignorance.
I agree that array bounds checking is a Good Thing. However, when it comes to memory management, there is a simple rule that applies to all languages out there: if you allocate a resource, you have to release it some time later.
In C++, this applies to all resources, including memory. In Java/C#, memory is handled by the garbage collector, and all other resources are simply a pain in the ass.
Consider this C++ code: ifstream f("filename"); ...do something
The SIMPLEST way to do this correctly in Java (without IoC tricks) is:
InputStream s = null; try { s = new FileInputStream("filename"); ... do something } finally { if (s != null) s.close(); }
Of course, InputStream s = new FileInputStream("..."); will also just work - for a while... (seen it so many times...sigh). And as memory is used more often than other resources (files, sockets, db connctions etc.), automatic memory management simply allows you to write much more of such broken code until you realize your stupidity.
Seriously, if you really find the concept of releasing resources hard to grasp, you should probably look for a 'flipping burger' job, will save you (and your colleagues) some frustration...
Admin
I once worked with a .Not programmer who wrote the following for his sleep function (I don't know VB so I'm writing it in Java):
public sleep(long milliSeconds) { long target = System.currentTimeMillis() + milliSeconds; while (System.currentTimeMillis() < target) { ; } }
And for some reason the process he would be waiting on to complete would rarely get done.
His qualifications: Well, I worked a lot with spreadsheets and decided to get into VB programming.
Unfortunately hiring managers (and recruiters) are happier at alphabet soup on resumes than anything else.
I had a few recruiters actually ask me "What is J2EE anyway?" after placing me. Of course this was back in the day where J2EE was just out but they wanted 5 years expierence.
Admin
If it's not obvious why this simplification works, think about conditions under which you'll be able to close the stream in the code. (Also note that you [i]can't[i] trivially combine this with catching a failure to open the stream; that requires another try round the outside...)
Admin
So there do you catch the exceptions FileInputStream("filename") can throw? This may be simple but it doesn't compile.
Admin
Haven't gone near Java for 7 years now, but should compile?
Side point, i like the C# way..
quite elegant, though if you wanted to catch Exceptions you would
Admin
INTRO -- arrogance and the ego
Being able to express open-mindedness and willingness to learn is hard when one receives an upper-level degree in Computer Science with honors and high GPA or spends tons of time to self-learn programming languages that creates a self-persuaded large ego that makes it natural to act arrogant (i.e., that they know more than everyone else). The trick is to deceive others around you that you're a novice, because the ego (i.e., selfishness that one is correct [and right] because of the time put into self-learning or the degree or both) wants you to keep the ego well-fed, which means remaining to feel superior over others. One can prevent the ego-emotions of feeling superior similar to one can prevent the hatred emotions that come when someone you feel has offended you.
ACT THE WAY OTHERS ACT -- silences the ego's emotional struggle (because you don't have to defend anything)
Ego is the inevitable curse to a person that has been proven by "society" (either high grades, SAT scores, hanging out with people that fully accept their ideas, or self-learning for a very long time) to be right and superior over others and the fact that being wrong makes you look stupid, powerless, and worthless. Saying that someone is wrong is basically saying that "my ideas are superior to yours". This persuasion is an argument that is being played out in every single conversation that you will endure. If [ your ego is crossed (e.g., if I were a slave owner, and somebody said that what I was doing is wrong) ] ; I'm going to feel inferior against the somebody that disagrees with me ("how dare they make me feel like I'm not doing 'right' and attempt to make me turn against myself"), especially if that somebody has no good support to suggest that I'm wrong with having slaves. I would feel that the only reason the person objects my slavery acts is to slap me in the face and say "My ideas are superior to yours". That's why if you spread ideas, do not spread ideas that the other person doesn't know or won't understand or is against their core beliefs, because they'll be convinced that they're ignorant and inferior; they'll resent you. Be safe. Act the way others act, and do what others do -- spreading information that makes you look "special" and more intelligent than everyone else will make others resent you for making them feel like they're wrong/stupid/ignorant (which multiplies if you make yourself look special with others around you), which in turn makes them feel powerless. Feeling powerless leads to resentment (secretive anger), and the person will blame you for how they feel about themselves (i.e., powerless). On the other hand, if you make someone feel powerful, they'll want you to stick around, and will reward you in any ways that they can (which is what you want, because you're steering them directly into your intentions). Their control over you becomes your control over them, which is a mutual relationship. You can deceive that you're helping them, but of course that depends on the situation.
DAMPENING EMOTIONAL RESPONSES THAT HURT CONTROL
To prevent being angry when being offended is to realize you're 100% responsible for being angry and what you think, and that arguments created by others are owned by them, and have nothing to do with you. You always have the choice to not be apart of their "game". Since you know the other person wants to start an argument and be "right", just agree with them, because control over them is more important than feeding an ego that is never fully quenched. Agreeing with somebody that you'd really like to disagree with is a great way to test yourself on how deceitful you can actually be. The more skilled you are at using deceit, the more easier it is to control others, because you aren't continually trying to feed your ego, you're trying to remain in the control seat. Remember, if they're convinced they're right, and if you were them, you'd be convinced too. It's a good technique to prevent your ego from persuading you that you should attack (either by the excuse of annoyance or being pushed around).
EXAMPLE OF SUPREME CONTROL -- babies
The argument everyone has about themselves is that they have to feel right/superior, which is an argument that everyone has within themselves. People that pretend to not be superior and therefore innocent (e.g., babies and kids) are the most powerful of all. They make you look superior and responsible, and they use that to their advantage to get what they want. "You should change my diaper because I can't do it, you'd hurt your ego of being a responsible parent if you didn't (and therefore how you think society looks at you is poor), and Mom is going to be pissed at your irresponsible actions". This feeling of "I need help because I can't do it and so therefore you should do it" is used a lot with females to get males to protect them, to give them money, and fix their cars. The problem with this scenario is that females will be labeled as "weaklings", which is used as a control-mechanism for the females to protect their intentions of wanting to be "lazy". We feel morally responsible to help, because if we don't, then we don't get our intentions fulfilled, reproduction, and our other intentions, like the need of fulfilling what the society thinks is "good" -- which comes with threats like jail time and people around you feeling inferior towards you.
But do babies need to feel superior? By pretending they are weak they get the control over the parents, and therefore they're practically superior.
ACT THE WAY OTHERS ACT 2 -- "society"
To prevent the ego wanting you to feel right/superior/correct over others (and therefore look at you with inferiority and resentment) is to consciously act the way they do. Nothing makes a person feel better and more accepted when you follow the rules that make a person "good" for the target person you want to control and use:
IDEAS ARE PERSUASIVE -- so use with care.
Aristotle said that ALL ideas are persuasive. If I said that 2=1, I'd bet you would be partially persuaded that 2 does equal 1, because deep inside you know that there is a probability that I'm right, therefore nobody could prove that I'm wrong. And since I didn't give any support, how could somebody counter-argue? Since I said 2=1, and you know that "society" would know that I'm wrong, you see me as inferior, because you see me as trying to be superior over others because I think I know more than everyone else. So even if I did know 2=1, or that Christianity is false or true, I would KEEP THAT TO MYSELF, because if everyone thinks I'm inferior, how am I suppose to get a pay raise? or a promotion? or fulfill my intentions? That's where deceit comes in. You keep your real ideas to yourself, and consciously play a power game. You play as the innocent, friendly, high-moral character, that makes others feel accepted into society, so that when it comes time for the boss to give a raise, the boss will choose the person that makes him/her feel less inferior (i.e., more in power, more in control, more RIGHT).
CONCLUSION -- back to jpaull
So back to the beginning. jpaull stated, "The ones [programmers] that turn into good developers are the ones that are open-minded and willing to learn." My response? It's the programmers that look open-minded and willing to learn. An employer can be deceived (e.g., with fake intentions that look real enough [e.g., to further pursue a career and my personal growth]) and therefore controlled to get what you want, which is the job. What you need to know is what you want, and how you can do get what you want cleverly, while using the potent weapon of deceit to your advantage.
Admin
They ask for 20K less than we do. Then you get the brilliant employers who think that the supposed cost savings is worth it and well written code is overrated and unnecessary.
Admin
I would gladly take James' place at that company. I know C# and apparently James does not. :P All I can say is "wow ...". My head hurts after trying to process his logic.
Admin
Boohoo in j2me world we still have to live with them, I remember the days of Collections, Autoboxing, Generics... but have to stick with Vectors and Enmerations and casting...
No enumeration for me... also, lots of people in my j2me team have never used Generics as they used j2se before it had them.
Captcha: gotcha
Admin
That's my thought. If you're prototyping, use the built-in sort. If you have to write your own, then you should have have one in your personal "library" that you can just call. In which case the complexity of merge-sort (or whatever) is packaged in an external function/method/class (depending on language).
Course I don't always do what I "should" do either ...
Admin
Here is a case where the radio and A/C do slow the car down:
http://www.seriouswheels.com/cars/top-2004-Caterham-Superlight-R500-Evolution.htm
0-100-0 in 10.73 seconds.
Admin
Reminds me of a University Educated guy I work with a while ago that used arrays instead of lists because they were faster. Never dawned on him that array resizing took so long. He also like to create his own specific bubble sort for each function.
Admin
Reminds me of something that happened on my 1st job after grad school (many, many years ago). I wanted to hang around the college since my wife was still working on her PhD and the only job I could get was in a gov't lab doing fortran & cobol. My boss hired a guy because he was down on his luck and went to his church. One of the first tasks we gave him involved reading a file from a tape. His program ran for hours on a small test tape before we killed the job. Upon examination he was opening and closing the tape before/after each read - open, find start of records, read one record, close, rewind tape, repeat...
One moral of this story - before you crab about civil servants, remember that you get the servants that you're willing to pay for.
Admin
However, with proper exception handling, your nested try catch approach is not really a simplification (in fact, when adding another stream its more complex). And putting cleanup code within another try block poses its own problems. Consider this:
versus
Admin
FYI: The simplest implementations of BubbleSort and InsertionSort for an array are about the same length, but InsertionSort for a linked-list is definitely shorter.
This article is the most WTF-like I've seen in a while!
Admin
Admin
Admin
That's right kids, if your employer hires nothing but idiots and you are hired there then that makes you and idiot to stay.
Admin
Rwy nimagu, zachet! ))))))
Admin
I take it you missed the "best-case" part. If the array is already sorted, this algorithm will run in O(n) time (i.e., the time it takes to check whether the array is sorted) even without bringing quantum effects into the discussion.
Admin
Better yet, don't implement sort at all. Use the builtin one :-)
Admin
Incorrect. What you actually want to do is sit on your behind and implement the synchronization at a higher level. This was already mentioned but I'll provide some examples just to prove the point. If you don't want to litter your entire code with synchronized blocks, make a higher-level wrapper that abstracts all operations that you need to be atomic (e.g. make a class ThreadSafeQueue or ThreadSafeStack). Actually, chances are that someone else already implemented a class that, unlike java.util.Vector, actually gives you the synchronization granularity you need. Now for the examples:
(Don't even get me started on iterating through the vector.)
None of the above examples is safe to use with java.util.Vector unless you add your own synchronized blocks. But if you need external synchronization anyway even for simple operations like these, and the internal synchronization will just add unnecessary overhead, why would you ever want to use it in the first place? Even if, as you say, the performance difference is small nowadays, the synchronization built into Vector and Hashtable will give n00bs a false sense of security.
"Your queue code has a race condition." n00b: "No way, I'm using java.util.Vector."