What's the best / most interesting piece of code you've ever seen?
reddit.com
reddit.com
Runner up for me would be K-means clustering, one specific implementation of it.
let me have a look, I think I downloaded the code.
edit: I can't find it in a couple of minutes, I was interested in it because one of the companies I'm involved in needed a way to classify items without any prior knowledge of the number of groups so I read up on that.
The most interesting feature of that particular implementation was how they chose their initial 'seeds', that is a big factor in how long it takes for the algorithm to converge on a solution.
I'll keep looking for it, I'm fairly sure I have at least a bunch of references to it in an email and a copy of some papers on my harddrive somewhere. Note to self: get organized better.
There are a variety of implementations that speed this process up. One is canopy clustering which allows you specify which clusters the points will check with (ie checking with all the clusters takes a while, but canopies can limit the search). You can also try different distance metrics such as the triangle inequality.
There should be lots of implementations online, specifically for Matlab which you can port over to another language if you wanted. It's one of the most widely used and most useful clustering algorithms and often provides initialization for lots of other algorithms such as Expectation Maximization (EM).
This link comes to mind: http://cseweb.ucsd.edu/~elkan/fastkmeans.html
TeX: by a large margin the best-documented substantial piece of code I've ever seen, and (by now) with an extremely low bug count. As for the actual code, there's plenty to love and plenty to hate.
Lots of things in Norvig's "Paradigms of artificial intelligence programming". Nothing in there is really astonishing, but the standard is consistently extremely high and of course it's accompanied by a wealth of insightful comment, namely the actual text of the book.
Various bits of low-level bit-twiddling. I love the population-count trick that begins with x & 0x55555555, but typically it's not actually the best way to do the job (four lookups in a 256-element table will usually be faster), so instead I'll pick the trick for finding whether a word has a zero-byte in it (useful, e.g., for operations like strlen), the idea of which is basically to subtract 0x01010101 and look for evidence of borrows across byte boundaries.
Hard as I try, I cannot for the love of god single out one piece among them. Interestingly, I can immediatly name the worst piece of source code I have ever seen: SAP DB. (I don't even know whether it's still available online, but it was ugly {formatting inconsistent, somplaces littered with German comments, etc.}--there are shurely better contenders for this place, but the fact that one of the biggest software companies produced it, let alone having clients using it sent shivers down my spine...)
EDIT: PageRank, how could I have forgotten...
I can see why formatting would not make it pretty but that would not say much about the algorithm, and why would German comments be ugly ? Programmers in different countries routinely comment their code in their native language, it's only natural really.
ad Language: Of course its only natural, albeit unprofessional. I would expect anybody in a project to code using English, comments and identifiers--regardless of where the people actually come from. IMHO this is good coding practice, but probably you have different experiences.
Why? This might make sense for open source code at the moment, but if it's code internal to (say) a German organization, commenting in the native language of the coders is likely to be easier and lead to less confusion.
Most multi-nationals have coding standards that mandate the use of English though. But you'd be surprised how many companies that grew to be multi-nationals from regional companies have tons of code documented and written in the native language.
if user.hasFoo() and company.isBar() then baz.play( jazz )
In German: if benutzer.hastFoo() and firma.istBar() then baz.spiel( jazz )
That's five language-switches in a single line. Much, much harder to read. (granted, only one if the language uses && and braces instead of and and then)Yes. Maybe not harder, but more strenuous.
> No proficient coder will be reading lines of code as English sentences
In that case, I'm not a proficient coder. Neither was the inventors of the IF..THEN..ELSE and WHILE .. DO .. and DO .. WHILE .. statements. Or the guys who made the convention to use hasXX/isXX for boolean getters. Or LINQ.
> I don't think having a few keywords in a different language makes much difference.
I do, and I've coded and maintained a lot of code in both camps. And, we're not aiming for "much difference", we're aiming for "easier".
> You might just as well say that the switch in languages makes the code easier to read, since it separates out keywords from variable names.
Well, I don't think any proficient coder has any problem with that. We have syntax (and syntax highlighting) for that purpose.
So, what you actually mean to say here is "no"? Sorry, but I just don't see that having non-English identifiers makes it any harder (or more strenuous) to read.
>In that case, I'm not a proficient coder.
You certainly are not proficient if you attempt to read lines of code as English sentences because they happen to contain a few English words.
>Well, I don't think any proficient coder has any problem with that.
Nor will any proficient coder have a problem with a few English keywords.
http://www.chinesepython.org/cgi_bin/cgb.cgi/english/english...
Well you can't be sure of anything, but sometimes you know for more-or-less certain that English speakers aren't going to read the code.
Personally I code up all my stuff with identifiers, function names and comments in English.
But I can't see why a German company writing code would be chastised for writing their code in German.
I'd rather have them use German and do it properly than in broken English. Now in Germany most if not all programmers will be fluent (to some degree) in English, but in plenty of other countries that is not the case.
Being able to program should not come with an automatic requirement to be fluent in English as well.
Obviously there are issues with character sets when writing comments, plenty of language implementations wouldn't know what to do if you threw utf-8 at them, but I think that any modern implementation should allow for identifier names and comments to be in any language. Why should the English speaking world get a free pass here, and everybody else be at a disadvantage ?
It doesn't, and I merely said that I think it's good practice to do so. (For various reasons, e.g. a multi-national team, possible maintenance and re-engineering scenarios where it might be helpful to use standardized abbreviations, etc.) I have personally worked on a reengineering project that used German identifiers because of a very specific ERP system--it was ultimately decided not to use English identifiers because the translations would be too cumbersome. (This was a small company, what I am trying to say is a company like SAP [big] code a DBMS [inherently English domain] using German comments makes no sense to me...)
SAP = BIG = not a reason to do stuff in english
DBMS = inherently English domain because people don't use their own language, that's self-referential
Using German comments makes perfect sense, SAP being a German company.
It works like this: a company starts out making a product for their local market, they get successful and branch out, and then a large amount of time later their code gets opened up to programmers not familiar with the native language.
Now, if the company were American, nobody would have a problem with dealing with identifiers and comments in what is for them a foreign language, say English.
But I really don't see why an American programmer would have a bigger problem dealing with identifiers and comments in German than vv.
Computer programming is not an exclusively English domain, and if to you code with identifiers and comments in a foreign language is hard to read then you have a bit of a hint how much effort the rest of the world has to make in order to make that step.
The fact that English got there 'first' is already enough of a head start, that shouldn't mean it becomes mandatory.
Something similar happens in the world of scientific publishing, where basically publishing in Spanish, Greek or Dutch is equal to not publishing at all.
An algorithm with Dutch, German or Swahili identifiers and comments is exactly equivalent to one in English.
Probably if you spent enough time you could figure out what the comments meant even if you didn't speak the language. Not that that would be very productive, and that's why most larger companies (and definitely multinationals) standardize on English.
You should be happy that's the case, but it is by no means a requirement or a sign of not being professional. If 90% of the team is German I'm all for them coding in German.
PS: I would like to edit my previous comment on the "nobody can change except me"-thing but seem unable to do so. For clarification: I did mean it that takes initiative from me to change my preference and not to be denying written word by someone else as being able to change my mind (which would be obtuse and absurd since it would preclude learning anything from someone else).
Because you can't and you can 'catch' your mistake with a follow up comment there is absolutely no problem and the whole page makes sense.
What about DBs make it inherently English? If a German person needs to store data in a way that an Englishman might employ a DBMS, what construct does he turn to?
PS: btw: I am not an English native, nor a resident of any English-speaking country.
I disagree (I'm designing and implementing a language right now, as it happens). The most important quality of an identifier is that it identifies. Consider the identifier BHOCT. Does it consist of Latin letters or Cyrillic ones? You can't tell -- in many monospaced fonts, they look the same. In fact you could have 2 (or even 32) identifiers that look like BHOCT but which are all different.
Then there's the issue of recognising/recalling letters. If I see an identifier written in Latin letters, I can recognise the individual letters, keep thme in memory and write them down. If I see an identifier in a script I'm unfamiliar with, it just looks like a squiggle so I can't remember it. There are millions of people who know one of the Cyrillic, Chinese, Korean, Thai, Arabic, Devanagari, Tamil and Hebrew scripts, but how many people know all those scripts? Very few. So for identifiers, it's best to specify one script, the one that most people who use computers worldwide are familiar with, the Latin script.
The same argument applies to domain names, which are just identifiers anyway.
For instance many companies and individuals simply can't have their names expressed in the Latin alphabet.
Mine is one of them, but since the difference is minor I couldn't care less, but if you are from any one of a multitude of countries who have a script different from the western 26 character set then you're completely out of luck.
The internet is global, the DNS is a major component of it and it is absolutely a good idea that they try to be inclusive.
That this will lead to some adaptation issues is a nuisance, but since global networking is here to stay and we're currently in the infancy of that now is the time to make these changes.
If someone wants a UK passport, they have to express their name in the Latin alphabet. Given that there are British people with linguistic backgrounds from all over the world, including many different writing systems, and they all manage to do this, I disagree with your statement that it can't be done. It clearly can be done, and in fact is done on a regular basis.
Another impressive stuff are IMHO genetic programming, neural networks and language interpreters. Every programmer should play with this stuff.
http://betterexplained.com/articles/understanding-quakes-fas...