Click here to Skip to main content

The Weird and The Wonderful

   

The Weird and The Wonderful forum is a place to post Coding Horrors, Worst Practices, and the occasional flash of brilliance.

We all come across code that simply boggles the mind. Lazy kludges, embarrasing mistakes, horrid workarounds and developers just not quite getting it. And then somedays we come across - or write - the truly sublime.

Post your Best, your worst, and your most interesting. But please - no programming questions . This forum is purely for amusement and discussions on code snippets. All actual programming questions will be removed.

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page  Show 
GeneralHow To Use Session VariablesmemberAnalogNerd17hrs 6mins ago 
I'm re-writing a legacy ASP application. There's an expensive call to an external webservice and a database to determine a calculated setting specific to the person logged in.
 
On the first page this calculation is done and the result is stored in a session variable.
 
On every subsequent page the session variable is ignore, the value is recalculated and then stored in the session variable again!
 
Some days I don't know if to laugh or cry.
GeneralRe: How To Use Session VariablesprotectorOriginalGriff15hrs 45mins ago 
Sometimes, you just want to go into a code review with a baseball bat and a spiked glove... Sigh | :sigh:
The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)

GeneralA Brilliant Replacement of float.toFixed(2) [modified]memberTNCaver17-Jun-13 9:25 
Our finance folks were a bit annoyed by one of our web pages, where it occasionally rounded the final total to the nearest dime. While troubleshooting a former colleague's JavaScript code that added 2% to the amount entered by the user, I found this gem. His intention, at least, was obvious by the function name.
 
function CurrencyFormatted(amount) {
	var i = parseFloat(amount);
	if (isNaN(i)) { i = 0.00; }
	var minus = '';
	if (i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if (s.indexOf('.') < 0) { s += '.00'; }
	if (s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = minus + s;
	return s;
}
Isn't that brilliant? I mean, why use the simple, single-line solution isFixed(2) when you can do the same thing in 9 lines? WTF | :WTF:
If you think 'goto' is evil, try writing an Assembly program without JMP.


modified yesterday.

GeneralRe: A Brilliant Replacement of float.toFixed(2)memberLloyd Atkinson17-Jun-13 9:33 
Perhaps he didn't know isFixed exists
           .-.
          |o,o|
       ,| _\=/_      .-""-.
       ||/_/_\_\    /[] _ _\
       |_/|(_)|\\  _|_o_LII|_
          \._./// / | ==== | \
          |\_/|"` |_| ==== |_|
          |_|_|    ||" ||  ||
          |-|-|    ||LI  o ||
          |_|_|    ||'----'||
         /_/ \_\  /__|    |__\

GeneralRe: A Brilliant Replacement of float.toFixed(2)protectorMarc Clifton15hrs 19mins ago 
Well, perhaps toFixed doesn't work the way he wanted?
 
Does it handle NaN?
Does it do rounding?
Does it round the absolute value of the value (which would always round up, right?)
 
Goofing around with the W3School's Try It[^] feature (snazzy) I think everything works correctly except the NaN handling.
 
Marc

GeneralRe: A Brilliant Replacement of float.toFixed(2)memberAmitosh Swain3hrs 32mins ago 
Marc Clifton wrote:
Does it handle NaN?

I don't think NaN will ever arise while currency formatting (going as per method name), without exceptions. He's not doing tan 90 anywhere is he?
GeneralDatetimes in java (again)professionalV.13-Jun-13 1:22 
GregorianCalendar date = new GregorianCalendar(1888, 0, 1, 0, 0, 0);
SimpleDateFormat sdf = new SimpleDateFormat("YYYY");
String convertedstring = sdf.format(date.getTime());
System.out.println(convertedstring);
 
output: 1887 (on my laptop anayway)
 
GregorianCalendar date = new GregorianCalendar(1888, 0, 1, 0, 0, 0);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
String convertedstring = sdf.format(date.getTime());
System.out.println(convertedstring);
 
output: 1888
 
For those who didn't notice, in sample 1 I put "YYYY", in sample 2 "yyyy".
I couldn’t immediately find something in the docs or google explaining this…

GeneralRe: Datetimes in java (again)memberBotCar13-Jun-13 1:38 
"YYYY" means week years.
 
Quote:
A week year is in sync with a WEEK_OF_YEAR cycle. All weeks between the first and last weeks (inclusive) have the same week year value. Therefore, the first and last days of a week year may have different calendar year values.
 
For example, January 1, 1998 is a Thursday. If getFirstDayOfWeek() is MONDAY and getMinimalDaysInFirstWeek() is 4 (ISO 8601 standard compatible setting), then week 1 of 1998 starts on December 29, 1997, and ends on January 4, 1998. The week year is 1998 for the last three days of calendar year 1997. If, however, getFirstDayOfWeek() is SUNDAY, then week 1 of 1998 starts on January 4, 1998, and ends on January 10, 1998; the first three days of 1998 then are part of week 53 of 1997 and their week year is 1997

 
SimpleDateFormat[^]
GregorianCalendar[^]
GeneralRe: Datetimes in java (again)professionalBrisingr Aerowing13-Jun-13 2:58 
Confused | :confused:
 
Quite odd, that is.
Gryphons Are Awesome! ‮Gryphons Are Awesome!‬

GeneralRe: Datetimes in java (again)memberRob Grainger19hrs 49mins ago 
Unfortunately, many human systems for dealing with dates are odd, all computers can do is reflect that.
"If you don't fail at least 90 percent of the time, you're not aiming high enough."
Alan Kay.

QuestionMessage Automatically RemovedmemberToywarrior12-Jun-13 21:44 
Message Automatically Removed
AnswerRe: HTML 5 Application.memberAmitosh Swain12-Jun-13 21:53 
Confused | :confused: An application that presents it's UI in html5 and runs in a browser as a regular webpage. Leveraging html5 technoligies, it can do pretty much the same thing that a standard user level application does... Like a word processor. Anyways Let me google that for you![^]
 
But don't tell about a disk defragmanter HTML5 App!
 
You're new, please read the announcement at the top, this ain't the right place for asking questions...
GeneralRe: HTML 5 Application.memberToywarrior12-Jun-13 21:57 
But.. Any Example???
GeneralRe: HTML 5 Application.memberAmitosh Swain12-Jun-13 22:02 
Draw.IO[^]
 
Remember...
Post your Best, your worst, and your most interesting. But please - no programming questions . This forum is purely for amusement and discussions on code snippets.

GeneralRe: HTML 5 Application.memberToywarrior12-Jun-13 22:16 
OK... But I have more questions about this topic...
What should I do
-Toywarrior

GeneralRe: HTML 5 Application.memberAmitosh Swain12-Jun-13 22:21 
We've got programming forums here in CP, see Discussions > Web Developments
GeneralRe: HTML 5 Application.memberToywarrior12-Jun-13 22:24 
Will You Join me There????
-Toywarrior

GeneralRe: HTML 5 Application.memberAmitosh Swain12-Jun-13 22:32 
If you post then... if I get you Laugh | :laugh:
JokeRe: HTML 5 Application.memberjim lahey17-Jun-13 10:15 
Looks like you've pulled Smile | :)
AnswerRe: HTML 5 Application.memberAmitosh Swain3hrs 40mins ago 
He was a first timer... little lax was on my part
GeneralBug of the Day (2)memberRob Grainger12-Jun-13 4:47 
I can beat that - I discovered a bug where new records were failing to be added.
After a number of false trails, I homed in on the following SQL user function:
 
ALTER function [dbo].[ufn_GetNextID](@IDTable as varchar(100), @IDColumn as varchar(100))
returns integer
as
begin
	declare @NewID as integer
	set @NewID=0
 
	return @NewID
end
"If you don't fail at least 90 percent of the time, you're not aiming high enough."
Alan Kay.

GeneralRe: Bug of the Day (2)professionalBrisingr Aerowing12-Jun-13 6:30 
D'Oh! | :doh:
Gryphons Are Awesome! ‮Gryphons Are Awesome!‬

JokeRe: Bug of the Day (2)memberrichcb17-Jun-13 10:43 
I don't see anything wrong there, what does "0" mean anyway?
GeneralRe: Bug of the Day (2)memberRob Grainger19hrs 48mins ago 
richcb wrote:
what does "0" mean anyway?

I believe its the shape of my mouth every time I come across examples like this!
"If you don't fail at least 90 percent of the time, you're not aiming high enough."
Alan Kay.

JokeRe: Bug of the Day (2)memberrichcb16hrs 48mins ago 
Nice, that had me rollin for a minute! Laugh | :laugh:
GeneralBug of the daymemberDelphi4ever12-Jun-13 4:18 
if(SomeThing == SomeOtherThing);
{
DoSomeThing;
}
 
This one has been sitting in the codebase for a couple of years... Cry | :((
At least it did SomeThing...
GeneralRe: Bug of the dayprofessionalRichard Deeming12-Jun-13 4:25 
Which language? The C# compiler will give you a warning for that: "Possible mistaken empty statement".



"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer


GeneralRe: Bug of the daymemberBobJanova12-Jun-13 4:37 
Even C back in the old days gave you a warning for that.
GeneralRe: Bug of the daymemberKlaus-Werner Konrad12-Jun-13 22:39 
Wich compiler ?
while (*dest++ = *source++);
is completely correct, isn't it ?
GeneralRe: Bug of the daymemberBobJanova12-Jun-13 23:39 
It's a warning, not an error, for that reason. This was back when I used Zortech's ANSI C compiler.
GeneralRe: Bug of the daymemberJacek Gajek14-Jun-13 1:13 
Klaus-Werner Konrad wrote:
Wich compiler?
FTFY: Witch compiler
 
Actually, in this case the C# produces three useless wormings: both for the "while(...);" (an empty statment), "x=y" (an assigment instead of a comparison) and the "*" (an "unsafe" code), does it?
Greetings - Jacek

GeneralRe: Bug of the daymemberKlaus-Werner Konrad14-Jun-13 2:35 
Thanks for the correction.
 
My example was - as a reply to the mention of C, of course a C code snippet,
and is the full working function body for strcpy().
 
Of course, it's unsafe - but lightning fast Smile | :)
GeneralRe: Bug of the daymemberBobJanova14-Jun-13 3:31 
Just in case you didn't get the joke there, he's making a funny about the compiler being witchcraft. The word you meant to use is 'which'.
GeneralRe: Bug of the daymemberBobJanova14-Jun-13 3:30 
They're not useless warnings, they're warning you that you did something unintended. Actually this wouldn't compile at all in C#, even with unsafe mode turned on, because the result type isn't boolean. It's a classic and well known piece of C code, and I think you only got a warning for the empty loop body (and if you did if(a = 3) by accident you were just screwed, hence writing if(3 == a) instead which is an error if you screw it up).
GeneralRe: Bug of the daymemberJacek Gajek14-Jun-13 4:31 
Right. Thumbs Up | :thumbsup:
Greetings - Jacek

GeneralRe: Bug of the dayprofessionalRavi Bhavnani12-Jun-13 5:42 
Does that even compile?
 
/ravi
My new year resolution: 2048 x 1536
Home | Articles | My .NET bits | Freeware
ravib(at)ravib(dot)com

GeneralRe: Bug of the day [modified]member ProgramFOX14-Jun-13 2:23 
No, it doesn't. But that's just the bug: it doesn't compile!
[EDIT]
I'm sorry, I didn't see the semicolon after the if statement. That's the bug! D'Oh! | :doh:
The quick red ProgramFOX jumps right over the Lazy<Dog>.
 
My latest article: Understand how bitwise operators work (C# and VB.NET examples)
 
My group: C# Programmers Group


modified 3 days ago.

GeneralRe: Bug of the daymemberanton_l12-Jun-13 19:53 
It seems that problem is in semicolon after "if" statement.
DoSomeThing will be fired any time the code executes.
GeneralRe: Bug of the daymemberKP Lee13-Jun-13 20:45 
anton_l wrote:
It seems that problem is in semicolon after "if" statement.
It seems that's the reason he posted it! Laugh | :laugh:
GeneralRe: Bug of the daymemberYvesDaoust12-Jun-13 21:08 
This is one of the dangers of C syntax (and friends). That's the price you pay for willing conciseness. Block-only statements like VB's
If Condition Then
  Statement
End If
or Modula's
IF Condition THEN
  Statement
END
are safer.
GeneralRe: Bug of the daymemberGary Wheeler13-Jun-13 0:13 
Or Pascal:
if condition then
begin
   DoStuff();
end
else
begin
   DoOtherStuff();
end;
Sort an 'are you sure?' prompt for every single conditional. Roll eyes | :rolleyes:
Software Zen: delete this;

GeneralRe: Bug of the daymemberBobJanova13-Jun-13 0:54 
Can't you do if condition then one-statement else other-statement though if you don't need a block?
GeneralRe: Bug of the daymemberGary Wheeler13-Jun-13 1:08 
Yes, but I've always hated doing those, unless you write it on a single line:
if condition then DoSomething() else DoOtherThing();
That's the only way in my mind to avoid stupid mistakes like this:
if condition then
    DoThing1();
    DoThing2();
    DoThing3();
MainStuff();
DoThing2() and DoThing3() look like they're part of the if, but they're not. I do the same thing in C-style languages. If an if-statement occupies more than one line, it gets braced.
Software Zen: delete this;

GeneralRe: Bug of the daymemberBobJanova13-Jun-13 2:12 
I agree and would always put that on a single line.
GeneralRe: Bug of the daymemberCornelius Henning13-Jun-13 4:13 
Quote:
If an if-statement occupies more than one line, it gets braced.

 
Maybe I'm naïve, but I thought everybody did that! Smile | :)
GeneralRe: Bug of the daymemberGary Wheeler13-Jun-13 4:21 
I've worked with people who did this:
if condition
    DoSomething();
else
{
    DoOtherThing1();
    DoOtherThing2();
}
or
if condition
{
    DoSomething1();
    DoSomething2();
}
else
    DoOtherThing();
Both of which give me the creeping heebie-jeebies.
Software Zen: delete this;

GeneralRe: Bug of the daymemberStatementTerminator13-Jun-13 5:21 
Gary Wheeler wrote:
if condition then
DoThing1();
DoThing2();
DoThing3();
MainStuff();

 
Yeah that's one of my all-time favorites. It usually starts out like:
 
    if(condition) 
        DoThing1();
 
And then someone comes along later and adds DoThing2(), and the compiler silently chuckles to itself and lets the code do a lot of Thing2.
 
Personally, I like to always use curly braces to block off code for that very reason, even if it's only one statement. If I see something like that with one statement, I'll add the braces so it's clear when someone comes along and changes it.
 
Putting it all on one line works fine as well, but I like to always create a code block because it makes it easier to add statements later. And really, what's the point of keeping it on one line? But I know a lot of programmers have an irrational fear of vertical space Smile | :)
JokeRe: Bug of the dayprofessionalBernhard Hiller12-Jun-13 21:27 
Oh no, you misunderstood the guy who wrote that piece of code.
Never did he intend that DoSomeThing() is executed only when some codition is true. He just wanted to make his colleagues (who'll have to maintain his buggy code after he quit his job) believe so.
GeneralRe: Bug of the daymemberR. Erasmus12-Jun-13 21:38 
Seems like a new style of comment... if you're compiler doesn't support any comments. Poke tongue | ;-P
"Program testing can be used to show the presence of bugs, but never to show their absence."
 
<< please vote!! >>

GeneralRe: Bug of the daymemberFabio Franco13-Jun-13 1:21 
I had to read it three times before spotting the problem. Go bugged by the fact that DoSomeThing didn't have parenthesis.
To alcohol! The cause of, and solution to, all of life's problems - Homer Simpson
----
Our heads are round so our thoughts can change direction - Francis Picabia

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   


Advertise | Privacy | Mobile
Web03 | 2.6.130617.1 | Last Updated 19 Jun 2013
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid