- 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
Where's the WTF? It's only a braindead method with quadratic time complexity. At University we had an assignment to develop a resource-planning application, A co-student of mine wrote code that performed a task that required only a linear-time algorithm in O(n^5) time. That is, use 10 times the amount of input data, and the time required to process it increases 10.000fold.
If only I had known about thedailywtf.com at that time.
Admin
Blast! Should've been 100.000fold.
Admin
That is precisely the point in The Mythical Man Month: not that adding *any* programmers to a project (i.e. more having more than one) is going to make it late, but that adding *new* programmers to a project which is already under way *is* going to necessarily delay it; because you need to get the new programmers up to speed on the design, implementation strategy, tools, etc., plus possibly introduce some communications problems and management complexity that weren't there before. All this occuring *during* the software implementation.
This is why "Brooks' Law" has usually been quoted as "adding manpower to a late software project makes it later".
I could certainly see why entering into a semantics argument with Mr. Brooks about having 1+N programmers as opposed to just 1 programmer, would make him throw his arms in the air and dismiss the argument altogether. It means you didn't get his point.
-dZ.
Admin
Why the hell would you try to assign "getRols() ? roles : roles.Clone()" (with roles being an uninitialized variable) to the rvalue "null == roles"? If you were "clever", you would first get your operator precedence straight. In this case, comparison > ternary conditional (?:) > assignment.
Yes, linear in the call to getRols(). Now if getRols() takes amortized constant time (i.e. if it caches the result), the total time complexity would be linear. But that seems to be wishful thinking.
Admin
Because the IDE won't let him proceed without doing something, but he has no idea what to do other than the default IDE stub.
Admin
I say three, three, three database calls for the price of one unnecessary wrapper function. So it goes from what should be O(n) to O(n^3) for no good reason at all. Scarry.
Admin
getRols() does in fact make a database call:
I suppose you're one of those people who fell for the "Read the entire test before starting," trick (where the last page of the test says, "Put your name on the paper, turn it over and sit quietly until the end of the test"). :) But at least you recognize the potential for a performance problem (ergo, you would probably not make the same mistake yourself).
I'll admit that I've been lazy and done something similar once or twice, but most of what I write is Python or Perl to help me spit out documentation faster. I'm a technical writer, not a programmer. And, holy shit, I've never been dumb enough to do it with a database.
Also, what are the conventions like in C#? It seems pretty weird (coming from somebody who started with C++ and Java) to have a function that can either return an ArrayList or null, which necessitates sanity checking whenever you call it. And GetRols() is as dumb a function name as anybody could come up with. Would it really have been a problem to say GetRolesFromDatabase() instead? Perhaps if they'd used that instead, the programmer writing this function would've realized they're making a buttload of slow SQL queries.
Admin
I think you're looking a little too far into this ...
Admin
Holy crap, this makes me want to cry. Imagining a whole application written like this makes me seriously depressed.
Admin
The real WTF is that a manager allows developers to write an application without a single comment in the code. We are then left to guess what a particular method is supposed to accomplish by wading through the code and debating whether it's a failed cache mechanism, or really, really bad code. If it's just really, really bad code, we are still left with guessing the original intent. After the senior developer had re-written the code, I'd have fired the manager.
To me, the best indication of a developer's worth is how well the code is documented with comments. When I started programming, I was given some very good advice by a very good coder: for (just about) every line of code, there should be a comment. If there aren't then I have a very low opinion of the developer. His/her view is, no doubt, that if someone is unable to follow the uncommented code, then then that someone is an idiot.
This reminds me of a book I had to read in college - on the way to my degrees in History and Philosophy. It started out in English, then started to include long untranslated quotations from French and German writers. This didn't seem too bad - I knew French and I had to do was find someone who knew German. Then came the quotations in Latin, and I gave up when I got to the quotations in Greek.
Nobody becomes a good programmer without being a bad programmer first. And as we get better, we will come across our own code and wonder "WTF?". The only thing that will help things along is good commenting. I remember writing my first dll in C, and there were many, many comments. Months later, we had to resolve a bug. Another developer had to look at my code - his speciality was SQL, not C. Instead of running to me, he was able to understand the code, and trace the problem to an incorrect ODBC setting on the server.
Admin
No, it should be one. One single call to the database is all that is required. This wonderful function turns it into 2n+1 calls to the database, where n is the number of items returned by getRols.
So, if there are 10 roles in the database, then this function makes 21 calls to the database when only 1 is necessary. If there are 50 roles in the database the function makes 101 calls to the database when only one is necessary. I mean what does the time it takes to loop through an array and populate another matter in the face of that?
Admin
the trouble with comments is that they are often neglected as the code evolves... better to choose a verbose and self explanatory naming policy, to keep things short and to always remember that programmers are stupid and forgetful.
Comments on every line tend to be a patronising duplication of the code itself.
Admin
getROFL();
(CAPTCHA: "genius")
'nuff sed.
Admin
That's not a WTF. That's an OMFG.
Admin
That's part of the reason for breaking big projects into smaller ones: that way, not as many lines of communication are required. Of course, that brings its own problems at integration time.
Admin
No. No. No^3.
The O function seems to be one of the most mightily abused concepts in this forum.
Just for the sake of correctness, and as previously stated, the code in question has linear time complexity depending on the number of roles, assuming the database access takes approximately constant time (i.e, can be done in O(1)). As we all have learned, O(2n+1) = O(n).
So the WTF is that she coded it in O(n) instead of O(1).
And for sacrilegious misuse of the O function I suggest the term OTF (Oh the...)
Sofayeti
Admin
Not exponentially, please. Its only O(n^2) (to be precise, n*(n-1)/2).
Admin
This is the perfect answer to this WTF. I simply don't get it : we have a lot of comments of people who did not understand this simple WTF. Go wonder why we need faster processors!
I would work with you anytime!
Philippe
Admin
All Your Comment Are Belong To Us!
Admin
But it's not O(1), getRols returns an array of some sort with the result set in it. Simply creating that array is O(n) where n is the number of items. Since we do it 2n+1 times that gives us O(n^2)
Admin
The problem with the 10 junior developers is that they're not just junior, they're 10 of them and they all don't know what to do when with what..
<sigh />
Admin
But it's not O(1), getRols returns an array of some sort with the result set in it. Simply creating that array is O(n) where n is the number of items. Since we do it 2n+1 times that gives us O(n^2)
now lets review O(n). it's not 2n^2+1 now is it? you end up in a loop that is 2^n so you don't just get 25 lists you get 2^25 lists people.
Admin
Admin
Ok, it's +2!!! +2!!!! (not like that matters anyway because it gets taken out, but still!)
getRols is called twice for each loop. It is also called once before, and once for the final not true loop check.
Simple table:
(assuming getRols has n Execution time (otherwise it's just too simple))
of items. # of calls to getRols getRols execution time total "time"
2 6 2 2^6 = 64
4 10 4 4^10 = 1048576
10 22 10 10^22 = 1x 10^22 (lol)
n^(2n+2)
u can simplify if u want.
#include <disclaimer>
char GoatCheez[]="brillant!";
Admin
The C# compiler doesn't optimize the loop out of the generated IL (check it out for yourself). So in fact this is a WTF. In anycase why anyone would want to write the code like this, even if the compiler optimized it out, is beyond me.
Admin
I'm guessing you're one of the 10 programmers they let go...
Admin
Where is this 2^n loop? WTF?
Admin
That is correct. If you want, you may say the WTF is that she coded it in O(n^2) instead of O(n).
At least we know that it _could_ be done in about O(1), e.g. by caching the results in an immutable ArrayList (if such thing exists in C#).
Sofayeti
Admin
You're right about the +2. Still I'd want to "simplify" to n*(2n+2).
Sofayeti
Admin
Why did you feel the need to obfuscate the code? The lack of types in PHP is compounded by your brilliant variable naming strategy, so I would expect lengthRoles to be an integer (the length / size of the array) and fetchRoles to perhaps be a function pointer (address of a function that fetches something)... no, wait, they're both assigned the return value of getRols(), so they must both be arrays of roles...
The code in the original post was at least easy to read, if not exactly optimized for performance...
Admin
This is what you get when you teach "premature optimization is the root of all evil."
captch: stfu
Admin
getRols(...) is not by definition an abbreviation. Assuming that this code is written in The Netherlands (as the name of the believer suggests), the author of this code has used the dutch word for Role, namely Rol.
However, it is of course quite a bad habit to mix up your languages. One should do it in all dutch, or in all English.
Second however: Rols is not the plural form of Rol, the method name should have been getRollen(...).
I'm not sure if this speaks for or against the author of this code ;-)
Admin
I have a very low opinion of a developer if there is a need "for (just about) every line of code" to have a comment. If they have to resort to explaining what (just about) every line does then something is fundamentally wrong with the way it is coded. As such, that advice from "a very good coder" was bollocks.
And this is relevant how? Comments are optional, whereas what you needed was an appendix, or at the very least a translation footnote for each quotation.
Admin
Exactly. An experienced programmer must be able to read well-written code without constant need for comments.
Comments are usefull to explain uncommon circumstances, the reason why something is necessary, warn against pitfalls. JavaDoc-Style comments are obviously usefull to generate formal documentation. The last thing I want in my code are redundant comments like
i++; // increase i by 1