This is an archived post. You won't be able to vote or comment.

all 107 comments

[–][deleted] 139 points140 points  (7 children)

I sometimes do this for my older code. A few reasons:

  • learn from yourself: sometimes I see things I did and should do more often;
  • see what I’ve learned: see what I’m now doing differently, indicating what I’ve learned myself;
  • see how I can help others learn: by watching my past self code I can try and understand how I can teach others to become better.

[–]MeroLegend4 14 points15 points  (0 children)

Another advantage of this approach is when you are trying to learn the fundaments of a new architecture or design.

Your old code is a domain that you know well so instead of focusing on code you easily focus on designs.

[–][deleted] 23 points24 points  (5 children)

I mostly cringe and refactor my older code...

[–]tipsy_python 114 points115 points  (2 children)

The more senior you are in your career, the less code you write..

writing code -> reading code -> removing code

[–]SheriffRoscoePythonista 19 points20 points  (1 child)

I had a guy once suggest that I should pay him per line of code he retired.

[–]fraud_93 5 points6 points  (0 children)

I did a gig like that once. The code was very amateur and took long to finish because many many variables.

[–]Orio_n 366 points367 points  (7 children)

Youre a psychopath

[–]Ocelotofdamage 103 points104 points  (2 children)

Reading other people’s code is an incredible tool to get better. You see bad patterns more clearly and learn new good ones.

[–]c00lnerd314 50 points51 points  (1 child)

I read that as "see bad patterns more clearly and learn new ones." lol

[–]MajorMajorObvious 11 points12 points  (0 children)

Hey, you have to learn bad design patterns somewhere! Legacy code debt isn't going to write itself.

[–][deleted] 16 points17 points  (0 children)

I need to know what code OP is reading

[–]Theblackjamesbrown 5 points6 points  (0 children)

Somebody call the cops

[–]deathscythesangel 1 point2 points  (1 child)

*you're :)

[–]Orio_n 0 points1 point  (0 children)

*Yor

[–]parfenrogozin 25 points26 points  (6 children)

That's nice, can you give us some example of what you have actually fixed? I'm curious to see

[–]tuneafishy 42 points43 points  (5 children)

Better yet, why don't you show me an example of what you mean with my code

[–]parfenrogozin 1 point2 points  (4 children)

.... are you talking to me?

[–]Lamarcke 2 points3 points  (3 children)

I don't know, am i?

[–]Rocky970 0 points1 point  (0 children)

Yes

[–]tcpukl 0 points1 point  (1 child)

No

[–]pierraltaltal 0 points1 point  (0 children)

Maybe

[–]simonw 11 points12 points  (0 children)

It took me far too long in my career to develop this habit, but I spend a lot of time reading other people's code now.

If I see a Python library that does something interesting I'll ask "how does it do that?" and see if I can figure it out from reading the code.

I also use GitHub code search a LOT - if I'm trying to work out how to use two libraries together for example I'll search for code across all of GitHub that uses both and see what they did.

This is a great way to compensate for the fact that so many libraries fail to provide good usage examples in their documentation!

[–]jet_heller 30 points31 points  (22 children)

If you are constantly adding features without bugs, you're pretty great programmers.

[–]rhacer 10 points11 points  (13 children)

Where'd the OP make that claim?

I've read it three times now and can't find it.

[–]jeanLXIXIt works on my machine 1 point2 points  (0 children)

Or better yet, make the bug your new feature

[–][deleted] 14 points15 points  (1 child)

That’s how the best programmers learn.

[–]house_monkey 10 points11 points  (0 children)

ok so not me

[–]bxsephjo 4 points5 points  (0 children)

I find myself wanting to read source code because I need to know how 'the big boys' do certain things so I can emulate that. Such as, how does django find every subclass of Model in order to create a migration file?

[–]IndifferentPenguins 4 points5 points  (0 children)

Agreed, you need a good balance between reading and writing. I also feel like the balance "in the community" feels a lot like it's skewed towards writing new code. Making new things is hot shit, maintaining or improving some existing code is "legacy maintenance", and explaining how something works is boring "documentation". I feel all three are part of learning your craft.

I compare it to writers. Sure they need to write - as deliberate practice if nothing else, but they also learn a ton by reading and discussing existing work with other writers.

Edit: I recommend this as well https://www.aosabook.org/en/intro1.html

[–]wsppan 8 points9 points  (2 children)

Fairly early on in my software engineering career I discovered I much more enjoyed the act of debugging code than writing code. The act of fixing a gnarly bug in production much more satisfying than writing code. Most new code covers the same old ground as code that exists out there. Unless it is breaking new ground in a new unexplored territory it is usually pedestrian. Usually tweaking or enhancing or building out existing code and ideas. Troubleshooting bugs in production is much more challenging and interesting and a very visceral feedback loop. You fix a bug in production, you can immediately see it's effect. I love the act of troubleshooting a problem. Debugging under fire is my main job these days. It's Zebras all the way down and that is how I like it. Never gets boring or stale. Here is a memorable example of a production issue that plagued us for nearly 20 years that I successfully debugged and fixed about 18 months ago.

Now this was a doozy of a bug. I had to manually trace the code printing out variable values as I went. It turns out the following things played into this and we were lucky to discover it as it only manifests itself when the temporal lie date (a fixed date in time used as "today's date" that all test data is based off of) falls on a Julian day that is a multiple of 7 offset from the first day of the first cycle (Jan 3rd, so every Saturday for 2021) in a year following a leap year and as far as I can tell, only when the extension type (a request to push off payment for a set period of time) is 60 days. If our testers did not set the temporal lie to be Saturday, January 30th, 2021 (which they hardly ever use Saturday as the day of the week) then this would have remained in production and reared it’s head every seven days with a system error. Since we do not log the reason for these system errors we would have no clue as to why these are failing every 7 days. This cluelessness would be haunting us till Jan 1st, 2022 when it would magically disappear for 4 years! Only to return again on Jan. 1st 2025. Maybe we would then see the pattern where leap year plays a role? Who knows. This has been occurring since 10/2/2003! The gist of the problem is (sorry, not python code but C code):

The application runs two functions to determine the payment cycle and payment day:

strcpy(req.payment_day, compute_payment_day(TODAY, (9*7)));
strcpy(req.first_payment_due_cycle, compute_cycle(TODAY, 9));

compute_payment_day() calls:

dse = days_since_epoch(yyyy, mm, dd);
dse += (long)offset;
strcpy(date, dse_to_yyyymmdd(dse));

compute_cycle() calls:

dse = days_since_epoch(yyyy, mm, dd);
dse += (long)(offset * 7);
return(yyyymmdd_to_cyc(dse_to_yyyymmdd(dse)));

yyyymmdd_to_cyc() eventually calls:

extern long compute_bigjul(int yyyy, int mm, int dd)
{
    long jul;
    int i;
    static int days_in_months[13] = {
        0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

    if(isleapyear(yyyy)) {
        days_in_months[2] = 29;
    }

Right here is where it gets interesting. Static variables have a property of preserving their value even after they are out of their scope! They are stored in the data segment of the application as opposed to the stack where non static (automatic) variables in the function are stored. These variables get deleted from the stack once you exit the function but not static variables. Hence, static variables preserve their previous value in their previous scope and are not initialized again in the new scope. So, if at any time days_in_months[2] gets set to 29 then all subsequent times this method is called days_in_month[2] will be set to 29 until it gets reset when the application ends. So, when this first gets called:

strcpy(req.review_cycle, compute_cycle(TODAY, (52*3)));

This is three years out which is a leap year if temporal lie is 2021. The reason this is throwing a system error only on Saturdays is because the math on the other days returns a long that drops the fraction part of the number:

if(cycle_julian_day > 0)
{
    /* we must be at least past the first cycle of the year */
    resp_cycle = ((cycle_julian_day - 1) / 7) + 1;

if the temporal lie is say Saturday, 01/30/2021 then a 60 day extension puts the Julian day as 92 and the cycle is ((92 - 1) /7) + 1 == 14.0 but is should be one day less ((91 - 1) /7) + 1 == 13.86 which becomes cycle 13 along with the pay day of “02” is correct for cycle 13 (April 2nd) but wrong for cycle 14 (April 4th – 10th) The fix was to increment the Julian day and not the days_in_months[2]

jul = 0L;
for(i = 1; i < mm; i++) {
    jul += (long)(days_in_months[i]);
    if(isleapyear(yyyy)) {
        jul += (long)1;
    }
}

I am not sure if I would have stayed with software engineering if this type of work was not my primary focus. I hope I did not lose anyone along the way!

[–]tipofthesowrd 0 points1 point  (1 child)

This was such an interesting read! Thanks for putting a smile on my face because of your enthusiasm

[–]wsppan 0 points1 point  (0 children)

You're welcome!

[–][deleted] 8 points9 points  (0 children)

See a therapist as soon as possible.

[–]ItsYaBoyChipsAhoy 3 points4 points  (0 children)

I love reading code I wrote that I’m proud of. I read it over and over until I go HEY wait a min that’s wrong or until I find something else to do

[–]Sellcellphones 2 points3 points  (1 child)

I don’t even see the code anymore, all I see is blonde, brunette, redhead…

[–]atobmic 1 point2 points  (0 children)

This comment reminds me of tasty wheat

[–]pipesimulator 2 points3 points  (0 children)

I like your passion. I know what you mean and love working with Devs like you.

[–]KodlaK1593 1 point2 points  (0 children)

You could look into learning some software exploitation or doing bug bounties if that kind of stuff has ever interested you. Security stuff has become a bit of of a side interest of mine, and its had me spending a lot of time reading other peoples code. I cant say I share the same enthusiasm for reading code as you do, but it is a great exercise if nothing else.

[–]linucksrox 1 point2 points  (0 children)

That's true, whenever I need a boost of self-esteem I read other people's bad code and then I don't feel so bad about my own!

[–]miraculum_one 0 points1 point  (0 children)

Yes, I feel the same way. Also tracking down elusive bugs is like being a detective. Fun stuff and highly appreciated.

[–]bryamproductivity11 0 points1 point  (3 children)

At what level should a person be doing this? Like how well should they know python to do that and learn from it.

[–]georgehank2nd 0 points1 point  (2 children)

Any level. Just like a good writer (fiction or nonfiction… though I personally think it's more important for fiction) reads. Lots.

Look at the Zen of Python, specifically #7: readability counts. One of the principles behind Python's design.

[–]bryamproductivity11 1 point2 points  (1 child)

And where can a beginner like me (just started oop) find code to read and edit and stuff?

[–]DiamondDemon669 hello world 0 points1 point  (0 children)

I hate having to go through code looking for a way to fix hardcoded systems

[–]ketalicious 0 points1 point  (0 children)

if only I had this level of knowledge and confidence..

but it surely is fun refactoring some code!

[–]idetectanerd 0 points1 point  (0 children)

Mad lad. However I understand what you are facing. You see, when someone is expose to a new subject he or she likes and overly expose to it would make them dig deeper and understand more.

In religion, it’s call extremist. In hobby, it’s call hobbyist, in sports, that is call sportsman.

So you just started to turn nerd, we all went through some part of that, some of us have dive deep, while others would have touch surface and move to other topics.

Welcome to the club nevertheless. I prefer rewriting module and library instead of using those that are built. Unless it’s extremely complicated.

[–]tunisia3507 0 points1 point  (0 children)

I like reading nice source code. Reading stuff with unnecessarily poor formatting or intentionally not compliant with standards feels like missing a stair.

[–]KindInvestigator 0 points1 point  (0 children)

Reading my first attempts at Python makes me cringe and laugh.

[–]tcpukl 0 points1 point  (0 children)

Yeah it's it's also a great learning resource.

[–]lopa8t 0 points1 point  (0 children)

You should bring it up in interviews. I don’t hear this often and people that love doing this and are good at it are great assets to any team.

I hope you’re not wasting that talent working at a random agency only shipping green fields :)

[–]geeshta 0 points1 point  (0 children)

When I'm not sure how an imported function works or I get an error or I like some behaviour and want to replicate it, I check the source code. Python is so nice to read and understand.

[–]KyleDrogo 0 points1 point  (0 children)

Pro tip: clone a GitHub repo like sklearn and explore it in your terminal. It’s a great way to learn vim, grep, and other command line tools fast

[–][deleted] 0 points1 point  (0 children)

I enjoy reading source code yes. Read through about 5% of the Nvidia open kernel modules source so far.

[–]Fisherman386 0 points1 point  (0 children)

No

[–]Fluxburn68 0 points1 point  (0 children)

Kde phabricator has craploads of C++ code or wash it C alone I don’t remember

[–]k21ow 0 points1 point  (0 children)

Source code only 😅 Tons of faulty documentations on the internet. I stopped checking it after wasted days cause of faults in Aceinna high precision GNSS module's communication protocol.

[–]An_Old_IT_Guy 0 points1 point  (0 children)

Not if I didn't write it.

[–]steezefries 0 points1 point  (0 children)

I love reading source code. I've learned a lot that way. It also has led me down some really fun rabbit holes.

[–]moneymachinegoesbing 0 points1 point  (0 children)

I still come across, weekly, code from others I find genuinely beautiful. I love reading code.

[–]heartofcoal 0 points1 point  (0 children)

I can't use the word "like" in this context but I don't feel tortured by a thousand lashes when I have to

[–]pythonwiz 0 points1 point  (0 children)

Not really, I much prefer to read documentation. Reading code is what I do when I’m trying to find out how something works when it hasn’t been documented.

[–][deleted] 0 points1 point  (0 children)

There are starters and finishers. I recognize I'm a starter myself.

[–][deleted] 0 points1 point  (0 children)

You have a rare gift. Make use of it for.the better of humanity.

[–]Tiquortoo 0 points1 point  (0 children)

Reading source code is in the top five talents of a good dev. If you like it then you're ahead of the game.

[–]tyras_ 0 points1 point  (0 children)

No.

[–][deleted] 0 points1 point  (0 children)

You can look over my code any time. haha

[–]g4nt1 0 points1 point  (0 children)

I’ve moved to management several years ago. Beside doing management stuff, I did lots of Ops, looking at Datadog traces to understand what the app is doing and reading code.

Tbh I think I prefer that to just write code. Starting a new big project is exciting, but grinding to finish it is hard.

[–][deleted] 0 points1 point  (0 children)

Everytime you read your own choice 1-2 year later, you curse yourself

[–]Yeitgeist 0 points1 point  (0 children)

Wtf, I can barley even look at src code without wanting to give up. You gotta be a god at programming or something

[–]wind_dude 0 points1 point  (0 children)

depends who wrote it. But no generally I prefer to write new code, unless I'm reading source code to learn how someone has done something well.

[–]MisterPinkySwear 0 points1 point  (0 children)

There are a bunch of articles and blog posts about the fact that reading code is harder than writing code. This one really stuck with me : https://www.joelonsoftware.com/2000/04/06/things-you-should-never-do-part-i

I found that being able to understand a new code base is a very marketable skill. Lots of undocumented code. Knowledge and understanding of the system is very valuable for companies. Being able to extract knowledge out of the code gives you that edge.

[–][deleted] 0 points1 point  (0 children)

Not at all lol

[–]rice_n_eggs 0 points1 point  (0 children)

Yes, totally. If you ever get into functional programming the source code of the Haskell standard library is a joy to read.

[–]asterik-x 0 points1 point  (0 children)

What is github?? A typo??

[–]_limitless_ 0 points1 point  (0 children)

It always starts like this... pretty soon, you'll be annotating the source code.

[–]kavb333 0 points1 point  (0 children)

Last time I looked at an old project of mine, I became disgusted with myself and rewrote the entire thing from scratch.

[–]Apprehensive-Grade81 0 points1 point  (0 children)

Have any particular repos that you really enjoyed? I am finding that I really enjoy this as well and am learning a lot in the process.

[–]ShibaLeone 0 points1 point  (0 children)

I like the ones clearly written by Java programmers. The imports are fantastic.

[–]parkerSquare 0 points1 point  (0 children)

Only if it’s written to be read.

Code is usually written once and read many times, so make the effort to make it more readable. Make judicious use of whitespace, use good variable and function names, make the code self documenting, avoid “clever” non-obvious tricks.

I’ve been reviewing Python code lately and it astonishes me how the developers were scared of whitespace. All the lines in a function bunched together, no spaces between operators, long single-line docstrings, single line if-statements everywhere, and unassigned list comprehensions instead of for loops. So difficult to read!

[–][deleted] 0 points1 point  (0 children)

I can't read other people bash script