What programming hack from your past are you most ashamed of? [closed]?

We had a large Fortran77 code base that had used one large globally allocated array as scratch space in builds released in the 70's and 80's. Functions would return indexes into this large memory buffer as requested by various routines. I modified the code to be able to use dynamic memory by tying an "empty" Fortran array to a memory address returned from malloc (via a function written in C).

So the Fortran half looked more or less like this: integer iscratch(*) pointer (ptriscratch, iscratch) ptriscratch = my_malloc_wrapper_code() This syntax (using pretty widely adopted language extensions) causes the address returned from my_malloc_wrapper_code() to be assigned to the base of the iscratch Fortran array. Obviously there was a bit more to this where I had to write a library of C code that managed blocks of memory allocated in C and returned those addresses to the Fortran client code. This bought us dynamic memory in Fortran77 compatible compilers (well, compilers that recognized this "integer pointer" extension -- which was all the compilers we cared about).

The company was not ready to adopt Fortran90 compilers at the time, plus the legacy code base that used various offsets into the large preallocated array needed to be supported moving forward.

In the days before AJAX, Internet Explorer exposed a method called ShowModalDialog. Working in classic ASP, I figured out a technique that called ShowModalDialog with an ASP page as the URL. This page would query the database and response.

Write the information out in the form of a client-side javascript object that was returned when the modal browser window was automatically closed. If the read operation took a bit of time, the modal browser window could even be made to show a progress bar. With this technique I was able to update page contents without a full reload, a la AJAX.

EDIT: I just looked this stuff up, and showModalDialog() premiered in IE 4 in 1997, and XMLHttpRequest (aka AJAX) came out with IE 5 in 1999. But the IFrame sort of beat them all in 1996. I'm ashamed because I came up with my AJAX-like hack in 2000, more than a year after AJAX premiered.

3 I did a similar thing before AJAX, what I loaded an iframe of data then rad the content of iframe and change the current page :) it was almost like AJAX before AJAX. I thought that was so clever. – dr. evil Apr 24 '09 at 10:27.

This is not actually my hack but a hack of some smart children to my own application. Many-many years ago, I wrote a math-training application for little students in Sinclair ZX-Spectrum micro computer I know, ancient technology. The student was asked a number of questions like the following: a.

What is the value of: SIN(0)? B. What is the value of the expression: 1 + 10/2 -2^3 and so on... The student's response was read as a string and then I used a function like this: EVAL$(ASNWER) to convert it to a number and compare it to the correct answer.

I was really surprised that some students were perfect! They answered correctly all tests and hit a perfect 100% score. The key to their success was they found out they can put the question itself as an answer!

The EVAL$() function was so powerful with arithmetic expressions, that it computed the correct answer for them. So, what is the value of SIN(0)? Why bother?

It's simply "SIN(0)"!

As part of my bachelors degree we (a group of four) spend half a semester studying a program called Fhourstones, which is an integer benchmark that solves positions in the game of connect-4, as played on a vertical 7x6 board. By default, it uses a 64Mb transposition table with the twobig replacement strategy. Positions are represented as 64-bit bitboards, and the hash function is computed using a single 64-bit modulo operation, giving 64-bit machines a slight edge.

The alpha-beta searcher sorts moves dynamically based on the history heuristic. A move causing a cutoff is rewarded as many points as moves previously tried, each of which gets a -1 penalty, thus preserving total weight and avoiding renormalization (uniform penalties were found to work much better than depth dependent ones). Although the initial assignment was only to study the techniques used and present this to our classmates, we set out to see if we could extend the program to solve 8x8 boards as well (just over the current limit).

This proved troublesome as the code was highly optimized, storing a single board position inside a (Java) long. // bitmask corresponds to board as follows in 7x6 case: // . .. .

.. . TOP // 5 12 19 26 33 40 47 // 4 11 18 25 32 39 46 // 3 10 17 24 31 38 45 // 2 9 16 23 30 37 44 // 1 8 15 22 29 36 43 // 0 7 14 21 28 35 42 BOTTOM For red stones on the board the numbered bit would be set to 1, for black stones to 0.By then taking the 'skyline' of the board and setting each bit above it to 1 (like a layer of snow over rooftops) the entire board position for boards up to 8x7 can be stored using 64 bits. The entire program relied on this encoding to efficiently test for a win using 8 shift/ands and 4 comparisons.

