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
Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope
Multiplication is a slow operation:
code:
#define SOME_CONSTANT 5
int someValue=SOME_CONSTANT << 2;
How to find out a value from an array:
code:
index = 0;
while( ! ( (( current_value - TOLERANCE / 2 ) < valuelist[ index ] ) &&
    ( ( current_value + TOLERANCE / 2 ) > valuelist[ index ] ) ) &&
    ( index < ARRAY_SIZE ) )
    {
        index++;
    }
if( index < ARRAY_SIZE )
{
    /* use value from array */
}

Adbot
ADBOT LOVES YOU

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope

UberJumper posted:

One more before i go to bed.

Holy poo poo that code owns.

The horrors I run into are mostly nested #ifdefs. Boring poo poo compared to that.

e: Although at one of my earlier coding jobs I had a colleague who loved to use single- and double letter variable names. So stuff like int a = 1; int b = 1; int aa = a + b;

Wheany fucked around with this message at 10:43 on Feb 24, 2009

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope

Richard Noggin posted:

code:
for (int index = 0; index <= carpalTunnel; index++)
{
    for (int index2 = 0; index2 <= rsi; index2++)
    {
        //you can see where this can get confusing
    }
}
Maybe not a true coding horror, but a pain in the rear end at the very least.

Short loop variables can be descriptive, though. For example using y and x instead of, say, the traditional i and j:
code:
for(int y=0 ; y<height ; y++)
{
    for(int x=0 ; x<width ; x++)
    {
        rgb[y*width+x]=whoa(x,y);
    }
}
Of course variable names such as theXCoordinate are dumb as hell.

Confession: I just made a program last week where I used variables int Δx and int Δy. Because I could. I copy-pasted the delta-character every time.

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope
code:
% grep -RP "\Wsome_flag\W" *
hal/driver.c:extern int some_flag;
hal/driver.c:if(some_flag == FALSE)
hal/driver.c:if(some_flag == FALSE) 
device.c:int some_flag = FALSE;
device.c:some_flag = FALSE;
device.c:some_flag = FALSE;
device.c:some_flag = TRUE;
Those two ifs should test for TRUE and FALSE, so there's a bug.

But also:
The flag is defined and manipulated, but never tested, in device.c.
It is declared extern and tested, but never manipulated, in driver.c.

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope

TSDK posted:

Why would you think this is a coding horror? Sure, it's a bit of an old-school way to get a status from a module, but then you are working in C.

Well, it doesn't really show from those lines, but there is no need for the flag to even exist in device.c. It pretty much goes like this:
code:
/* device.c */
    /* ... */
    some_flag = TRUE;
    make_device_status_true();
    /* ... */

/* driver.c */
/* ... */
make_device_status_true()
{
    if(some_flag == FALSE)
    {
        foo_device_set_status(TRUE);
    }
}
/* ... */

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope
How about "Coding annoyances: post the code that bothers you"

I see numbers needlessly defined in hex all the time.

E.g.
int numberOfApples = 0x05;

What's wrong with a simple
int numberOfApples = 5;

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope

quote:

There's a bit of controversy going on as to why the passwords were stored in plain text in the first place, with some arguing that it was because of the very large user base. Password hashing was apparently on the TODO list for the future, but that has become a priority now.

Now that's premature optimization.

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope
In a Qt project:
A colleague had added two text edit widgets to our UI, but they were hidden before the UI was made visible. They were used for getting the row number from a table view.

The table view's activated-signal was connected to a QDataWidgetMapper that set the text edit's value to the row number. Then the text edit's textChanged signal was connected to a custom slot on the UI's parent object. The slot then saved the row number in a private variable.

Then when the user presses a button, the button's event handler uses the private variable to recognize what row in the table view is currently selected.

The other text edit was used in a similar way, except for another table.

Okay, so maybe you don't have a lot of experience with Qt, but I'm pretty sure every framework has a better solution to the problem than "// TODO: ugly hack, needs to be removed"

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope

Lexical Unit posted:

I'll just assume your company routinely hires kindergartners then.

I read somewhere that often those coding tests aren't really meant to show how clever you are, just to prove that you can program at all.

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope
code:
if(!(status == OK))
{
    POST_ERROR("not OK");
    //...
}
I had to do a double take at the POST_ERROR line because the if statement tested for equality instead of inequality.

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope

MrBishop posted:

I'd punch someone in the face if I had to maintain that. It's the code equivalent of, "Everything's ok...... NOT!!".

If everything is ok on opposite day, do this:

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope

Fenderbender posted:

code:
sub stringlength {
    my $string=$_[0];
    my $length;
    $string=$string . "*****";
    $length=index($string,"*****");
    return($length);
}
._.

That's adorable

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope

Today I learned the concept of defensive copying. In hindsight it's totally obvious, and I've even thought that doing stuff like Point getPoint(){ return this.point; } is kind of dangerous, but it never occurred to me that I could do a return new Point(this.Point);. Also, I never thought that I should also make a copy of the object passed to my constructor for the same reason.

I'm a coding horror :(

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope
JOrbisPlayer:
code:
public void actionPerformed(ActionEvent e){
    if(e.getSource()==stats_button){
      String item=(String)(cb.getSelectedItem()); // cb is a combobox
/* ... */
      byte[] foo=item.getBytes();
      for(int i=foo.length-1; i>=0; i--){
        if(foo[i]=='/'){
          item=item.substring(0, i+1)+"stats.xml";
          break;
        }
      }
Okay, this was done in the year 2000, and it is supposed to work as an applet, so I thought that maybe it was done targeting Java 1.1, and maybe Java 1.1 didn't have String.lastIndexOf(). But as far as I can tell, lastIndexOf has always been a member of String.

And I just realized that it's not an Applet, but a JApplet, so it's at minimum Java 1.2, which definitely has lastIndexOf().

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope

Broken Knees Club posted:

So what exactly did those 500 lines do? I don't think I could write 500 lines for something that simple even if I tried. :psyduck:

if(x<=10) tilex=0;
else if(x<=20) tilex=1;


And so on. It's not that hard.

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope
And anyway, isn't the point of a flowchart to describe the logic flow of the program, not to format the actual code in a weird way?

'Ah yes, array_aaa(array_pos)="y"+left(aa,tt-1)+"y"'

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope

Zhentar posted:

Edit: (1/(Speed/Fastest Speed)) :psyduck:

More = better than.

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope

JasonV posted:

code:
$ grep function *.php | wc -l
5

http://betterthangrep.com/

(okay so it doesn't provide any improvements in this particular use-case, but...)

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope

Parantumaton posted:

Swing apps do, ours start at 24 megs (that's a lot too, agreed) and never pass 300 megs. Then again, our Java apps don't plain suck as apparently a lot of others' do...

24 megs is nothing, 300 megs could be a problem on a low-end machine.

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope

Zombywuf posted:

Depends if all the programs on your system think 24MB is nothing.

Well, if every background process and service running on this machine would take 24MB, I would be at about 1,5 GB. It would not be pleasant, since this machine only has a gig of RAM.

Then again, computers with 2 GB RAM is not exactly rare these days.

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope

markerstore posted:

If only there were some way to temporarily store the contents of RAM on your hard drive while they weren't being used!

Like I said: "It would not be pleasant, since this machine only has a gig of RAM." And besides, I was the one arguing that 24MB is nothing.

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope

Parantumaton posted:

Our software supports millions of users per day and are generally run on clustered servers (mostly because clients demand it) so 300 megs is nothing to us.

I just took a look at a price watch site and listed their computers by price. The cheapest computers were nettops that had 1 GB of memory. There was one thin client that had 512MB, and a bunch of "media" computers that also had 1 GB.

Pretty much any "real" desktops these days come with 2GB, often 3 or 4 gigs.

I'd say memory usage on software designed for a desktop computer (or higher) is not that big of a worry these days. Memory leaks are of course a different matter.

e: (I'm not disagreeing with you.)

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope
which is worse:

code:
int status = OK;
int phase = 0;

status = function1();
if(status == OK)
{
   phase++;
   status = function2();
}
if(status == OK)
{
   phase++;
   status = function3();
}
if(status == OK)
{
   phase++;
   status = function4();
}
if(status != OK)
{
   DEBUG("failed in phase %d, status %d", phase, status);
}
vs
code:
int status = OK;

status = function1();
if(status == OK)
{
	status = function2();
	if(status == OK)
	{
		status = function3();
		if(status == OK)
		{
			status = function4();
			if(status != OK)
				DEBUG("function4 failed, status %d", status);
		}
		else
		{
			DEBUG("function3 failed, status %d", status);
		}
	}
	else
	{
		DEBUG("function2 failed, status %d", status);
	}
}
else
{
	DEBUG("function1 failed, status %d", status);
}

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope

Mista _T posted:

If the test fails after function1() in Wheazy's first example, you are still testing "status" an unnecessary number of times.
It's a bunch of initialization functions that need all the previous functions to complete successfully. There is no point in send_http_request() if open_socket() has failed. And I really don't understand what 'testing "status" an unnecessary number of times' even means. Except that I probably could have used a goto after a failure.

That said, my third idea was having an array of function pointers, with the last array entry being NULL, then doing a
code:
status = OK;
while(functions[phase] && status == OK)
{
    status=functions[phase](); // or whatever the syntax is
    phase++;
}

if(status != OK)
{
    DEBUG("failed in phase %d, status %d", phase, status);
}

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope

markerstore posted:

Good lord, just use a real language with exception handling.

I would if I could.

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope
His lovely attitude has nothing to do with PHP, or any other language. He's just a bad programmer.

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope
Maybe they didn't know that in Java you can do a Course [] returnCourses = new Course [numberOfCourses];

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope

markerstore posted:

What if the next element in the array is FALSE?

Then the function throws a EverytingIsFine exception, which evaluates to FALSE.

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope

Flobbster posted:

Oh, I don't actually believe that, I just don't care enough to change it right now. I've got bigger fish to fry, and this is a hobby project anyway.

Just bookmark Janin's post for when you need to change it.

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope

Mustach posted:

Baby-sized horror that popped up today:
code:
char name[MAX_SIZE];
memset(name, 0, MAX_SIZE);
strcat(name, "Default");

I don't get it.

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope

Spazmo posted:

Comparison functions shouldn't return on first difference to avoid timing attacks on password comparison.

You shoudn't store plaintext passwords anyway?

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope

HFX posted:

You shouldn't store passwords in plaintext or non salted non block level password schemes. Anyway who cares.

Well I care in the sense that if your password system would be vulnerable to strcmp timing attacks, you are already doing it wrong.

I just wasn't sure if I missed a joke :(

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope

Zombywuf posted:

Yeah, maybe you'd learn to regex. They are not some deep mystery only comprehensible by the gods, they're the most trivial way of expressing a string pattern.

Yeah, this. You really should learn them, and they're really not that hard. They have applications other than programming too.

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope

TRex EaterofCars posted:

:mmmhmm: Hey baby, s/your pants//g
:nyd: gently caress off, nerd

Well, I couldn't actually think of that many examples, and even those are close to programming.

Anyway: learn regular expressions, they're really loving useful and not hard at all.

(try http://www.weitz.de/regex-coach/ )

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope

Lurchington posted:

Not so much a coding horror, but after seeing it on the official (although internal) system visualization documentation/presentations, I'm shaking my head. I had nothing to do with it.

There's a daemon process that watches for events and does stuff. Not too complicated, but imagine a visio with a bunch of web servers here, a bunch of database there, the occasional cloud of things, and with arrows going to and from, THIS in glorious retarded glory:



I would love to work in such a zany place :D

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope

qntm posted:

Except this doesn't work, because outside of a pair of square brackets, the ^ metacharacter isn't "negation", it matches the end of the string. So /^\s+/ will match nothing. I think what's needed is either /[^\s]+/ or /\S+/.

^ is the start, $ is the end

e: so it's "the start of the string, followed by 1 or more whitespace"

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope
$get = sqlInjectionProtect($_GET);

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope

ymgve posted:

Might not be a horror (except for the fact that they're probably not used parameterized queries) - I assume the function does something like "Check if gpc_magic_quotes is enabled, if not, do manual escape of all variables."

Something like that, yeah.

Then on the next row after calling that function: $thing = getAThingById($get["id"]);

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope
code:
    function hasRights($i) {
      return ((pow(2, $i) & $this->getRights()) == pow(2, $i));
    }
That function is used like this:

if(hasRights(5)){...}

What does '5' mean? Who knows!

Well, actually there is an explanation about the bits in the rights-field in a comment near 'hasRights', but it's off-by-one. And for some reason it does not use the least significant bit at all :rolleye:

This codebase is pretty :krad:

Adbot
ADBOT LOVES YOU

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope
FUCKIN' AJAX, MOTHERFUCKERS! :science:
code:
row = table.insertRow(-1);
cell = row.insertCell(-1);
cell.colSpan = 2;
div = document.createElement('div');
div.appendChild(document.createElement('br'));
cell.appendChild(div);
Much better (and clearer and easier to maintain) than, say
code:
html += '<tr><td colspan="2"><div><br></div></td></tr>';
This table(layout) has 8 rows of UI plus those empty rows. The non-empty rows are of course even more horrible. A function generates this table. There are several similar functions. Imagine the size of that javascript file.

Wheany posted:

This codebase is pretty :krad:

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