best/worst code youve seen?

Pages: 123
yeah i was just kidding lol. anytime i see system (unless i am in a hurry and dont plan on reusing the program) i automatically flag the code as bad
Not an exact replica but, worst code I've seen in a program I've worked on:

1
2
3
4
5
6
7
8
9
10
if(AList.size() > 0)
{
    for(int i = 0; i < AList.size(); i++)
    {
        if(i > 0)
        {
            //Do stuff with List and i
        }
    }
}
Like I said on another best/worst topic from another site I'm part of. The best code a programmer will ever write is the code for hello world. After that it is all downhill.
By the way, there is a whole site dedicated to some occasions like this: http://thedailywtf.com/
I used to read this a lot in high school.
Love that site, can't get enough of it.
OMG great site!
Are we restricting this to C\C++? Because if not I have some pretty brutal incompetence here, if we are I'm just going to go ahead and post them anyway.

I have swapped out the names of specific elements. Try to keep in mind that their instructions were to copy all of these scripts and files to thumb drives and for us to run them manually on some 200+ desktops.

'Install.bat'"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@echo off

echo Uninstalling Program

"%~dp0package\copyFilesLocally.vbs"

"%~dp0package\runOnce817.vbs"

"%~dp0package\uninstall75.vbs"

shutdown -t 30 -r -f

cls 

echo Install Complete


copyFilesLocally.vbs, a few notes about this one 'SERVER' is the DNS name of an actual machine in their network. We are a separate company in a different state with no cross domain access to this machine. Note the copy commands and imagine me trying for 20 mins to explain to them why this is relevant. The file vc_red is a component of the MSVS redistribution package, not the whole thing mind you, just the parts that they thought we needed. They were wrong of course, their application required the whole thing. I still laugh remembering the conference call when the other companies that were forced to use this didn't figure this out and couldn't understand why the package the client provided didn't work.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
on error resume next

Dim objFSO, objFileCopy

Set objFSO = CreateObject("Scripting.FileSystemObject")

If not objFSO.FolderExists("C:\Build") Then objFSO.CreateFolder("C:\Build")

If not objFSO.FolderExists("C:\Build\logs") Then objFSO.CreateFolder("C:\Build\logs")

If not objFSO.FolderExists("C:\Build\RunOnce\APPLICATION") Then
      objFSO.CreateFolder("C:\Build\RunOnce")
      objFSO.CreateFolder("C:\Build\RunOnce\APPLICATION")
End If

Set objFileCopy = objFSO.GetFile("\\SERVER\prod$\Apps\DesktopServices\Common\Microsoft Report Viewer Redistributable 2008 SP1\package\ReportViewer.exe")
objFileCopy.Copy ("C:\Build\RunOnce\APPLICATION\ReportViewer.exe")


Set objFileCopy = objFSO.GetFile("\\SERVER\prod$\Apps\DesktopServices\Common\Microsoft Visual C++ 2010 Redistributable\package\x86\vc_red.cab")
objFileCopy.Copy ("C:\Build\RunOnce\APPLICATION\vc_red.cab")

Set objFileCopy = objFSO.GetFile("\\SERVER\prod$\Apps\DesktopServices\Common\Microsoft Visual C++ 2010 Redistributable\package\x86\vc_red.msi")
objFileCopy.Copy ("C:\Build\RunOnce\APPLICATION\vc_red.msi")


Set objFileCopy = objFSO.GetFile("\\SERVER\prod$\Apps\DesktopServices\Common\APPLICATION\package\INSTALLATION_PACKAGE")
objFileCopy.Copy ("C:\Build\RunOnce\APPLICATION\INSTALLATION_PACKAGE")

Set objFileCopy = objFSO.GetFile("\\SERVER\prod$\Apps\DesktopServices\Common\APPLICATION\package\SEPERATE_INSTALLATION_UPDATE_PACKAGE.msi")
objFileCopy.Copy ("C:\Build\RunOnce\APPLICATION\SEPERATE_INSTALLATION_UPDATE_PACKAGE.msi")


Set objFileCopy = objFSO.GetFile("\\SERVER\prod$\Apps\DesktopServices\Common\APPLICATION\package\install_REDACTED.bat")
objFileCopy.Copy ("C:\Build\RunOnce\APPLICATION\install_REDACTED.bat")


runOnce817.vbs:
1
2
3
4
5
6
7
8
9
10
11
12
13
on error resume next

dim ws, key1, value1

key1 = "HKLM\Software\Microsoft\Windows\CurrentVersion\RunOnce\runOnce_REDACTED"

value1 = "C:\Build\RunOnce\Click 8.1.7\install_REDACTED_.bat"

Set ws = CreateObject("WScript.Shell")

ws.RegWrite key1,value1,"REG_SZ"

WScript.Quit


uninstall75.vbs (note here that REDACTED_SID is the SID of a previous version of this application that no one was ever told about much less given to install.)
1
2
3
4
5
6
7
on error resume next

dim ws

set ws = CreateObject("WScript.Shell")

ws.run "cmd /c msiexec /x {REDACTED_SID} /qn",0,true


If anyone is interested I think I have a few more examples of stupidity from these aspiring scriptkiddies.
Last edited on
do you still have the one that just ran msiexec?
@ Little Bobby Tables: I almost forgot about that one! I'm trying to remember which package it came with. I could just write it here but I want to get a direct copy and paste to preserve the sense of incompetence.
Last edited on
Bingo! By request I give you installStandard.vbs:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
on error resume next

Dim ws, fs

Set ws = CreateObject("WScript.Shell")
Set fs = CreateObject("Scripting.FileSystemObject")

If (fs.FolderExists("C:\build\logs")) Then
      'Nothing here, folder exists
Else
      fs.CreateFolder("C:\build\logs")
End If

ws.run "cmd /c msiexec /i ""APPLICATION-Standard.msi"" /qb /l*v C:\build\logs\APPLICATION-standard480.log", 0, true 

i love it
this prime checker function is pretty nice. i found it somewhere then edited it slightly.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
typedef uint_fast64_t bignum;
inline bool isprime(const bignum &number)
{
    if (number < 2) return false;
    if (number == 2) return true;
    if (number == 3) return true;
    if (number == 5) return true;
    if (!(number % 2)) return false;
    if (!(number % 3)) return false;
    if (!(number % 5)) return false;
    if (number < 7*7) return true;
    int stepset[] = {7,11,13,17,19,23,29,31};
    bignum step = sqrt((double)number);
    for(bignum r = 0; r < step; r += 30)
        for(int i = 0; i < 8; ++i)
            if (!(number % (r+stepset[i])))
                return false;
    return true;
}
My latest job has me combining two projects, one that is "feature rich" and the other that is well tested but small. The boss wants some features from the small project brought into the larger one, and it revealed a couple bugs.

The error pointed to this method:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
def genertateCategory(sorted_keys)
  n = 0
  cat1 = create_or_find(sorted_keys[2][0], nil)  // categories have children, the second argument is the parent id
  cat2 = nil

  sorted_keys[0..-2].each do |s|
    if n == 3
      cat2 = create_or_find(s[0], cat1.id)
    elsif n > 3
      cat2 = create_or_find(s[0], cat2.id)
    end
    n = n+1
  end
  cat2 = create_or_find(sorted_keys.last[0], cat2.id)
  cat2.isLeaf = true
  cat2.save
end


We are going to hold our tongues about the fact that our library has a find_or_create_by method which has apparently been self rolled, and focus on why line 14 says there is no method "id" for nil class.

Oh, because sorted keys is an array of arrays that is too short. Turns out this method can't cope with the different data that is now being uploaded. Sorted_keys MUST be 4 elements long.

The creator of the code came up with a nice solution though:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
def genertateCategory(sorted_keys)
  n = 0
  cat1 = create_or_find(sorted_keys[2][0], nil)
  cat2 = nil
  if sorted_keys.length >= 4
    sorted_keys[0..-2].each do |s|
      if n == 3
	cat2 = create_or_find(s[0], cat1.id)
      elsif n > 3
	cat2 = create_or_find(s[0], cat2.id)
      end
      n = n+1
    end
  else
    cat2 = create_or_find(sorted_keys[-2][0], cat1.id)
  end
  cat2 = create_or_find(sorted_keys.last[0], cat2.id)
  cat2.isLeaf = true
  cat2.save
end


I'll see you when we have an array of length 5.
Last edited on
Is this Ruby?
Indeed.
Topic archived. No new replies allowed.
Pages: 123