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.
Anyone else don't like reading code unless they absolutely have to?
Started by DarkLordoftheMonkeys, Dec 17 2009 09:44 AM
44 replies to this topic
#1
Posted 17 December 2009 - 09:44 AM
Life's too short to be cool. Be a nerd.
|
|
|
#2
Posted 17 December 2009 - 11:38 AM
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
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
Posted 17 December 2009 - 11:43 AM
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:
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.
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
Posted 17 December 2009 - 01:39 PM
Nah, reading code doesn't intimidate me unless the formatting sucks. Then I reformat first, and read second.
#5
Posted 21 December 2009 - 07:34 PM
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.
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
Posted 21 December 2009 - 09:05 PM
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.
Science is only an educated theory, which we cannot disprove.
#7
Posted 23 December 2009 - 06:05 AM
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
Posted 24 December 2009 - 04:25 PM
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
Posted 25 December 2009 - 08:20 AM
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 :)
I "decompressed" a JavaScript application a while back using jEdit and a few regular expressions :)
#10
Posted 28 December 2009 - 05:06 AM
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.
However, when the thing is designed properly, usually I have no problems understanding code I haven't written.
#11
Posted 28 December 2009 - 08:38 AM
Which design patterns do you have an issue with?
#12
Posted 28 December 2009 - 09:27 AM
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.
The real design patterns are KISS and DRY. Rest should be left to the programmer's / architect's imagination.


Sign In
Create Account


Back to top









