|
I cam across this C++ gem today in some code that a co-worker wrote. In his defense, the bulk of his programming experience is in OS/390 Assembler.
char *ShowDrive( TAPEDRIVE *DRV, char *out ) {
int status;
char *msg;
status = 0x2D2D;
if ( DRV->stat & POWERON ) status = 0x4E4F;
if ( DRV->stat & MOUNTED ) status = 0x544D;
if ( DRV->stat & STARTED ) status = 0x5453;
if ( DRV->stat & INTREQD ) status = 0x5249;
sprintf_s( out, 80,"%2d %4.4s %4.4s %2.2s %6.6s %8.8s %8.8s\n ",
DRV->slot, DRV->host, DRV->name, &status, DRV->vols, DRV->msg1, DRV->msg2 );
msg = out + strlen( out );
sprintf_s( msg, 80," Block=%d\n", DRV->blockID );
return out + strlen( out );
}
Where do I begin? I especially like the int status variable that he uses in the sprintf_s with a format specifier of "%2.2s". It took me a while to realize that since he used "&status" as the argument, he was forcing sprintf_s to use it as a char pointer, and that the value of the int was two ASCII characters (reversed, because of Little Endianness).
I also like the return of the char pointer that points to the middle of the constructed string. (In his code the return value was ignored.) I'm not wild about the way it plays willy-nilly with the char pointers, etc. Too much horrible crap to even go into it all. My head hurts.
WE ARE DYSLEXIC OF BORG. Refutance is systile. Your a$$ will be laminated.
There are 10 kinds of people in the world: People who know binary and people who don't.
|
|
|
|
|
Tom Delany wrote: I also like the return of the char pointer that points to the middle of the constructed string.
Tom Delany wrote: return out + strlen( out );
I'd have thought that would return a pointer to the character after the constructed string. Could be excusable if he's intending to add further text to the buffer , although judging by your comments that isn't the case.
|
|
|
|
|
|
PIEBALDconsult wrote: To avoid the Shlemiel the painter's algorithm[^] problem.
As noted, it is often useful to have functions that append to strings return either a count of the number of bytes written or a pointer to the end of the new data. While C-style string handling is often dangerous because of a general lack of bounds checking, suitable coding techniques can at least make it efficient.
BTW, the painter's problem could be adjusted to give whatever performance dropoff is desired between day #2 and day #3 by specifying that the paint dries out with time. When the painter makes shorter trips, he can use up all the paint on his brush before it dries out. After a certain point, however, drying will become a factor. Depending upon whether the paint is useful until it dries out and then becomes useless, or whether painting becomes slower as the paint gets drier, different performance characteristics may be achieved.
|
|
|
|
|
supercat9 wrote: the painter's problem could be adjusted to give whatever performance dropoff is desired between day #2 and day #3 by specifying that the paint dries out with time. When the painter makes shorter trips, he can use up all the paint on his brush before it dries out. After a certain point, however, drying will become a factor. Depending upon whether the paint is useful until it dries out and then becomes useless, or whether painting becomes slower as the paint gets drier, different performance characteristics may be achieved.
I imagine an optimalization task based on that in an exam of computers' architecture (+ a bunch of extra triple integrals to do).
Greetings - Gajatko
Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.
|
|
|
|
|
Rob Grainger wrote: I'd have thought that would return a pointer to the character after the constructed string.
You're right. My head was hurting so much from the rest that I misread it.
WE ARE DYSLEXIC OF BORG. Refutance is systile. Your a$$ will be laminated.
|
|
|
|
|
Quite understandable - I think returning the pointer to the next character is the only redeeming factor I could spot
|
|
|
|
|
Looks like a C gem to me.
|
|
|
|
|
Yeah. He gave the file a .CPP extension, but there was nothing really C++ about it.
WE ARE DYSLEXIC OF BORG. Refutance is systile. Your a$$ will be laminated.
|
|
|
|
|
Tom Delany wrote: He gave the file a .CPP extension, but there was nothing really C++ about it.
That actually makes sense sometimes. If he really knows C, but wants to make use of a C++ stricter type system and catches more errors at compile time, that is a way to go.
|
|
|
|
|
C is infamous for pointer abuse. I think this would've been pretty standard C code 10 - 15 years ago.
|
|
|
|
|
The code works, so this is not a coding horror? I don't see any problem here. Maybe it's just your head. You should get a new one and then look at the code again.
Tom Delany wrote: he was forcing sprintf_s
This does not mean that he was "forcing" anything.
|
|
|
|
|
DECLARE p_cursor CURSOR FOR
SELECT TOP 1 SortDateStart AS DateStart, SortDateEnd AS DateEnd
...
This code was in a trigger...
|
|
|
|
|
What especially are you considering to be a horror?
1. Using a database trigger at all (this is IMHO the true horror here ) ?
2. Using TOP 1 without being explicit about it ?
Regards
Thomas
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
Programmer - an organism that turns coffee into software.
|
|
|
|
|
Well, I am ok with triggers so far you make a sensible use of them (which is not the case for the database where I found this).
You should avoid the use of cursors with SQL Server, that is not good but most people would not call that an horror.
The point is: Why on earth would someone open a cursor on a 1 row set????
In this case it is far more simple and efficient to type: SELECT TOP 1 @A=Column1, @B=Column2.... instead of declaring a cursor, fetch the data and close it.
|
|
|
|
|
Arnaud Lhopiteau wrote:
The point is: Why on earth would someone open a cursor on a 1 row set?
Not sure. Perhaps it was a rush job at the end of a Friday afternoon and they forgot what they were supposed to be doing?
"The clue train passed his station without stopping." - John Simmons / outlaw programmer
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
"Not only do you continue to babble nonsense, you can't even correctly remember the nonsense you babbled just minutes ago." - Rob Graham
|
|
|
|
|
Ah, now I get it...
Sorry, I'm not very familiar reading such things. So I missed the point...
Regards
Thomas
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
Programmer - an organism that turns coffee into software.
|
|
|
|
|
Assume that pTimeText is containing 2 dates seperated by space.
Like
pTimeText = "31/12/2008 15:36 29/12/2008 15:36"
this.txtTime.Text = Convert.ToDateTime(pTimeText.Substring(0, pTimeText.IndexOf(":") + 3)).ToString(Logic.DateConfigurer.DateFormatWithTime) + Environment.NewLine + Convert.ToDateTime(pTimeText.Substring(pTimeText.IndexOf(":") + 3)).ToString(Logic.DateConfigurer.DateFormatWithTime);
Guess what will above code return.
I found it in a file.
Ahsan Ullah
Senior Software Engineer
|
|
|
|
|
I call it ugly.
I call it a "code" bug.
I call it refactoring target #1.
|
|
|
|
|
Now that deserves to be listed in the dictionary as a definition of "fugly"!
Must have been a Power User who wrote that -- you know, someone who figured out how to do desktop word processing using the Calculator.
The PetroNerd
Walt Fair, Jr.
Comport Computing
Specializing in Technical Engineering Software
|
|
|
|
|
It seems to me that your job is really really hard...
I have no words for the code snippet you presented - at least none that I will use in public.
It is enough that one has to use the word guess when it comes to source code. This IMHO says it all.
Poor you.
Regards
Thomas
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
Programmer - an organism that turns coffee into software.
|
|
|
|
|
Thomas Weller wrote: It seems to me that your job is really really hard
Programming is never hard ( if it is programming )
Ahsan Ullah
Senior Software Engineer
|
|
|
|
|
AhsanS wrote: Programming is never hard
I fully agree. But psst, don't you tell this to a customer or boss...
AhsanS wrote: if it is programming
Is it ? Sounds more like refactoring/code review.
Regards
Thomas
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
Programmer - an organism that turns coffee into software.
|
|
|
|
|
Re-factoring is part of programming i guess. isn't it?
Ahsan Ullah
Senior Software Engineer
MCTS 2.0
|
|
|
|
|
Sure, you can see it that way...
Regards
Thomas
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
Programmer - an organism that turns coffee into software.
|
|
|
|