Braindead Code

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

User avatar
segmentation fault
Posts: 1770
Joined: Wed Dec 05, 2007 4:10 pm UTC
Location: Nu Jersey
Contact:

Re: Braindead Code

Postby segmentation fault » Fri Jan 04, 2008 6:55 pm UTC

pieaholicx wrote:Unless you're working in a language that doesn't support that


i cant think of one that doesnt.

pieaholicx wrote:, or you want to make the code clear.


the extra unneeded comparison might be wasting precious clock cycles.
people are like LDL cholesterol for the internet

User avatar
pieaholicx
The cake is a lie!
Posts: 531
Joined: Mon Oct 22, 2007 12:51 pm UTC
Contact:

Re: Braindead Code

Postby pieaholicx » Fri Jan 04, 2008 6:57 pm UTC

segmentation fault wrote:
pieaholicx wrote:Unless you're working in a language that doesn't support that


i cant think of one that doesnt.

Java. I believe I win?

segmentation fault wrote:
pieaholicx wrote:, or you want to make the code clear.


the extra unneeded comparison might be wasting precious clock cycles.

Wasting clock cycles is one thing, conforming to company standards is another thing.
It's okay, I'm Chaotic Neutral. I can kill him over the loot.
Overexposure to pieaholicx may, in semi-rare cases, emancipate dental fillings, crowns, tooth enamel, and teeth.

Pike
Posts: 6
Joined: Fri Nov 02, 2007 2:19 pm UTC

Re: Braindead Code

Postby Pike » Fri Jan 04, 2008 7:11 pm UTC

Most bits of braindead code I've found were originally written by me. What a great tool hind-sight is.
I should gather lots of hind-sight together and bottle it. I think lots of people would buy it.

Mike

User avatar
segmentation fault
Posts: 1770
Joined: Wed Dec 05, 2007 4:10 pm UTC
Location: Nu Jersey
Contact:

Re: Braindead Code

Postby segmentation fault » Fri Jan 04, 2008 7:13 pm UTC

pieaholicx wrote:Java. I believe I win?


fine. java is retarded.
people are like LDL cholesterol for the internet

EvanED
Posts: 4331
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI
Contact:

Re: Braindead Code

Postby EvanED » Fri Jan 04, 2008 7:15 pm UTC

GrammarBatDeath wrote:
edanite wrote:My favorite leap-year related code is

Code: Select all

$leap = $year % 4 ? 0 : $year % 100 ? 1 : $year % 400 ? 0 : 1;

which, while correct, would benefit greatly from some parentheses and perhaps some comparisons.
Ha, ha. Correct but with unnecessaries.

Code: Select all

$leap = $year%4 ? 0 : 1;

isleapyear(1900) would return true. Oops.

pieaholicx wrote:, or you want to make the code clear.

I have a tendency to use explicit comparisons, but lacking them is not a serious hit to readability if you are used to programming in a language.

segmentation fault wrote:the extra unneeded comparison might be wasting precious clock cycles.

What code do you think the compiler is generating if you leave off the != 0? If you compile with optimization, I can almost guarantee that there is no difference in the generated code.

pieaholicx wrote:Java. I believe I win?

You're correct, but clearly the code is not Java.
Last edited by EvanED on Fri Jan 04, 2008 7:18 pm UTC, edited 1 time in total.

User avatar
pieaholicx
The cake is a lie!
Posts: 531
Joined: Mon Oct 22, 2007 12:51 pm UTC
Contact:

Re: Braindead Code

Postby pieaholicx » Fri Jan 04, 2008 7:18 pm UTC

EvanED wrote:
pieaholicx wrote:Java. I believe I win?

You're correct, but clearly the code is not Java.

Yes, but that was not what that was responding to. It was pointing out a language where false != 0.

Also, years that don't work with a pure %4 include 2100, 2200, and 2300.
It's okay, I'm Chaotic Neutral. I can kill him over the loot.
Overexposure to pieaholicx may, in semi-rare cases, emancipate dental fillings, crowns, tooth enamel, and teeth.

User avatar
GrammarBatDeath
Posts: 26
Joined: Thu Dec 13, 2007 7:11 pm UTC

Re: Braindead Code

Postby GrammarBatDeath » Sat Jan 05, 2008 8:17 pm UTC

EvanED wrote:isleapyear(1900) would return true. Oops.

Yes, oops. I had forgotten that leap years skip the 100's that aren't 400's. Sorry!

Code: Select all

( an early book on dBASE III )

  Infinite Loop........see Loop, Infinite

( and then ):

  Loop, Infinite.......see Infinite Loop

User avatar
Berengal
Superabacus Mystic of the First Rank
Posts: 2707
Joined: Thu May 24, 2007 5:51 am UTC
Location: Bergen, Norway
Contact:

Re: Braindead Code

Postby Berengal » Sun Jan 06, 2008 2:56 pm UTC

EvanED wrote:
segmentation fault wrote:the extra unneeded comparison might be wasting precious clock cycles.

What code do you think the compiler is generating if you leave off the != 0? If you compile with optimization, I can almost guarantee that there is no difference in the generated code.

With optimization? I don't think it's even possible to waste more processor cycles without optimization even. Think of it, in (faux) assembly it would be something like:

Code: Select all

cmp a, 0 ;compare a to 0 (you always have to compare to something. Writing nothing is just shorthand for zero anyway)
jz comparisonfalse ;(jz = jump if equal)
[if true code]
jmp endif
:comparisonfalse
[optional else code]
:endif

All different kinds of conditionals can be expressed with just two opcodes, the compare followed by either jump if equal, jump if not equal, jump if below, jump if above, jump if not below or jump if not above. As long as there's no extra calculation required, like calling a function, all of these will be equally fast: two clock cycles, independent on what is compared to what.
It is practically impossible to teach good programming to students who are motivated by money: As potential programmers they are mentally mutilated beyond hope of regeneration.

arcoain
Posts: 56
Joined: Thu Dec 20, 2007 12:34 am UTC

Re: Braindead Code

Postby arcoain » Sun Jan 06, 2008 4:52 pm UTC

pieaholicx wrote:or you want to make the code clear.

gah!! who would ever want to do that?

User avatar
Cosmologicon
Posts: 1806
Joined: Sat Nov 25, 2006 9:47 am UTC
Location: Cambridge MA USA
Contact:

Re: Braindead Code

Postby Cosmologicon » Sun Jan 06, 2008 11:30 pm UTC

pieaholicx wrote:It was pointing out a language where false != 0.

If you were writing in C, you'd only have to say "It was pointing out a language where false."

EvanED
Posts: 4331
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI
Contact:

Re: Braindead Code

Postby EvanED » Mon Jan 07, 2008 3:03 am UTC

Berengal wrote:
EvanED wrote:
segmentation fault wrote:the extra unneeded comparison might be wasting precious clock cycles.

What code do you think the compiler is generating if you leave off the != 0? If you compile with optimization, I can almost guarantee that there is no difference in the generated code.

With optimization? I don't think it's even possible to waste more processor cycles without optimization even.

I'm not sure... I could see certain compilers (cough GCC cough*) loading the 0 into a register as an immediate, then doing a compare against that.

You very well may be right, but I didn't want to make such a bold statement without checking into it first. ;-)

(* GCC tends to create really stupid code without optimization. About the dumbest translation you could come up with without actively trying to be bad. This is probably a good thing, as it means less code to go wrong in the compiler.)

I'll take a look and see what it does, though not this very moment.

User avatar
Sc4Freak
Posts: 673
Joined: Thu Jul 12, 2007 4:50 am UTC
Location: Redmond, Washington

Re: Braindead Code

Postby Sc4Freak » Mon Jan 07, 2008 3:08 am UTC

Cosmologicon wrote:
pieaholicx wrote:It was pointing out a language where false != 0.

If you were writing in C, you'd only have to say "It was pointing out a language where false."

C doesn't have the false keyword. :P

EvanED wrote:
Berengal wrote:
EvanED wrote:
segmentation fault wrote:the extra unneeded comparison might be wasting precious clock cycles.

What code do you think the compiler is generating if you leave off the != 0? If you compile with optimization, I can almost guarantee that there is no difference in the generated code.

With optimization? I don't think it's even possible to waste more processor cycles without optimization even.

I'm not sure... I could see certain compilers (cough GCC cough*) loading the 0 into a register as an immediate, then doing a compare against that.

You very well may be right, but I didn't want to make such a bold statement without checking into it first. ;-)

(* GCC tends to create really stupid code without optimization. About the dumbest translation you could come up with without actively trying to be bad. This is probably a good thing, as it means less code to go wrong in the compiler.)

I'll take a look and see what it does, though not this very moment.

Literals like that are usually encoded in the instruction stream. Some bad mojo can happen if you load it from memory, I think. Especially if someone decides to change the value of '0'. :lol:

User avatar
Hangar
Posts: 171
Joined: Fri Nov 23, 2007 3:41 am UTC

Re: Braindead Code

Postby Hangar » Mon Jan 07, 2008 4:35 am UTC

Sc4Freak wrote:C doesn't have the false keyword. :P


You should take a look at C99.


By the way, I've heard that gcc is more buggy without optimization, since it's used less that way.

EvanED
Posts: 4331
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI
Contact:

Re: Braindead Code

Postby EvanED » Mon Jan 07, 2008 5:18 am UTC

Nope, I'm wrong, at least with GCC on x86. The != 0 makes absolutely no difference in generated code, even with no optimization.

User avatar
evilbeanfiend
Posts: 2650
Joined: Tue Mar 13, 2007 7:05 am UTC
Location: the old world

Re: Braindead Code

Postby evilbeanfiend » Mon Jan 07, 2008 1:28 pm UTC

Hangar wrote:
Sc4Freak wrote:C doesn't have the false keyword. :P


You should take a look at C99.


By the way, I've heard that gcc is more buggy without optimization, since it's used less that way.


in theory yes, but practically you rarely run into genuine compiler issues (although they suck ass when you do)
in ur beanz makin u eveel

Vempele
Posts: 69
Joined: Wed Nov 14, 2007 8:05 am UTC

Re: Braindead Code

Postby Vempele » Mon Jan 07, 2008 3:01 pm UTC

Cosmologicon wrote:
pieaholicx wrote:It was pointing out a language where false != 0.

If you were writing in C, you'd only have to say "It was pointing out a language where false 0."


The difference between 0 and false is that false is defined and never true, but 0 might be undefined or even Yes.
const int ALMOST_FIFTY = 80;

Rysto
Posts: 1460
Joined: Wed Mar 21, 2007 4:07 am UTC

Re: Braindead Code

Postby Rysto » Mon Jan 07, 2008 3:28 pm UTC

EvanED wrote:Nope, I'm wrong, at least with GCC on x86. The != 0 makes absolutely no difference in generated code, even with no optimization.

Well, by the standard if(x) is exactly equivalent to if(x != 0), so maybe that's why.

User avatar
Tawnos
Posts: 67
Joined: Mon Mar 05, 2007 3:40 am UTC
Location: Sammamish, WA
Contact:

Re: Braindead Code

Postby Tawnos » Tue Jan 08, 2008 7:37 am UTC

wing wrote:
Spoiler:

Code: Select all

// checkForRow2Rdy()
   //********************************************
   // check to see if all dealt cards are in row 2
   //********************************************
   private void checkForRow2Rdy()
   {   
      boolean ready[] = new boolean[hand.length];
      boolean ok = true;
      NoEqualButton = true;

      for(int i=0; i<hand.length; i++)
      {
         if(hand[i].getY() == 285)
         {
            ready[i]=true;
         }
         else
         {
            ready[i]=false;
         }
      }

      for(int i=0; i<ready.length; i++)
      {
         if(!ready[i])
            ok = false;
      }


      NoEqualButton = !ok;
   }


The only thing I really care to comment on is this, because I see it all the time. If you refactored this, why are you using if/else to set a boolean? The above could be done as such:

Code: Select all

private void checkForRow2Rdy()
   {   
      boolean ready[] = new boolean[hand.length];
      boolean ok = true;
      NoEqualButton = true;

      for(int i=0; i<hand.length; i++)
      {
         ready[i] = (hand[i].getY() == 285);
      }

      for(int i=0; i<ready.length; i++)
      {
         if(!ready[i])
            ok = false;
      }


      NoEqualButton = !ok;
   }

(and there could be a break on the first not ready...but that's another thing... also, why is there a separate boolean for NoEqualButton? Why not just invert ok?)
If determinism is right, does it matter that I believe in free will?

User avatar
Berengal
Superabacus Mystic of the First Rank
Posts: 2707
Joined: Thu May 24, 2007 5:51 am UTC
Location: Bergen, Norway
Contact:

Re: Braindead Code

Postby Berengal » Tue Jan 08, 2008 8:20 am UTC

Vempele wrote:
Cosmologicon wrote:
pieaholicx wrote:It was pointing out a language where false != 0.

If you were writing in C, you'd only have to say "It was pointing out a language where false 0."


The difference between 0 and false is that false is defined and never true, but 0 might be undefined or even Yes.

Case in point: Ruby.
Wikipedia wrote:Boolean evaluation of non-boolean data is strict: 0, "" and [] are all evaluated to true. In C, the expression 0 ? 1 : 0 evaluates to 0 (i.e. false). In Ruby, however, it yields 1, as all numbers evaluate to true; only nil and false evaluate to false.
It is practically impossible to teach good programming to students who are motivated by money: As potential programmers they are mentally mutilated beyond hope of regeneration.

User avatar
pieaholicx
The cake is a lie!
Posts: 531
Joined: Mon Oct 22, 2007 12:51 pm UTC
Contact:

Re: Braindead Code

Postby pieaholicx » Tue Jan 08, 2008 3:15 pm UTC

Berengal wrote:Case in point: Ruby.
Wikipedia wrote:Boolean evaluation of non-boolean data is strict: 0, "" and [] are all evaluated to true. In C, the expression 0 ? 1 : 0 evaluates to 0 (i.e. false). In Ruby, however, it yields 1, as all numbers evaluate to true; only nil and false evaluate to false.

Wow, Ruby is even more retarded than Java. No wonder I never wanted to learn it.
It's okay, I'm Chaotic Neutral. I can kill him over the loot.
Overexposure to pieaholicx may, in semi-rare cases, emancipate dental fillings, crowns, tooth enamel, and teeth.

User avatar
GrammarBatDeath
Posts: 26
Joined: Thu Dec 13, 2007 7:11 pm UTC

Re: Braindead Code

Postby GrammarBatDeath » Tue Jan 08, 2008 3:48 pm UTC

pieaholicx wrote:Wow, Ruby is even more retarded than Java. No wonder I never wanted to learn it.

You took the words right out of my mouth.
Things like that, and this (in perl):

Code: Select all

$f = 'i';
$i = "Hello"
print $$f;

That make me (a C programmer) cringe in fear and pain.

Code: Select all

( an early book on dBASE III )

  Infinite Loop........see Loop, Infinite

( and then ):

  Loop, Infinite.......see Infinite Loop

User avatar
segmentation fault
Posts: 1770
Joined: Wed Dec 05, 2007 4:10 pm UTC
Location: Nu Jersey
Contact:

Re: Braindead Code

Postby segmentation fault » Tue Jan 08, 2008 4:10 pm UTC

Rysto wrote:
EvanED wrote:Nope, I'm wrong, at least with GCC on x86. The != 0 makes absolutely no difference in generated code, even with no optimization.

Well, by the standard if(x) is exactly equivalent to if(x != 0), so maybe that's why.


i didnt think it was. i thought x alone will generate code to compare it with 0, and x != 0 will first evaluate the expression, and then compare that return value with 0.

pieaholicx wrote:Wow, Ruby is even more retarded than Java. No wonder I never wanted to learn it.


isnt Java's "false" the only thing that evaluates to false?

i dont get why its so retarded. since everything is a reference, 0 "exists" in a way and should evaluate to true. the only things that should evaluate to false are things that explicitly return nil or false. i believe Java is the same way.
people are like LDL cholesterol for the internet

User avatar
GrammarBatDeath
Posts: 26
Joined: Thu Dec 13, 2007 7:11 pm UTC

Re: Braindead Code

Postby GrammarBatDeath » Tue Jan 08, 2008 6:04 pm UTC

segmentation fault wrote:i dont get why its so retarded. since everything is a reference, 0 "exists" in a way and should evaluate to true. the only things that should evaluate to false are things that explicitly return nil or false. i believe Java is the same way.

In C, things are different. If, when we try and interpret them as true or false, they make sense as false, they're false. It's the same with Ruby, I suppose. But Ruby is brain dead because of the reasons for the fact, not because of the fact itself.

Code: Select all

( an early book on dBASE III )

  Infinite Loop........see Loop, Infinite

( and then ):

  Loop, Infinite.......see Infinite Loop

User avatar
segmentation fault
Posts: 1770
Joined: Wed Dec 05, 2007 4:10 pm UTC
Location: Nu Jersey
Contact:

Re: Braindead Code

Postby segmentation fault » Tue Jan 08, 2008 11:37 pm UTC

so in trying to make Ruby a powerful language, they had to sacrifice non-strict boolean, and thats bad? its a small price to pay for what Ruby can do.
people are like LDL cholesterol for the internet

User avatar
pieaholicx
The cake is a lie!
Posts: 531
Joined: Mon Oct 22, 2007 12:51 pm UTC
Contact:

Re: Braindead Code

Postby pieaholicx » Wed Jan 09, 2008 1:03 am UTC

segmentation fault wrote:
pieaholicx wrote:Wow, Ruby is even more retarded than Java. No wonder I never wanted to learn it.


isnt Java's "false" the only thing that evaluates to false?

i dont get why its so retarded. since everything is a reference, 0 "exists" in a way and should evaluate to true. the only things that should evaluate to false are things that explicitly return nil or false. i believe Java is the same way.

Depends on whether you're using the native type boolean, or the class Boolean. With the class Boolean than you can give it a string, however unless said string is "true", it's false. Java has highly defined boolean types.
It's okay, I'm Chaotic Neutral. I can kill him over the loot.
Overexposure to pieaholicx may, in semi-rare cases, emancipate dental fillings, crowns, tooth enamel, and teeth.

mrkite
Posts: 336
Joined: Tue Sep 04, 2007 8:48 pm UTC

Re: Braindead Code

Postby mrkite » Wed Jan 09, 2008 2:34 am UTC

EvanED wrote:I'm not sure... I could see certain compilers (cough GCC cough*) loading the 0 into a register as an immediate, then doing a compare against that.


gcc is actually much better since version 3. It will compile:

Code: Select all

a=b?0:1;

into:

Code: Select all

mov eax,[dword ptr b]
cmp eax,0
setnz eax
mov [dword ptr a],eax


by default. With optimization gcc will usually factor the entire thing out.

User avatar
Iori_Yagami
Posts: 606
Joined: Wed Oct 03, 2007 8:37 pm UTC

Re: Braindead Code

Postby Iori_Yagami » Tue Jan 15, 2008 6:05 pm UTC

This doesn't necessarily qualify as brain-dead. It is more like a competition between practical and beautiful.

What I had - a sub-menu to launch some reports - custom forms. Forms are quite similar, and I did it like this:

Somewhere, gets changed when a new report is added.
Spoiler:

Code: Select all

private const int REPORT_COUNT = 6;


The logic to build menu at launch time. Quite beautiful, I think.

Spoiler:

Code: Select all

private void AddMenus()
      {
         ResourceManager rm =
            new ResourceManager("Tickets.Properties.Resources", Assembly.GetExecutingAssembly());
         for (int i = 1; i < REPORT_COUNT + 1; i++)
            AddReport(
               rm.GetString("report" + i.ToString() + "MenuName"),
               i - 1,
               "frmR" + i.ToString("000"));
      }

      protected void AddReport(string menuName, int position, string formName)
      {
         if (position > this.miReports.DropDownItems.Count)
            position = this.miReports.DropDownItems.Count;
         if (position < 0)
            position = 0;
         ToolStripMenuItem mi = new ToolStripMenuItem(menuName);
         mi.Tag = formName;
         mi.Click += new EventHandler(mi_Click);
         this.miReports.DropDownItems.Insert(position, mi);
      }

      private void mi_Click(object sender, EventArgs e)
      {
         frmEntry frm = null;
         string fname = ((ToolStripMenuItem)sender).Tag.ToString();
         frm = (frmEntry)Activator.CreateInstance(Type.GetType("Tickets.ReportForms." + fname));
         frm.MdiParent = this;
         frm.Show();
      }


Somewhere in XML resource files:

Spoiler:

Code: Select all

<data name="report1MenuName" xml:space="preserve">
    <value>Acquired tickets for period</value>
  </data>
  <data name="report2MenuName" xml:space="preserve">
    <value>Spent tickets for period</value>
  </data>
  <data name="report3MenuName" xml:space="preserve">
    <value>Spared tickets list</value>
  </data>


This all worked - you added a report, wrote it name in resources, changed report count and compiled.
I actually got yelled at because "I always create everything the most perverse way" and it got changed to this, arguably simpler but so ugly:

Spoiler:

Code: Select all

private void miR001_Click(object sender, EventArgs e)
      {
         Tickets.ReportForms.frmR001 frm = new Tickets.ReportForms.frmR001();
         frm.MdiParent = this;
         frm.Show();
      }

      private void miR002_Click(object sender, EventArgs e)
      {
         Tickets.ReportForms.frmR002 frm = new Tickets.ReportForms.frmR002();
         frm.MdiParent = this;
         frm.Show();
      }

      private void miR003_Click(object sender, EventArgs e)
      {
         Tickets.ReportForms.frmR003 frm = new Tickets.ReportForms.frmR003();
         frm.MdiParent = this;
         frm.Show();
      }
private void miR004_Click(object sender, EventArgs e)
      {
         Tickets.ReportForms.frmR004 frm = new Tickets.ReportForms.frmR004();
         frm.MdiParent = this;
         frm.Show();
      }

      private void miR005_Click(object sender, EventArgs e)
      {
         Tickets.ReportForms.frmR005 frm = new Tickets.ReportForms.frmR005();
         frm.MdiParent = this;
         frm.Show();
      }

      private void miR006_Click(object sender, EventArgs e)
      {
         Tickets.ReportForms.frmR006 frm = new Tickets.ReportForms.frmR006();
         frm.MdiParent = this;
         frm.Show();
      }
They cannot defend themselves; they cannot run away. INSANITY is their only way of escape.

User avatar
rrwoods
Posts: 1509
Joined: Mon Sep 24, 2007 5:57 pm UTC
Location: US

Re: Braindead Code

Postby rrwoods » Tue Jan 15, 2008 7:01 pm UTC

I definitely like your way better, but I'll add an improvement: You shouldn't need to manually change the report count. Since you're storing the report definitions in XML, you should be able to count the subnodes of your top-level node. Also, if you load the report definitions at start time, you don't even need a recompile.
31/M/taken/US
age/gender/interest/country

Belial wrote:The sex card is tournament legal. And I am tapping it for, like, six mana.

User avatar
hotaru
Posts: 1045
Joined: Fri Apr 13, 2007 6:54 pm UTC

Re: Braindead Code

Postby hotaru » Tue Jan 15, 2008 10:37 pm UTC

Code: Select all

  while(!feof(post_file_in)){
    fgets(line,MAX_POST_LENGTH*5+29,post_file_in);
    if(strstr(line,post_num)||(strstr(line,"</posts>")&&!wrote_post)){
      fprintf(post_file_out," <post id=%s<text>%s</text></post>\n",post_num,text_out);
      wrote_post=1;
    }
    if(!strstr(line,post_num)&&!feof(post_file_in)){
      fputs(line,post_file_out);
    }
  }

Code: Select all

factorial product enumFromTo 1
isPrime n 
factorial (1) `mod== 1

User avatar
segmentation fault
Posts: 1770
Joined: Wed Dec 05, 2007 4:10 pm UTC
Location: Nu Jersey
Contact:

Re: Braindead Code

Postby segmentation fault » Wed Jan 16, 2008 6:43 am UTC

wow. i cant help but notice that they take the effort to define MAX_POST_LENGTH, but not 5, 29, "</posts>", "<text>", and so on.
people are like LDL cholesterol for the internet

GMontag
Posts: 209
Joined: Mon Apr 16, 2007 5:47 am UTC
Location: Bellingham, Washington

Re: Braindead Code

Postby GMontag » Wed Jan 16, 2008 11:35 am UTC

wing wrote:
segmentation fault wrote:i love constructors that are a billion arguments long. its like the point of OO went right over the authors' heads.

Oh no, my favorite is constructors that take one object as a parameter instead of taking a bajillion arguments. Take a look at this:

Code: Select all

public foo(fooArgs args)
{
//construct stuff.
}

Code: Select all

public fooArgs(arg1, arg2, arg3................argn-1, argn)
{
   flag1 = arg1;
   flag2 = arg2;
   flag3 = arg3;
   ....................
   flagn-1=argn-1
   flagn=argn
}


That's pretty dumb, but constructors with one parameter of the type of the constructor like:

Code: Select all

public foo(foo oldFoo){
}

seems like a perfectly valid stylistic alternative to foo.clone to me.

User avatar
Sc4Freak
Posts: 673
Joined: Thu Jul 12, 2007 4:50 am UTC
Location: Redmond, Washington

Re: Braindead Code

Postby Sc4Freak » Wed Jan 16, 2008 1:50 pm UTC

That's how it's done in C++, at least. (it's called a "copy constructor").

User avatar
segmentation fault
Posts: 1770
Joined: Wed Dec 05, 2007 4:10 pm UTC
Location: Nu Jersey
Contact:

Re: Braindead Code

Postby segmentation fault » Wed Jan 16, 2008 4:34 pm UTC

GMontag wrote:seems like a perfectly valid stylistic alternative to foo.clone to me.


as Sc4 said, its a copy constructor. the clone methods are virtual copy constructors.

the problem are constructors taking waaay too many arguments, instead of passing it a class or struct of some sort that contains all that data.

not to mention the use assignment instead of an initializer list.
people are like LDL cholesterol for the internet

User avatar
Xanthir
My HERO!!!
Posts: 5426
Joined: Tue Feb 20, 2007 12:49 am UTC
Location: The Googleplex
Contact:

Re: Braindead Code

Postby Xanthir » Thu Jan 17, 2008 1:48 am UTC

Just to jump back a bit to the boolean discussion, the entirety of the lisp family works differently than C. There, *everything* evaluates to true, except for the special false value (varies between languages, but it's nil in Lisp and #f in Scheme).

Luckily the Lisp family is loosely typed, so you don't have to mess with an explicit boolean unless you want to. The benefit of this is that a function can just use its return value as a success boolean. There's also a trivial design pattern for handling when nil *is* a valid return value (use a (values) form when returning values, and use a (multiple-value-bind) form when grabbing return values).
(defun fibs (n &optional (a 1) (b 1)) (take n (unfold '+ a b)))

HappySmileMan
Posts: 52
Joined: Fri Nov 09, 2007 11:46 pm UTC

Re: Braindead Code

Postby HappySmileMan » Sun Jan 20, 2008 10:17 pm UTC

I just wrote this

Code: Select all

#include"ios"
main(){char*d=" of hamaji",a[]=" shinichiroes",*f=" on the wall";for(int i=99;i;){printf("%d%s%s%s, %d%s%s.\n",i,a,d,f,i,a,d);--i;if(i<2)a[11]=0;i?printf("Take one down and pass it around, %d%s%s%s.\n\n",i,a,d,f):printf("Go to the store and buy some more, 99%ses%s%s.",a,d,f);}}


To solve this challenge. It's 292 characters, I'm wondering now how most of the people who did it in C did it in much less than that, in theory it shouldn't be any easier in C than C++

User avatar
hotaru
Posts: 1045
Joined: Fri Apr 13, 2007 6:54 pm UTC

Re: Braindead Code

Postby hotaru » Tue Jan 22, 2008 4:45 am UTC

Code: Select all

SELECT time FROM posts ORDER BY CONCAT(SUBSTR(time, 21, 4), LPAD(FIND_IN_SET(SUBSTR(time, 5, 3), 'Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec'), 2, '0'), SUBSTR(time, 9, 2), SUBSTR(time, 12, 2),SUBSTR(time, 15, 2),SUBSTR(time, 18, 2)) DESC LIMIT 1;

Code: Select all

factorial product enumFromTo 1
isPrime n 
factorial (1) `mod== 1

User avatar
segmentation fault
Posts: 1770
Joined: Wed Dec 05, 2007 4:10 pm UTC
Location: Nu Jersey
Contact:

Re: Braindead Code

Postby segmentation fault » Tue Jan 22, 2008 3:38 pm UTC

now now hotaru, that SQL statement could be perfectly fine if youre working with an engine that doesnt support datatypes like date and time.


although i cant think of one that doesnt.
people are like LDL cholesterol for the internet

Rysto
Posts: 1460
Joined: Wed Mar 21, 2007 4:07 am UTC

Re: Braindead Code

Postby Rysto » Thu Jan 24, 2008 2:06 am UTC

Just today I wrote the following code:

Code: Select all

public String symbolAfterDot() {
   //...
}

// worst method name ever...
public List<String> sententialAfterSymbolAfterDot() {
   //...

User avatar
'; DROP DATABASE;--
Posts: 3284
Joined: Thu Nov 22, 2007 9:38 am UTC
Location: Midwest Alberta, where it's STILL snowy
Contact:

Re: Braindead Code

Postby '; DROP DATABASE;-- » Thu Jan 24, 2008 4:17 am UTC

segmentation fault wrote:although i cant think of one that doesnt.
Where have I heard this before? ;)

One of my earliest VB apps had this gem:

Code: Select all

if something = true then
dosomething
else if something = false then
dosomethingelse
else
msgbox "Oh me yarm Oh haxor alert"
end
end if
'something' was of course a boolean.

Also, this goes without saying.
poxic wrote:You suck. And simultaneously rock. I think you've invented a new state of being.

User avatar
Sc4Freak
Posts: 673
Joined: Thu Jul 12, 2007 4:50 am UTC
Location: Redmond, Washington

Re: Braindead Code

Postby Sc4Freak » Thu Jan 24, 2008 11:17 am UTC

'; DROP DATABASE;-- wrote:
segmentation fault wrote:although i cant think of one that doesnt.
Where have I heard this before? ;)

One of my earliest VB apps had this gem:

Code: Select all

if something = true then
dosomething
else if something = false then
dosomethingelse
else
msgbox "Oh me yarm Oh haxor alert"
end
end if
'something' was of course a boolean.

Also, this goes without saying.

That piece of code is not as crazy as it looks. Nullable types in .NET 2.0 mean that the boolean 'something' could actually be null, instead of true or false. In that case, neither of the two comparisons would evaluate as true, and you'd get your "Oh me yarm Oh haxor alert" message.


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 8 guests