Obviously we needed something bigger if we wanted to store larger boards, but because Java does not provide a primitive 128 bit data type, we had to look at other ways to go about storing the board positions. However, any solution we tried proved to be anywhere from 25 to 100 times slower than the original, the 'best' one being BigInteger. As the projected runtime for the original code for the larger board was already well over a day, this increase in time required was unacceptable.

Determined not to let the project fail, and somehow beat the odds at finding a faster solution, I set out to create my own number monstrosity, called IntLong As the name implies is was an int concatenated with a long, designed to provide us with just enough bits to be able to store the 8 x 8 + 8 = 72 bits needed. Next came the problem of making these two numbers work together to represent one larger number, and implementing all the bit operations used throughout the code base. We used the int to store the higher bits, and the long to store the lower bit, with a overflow from long to int on the 60th bit.

This made it trivially easy to implement the and, or and xor operations. The shift left and shift right operations were easy as long as you were careful to move the right bits over using ands and shifts. We worked around not having to implement addition, subtractions, mulitplication and division by replacing them >, & and |, which worked surprisingly well as these were only few and far between.

The real problem came when I was trying to figure out how to implement the modulo operation in a fast and accurate way, as this was used for hashing the board positions, which would happen a few million times in our application. After two weeks of trying every possible algorithm and finding them all too slow, I was damn near ready to give up. Then it hit me that we were only ever calculating the modulo by a known large primitive number, as part of our transposition table hashing mechanism.

So with that in mind I implemented the following function: public int modulo(int divider, int shifts) { IntLong temp = new IntLong(this. High, 0). ShiftRight(shifts); if (0 Modulo(int divider): argument too small, high still " + Integer.

ToString(temp. High, 2)); temp = new IntLong(0, temp. Low % divider).

ShiftLeft(shifts); temp = temp. Or(new IntLong(0, this. Low % divider)); return (int) (temp.

Low % divider); } It takes the divider and the number of bits in the divider as arguments, and performs the following: take the higher bits in the int, and shift them shifts times to the right, ignoring the lower bits in the long divide that by the divider and shift the remainder back to the left shifts times take the lower bits from the long, divide them by the divider and keep the remainder overlay the bits from two and three (which by now should not overlap) into one long return the remainder from one final division by divider and you should have the correct result It took me a very long time before I could finally convince myself that the end result was indeed correct, even after all my Unit tests passed flying colors. In the end it turned out to be 2.5 times faster than the BigInteger implementation (still 10 times slower than the original), but it was just enough to run our calculations in a realistic time frame.It's the ugliest hack I've ever had to implement, but I'm also still proud about having somehow beaten the odds and saved the project. :).

This is in VBA and I gather in this crowd that's reason enough to be ashamed ;D But it's a pretty common task in Excel Automation (in C# too for that matter) to need to replace the formulas with just the results. Particularly if you are distributing the workbook outside the company. At some point I realized this could be done by assigning Range.

Value to itself. Thus this: Sheet1.UsedRange. Value = Sheet1.UsedRange.

Value Is enough to cause all formulas to become literals.

In my teenage days, I was writing a code-golfed biorhythms calculator in x86 assembly. As x86 programmers know, the shortest way to divide by small numbers is to use the aam instruction (see my fizzbuzz solution for an example of its use). Well, aam's argument is an immediate operand.So, in order to loop through with the various divisors (23, 28, 33), my code incremented the immediate operand by 5 each time.

Yes, self-modification of the running code. Luckily in DOS code, this is all very kosher. :-P.

In Fortran one did not need to declare variables. They were automagicly REAL unless the name began with I, J, K, L, M or N. So to force all variables to be declared a programming standard employed the IMPLICIT statement which would modify the default of I-N for INTEGER and the rest REAL.

IMPLICIT COMPLEX This would cause most uses of undeclared variables to produce compiler errors. Later IMPLICIT NONE was a more direct method.

I was working with an embedded device that incorporated a simple webserver for configuration. It didn't have a filesystem of any sort, so rather than writing and saving static HTML pages, you had to write the webpages using special tags for form elements etc. When you built the downloadable image it had a build step which took the webpages you'd written and pre-processed them into large static C string arrays, and replaced the custom tags with dynamic code snippets.To serve up the webpage it re-generated the HTML from the static string arrays and the code snippets. All fine and good; the problem came when I needed to present a page with some enormous number of checkboxes on it, but depending on other configuration options some of the checkboxes would sometimes be disabled.

The normal way to do this would be to add both enabled and disabled checkboxes to the form using the custom tags, and then use the dynamic code to serve up the correct variety, but I hit the limit on the number of custom tags the pre-processor could handle.My solution was to include just the enabled checkbox on the page as a custom tag element, but also to include two images of a disabled checkbox (one checked, one not checked) on the page as well in each location where I needed a checkbox. I then wrapped everything in HTML end-comments "-->" and used the dynamic code snippets to insert HTML open-comments ".

I was young, inexperienced and way behind budget and deadlines... I needed to add a feature to an accounting system I had written, this feature was supposed to pull a list of unique checks from an array of payments (One check could contain money for more than one payment). I wrote this: Dim UniqueCheckNum(1000) Dim TotalUniqueChecks TotalUniqueChecks = 0 for I = 0 to TotalUniqueChecks for j = 1 to TotalElem if NOT StCheckNum(j) = UniqueCheckNum(i) then UniqueCheckNum(TotalUniqueChecks) = StCheckNum(j) TotalUniqueChecks = TotalUniqueChecks + 1 end if next next To this day, I don't know why I did it that way, especially considering the strange way I abused the for statement...but it works, and the system is still production, handling payments everyday... This is my favorite snippet of WTF the code, and I've always kept a copy of that file around.

This was with Java and I was actually working as a junior Java developer. Long long time ago I stuffed various objects in a list. But objects were not representing same class... If I remember right the reason for this was to allow method to return multiple objects.

List funkyMethod() { List resultList = new ArrayList(); resultList. Add(new BusinessItem()); resultList. Add(new StatusBean("2", "ok")); return resultList; } Accessing was then serious casting: StatusBean status = (StatusBean)funkyMethod.

Get(2); Horrible for team mates but luckily I rewrote it afterwards. I wonder how come my mentor missed that...

A very neat hack in VBA and VB6 that tends to clean up complex If-Then-Else constructs is to use the Select Case Statement in this way: Caveat: This is pseudocode, and a trivial example at best - it really shines to unravel deeply nested If-Then-Else blocks Public Sub VitalProcess(Test1 As Boolean, Test2 As Boolean, Test3 As Boolean) Select Case True Case Test1 'do some processing for this test' Case (Not Test1) And Test2 'Typical special case' Case Test2, Test3 'do some alternate processing' Case Else Debug. Print "No Processing was done - check VitalProcess ", Test1, Test2, Test3 End Select End Sub.

In a templated collection class, I once ran across the following. Template class SomeCollection { void Add(T* pValue) { ... memcpy(&someInternalNode, pValue, sizeof(T)); } }; I found this when I attempted to use a type which depended on value semantics in conjunction with this collection. My best guess is they ran into issues when doing a direct assignment (which is an indication BTW that there is a bug in the usage).

I can't claim credit for this one, but I was looking for how to declare const variables in Python and ran across this page: code.activestate.com/recipes/65207/ It revolves around this: "In Python 2.1 and up, no check is made any more to force entries in sys. Modules to be actually module objects.

Passing a whole file (a PNG image around 4Kb long) as a command-line argument (Base64 encoded. ).

I moved from a Fortran IV on RSX-11M system to VAX Fortran77 on VAX/VMS. So, I read the Fortran manual to find out about the differences. While reading it I came across the alternate return feature of Fortran which it turned out to be a standard feature not a VAX extension.

I even showed it about ridiculing it and anyone who would use such a thing. I was assigned a project to modify an in-house product MenuGraph which prompted the user for info to define the layout and contents for displaying graphs using the ISSCO DISPLAY Fortran graphics library. One of the design features allowed the user to back up to previous questions and change the answers.

This was a terminal text based user interface. In order to support the ability to back up from anywhere the code was very messy even for 1980's Fortran. Some how the alternate return popped into my mind.

Beware of what you read. Real gpa integer units character*10 name 10 type*, 'Computes grade points from GPA and units. ' 20 call sdsu_ask_real ( .'$Enter GPA: ',!

Prompt user with this. .0.0,! Minimum value user can enter.

.4.0,! Maximum value user can enter. .'N',!

Default not allowed (input required). . Gpa,!

Variable to receive user's valid input. . *10,!

Previous question label. . *999)!

End of input label. 30 call sdsu_ask_integer ('$Enter units attempted: ', .1, . 500, .

'N', units, *20, *999) call sdsu_ask_string ('$Enter your name: ', .1, . 10, . 'N', name, 10, *30, *999) type*, name, ', your grade points =', units * gpa 999 end The starred arguments are the alternate return labels.

The routines return to the first one ( 10, 20, or 30) if the user enters "^" to backup to the previous question. If the user enters "^Z" the routines return to the second alternate return (999). The normal flow is when the user enters valid data then it advances to the next line just like regular routine calls ignoring any alternate return values.

The nasty alternate return is used in this case to make the code very clean and easy to follow. See SDSU_AskLib.

As the Database Administrator was away on holiday's, management wanted a feature ASAP. We ended up implementing a flag on a single Id in compiled code for an c# events system. If (Id == 141) { //Do Some Action }.

Well, this hack comes because of the tight deadline. I guess all the hacks come out this way. Anyway, I was working on a school project and the next day the students must get an exam but they are not supposed to start the exam until 8:00 AM next day.So, I just hard coded the line: if(date is less than 8:00 datetime) then tell the student that it is not time to start the exam.

Sometimes the working software is more important then a hack.

1 Hard-coding a constraint isn't really a hack, IMO. It's ugly, but not a hack. – Brian Feb 8 '10 at 19:53.

I needed to read the file listing of pkziped files in a Quake III launcher app I wrote something like 8 years ago. This would let me get the maps included in the pak files installed in the system. I ended up patching in the source from the open source unzip executable by renaming its main and then changing the code to write the file listing to a global buffer.

I then called the unzip_main with the pak file name and the parameter to list the contents in the argc and argv parameters. Much later, I saw that there was an open source library that probably would have been better and easier.

My biggest hack so far has been when writing some music in Lilypond. I wanted a horizontal bracket over some music, so I hacked it out of the ottavation bracket. I just removed the ottavation text and set it to not transpose by an octave... startBracket = { \ottava #1 %sets a one octave transposition \set Staff.

Ottavation = #"" %this gets rid of the ottavation text \set Staff. MiddleCPosition = #0 %this puts middle C back where it came from % ie it "untransposes" it }.

A long time ago when I was still learning C I was having trouble getting a shell to work(I was also writing my own 16 bit OS. Yes, dumb idea while learning C). As I was looking for possible sources I just thought I would try changing char buffer100; to char *buffer100.. but here is the more amazing thing.It worked.

I have no idea how, but somehow the way my code was written strlen and everything just worked after that.. That doesn't make it any less shameful though(note: buffer stored a string. Not an array of strings) Also, in a C++ project I was having severe problems with having things needing to be declared before they could be(dependencies on each other).. so I worked up the skill of including a header file.Twice. It took some definite macro magic.. Also in the same project, it was a library.

So the user of the library needed to implement things before it would actually link. Well, In order to prevent having a huge header file distributed with the library(over 20k iirc) with all of the internal methods and such. I worked things into a x86Lib.

H and x86Lib_internal.h. If a certain macro was defined, then it knew it was building it internally and included the _internal file, else it didn't include it and changed any references to it to void * or similar.

2 char* is bigger than char, so you probably made the buffer big enough for things to work. – quant_dev Apr 11 '10 at 0:08.

There's this one I used to do (and I see done all the time) to convert a number to a string in Java: int n = 12345; //... String s = "" + n; Of course, this is better done like this: int n = 12345; //... String s = Integer. ToString(n); Heck, for all I know the Java compiler may have a special case for empty-string plus primitive that makes this hack just as efficient. But if not, this method requires creating a StringBuilder object behind the scenes, which is just unnecessary.

3 Holy Crap it works in C#! – Lucas McCoy Jun 18 '09 at 15:47.

Defining two almost identical constructors in a Java class: public class Class { final private double data; public Class(double data) { this. Data = data.clone(); } private Class(double data, int i) { this. Data = data; // no cloning } } Only later did it occur to me that I could do it in a nicer way.

2 Use a factory methods for the less common version, or take a boolean copy parameter. – quant_dev Apr 11 '10 at 0:07.

Not sure if anyone answered, this but I see people add the signature of a function at the site where the function is called instead of a header file. This works, but now the signature is in two places, and nothing to check it. In C++ at least the name-mangaling would prevent it to link.In C, it'll link, and then crash at runtime -- so you just hope crash is in area of code that gets tested before the software ships.

:).

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions