What is the best comment in source code you have ever encountered?
问题:
回答1:
I am particularly guilty of this, embedding non-constructive comments, code poetry and little jokes into most of my projects (although I usually have enough sense to remove anything directly offensive before releasing the code). Here's one I'm particulary fond of, placed far, far down a poorly-designed 'God Object':
/** * For the brave souls who get this far: You are the chosen ones, * the valiant knights of programming who toil away, without rest, * fixing our most awful code. To you, true saviors, kings of men, * I say this: never gonna give you up, never gonna let you down, * never gonna run around and desert you. Never gonna make you cry, * never gonna say goodbye. Never gonna tell a lie and hurt you. */
I'M SORRY!!!! I just couldn't help myself.....!
And another, which I'll admit I haven't actually released into the wild, even though I am very tempted to do so in one of my less intuitive classes:
// // Dear maintainer: // // Once you are done trying to 'optimize' this routine, // and have realized what a terrible mistake that was, // please increment the following counter as a warning // to the next guy: // // total_hours_wasted_here = 42 //
回答2:
Exception up = new Exception("Something is really wrong."); throw up; //ha ha
回答3:
//When I wrote this, only God and I understood what I was doing //Now, God only knows
回答4:
stop(); // Hammertime!
回答5:
This seems to stop morons from messing my code...
// Autogenerated, do not edit. All changes will be undone.
回答6:
// sometimes I believe compiler ignores all my comments
回答7:
// I dedicate all this code, all my work, to my wife, Darlene, who will // have to support me and our three children and the dog once it gets // released into the public.
回答8:
//Code sanitized to protect the foolish. using System; using System.Collections.Generic; using System.Text; using System.Reflection; using System.Web.UI; namespace Mobile.Web.Control { ///
/// Class used to work around Richard being a fucking idiot /// ////// The point of this is to work around his poor design so that paging will /// work on a mobile control. The main problem is the BindCompany() method, /// which he hoped would be able to do everything. I hope he dies. /// public abstract class RichardIsAFuckingIdiotControl : MobileBaseControl, ICompanyProfileControl { protected abstract Pager Pager { get; } public void BindCompany(int companyId) { } public RichardIsAFuckingIdiotControl() { MakeSureNobodyAccidentallyGetsBittenByRichardsStupidity(); } private void MakeSureNobodyAccidentallyGetsBittenByRichardsStupidity() { // Make sure nobody is actually using that fucking bindcompany method MethodInfo m = this.GetType().GetMethod("BindCompany", BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); if (m != null) { throw new RichardIsAFuckingIdiotException("No!! Don't use the fucking BindCompany method!!!"); } // P.S. this method is a joke ... the rest of the class is fucking serious } ////// This returns true if this control is supposed to be doing anything /// at all for this request. Richard thought it was a good idea to load /// the entire website during every request and have things turn themselves /// off. He also thought bandanas and aviator sunglasses were "fuckin' /// gnarly, dude." /// protected bool IsThisTheRightPageImNotSureBecauseRichardIsDumb() { return Request.QueryString["Section"] == this.MenuItemKey; } protected override void OnLoad(EventArgs e) { if (IsThisTheRightPageImNotSureBecauseRichardIsDumb()) { Page.LoadComplete += new EventHandler(Page_LoadComplete); Pager.RowCount = GetRowCountBecauseRichardIsDumb(); } base.OnLoad(e); } protected abstract int GetRowCountBecauseRichardIsDumb(); protected abstract void BindDataBecauseRichardIsDumb(); void Page_LoadComplete(object sender, EventArgs e) { BindDataBecauseRichardIsDumb(); } // the rest of his reduh-ndant interface members public abstract string MenuItemName { get; set; } public abstract string MenuItemKey { get; set; } public abstract bool IsCapable(CapabilityCheck checker, int companyId); public abstract bool ShowInMenu { get; } public virtual Control CreateHeaderControl() { return null; } } }
Update: The original author of the code has outed himself so I must give credit where it is due. Dan McKinley left the company I was with shortly after I started, and he talks more about the code, explaining some background and a few more "WTF's" that 'Richard' wrote.
回答9:
// somedev1 - 6/7/02 Adding temporary tracking of Login screen // somedev2 - 5/22/07 Temporary my ass
回答10:
// drunk, fix later
Wish I were kidding. And knowing the developer who wrote the code, I think he meant it literally.
回答11:
// Magic. Do not touch.
回答12:
#define TRUE FALSE //Happy debugging suckers
回答13:
// I'm sorry.
回答14:
return 1; # returns 1
回答15:
/* This is O(scary), but seems quick enough in practice. */
followed by four nested for-loops
回答16:
// Replaces with spaces the braces in cases where braces in places cause stasis $str = str_replace(array("\{","\}")," ",$str);
回答17:
It speaks volumes about our profession that when asked about the "best comment", we all answer with the worst comments we can find...
回答18:
long john; // silver
回答19:
Catch (Exception e) { //who cares? }
回答20:
/** * Always returns true. */ public boolean isAvailable() { return false; }
Never rely on a comment...
回答21:
/* * You may think you know what the following code does. * But you dont. Trust me. * Fiddle with it, and youll spend many a sleepless * night cursing the moment you thought youd be clever * enough to "optimize" the code below. * Now close this file and go play with something else. */
回答22:
try { } finally { // should never happen }
回答23:
From: https://github.com/zepouet/Xee-xCode-4.5/blob/master/XeePhotoshopLoader.m#L108
// At this point, I'd like to take a moment to speak to you about the Adobe PSD // format. PSD is not a good format. PSD is not even a bad format. Calling it // such would be an insult to other bad formats, such as PCX or JPEG. No, PSD // is an abysmal format. Having worked on this code for several weeks now, my // hate for PSD has grown to a raging fire that burns with the fierce passion // of a million suns. // // If there are two different ways of doing something, PSD will do both, in // different places. It will then make up three more ways no sane human would // think of, and do those too. PSD makes inconsistency an art form. Why, for // instance, did it suddenly decide that *these* particular chunks should be // aligned to four bytes, and that this alignement should *not* be included in // the size? Other chunks in other places are either unaligned, or aligned with // the alignment included in the size. Here, though, it is not included. Either // one of these three behaviours would be fine. A sane format would pick one. // PSD, of course, uses all three, and more. // // Trying to get data out of a PSD file is like trying to find something in the // attic of your eccentric old uncle who died in a freak freshwater shark // attack on his 58th birthday. That last detail may not be important for the // purposes of the simile, but at this point I am spending a lot of time // imagining amusing fates for the people responsible for this Rube Goldberg of // a file format. // // Earlier, I tried to get a hold of the latest specs for the PSD file format. // To do this, I had to apply to them for permission to apply to them to have // them consider sending me this sacred tome. This would have involved faxing // them a copy of some document or other, probably signed in blood. I can only // imagine that they make this process so difficult because they are intensely // ashamed of having created this abomination. I was naturally not gullible // enough to go through with this procedure, but if I had done so, I would have // printed out every single page of the spec, and set them all on fire. Were it // within my power, I would gather every single copy of those specs, and launch // them on a spaceship directly into the sun. // // PSD is not my favourite file format.
回答24:
const int TEN=10; // As if the value of 10 will fluctuate...
回答25:
#Christmas tree initializer toConnect = [] toRead = [ ] toWrite = [ ] primes = [ ] responses = {} remaining = {}
回答26:
About the middle of a 30 page xslt
回答27:
long long ago; /* in a galaxy far far away */
回答28:
in a completely uncommented 2000 line method
{ { while (.. ){ if (..){ } for (.. ){ } .... (just putting in the control flow here, imagine another few hundred ifs) if(..) { if(..) { if(..) { ... (another few hundred brackets) } } } //endif
(I actually grepped out all the brackets one day just to see how bad it was, and, sans formatting, got this:
{{{{}}{}{}{}{}}{{}{{}{}{}{}{}{}{{}{}}{}{}{{}{}{}{}{}{}{}{}{}{}{}{{}}}{{}{{}}{{{}}}{{}{}{}{}{}{}{}{{}}{}{{{}}{}{{}{}}{{{}}{}{}{}{}}{{}}}{}{{}{}{}{{}{{}}{}}{{}}}{{}}{{}}{{}}{}{{}}{{}}{{}}{{}{}{}}{}{}{{{}}{{}}}{}{}{}{}}{{{}{{}{}{}{{}{}{}{}{}{}}{}}{{}}{{}{}}}{{}}{{}}}{{}}{{}}{}{}{}{}{{}}{{}{}{}{}}}}{}{}}{{}{{{}{}{}{}}}}{{}{{{}}}}{{}{{{}{{}}{}{{}}{}{{}{}}{{}}{}{{}}}{{}}}}{{}{}{}{}{}{{{} {{{{}}{}{}{}{}}{{}{{}{}{}{}{}{}{{}{}}{}{}{{}{}{}{}{}{}{}{}{}{}{}{{}}}{{}{{}}{{{}}}{{}{}{}{}{}{}{}{{}}{}{{{}}{}{{}{}}{{{}}{}{}{}{}}{{}}}{}{{}{}{}{{}{{}}{}}{{}}}{{}}{{}}{{}}{}{{}}{{}}{{}}{{}{}{}}{}{}{{{}}{{}}}{}{}{}{}}{{{}{{}{}{}{{}{}{}{}{}{}}{}}{{}}{{}{}}}{{}}{{}}}{{}}{{}}{}{}{}{}{{}}{{}{}{}{}}}}{}{}}{{}{{{}{}{}{}}}}{{}{{{}}}}{{}{{{}{{}}{}{{}}{}{{}{}}{{}}{}{{}}}{{}}}}{{}{}{}{}{}{{{}{}{{}}{}}}{}}{{}}{{}{}}{{}{{}{{}}}}{{{}{{{}}}}}{{{{{}}}}}{}{}{}{{{{}}}{}{}}{{}{{}}}}{}{{}}{}}}{}}{{}}{{}{}}{{}{{}{{}}}}{{{}{{{}}}}}{{{{{}}}}}{}{}{}{{{{}}}{}{}}{{}{{}}}}
The endif showed up around line 800)
回答29:
//This code sucks, you know it and I know it. //Move on and call me an idiot later.
回答30:
// If this comment is removed the program will blow up