• (cs) in reply to Mike
    Mike:
    C# initializes integers to 0 automatically. Nice try, though.

    um, no it doesn't. At least not in the version I'm programming in. Nice try, though.

    int a, b;
    a = b;
    Error	1	Use of unassigned local variable 'b'	
    
  • (cs) in reply to jimlangrunner
    jimlangrunner:
    Just my $0.02. VB, C#, Whatever. Failure to initialize a variable is a hanging offense. At least in my town.

    When I started my current job 9 months ago, I inherited a load of perl code, along with some shell stuff. I still do stuff like "my $a = 0;" even though perl will happily recast that "int" to a string if you assign a string to it or whatever. In fact, having just "my $a;" is perfectly fine as well for that same reason, though I have been in the habit of initializing variables according to what their role is (as well as adding comments about what they are supposed to be used for).

    Anyway, the guy whose code I inherited didn't do any of that. Sometimes he didn't even lexically scope things. That sort of thing I change when I come across it (found a couple bugs along the way, too), but I have a hard time with the plain old non-initialized variables. If I add a var to a piece of code, do I go back and initialize the rest of the bare ones? And what do I initialize them to? Should I spend time searching through the code to see whether they are an int, string, hashref, whatnot? Because if I initialize, I'd like to set it to what it'll actually be used as so that some other guy a year from now will be able to tell that $tcnt is actually a numeric counting variable or whatever and is explicitly set to 0 because of that.

    It's a lot of work and takes time from other stuff, but it makes future maintenance easier. I'm having a hard time justifying it, though. But it looks really awkward to have one initialized variable next to a bunch that aren't. I'm conflicted.

    (And before you wags come off with "The real WTF is perl..." or some inanity, perl actually is the right tool for the job in this case. Though python would have been fine as well, and I kinda would have preferred it since it's generally easier to maintain unless the perl is very well written. But life is what it is.)

  • (cs) in reply to nate
    nate:
    "Excel 2007 has more columns."

    But the function was written in 2005.

    They were planning ahead.

  • Cheese Cake (unregistered) in reply to Quietust
    Quietust:
    Kim Johansson:
    Ok... n elements, I'm done.
    public int addUpAllExcelColumns ( params int[] args ) {
    	int a;
    	foreach(int b in args)
    		a += b;
    
    	return a;
    }
    

    You forgot to initialize "a" to zero. Nice try, though.

    Can't oneself just do this, to truly replace the function... I'm assuming its in Java of course.

    public long addUpAllExcelColumns(int... xs) { long sum = 0;

    for(long x : xs) sum += x;
    
    return sum;
    

    }

  • Buffled (unregistered) in reply to Kim Johansson
    Kim Johansson:
    Ok... n elements, I'm done.
    public int addUpAllExcelColumns ( params int[] args ) {
    	int a;
    	foreach(int b in args)
    		a += b;
    
    	return a;
    }
    

    Your prowess astounds and amazes sir or madam! Why, I'll bet nobody here could have figured out a more efficient way to do this until you posted!

  • Occasional ASP debugger (unregistered) in reply to wee

    [quote user="wee"][quote user="jimlangrunner"]... unless the perl is very well written. ...)[/quote]

    Still laughing at that one. Let me know if you ever find:

    1. A unicorn
    2. A pot of gold at the end of a rainbow
    3. Some well written PERL code.
  • (cs) in reply to Voodoo Coder
    Voodoo Coder:
    robisjarig:
    I don't believe it. People can't be that stupid

    I remember when I was a freshman in high school, learning BASIC on my TI-82...several of the early "programs" I wrote were, while not quite as abysmal as this, still fully qualified WTF's.

    <snip>

    A few short years later, we get to laugh at it, and pray that the writer has since moved away from programming and found a comfortable job providing full time desktop support, or installing broadband, or anything far away from programming. Or, he has at least learned how wrong this was. We all had to start somewhere...

    I daresay such could be said of some Igor Pro code I wrote, to automate the loading, plotting and analysis of a bunch of data. Hideous code, but works, and better than doing the same thing 3 hundred times over by hand.

  • (cs)

    It's nice to know there's worse code out there than the shit I just (hopefully) finished dealing with. 200+ lines to display the built-in keyboard. <____<

  • Capt. Obvious (unregistered) in reply to ShatteredArm
    ShatteredArm:
    Or, even if C# just had a way of taking in a flexible number of items of the same type as the last parameter to a function...
    Or, even if C# has a way of using the plus sign to delimit a flexible number of items you want to add. I would rewrite the function:
    /* adds up all the column values for a specific row from an excel file.
     * 20050823 - BSR - v1.0 - Adds only columns a to z.
     * 20050909 - BSR - v2.0 - Added support for adding columns aa to zz.
     * 20081231 - CptO - v3.0 - Added support for unlimited columns, using syntax "arg1+arg2+...+argN" instead of "arg1,arg2,...,argN"
     */
    public long addUpAllExcelColumns( long n )
    {
        return n;
    }
    
  • christian (unregistered) in reply to Robajob

    Well, there are 26*26=676 elements in original parameter list. He wants all overloads of that, so he wants to have 676 functions: a function of one argument, a function of two arguments, ..., a function of 676 arguments.

    Let's assume that he's okay with using just a single type for float-point numbers (long double) and a single type for ints (long long).

    Also, I assume that C# supports the idea where if the caller supplies the first n parameters, then the remaining parameters can take on default values. If that's the case, then the programmer can just write the versions of the function that take 676 parameters, and specify a default value of zero for all parameters. Of course, each of those 676 parameters must be either a (long long) or a (long double), so he'd still need to write 2^676 versions of that function.

    In base 10, that's means he'd have to write this many functions: 313528531882069915964662425689704934689422834087799421518938595239444503444259215719322355763768461419760165742407930953086580768238532773442734168210737755891992072530296122920016274326105279759841755136

    Which is a 204 digit number.

  • (cs)

    This shit can't be real...

  • coolpun (unregistered) in reply to JJ

    Its a common n00bism.

  • coolpun (unregistered) in reply to JJ
    JJ:
    Kim Johansson:
    Ok... n elements, I'm done.

    Not quite... you have an uninitialized variable there.

    Its a common n00bism.

  • (cs)

    "I don't understand programming at all. It's like the compiler is completely ignoring my comments."

  • (cs)

    TRWTF is that they didn't use VB. It has optional parameters!

  • nou (unregistered) in reply to Cheese Cake
    Cheese Cake:
    public long addUpAllExcelColumns(int... xs) { long sum = 0;
    for(long x : xs) sum += x;
    
    return sum;
    

    }

    LOL. Should be: for(int x : xs) sum += x;
  • Thomas (unregistered) in reply to jimlangrunner
    jimlangrunner:
    Voodoo Coder:
    Mike:
    C# initializes integers to 0 automatically. Nice try, though.

    Contrary to what some think, C# is not simply VB.NET with a semi-colon on the end.

    In any event, if C# does do that, would you mind telling my compiler? It seems to think I need to initialize it or something...

    Just my $0.02. VB, C#, Whatever. Failure to initialize a variable is a hanging offense. At least in my town.

    It is a ValueType so it is initialized...implicitly. I challenge you to instantiate a ValueType that isn't initialized. ;->

  • pob (unregistered)

    answer = addUpAllExcelColumns(1,2,3,...);

    or

    answer = 1+2+3+...;

    22 less keystrokes... why would you even have a function for this?

  • (cs)
  • fub (unregistered) in reply to nou

    it's called casting lolz

    hard working + dumb = segmentation fault

  • Cacci (unregistered) in reply to Rob

    Best post!!

  • hmmm (unregistered)

    Someone should teach the "Business owner" of this system how to write Excel formulas.

    =SUM(A1:ZZ1)

  • Intruder VS1400 (unregistered)

    The real question is... how fast is his "algorithm". If speed is more important than code size, this could be an ideal solution.

  • Dascandy (unregistered) in reply to Kim Johansson

    C++ ey...

    template <typename T> class detail { T addUpAllExcelColumns(T... arguments);

    T addUpAllExcelColumns(T first, T... arguments) { return first + addUpAllExcelColumns(arguments...); }

    T addUpAllExcelColumns() { return 0; } };

    Made it generic too; you can now add 702 dates too! And more! And less!

  • Junkman (unregistered) in reply to Intruder VS1400
    Intruder VS1400:
    The real question is... how fast is his "algorithm". If speed is more important than code size, this could be an ideal solution.
    I'd speculate that if they're tearing these values off an Excel sheet the bottleneck might be elsewhere...

    I don't think there's any excuse for that function to exist at all. If I got myself into a position requiring that kind of solution my brain would be screaming bad design at me...

  • 13374u (unregistered) in reply to werdan

    When I wrote this function, I wanted to be efficient. This will actually execute faster on the low level than using several conditional branches. This is assuming that your machine actually has this many registers. We, of course, have employed 20 CEs and EEs to make this happen.

  • Anders Hesselbom (unregistered)

    This is nothing to worry about. It looks like it can be rewritten in one minute or two. What strikes me is that the original programmer didn't stopped coding and started to wonder what he was doing after one or two days of adding parameters....

  • iMalc (unregistered)

    Laziness is the foundation of efficiency, thus the desireable quadrant really is the top right.

  • CrashCodes (unregistered)

    So who's calling this function that would rather call one of the overloads like this:

    sum = addUpAllExcelColumns(a, b, c, d, e);

    instead of

    sum = a + b + c + d + e;

  • Anonym (unregistered)

    Why "_as"?

  • Slicerwizard (unregistered) in reply to coolpun
    coolpun:
    Its (sic) a common n00bism.
    Indeed it is.
  • square brackets (unregistered) in reply to Slicerwizard
    Slicerwizard:
    coolpun:
    Its [sic] (sic) [sic] a common n00bism.
    Indeed it is.
  • (cs) in reply to Bob
    Bob:
    Excel 2007 has more columns.
    New! and Improved! Now with more columns!
  • (cs) in reply to Anonym
    Anonym:
    Why "_as"?
    "as", "is" and "do" are keywords in c#. There are probably more two letter keywords in c#.
  • Real-modo (unregistered) in reply to Voodoo Coder
    Voodoo Coder:
    Contrary to what some think, C# is not simply VB.NET with a semi-colon on the end.
    Waaah! Another illusion shattered! I wanna go back to 2008!
  • The Colonel (unregistered)

    We need column C to accept decimal values (but the return value has to remain long), and for the function to output the result as the last argument. Additionally, we want to populate a global variable. Also, in order to access the data from VBA, we need you to deserialize a comma-separated list of values we outputted from Excel to system environmental variables. Due to restrictions on how much data they can hold, we have devised a clever plan that names them as follows (xcelCount, xcelData1, xcelData2, xcelData, etc) (only 100 bytes are stored in each environmental variable, because we standardized on base 10). Please have this done before you leave.

  • Dlareg (unregistered) in reply to hmmm

    Or even smaller =SUM(A:ZZ)

  • (cs) in reply to werdan
    werdan:

    http://en.wikiquote.org/wiki/Kurt_von_Hammerstein-Equord

  • (cs) in reply to Ie

    http://msdn.microsoft.com/en-us/library/s1ax56ch(VS.80).aspx

    "Local variables in C# must be initialized before being used." I don't think its a warning by that statement.

  • (cs) in reply to Maurits
    Maurits:
    http://en.wikiquote.org/wiki/Kurt_von_Hammerstein-Equord

    lol. I wonder if rule 34 applies too. :-)

  • (cs) in reply to Dave
    Dave:
    C# 2
    Isn't that C#++?
  • m^2 (unregistered)

    What problems did the programmers have with 600 params? for(int i=0;i<600;++i) { __asm{push 0} }

  • Kilgore (unregistered) in reply to Patrick Harrington

    Its future-proof!

  • A-Nona-Mouse (unregistered)

    just wait for C# 4.0, and use optional parameters <grin>

  • (cs)

    Job security get.

    import java.io.BufferedOutputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.PrintStream;
    import java.util.ArrayList;
    

    public final class CodeGen { public static final void main(String[] args) throws IOException { excelCNameBuffer(18278); boolean java = true; int col = 2; int max = 1;

    	PrintStream out = new PrintStream(new BufferedOutputStream(new FileOutputStream("generated."+(java?"java":"cs")),1024*1024));
    	out.println("//Totally not automagicly generated.\n");
    
    	if(java) out.println("public final class ExcelColumnAdder {\n  private ExcelColumnAdder(){}\n");
    	for(int i=1;i<=col;i++) max+=Math.pow(CHARS.length,i);
    	for(int i=1;i<max;i++) {
    		if(i%100==0) System.out.println("Generated: "+(i/(float)max)*100+"%");
    		out.println(
    			(java?"  /**\n   ":"/")+"* adds up all the column values for a specific row from an excel file.\n" +
    			(java?"  ":"")+" * 20050823 - BSR - v1.0 - Adds only columns a to z.\n" +
    			(java?"  ":"")+" * 20050909 - BSR - v2.0 - Added support for adding columns aa to zz.\n" +
    			(java?"  ":"")+" * 20100823 - Lym - v3.0 - Overloaded function.\n" +
    			(java?"  ":"")+" * 20100824 - Lym - v4.0 - Fixed overflow bug.\n" +
    			(java?"  ":"")+" */\n" + 
     			(java?"  ":"")+"public"+(java?" static":"")+" long addUpAllExcelColumns("
    		);
    		for(int j=0;j<i;j++) {
    			if(j%26==0||(j%26)%8==0) out.print((java?"  ":"")+"  ");
    			out.print("long c_"+excelCName(j));
    			if(j!=i-1) {
    				out.print(", ");
    				if(j%26==25) out.print("\n\n");
    				if((j%26)%8==7) out.print("\n");
    			}
    		}
    		out.println(")\n"+(java?"  ":"")+"{\n"+(java?"  ":"")+"    return");
    		for(int j=0;j<i;j++) {
    			if(j%26==0) out.print((java?"  ":"")+"      ");
    			out.print("c_"+excelCName(j));
    			if(j!=i-1) {
    				out.print("+");
    				if(j%26==25) out.print("\n");
    			}
    		}
    		out.println(";\n"+(java?"  ":"")+"}\n");
    		out.flush();
    	}
    	if(java) out.println("}");
    	out.flush();
    }
    
    private static final char[] CHARS = "abcdefghijklmnopqrstuvwxyz".toCharArray();
    private static final ArrayList<String> BUFFER = new ArrayList<String>();
    private static final void excelCNameBuffer(int l) {
    	BUFFER.clear();
    	for(int i=0;i<l;i++) BUFFER.add(excelCName(i));
    }
    private static final String excelCName(int i){
    	if(BUFFER.size()>i) return BUFFER.get(i);
    	StringBuilder result = new StringBuilder();
    	while(i>=CHARS.length) {
    		result.append(CHARS[i%CHARS.length]);
    		i/=CHARS.length;
    		i--;
    	}
    	result.append(CHARS[i]);
    	return result.reverse().toString();
    }
    

    }

  • MIKE (unregistered) in reply to Anonymous
    Anonymous:
    Someone:
    It is C# you don't need to initialize variables.

    This is incorrect.

    Yes, it is true that ints default to 0, but you should still initialize.

Leave a comment on “Classic WTF: A Function to Quit For”

Log In or post as a guest

Replying to comment #:

« Return to Article