Jun 222011
 

Its great when I can create posts without having to write much. This post is a great example. I found the following C code in a software I was reading. My only comment: Wow!

bool value0 = False;
bool value1 = False;
bool value2 = False;
bool value3 = False;

if(queue.size() == 0)
{
    value0 = True;
}
else if(queue.size() == 1)
{
    value1 = True;
}
else if(queue.size() == 2)
{
    value2 = True;
}
else if(queue.size() == 3)
{
    value3 = True;
}


write_port(value0, value1, value2, value3); 




// Somewhere in a distant file 


bool value0;
bool value1;
bool value2;
bool value3; 

read_port(&value0, &value1, &value2, &value3);

if(value0)
{
   some_foo_value = 7;
   if(some_other_condition)
   {
       stream &= True;
   } 
   else
   {
       stream &= False;
   }
} 
else if(value1)
{
   some_foo_value = 14;     
   if(some_other_condition)
   {
       stream &= True;
   } 
   else
   {
       stream &= False;
   }
}
else if(value2)
{
   some_foo_value = 21;     
   if(some_other_condition)
   {
       stream &= True;
   } 
   else
   {
       stream &= False;
   }
}
else if(value3)
{
   some_foo_value = 28;     
   if(some_other_condition)
   {
       stream &= True;
   } 
   else
   {
       stream &= False;
   }
}

  13 Responses to “A code snippet I just found”

  1. This code isn’t just some two-bit code. It’s four-bit code. Haha.

  2. The only thing I see here is some poorly written code. I’m not entirely sure what you mean that it is “sparse.” If anything, how I would have written it would be more sparse.

    • Really? This code could be written in exactly 3 lines without a single if statement and no braces. Its sparse by a factor of 22. How would you have written it to make it even more sparse? Adding blank lines wouldn’t have counted.

      • That’s actually what I was referring too; that you could write it in such a small number of line. (either I’m missing something or I need to re-evaluate my definition of “sparse”)
        Edit: Alright, seems I had the definition of sparse wrong. So, in that case, you are correct. Also, I got it in 2 lines (if I didn’t screw anything up:
        some_foo_value = (queue.size()+1)*7;
        stream &= (some_other_condition)?true:false;

        • Oh I am sorry. I misunderstood as well. Your code seems great. Even if we give more credit to the programmer, and assume that the write and read were required, it takes only a few more lines. By the way, you could simplify the last line as follows:
          //one line on producer side
          write(queue.size());
          //four lines on read side
          int size;
          read(&size);
          some_foo_value = (size+1)*7;
          stream &= (some_other_condition);

  3. I am sorry to admit, but this is the type of code I encounter all the time in ‘professional’ type code.

    Outsourcing companies seem to charge by LOC instead of clean code.

    A lot of times these types of code garbage comes from cut’n paste situation where the programmer is too lazy to read and understand the code before he puts his fat fingers on.

    I have seen >5000 lines switch statements, files with +100000 LOC, and a complete disregard for templates (or even base classes), which leads to hundreds of files with the exact same code.

    It’s ugly out there …

    • Hey,

      Didn’t see a comment from you in a long time. How is it going?

      I work with some real ninja coders so I get to see less crappy code but I know that it exists out that everywhere. Your point about outsourcing and software quality is funny and sad at the same time. LOC is a horrible metric indeed.

      Aater

      • funny that you mention Ninja programmer. My take on them is :

        I don’t want to hire for people who spy on you, stab you in the back, and whose code explodes all around you. I want Samuraii coder, well trained and loyal.

        Look at the definition of Ninja on wikipedia
        Ninja
        and Samurai
        Samurai

        Thought I mention it even though it is way off topic :)

        • Lol. Damn! that explains why all my colleagues quit work within six months and then we see competition coming up with a product similar to what we were doing. I will use Samurai programmers in the future. Where do I find them though?

          • Very funny comment indeed!
            But, seriously, I really like your blog and I was wondering when a code is bad written and when a code must be explode it in several lines to optimize the work of the compiler and, the code runs faster?
            To answer that you have to understand compilers and, my degree didnt cover that :-(

          • :-) Thanks for your complement. I can really use the encouragement when I write my posts at 3AM after a long day at work.

            I have actually been thinking about it a lot myself. Is it better to write complex code or simple code if you want the compiler to do its job well. Not sure if you read my post about compilers (http://www.futurechips.org/tips-for-power-coders/how-to-trick-cc-compilers-into-generating-terrible-code.html). My examples there seem to indicate that the goal should be to obfuscate as little as possible from the compiler. The more big picture you can convey using annotations and keywords, the better the code will be.

            Aater

 Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>