Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed
code:
Extents3d extenst = blockRef->GeometricExtents;
for (int i = 0; i < 4; i++)
{
	switch(i)
	{
	case 0:
		p.SetValue(extenst.MinPoint.X, extenst.MinPoint.Y, 0.);
	break;

	case 1:
		p.SetValue(extenst.MaxPoint.X, extenst.MinPoint.Y, 0.);
	break;

	case 2:
		p.SetValue(extenst.MaxPoint.X, extenst.MaxPoint.Y, 0.);
	break;

	case 3:
		p.SetValue(extenst.MinPoint.X, extenst.MaxPoint.Y, 0.); 
	break;
	}
	pointArray->SetValue(p, i);
}
17 lines to avoid duplicating a trivial line of code three times.

Adbot
ADBOT LOVES YOU

Cocoa Crispies
Jul 20, 2001

Vehicular Manslaughter!

Pillbug

Plorkyeran posted:

code:
Extents3d extenst = blockRef->GeometricExtents;
for (int i = 0; i < 4; i++)
{
	switch(i)
	{
	case 0:
		p.SetValue(extenst.MinPoint.X, extenst.MinPoint.Y, 0.);
	break;

	case 1:
		p.SetValue(extenst.MaxPoint.X, extenst.MinPoint.Y, 0.);
	break;

	case 2:
		p.SetValue(extenst.MaxPoint.X, extenst.MaxPoint.Y, 0.);
	break;

	case 3:
		p.SetValue(extenst.MinPoint.X, extenst.MaxPoint.Y, 0.); 
	break;
	}
	pointArray->SetValue(p, i);
}
17 lines to avoid duplicating a trivial line of code three times.

Take a drink every time you see a for-switch in this thread.

Hughlander
May 11, 2005

This is mild for the thread but I need to vent since the person who wrote this left the company right before shipping and left me to take over their systems...

How do you know when you need to refactor someone's code?

When a 300 line function ends with:
code:
                                                }
                                          }
                                    }
                              }                       
                        }
                  }
            }
      }
}

revmoo
May 25, 2006

#basta
^^Holy crap!


This cracked me up:

function xhtml_foot()
{
?>
</body>
</html>

<?
}

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe

revmoo posted:

function xhtml_foot()
{
?>
</body>
</html>

<?
}

What possible reason is there for this to be valid :ssj:

Coffee Mugshot
Jun 26, 2010

by Lowtax
EDIT: Nevermind, I see the differences now. I'm the coding horror.

pr0metheus
Dec 5, 2010

Plorkyeran posted:

17 lines to avoid duplicating a trivial line of code three times.

Shouldn't this just be

code:
Extents3d extenst = blockRef->GeometricExtents;
for (int i = 0; i < 4; i++)
{
        p.SetValue(extenst.MinPoint.X, extenst.MaxPoint.Y, 0.); 	
	pointArray->SetValue(p, i);
}
EDIT: oh, never mind.

Opinion Haver
Apr 9, 2007

Yeah, the coding horror is that they didn't figure out whether to use MaxPoint or MinPoint by examining i/2 and i % 2 :colbert:

Blue Footed Booby
Oct 4, 2006

got those happy feet

Hughlander posted:

This is mild for the thread but I need to vent since the person who wrote this left the company right before shipping and left me to take over their systems...

How do you know when you need to refactor someone's code?

When a 300 line function ends with:
code:
                                                }
                                          }
                                    }
                              }                       
                        }
                  }
            }
      }
}

I think I might have found some other ways to tell.

I just inherited some more VB6 code today. This one class is 2700 lines long, and starts with seventeen arrays of Variants. All the variables in this app are named using Hungarian notation. The names of these public variables suggests that they are module level. They are in fact public, and used by dozens of totally unrelated modules.

Blue Footed Booby fucked around with this message at 04:00 on Sep 10, 2011

kalleth
Jan 28, 2006

C'mon, just give it a shot
Fun Shoe
code:
 def profile_update
    @user = current_user
    @user.transaction do
      @user.update_attributes(params[:userdata])
      #Destroy and recreate all capabilities to handle the user having deleted some.
      #This is why weu se a transaction
      attrs = %w(capabilities experiences educations skills)
      attrs.each do |attr|
        #for each of the above, destroy them
        @user.send(attr).each{|e| e.destroy}
        const = attr.singularize.constantize
        params[attr.to_sym].each do |el|
          next if el.nil? || el.blank?
          #now create them
          obj = const.create!(params[attr])
          @user.send(attr) << obj
        end
        @user.save!
      end
    end
  end
cool story? I wrote this. Then promptly deleted it again. now I'm staring at a black screen :(

A MIRACLE
Sep 17, 2007

All right. It's Saturday night; I have no date, a two-liter bottle of Shasta and my all-Rush mix-tape... Let's rock.

kalleth posted:

code:
 def profile_update
    @user = current_user
    @user.transaction do
      @user.update_attributes(params[:userdata])
      #Destroy and recreate all capabilities to handle the user having deleted some.
      #This is why weu se a transaction
      attrs = %w(capabilities experiences educations skills)
      attrs.each do |attr|
        #for each of the above, destroy them
        @user.send(attr).each{|e| e.destroy}
        const = attr.singularize.constantize
        params[attr.to_sym].each do |el|
          next if el.nil? || el.blank?
          #now create them
          obj = const.create!(params[attr])
          @user.send(attr) << obj
        end
        @user.save!
      end
    end
  end
cool story? I wrote this. Then promptly deleted it again. now I'm staring at a black screen :(

That's not that bad.

kalleth
Jan 28, 2006

C'mon, just give it a shot
Fun Shoe

A MIRACLE posted:

That's not that bad.

Ruby gives you loads of rope to do fancy poo poo with metaprogramming. When I start seeing myself write stuff like "string".singularize.classify.constantize.send("another_string") I realise that I'm probably using that rope to hang myself, and should probably stop :(

Bruegels Fuckbooks
Sep 14, 2004

Now, listen - I know the two of you are very different from each other in a lot of ways, but you have to understand that as far as Grandpa's concerned, you're both pieces of shit! Yeah. I can prove it mathematically.

kalleth posted:

Ruby gives you loads of rope to do fancy poo poo with metaprogramming. When I start seeing myself write stuff like "string".singularize.classify.constantize.send("another_string") I realise that I'm probably using that rope to hang myself, and should probably stop :(

I've never even so much as looked at Ruby before but I looked up what that does and wow:

quote:

Constantize tries to find a declared constant with the name specified in the string. It raises a NameError when the name is not in CamelCase or is not initialized.

Examples

"Module".constantize #=> Module
"Class".constantize #=> Class

quote:

classify(table_name)

Create a class name from a table name like Rails does for table names to models. Note that this returns a string and not a Class. (To convert to an actual class follow classify with constantize.)

Examples

"egg_and_hams".classify #=> "EggAndHam"
"post".classify #=> "Post"

[ show source ]

quote:

singularize(word)

The reverse of pluralize, returns the singular form of a word in a string.

Examples

"posts".singularize #=> "post"
"octopi".singularize #=> "octopus"
"sheep".singluarize #=> "sheep"
"word".singluarize #=> "word"
"the blue mailmen".singularize #=> "the blue mailman"
"CamelOctopi".singularize #=> "CamelOctopus"


I'm going to have to look into this Ruby thing in the future, I've been looking at c++ COM objects too long to wrap my mind around this stuff.

NotShadowStar
Sep 20, 2000
Most of those methods only come from the ActiveSupport library, not the standard Ruby library. You can do it with standard Ruby, those methods just make it easier for you.

Ensign Expendable
Nov 11, 2008

Lager beer is proof that god loves us
Pillbug
That's a really cool language feature, actually! I have thought that something like that would be neat in the past, but I didn't know this existed.

Cocoa Crispies
Jul 20, 2001

Vehicular Manslaughter!

Pillbug

kalleth posted:

code:
 def profile_update
    @user = current_user
    @user.transaction do
      @user.update_attributes(params[:userdata])
      #Destroy and recreate all capabilities to handle the user having deleted some.
      #This is why weu se a transaction
      attrs = %w(capabilities experiences educations skills)
      attrs.each do |attr|
        #for each of the above, destroy them
        @user.send(attr).each{|e| e.destroy}
        const = attr.singularize.constantize
        params[attr.to_sym].each do |el|
          next if el.nil? || el.blank?
          #now create them
          obj = const.create!(params[attr])
          @user.send(attr) << obj
        end
        @user.save!
      end
    end
  end
cool story? I wrote this. Then promptly deleted it again. now I'm staring at a black screen :(

At least move it to the model so you can unit test it.

lord funk
Feb 16, 2004

My own idiocy that cost me 10 minutes of confusion:

code:
    for (int i=0; i<[array0 count]; i++) {
        [array0 removeObjectAtIndex:i];

        //other crap
    }
:derp: How come it's not removing all the objects?

kalleth
Jan 28, 2006

C'mon, just give it a shot
Fun Shoe
This one wasn't me, but one of the indian developers:

code:
  def validate_url
    unvalidate_url = self.url.downcase
    self.url = if unvalidate_url.index("http://").eql?(nil) or unvalidate_url.index("http://") > 0
      if unvalidate_url.index("https://").eql?(nil) or unvalidate_url.index("https://") > 0
        "http://" + unvalidate_url
      else
        unvalidate_url
      end
    else
      unvalidate_url
    end
  end
$3 per hour gets you software developers, guys, remember that

Cocoa Crispies
Jul 20, 2001

Vehicular Manslaughter!

Pillbug

kalleth posted:

$3 per hour gets you software developers, guys, remember that

I have one experience with the cheap kind of developer, and I will never fear offshoring again.

Lumpy
Apr 26, 2002

La! La! La! Laaaa!



College Slice

lord funk posted:

My own idiocy that cost me 10 minutes of confusion:

code:
    for (int i=0; i<[array0 count]; i++) {
        [array0 removeObjectAtIndex:i];

        //other crap
    }
:derp: How come it's not removing all the objects?

is it because you forgot to do this?

[array0 removeAllObjects]

I'd do that and then wonder why my loop only ran once.

Quebec Bagnet
Apr 28, 2009

mess with the honk
you get the bonk
Lipstick Apathy
code:
typedef std::stack<Job *> JobQueue;

corgski
Feb 6, 2007

Silly goose, you're here forever.

code:
function ProcessCrew($ID) {
$crewStr="";

	if(get_user_meta($ID,'iselect',true)==("Yes")){ 
			
			if($crewStr ==""){
			$crewStr="Elects";
			}else{
			$crewStr=$crewStr . ",Elects";
			
			}
	}
	
	
		if(get_user_meta($ID,'iscarp',true)=="Yes"){
			
			if($crewStr ==""){
			$crewStr="Carp";
			}else{
			$crewStr=$crewStr . ",Carp";
			
			}
	}
	
	
		if(get_user_meta($ID,'isrigger',true)=="Yes"){
			
			if($crewStr ==""){
			$crewStr="Rigger";
			}else{
			$crewStr=$crewStr . ",Rigger";
			
			}
	}
	
		if(get_user_meta($ID,'isprops',true)=="Yes"){
			
			if($crewStr ==""){
			$crewStr="Props";

			}else{
			$crewStr=$crewStr . ",Props";
						}
	}
	
		if(get_user_meta($ID,'iswardrobe',true)=="Yes"){
			
			if($crewStr ==""){
			$crewStr="Wardrobe";
			}else{
			$crewStr=$crewStr . ",Wardrobe";
			
			}
	}
	
		if(get_user_meta($ID,'isaudio',true)=="Yes"){
			
			if($crewStr ==""){
			$crewStr="Audio";
			}else{
			$crewStr=$crewStr . ",Audio";
			
			}
	}
	
		if(get_user_meta($ID,'isfly',true)=="Yes"){
			
			if($crewStr ==""){
			$crewStr="Fly Rail";
			}else{
			$crewStr=$crewStr . ",Fly Rail";
			
			}
	}
		if(get_user_meta($ID,'isloader',true)=="Yes"){
			
			if($crewStr ==""){
			$crewStr="Loader";
			}else{
			$crewStr=$crewStr . ",Loader";
			
			}
	}
	
		if(get_user_meta($ID,'isrunner',true)=="Yes"){
			
			if($crewStr ==""){
			$crewStr="Runner";
			}else{
			$crewStr=$crewStr . ",Runner";
			
			}
	}
	
		if(get_user_meta($ID,'issecurity',true)=="Yes"){
			
			if($crewStr ==""){
			$crewStr="Security";
			}else{
			$crewStr=$crewStr . ",Security";

			
			}
	}	
	return $crewStr;
}
I... just... why :psyduck: Has the author of this code never heard of implode()? Not to mention, using "yes" instead of a boolean.

