Jump to content

Anyone else don't like reading code unless they absolutely have to?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
44 replies to this topic

#1
DarkLordoftheMonkeys

DarkLordoftheMonkeys

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 255 posts
Whenever I see code my mind just switches off and says "I don't feel like reading that." I don't even like reading my own code. I'm wondering if anyone else has experienced this problem.
Life's too short to be cool. Be a nerd.

#2
NatalieM

NatalieM

    Learning Programmer

  • Members
  • PipPipPip
  • 77 posts
If it's a long section of code, I sometimes feel the same until I remind myself that all code can be subdivided in pretty quick to digest sections and therefore, the fear of having to make sense of 1,000 lines at once goes away.

I find that if I push myself to start going through it, I normally get over my initial reticence in 10 minutes, as I get stuck into it and my brain does enjoy the challenge of making sense of and improving on the code.

I think it's human nature, it's the same symptom as writers procrastinating to avoid looking at the blank page, musicians delaying the start of a jamming session, a manager pushing a thick report s/he's been waiting for weeks to the bottom of the intray etc

#3
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
This shouldn't be a problem, fact is you have to read code, especially your own. This is why we spend so much time making it so it is readable.

I don't like reading code that hasn't been properly commented, indented, or when variables aren't given descriptive names. When people do commit these sins, reading what they wrote is pointless since it's woefully difficult to ascertain what the code is supposed to do. Observe the two code snippets below:
#include<iostream>
#include<iomanip>
int main(){
double a;int b,c,d,e;
a=1.05;b=5000;
std::cout<<"Good Times $"<<b/100<<"."<<std::setfill('0')<<std::setw(2)<<b%100<<std::setw(0)<<std::endl;
c=300*a;d=500*a;e=1000*a;b=b-(c+d+e);
std::cout<<"Oh my... $"<<b/100<<"."<<std::setw(2)<<b%100<<std::setw(0)<<std::endl;}
Looking at that, just a basic set of arithmetic is exceedingly difficult to read through, but the compiler will take it without batting an eye. Now let's try a more readable version...
#include <iostream> // For cout
#include <iomanip>  // For setw and setfill

void printValue(const char* string, int valueToPrint)
{
    // Print out the string passed and the money value passed in
    // pennies. Since it's an int in cent denominations, I'll have to
    // first divide the value by 100, then put a ".", set the width to
    // two and the fill to '0', and then print the value modulus 100.

    using namespace std;

    cout << string << " $" << valueToPrint / 100 << "." << setfill('0')
         << setw(2) << valueToPrint % 100 << setw(0) << setfill(' ') << endl;
}

int main(/* void */)
{
    // I start with $50.00, and the tax rate is 5%.
    double taxRate = 1.05;
    int    myMoney = 5000; // In cents, not dollars

    printValue("Good times", myMoney);

    // I buy 3 items, one $3, one $5, and one $10. Calculate taxes too!
    int boughtItem1 = 300  * taxRate,
        boughtItem2 = 500  * taxRate,
        boughtItem3 = 1000 * taxRate;

    myMoney = myMoney - (boughtItem1 + boughtItem2 + boughtItem3);

    printValue("Oh my...", myMoney);
}
These programs are functionally equivalent, but I ask you which one is easier to read? The second one makes it clear what is even going on in the calculation, but the first one fails altogether to convey any meaning without significant effort on the part of the reader. This is a contrived example, true, but nonetheless it shows that formatting is vital, and can even result in more pleasant to read code.

Perhaps the code you've encountered was simply not as readable as it should have been. I've encountered plenty of badly written code and in those times I really don't want to go through it, but when it's done right I have no problems. :)


EDIT: As a final note, you should never have to read the exact way a function works, it should be well documented and descriptively named all on it's own. This documentation and name should provide key indicators that tell you what the function does. If the function does more than what is indicated you've written the docs/name wrong. When you do this properly, it makes it so you can read less code and still understand what's going on, thus expediting the bug fixing and making it so adding features is far easier to accomplish. You should never have to read an implementation with a proper interface and docs.

Edited by ZekeDragon, 17 December 2009 - 11:51 AM.
See edit

Wow I changed my sig!

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Nah, reading code doesn't intimidate me unless the formatting sucks. Then I reformat first, and read second.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
sourlemon

sourlemon

    Programmer

  • Members
  • PipPipPip
  • 99 posts
I have trouble reading my own code as well. I horrible with naming my variables and functions. I understand you want something that states its purpose. But you also don't want it too long so you don't have trouble retying it. I also run into trouble of both functions doing similar thing.

I tend to be more careful when I know other are reading my code. But I get sloppy when it's just me.

So far, I've only read codes from tutorial, so they are generally simple and detailed. But I've read a few with no formatting. Now, that is just hellish.

#6
CommittedC0der

CommittedC0der

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,565 posts
Almost the same thing with me I can read my own code but as soon as I see some one elses code my brain go's to sleep on me so I copy and paste the code into a program (like notepad++)and I can read it a lot better.
A man can be defined by what he does when no one is looking.
Science is only an educated theory, which we cannot disprove.

#7
njr1489

njr1489

    Learning Programmer

  • Members
  • PipPipPip
  • 70 posts
It depends what code i'm reading. if it's designer code in Visual Studio C#, I normally leave the code in #region/#endregion blocks to hide the code, since I know what that's for.

#8
DarkLordoftheMonkeys

DarkLordoftheMonkeys

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 255 posts
One thing I absolutely hate is when an entire program or script is written on one line. It makes it impossible to read. You see this with Javascript on web pages all the time. Obviously, these scripts are generated by a computer program, but jeez, do they have to do it like that? JQuery is like this too; I looked at the JQuery file on my computer and it was completely illegible.
Life's too short to be cool. Be a nerd.

#9
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Compressing JavaScript can have a significant impact on web-page performance. Usually, there are two versions of the JavaScript: the compressed version, and the uncompressed version. jQuery has an uncompressed version that is meant for development work.

I "decompressed" a JavaScript application a while back using jEdit and a few regular expressions :)
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#10
JCoder

JCoder

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 245 posts
Reading code is more difficult than writing. It is sometimes much easier to write something by yourself from scratch than to fix someone else's code. Especially if that someone else's code is written by a fan of C++ templates or a fan of so called "design patterns" :mad:

However, when the thing is designed properly, usually I have no problems understanding code I haven't written.

#11
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Which design patterns do you have an issue with?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#12
JCoder

JCoder

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 245 posts
None. IMHO the idea of "design patterns" is flawed. People who read clever books on design patterns and then try to use them everywhere in the code end with overengineered, bloated code that is neither really flexible nor readable. Design patterns are like code snippets you can find on Internet to copypaste into your program, but instead of the code you have higher-level structures.But still it is just copypaste. And copypaste is a BAD THING.

The real design patterns are KISS and DRY. Rest should be left to the programmer's / architect's imagination.