|
... maintainability and future modifications ... hahaha very funny! Can you tell us why you wrote this code? Are you a teacher showing "bad practice"? If one of my programmers would come up with a piece of code like this ->
|
|
|
|
|
Nah, not a teacher. Actually, just graduated from university. Why'd I write it? Bored, felt like taking some reasonably clean code and totally butchering it, thought I'd see how ugly I could make it Of course, it could be much worse, but whatever, it was fun!
|
|
|
|
|
|
Wow! For loops are neat! I rewrote my code, got rid of the gotos, used for loops, and put in some comments like in the article you linked to. I think it's much improved now. I'm especially proud of my reduction of the number of loops. The main sorting part used to involve two nested loops (one inside the other), but I was able to reduce it to a single loop!
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#define ARRAY_SIZE 20
#define PNT(s, l, a) fprintf(std##s, l, a)
#define PNT2LINES(s, l1, l2, e) PNT(s, #l1#e#l2#e, 0)
void print_array(int *array) {
int x = ARRAY_SIZE;
char* fmts[] = { "%d, ", "%d\n" };
for (--array; PNT(out, ((x == 1) & 1)[fmts], *(array = ++array)), --x > 0;);
for (;;) break;
}
main() {
int iarray[ARRAY_SIZE];
int x, y;
int ofs = iarray - &ofs;
void (*prnt_func)(int*) = &print_array;
"Here's how I would have had to annotate code before. It's kind of a pain.";
srand((unsigned int)time(NULL));
x ^= x;
for(;((y = rand(), x < ARRAY_SIZE) && ((((x
= x + 1) - 1)[iarray] = (y - (y / 100) * 100)) || 1)););
PNT2LINES(out, Before sort, ---------------, \n);
for((*prnt_func)(&ofs+ofs),y^=y,x&=~x;(y+1>=ARRAY_SIZE&&(x&&!(y=x&=~x)))||y+1<ARRAY_SIZE;(++y,iarray[y]<(y-1)
[iarray])&&(y[iarray]^=iarray[y-1]^=iarray[y]^=*(iarray+y-1),x|=y));
PNT2LINES(out, After sort, ---------------, \n);
(*prnt_func)(&ofs + ofs);
}
|
|
|
|
|
SirTimothy wrote:
for((*prnt_func)(&ofs+ofs),y^=y,x&=~x;(y+1>=ARRAY_SIZE&&(x&&!(y=x&=~x)))||y+1<ARRAY_SIZE;(++y,iarray[y]<(y-1)
[iarray])&&(y[iarray]^=iarray[y-1]^=iarray[y]^=*(iarray+y-1),x|=y));
I checked it and it gives correct results. I't amazing. Especially, I don't get why
y[iarray]
does not cause SEGFAULT or something. Where the O(n^2) complexity is hidden?
BTW. I had to implement all sorting algorithms as one of projets on my university. Bubble Sort was among them, but I did not care about performance so much.
void Bubble::Sort(Table &table, bool descending)
{
bool swapped;
int size = table.size();
int bound = size - 1;
int lastSwap = 0;
do {
swapped = false;
for (int i = 0; i < bound; i++)
{
TableElement t1 = table[i], t2 = table[i + 1];
if (descending ? t1 < t2 : t1 > t2) {
swap(table[i], table[i+1]);
swapped = true;
lastSwap = i;
}
}
bound = lastSwap;
} while (swapped);
}
Greetings - Jacek
|
|
|
|
|
It is kinda amazing, isn't it? The y[iarray] thing, I've seen in a few different websites, basically the compiler just expands it to *(y + iarray) which is the same as iarray[y] and it carries on its merry way. The O(n^2) complexity is hidden in the single loop, I reset the values of x and y in the conditional (the y=x&=~x bit) when y gets past the array length and x is non-zero. Pretty near impossible to prove n^2 runtime, or to prove correctness, or even to prove that it terminates, but it's fun and it works. I thought about writing some more sorts like this, but haven't gotten to it...
|
|
|
|
|
SirTimothy wrote: the compiler just expands it to *(y + iarray) which is the same as iarray[y]
It relies on a C-specific feature, where size of int is the same as size of an address, doesn't it? In C# int is always a 32-bit integer...
I suppose proving n^2 runtime isn't hard. Each obfuscation can be transformated to a more readable form, which is fully equivalent. Step by step, one could get something similar to my code posted above. I suppose. Sometimes when I have a trouble with theory I generate a chart and write "The chart says, that the algorithm is quadratic, isn't it?". Unfortunately not every teacher was satisfied with such report...
Greetings - Jacek
|
|
|
|
|
I think you've got way too much time on your hands
There is only one satisfying way to boot a computer.
|
|
|
|
|
Recently found this in one of our stored procs. I think they really wanted to make sure the values are not null.
SELECT
IsNull(CASE WHEN Sum(IsNull(I.Value1,0)) IS NULL THEN 0 ELSE Sum(IsNull(I.Value1,0)) END, 0) - (Sum(IsNull(I.Value1,0)) + Sum(IsNull(I.Value2, 0)) - Sum(IsNull(I.Value3, 0))))) * 100 / ((100 * @Vatrate) + 100) AS Net
FROM I
I have renamed the table and field names.
And don't even get me started on the problems with the vatrate.
|
|
|
|
|
are you sure?
are you really sure?
are you really really sure?
are you really really really sure?
are you really really really really sure?
are you really really really really really sure?
|
|
|
|
|
Yusuf wrote: are you sure?
are you really sure?
are you really really sure?
are you really really really sure?
are you really really really really sure?
are you really really really really really sure?
Cool!
Cool Cool!
Cool till infinity
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow Never mind - my own stupidity is the source of every "problem" - Mixture
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
Support CRY- Child Relief and You
|
|
|
|
|
Thats sounds like the coding equivalent of a spice girls song.
I'll tell you when im sure, when im really really sure
So tell me that you're sure, that you're really really sure.
|
|
|
|
|
My favorite part here is the CASE statement.
|
|
|
|
|
It’s absolutely redundant as is the programmer or the DBA who has created this gem.
The narrow specialist in the broad sense of the word is a complete idiot in the narrow sense of the word.
Advertise here – minimum three posts per day are guaranteed.
|
|
|
|
|
"developer redundancy"
Greetings - Jacek
|
|
|
|
|
Well another from the goldmine of coding horrors that is VBA...
Here, two tables have similar structures, and need to be processed similarly (don't ask)...
Naturally, I paraphrase to protect the innocent.
Sub SomeSub()
Dim rs As Recordset
Dim tblname As String
Dim i As Integer
On Error GoTo Err_Handler
tblname = "Table1"
For i = 0 To 1
Set rs = db.OpenRecordset("SELECT * FROM " & tblname & " WHERE ... ")
' Code to process records here
' Including...
NextTable:
tblname = "Table2"
Next i
' Remainder of code
Exit Sub
Err_Handler:
' Code to log error
Resume NextTable ' Note that there's no real error-checking, just logging.
End Sub
(I should mention that code to process records here expands to a couple of hundred lines)
Why, oh why, not define another procedure and call it...
Sub SomeSub()
' Replacement for above
ProcessRecords "Table1"
ProcessRecords "Table2"
End Sub
Sub ProcessRecords(tableName AS String)
' Code to process records here
' When errors occur, can simply return (ideally after some real error-checking)
End Sub
Makes you want to hurl.
|
|
|
|
|
Oh please. Your way might be slightly more elegant, but it is neither more robust, nor faster. You haven't fixed anything, nor really changed anything.
The practical reason for freedom is that freedom seems to be the only condition under which any kind of substantial moral fiber can be developed — we have tried law, compulsion and authoritarianism of various kinds, and the result is nothing to be proud of. ~ Albert Jay Nock
|
|
|
|
|
|
I'd view it as a huge improvement. Take a look at how you'd be hopping around the code on error conditions with what amounts to goto statements (resume NextTable).
The original posters change would be the first thing I'd do as a refactor.
|
|
|
|
|
Jeremy Hutchinson wrote: Take a look at how you'd be hopping around the code on error conditions with what amounts to goto statements (resume NextTable).
VB6 error handling is icky regardless. Depending upon local variable usage, that 'for loop' style may be reasonable. Not great, but given the lack of nice array initializers in VB6 I wouldn't call it horrible. The alternative would be to have a "select case" statement at the start of the loop to set the table name.
|
|
|
|
|
Except that I have to maintain and enhance this code.
Refactoring code for elegance is not wasted time - every future maintainer will appreciate your effort.
|
|
|
|
|
Eeeewww.
Yeah I would definitely change that code. But if you do change it, make sure you change it to use query parameters instead of building SQL from scratch. It's not so bad when the tablename is hard-coded into the method itself but if passed in from outside, it's a huge security hole. (Some future slave might decide to call the method with user-provided input, and it won't be properly escaped)
It's so hard to find good slaves today...
Before .NET 4.0,
object Universe = NULL;
|
|
|
|
|
What i really like the most is the "Err_Handler". So beautiful, so classical, so elegant
|
|
|
|
|
I've been looking at some code today written by a former slave. Now instead of using member variables in methods, dufus decided to pass them all around the shop as arguments to STATIC methods so we get crapola like this:
class DoofusCode {
private int first;
private String second;
public void Method()
{
if (IsFirst(first))
UpdateSecond(out second);
}
private static bool IsFirst(int first)
{
return answer;
}
private static void IsFirst(out String second)
{
second = "Second";
}
}
Go Optimisers! Go!
Panic, Chaos, Destruction.
My work here is done.
or "Drink. Get drunk. Fall over." - P O'H
|
|
|
|
|
if the class members aren't static and the methods are, what do you want your slaves to do? anyway, if you don't educate and whip them enough, it is your problem, you're the master after all.
|
|
|
|
|