YardKGnome
Jan 18, 2003
Grimey Drawer

Hughlander posted:

This is mild for the thread but I need to vent since the person who wrote this left the company right before shipping and left me to take over their systems...

How do you know when you need to refactor someone's code?

When a 300 line function ends with:
code:
                                                }
                                          }
                                    }
                              }                       
                        }
                  }
            }
      }
}

I had a professor in college that would end his Java programs like this:
code:
}}}}}}}}}
It was pretty amazing.

Space Kablooey
May 6, 2009


Hughlander posted:

This is mild for the thread but I need to vent since the person who wrote this left the company right before shipping and left me to take over their systems...

How do you know when you need to refactor someone's code?

When a 300 line function ends with:
code:
                                                }
                                          }
                                    }
                              }                       
                        }
                  }
            }
      }
}

Why's this so awful?

Sinestro
Oct 31, 2010

The perfect day needs the perfect set of wheels.

HardDisk posted:

Why's this so awful?

It means lotso nested ifs and loops.

Cocoa Crispies
Jul 20, 2001

Vehicular Manslaughter!

Pillbug

HardDisk posted:

Why's this so awful?

How are you supposed to keep track of all that indentation? It's also a 300 line function, which is 200 lines longer than fits on my screen.

Something that big could probably be turned into 30 (or fewer) ten-line functions.

raminasi
Jan 25, 2005

a last drink with no ice

YardKGnome posted:

I had a professor in college that would end his Java programs like this:
code:
}}}}}}}}}
It was pretty amazing.

Did he come from Lisp?

YardKGnome
Jan 18, 2003
Grimey Drawer

GrumpyDoctor posted:

Did he come from Lisp?

It looks like he taught Lisp back in the 80s, so there ya go!

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe

HardDisk posted:

Why's this so awful?

There is trailing whitespace after one of the }s. :spergin:

lord funk
Feb 16, 2004

Lumpy posted:

is it because you forgot to do this?

[array0 removeAllObjects]

I'd do that and then wonder why my loop only ran once.

It was because of the // other crap part. It wasn't the only array under the knife, and I have to fire off messages about the removal of some of those objects. I figured if I had to iterate anyway, why not just dump it. Derp indeed.

Save Russian Jews
Jun 7, 2007

who the fuck is this guy anyway, i can't even see his face

Lipstick Apathy
I know I seriously don't belong here, but I figure why not. I know it isn't code (pseudocode), but I think it's definitely some sort of horror.

I'm in Comp Sci 101 and the assignment was "write a code with a bug in it and describe how you would fix the bug." This was posted by another student on the message board for the class.

code:
MAIN
Declare a, b as Character
write "Enter a Character: "
input a
if (blue * yellow)
set b = green
write "The color is: ", b
else
set b = a (blue * yellow)
write "Your color is: ", b
endif
END

Fix: blue * yellow should be blue + yellow.

Scenarios:

Fails: yellow + Purple1, 10
Passes:  yellow + blue
I still don't know what to make of "blue*yellow doesn't make sense, it should be blue+yellow." I just... :psyduck:

shrughes
Oct 11, 2008

(call/cc call/cc)

Save Russian Jews posted:

I still don't know what to make of "blue*yellow doesn't make sense, it should be blue+yellow." I just... :psyduck:

This, folks, is why friends don't let friends learn pseudocode.

qntm
Jun 17, 2009
Seriously though, coming up with good examples to demonstrate language constructs and programming techniques is something which stumps me more often than not. It's like when you go up to test a microphone and somebody says "Say something" and suddenly you forget how to talk. "Fix the bug in this code" is frequently easier than "introduce a deliberate bug into this code" just because I have more experience with the former.

tef
May 30, 2004

-> some l-system crap ->
I have a lot of experience introducing bugs into code

Zamujasa
Oct 27, 2010



Bread Liar

thelightguy posted:

I... just... why :psyduck: Has the author of this code never heard of implode()? Not to mention, using "yes" instead of a boolean.

At the place I used to work they used "good" or "verified" or "success" as comparisons instead of true/false. The author of that code was the CTO who had supposedly been with the company for ten years.

I don't know what it is about people but it often seems like they cannot grasp the concept of booleans.


Then again, the CTO was the kind of guy who would check if an error message was returned from a function and then disregard it in favor of saying "An error occurred" (with no other information anywhere, not even logs), so I guess it's not really surprising.



quote:

code:
    function xhtml_foot()
    {
    ?>
    </body>
    </html>

    <?
    }
What possible reason is there for this to be valid

It's vaguely useful as a way to embed strings without having to escape everything, but considering that php has "theredoc" (:what:) now it should hopefully be obsoleted soon. I find it useful when doing small things on rare occasions, though.

Hughlander
May 11, 2005

Hammerite posted:

There is trailing whitespace after one of the }s. :spergin:

I'm actually glad someone noticed that, I left it in as a spergin present...

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope
Object oriented programming!

code:
aDumbThingThatBreaksConstantly = {
	_ff_engine:null;
	_ie_engine:null;
	
	init: function(){
		var is_ff = navigator.userAgent.toLowerCase().indexOf('firefox') > -1;	
		var is_ie = navigator.userAgent.toLowerCase().indexOf('msie') > -1;
		
		if (is_ff) {
			init_ff();
		}
		if (is_ie) {
			init_ie();
		}
	}
	
	do_a_thing: function() {
		if (this._ff_engine) {
			_ff_engine.do_a_thing_for_ff();
		} else if (this._ie_engine) {
			ie_engine.do_a_thing_for_ie();
		} else throw new Error("gently caress your browser");
	}
	
	init_ff: function(){
		_ff_engine = {
			do_a_thing_for_ff: function(){}
		}
	}
	
	init_ie: function(){
		_ie_engine = {
			do_a_thing_for_ie: function(){}
		}
	}
}
Everything about this is poo poo, but what bothers me right now is why wouldn't your engines have a unified interface?

Catalyst-proof
May 11, 2011

better waste some time with you

Wheany posted:

Object oriented programming!

Everything about this is poo poo, but what bothers me right now is why wouldn't your engines have a unified interface?

"No subclassing in Javascript? Oh well :("

19 o'clock
Sep 9, 2004

Excelsior!!!
I don't do much coding since leaving my CS degree, but in my job I have the opportunity to put out some Excel Macros. It also means I am the fixit guy for Excel Macros made by our company head.

code:
    For n = 1 + 6 To Rows_actual
       If Cells(n, 3).Value < 1 Then
            Cells(n, 3) = ""
       End If
    Next n
I like the "n = 1 + 6." He does this stuff all over the place and I imagine that the numbers meant something to him during the 5 minute window he was writing the function. They become utterly useless to everyone who has to look at the code later.

Rows_actual is a pretty sweet constant he determines as being the upper bound for any iteration of this spreadsheet in the future. Five years later and I have to fix it because the spreadsheet has gotten bigger than his imagination could have fathomed at the time of committing this to a VBA module.

Adbot
ADBOT LOVES YOU

Lysandus
Jun 21, 2010

Hughlander posted:

This is mild for the thread but I need to vent since the person who wrote this left the company right before shipping and left me to take over their systems...

How do you know when you need to refactor someone's code?

When a 300 line function ends with:
code:
                                                }
                                          }
                                    }
                              }                       
                        }
                  }
            }
      }
}

I see your bid and raise you!

code:
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
									}
								}
							}
						}
					}
				}
			}
		}
	}
}
This is out in the real world. Note: A lot of white space was removed to prevent table breakage.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply