18 ms·
Tips for Reading Code (2014)
- microcolonel 10y agoThe new c2 browsing experience makes me physically angry. My skin temperature goes up, and my face makes an automatic scowl. I don't know how they turned the most basic HTML page into something that takes 30 seconds of spinner to load the same thing. I counted 20 seconds to load http://wiki.c2.com/?SoftwareMasterpiece http://wiki.c2.com/?SoftwareMasterpiece
- sgift 10y agoFor a moment I thought this was another of the "meh, it uses JS, I want to surf with my Lynx!!!!" rants but wow that page is really slow. I only counted 9 secs for the original page and 11 secs for your link, still ... for a bit of text? Not even an image? What are they doing? setTimeout before they show the HTML? Edit: opening the same link again is instant. Weird.
- jazoom 10y agoIronically, this page looks like it doesn't even need JS. It's comical how long it took to load.
- dkarapetyan 10y agoLoads instantly for me. Might have something to do with your network or service provider.
- Groxx 10y agoSpinner loads instantly for me. Everything else takes ~10 seconds. I'm on a 250 megabit connection. I haven't seen a page take this long to load in months. Once a particular page is visited, it does load quickly. But for new ones it's always the same experience. Unless they're buckling under load of some kind and this provides a fancy cache, it's pretty ridiculous. --- edit: decided to watch the network on a new page, http://wiki.c2.com/?LiterateProgramming http://wiki.c2.com/?LiterateProgramming for instance. Most everything downloaded in 10s of milliseconds, but the LiterateProgramming XHR request sat waiting for 6.5 seconds. Maybe they are struggling for some reason?
- dkarapetyan 10y agoYou're right. Took a while for me on that one but second time around loaded instantly.
- shakna 10y agoWard switched to using his project, Smallest Federated Wiki, which basically doesn't do a thing without JS. In fact, if I remember correctly, the slow load times are because it loads the entire wiki tree in the background to make linking easier for the engine... But a worse experience for the user.
- cryptarch 10y agoHi, I'm working on a static renderer for the c2 source json, AMA. I'll be hosting a mirror (I think the license allows for that but I'll have to check) and then integrating it with the one hosted on the original domain. I also kind of want yo set up some kind of edge caching static rendering with loose constraints on how up-to-date pages need to be (because the point is providing a read-only Lynx/CURL experience), but I think this might end up being too much work technically or politically.
- stewbrew 10y agoIt really seems they time jumped from 1995 to 2016 but made it even worse.
- thomasahle 10y agoI gave up after 60 seconds on LTE.
- crncosta 10y agoThe page is not loading... even the cached version from http://webcache.googleusercontent.com/ http://webcache.googleusercontent.com/ is not loading! I am really frustrated with the new c2.com pages :P
- NoGravitas 10y agoOn my browser (up-to-date Firefox with uBlock Origin), I can load pages from links from other sites (about 5 seconds to load SoftwareMasterpiece), but trying to follow links within the site gives me nothing but a spinner, no matter how long I wait. I don't feel this is what Wiki was meant to be.
- dang 10y agoThose pages are loading instantly for me at present, but even if they were slow, your comment is far too uncharitable to make for a good HN post. Physically angry? Come on—it's a single programmer's project from decades ago. He's probably tackling the problem of upgrading it by himself. The last thing he needs (or we, for that matter) is an angry chorus of internet entitlement. If you're hot under the collar, please cool down before commenting here.
- enibundo 10y agoI thought this was a fake article because of the spinning gif.
- minipci1321 10y agoNot sure in which context these tips were written. Having been through all of this, and frequently comparing my code-reading abilities with those of my junior colleagues (hey, I even managed to make it to a point where I essentially make my living from reading other's code), maybe a few more-realistic tips (and a shorter list): 1. actually read the bloody code until you understand well what it does. Don't stop before that, or it won't count. 2. read as many different projects as you can -- different languages, technologies, paradigms, coding styles. 3. Don't spend time on code-comprehension tools, it won't work (speaking from personal experience. The latest was with "Understand C++" from SciTools, a great tool BTW). Master grep. 4. Drawing flowcharts, running under debugger, etc -- all that would be great but nobody allots time for that anymore (neither yourself nor your managers). 5. Don't hope for usable doc for external libraries. Exceptions exist, but it won't be a rule.
- drvdevd 10y agoI'm interested in your take on #4: why wouldn't you allot time to run code in a debugger? Personally I would like all my code to be written against a debugger and with test suites allowing me to step through it. I've been chided for not doing this in the past (I just read the code usually) and would actually agree with that person in retrospect.
- minipci1321 10y agoBecause, in many cases, getting it into the debugger has a huge fixed cost -- imagine your code should run on an embedded platform: find the right board, get it connected it into the infrastructure (getting right signals in and out), get the debug build of the image etc -- you see the point. More generally, studying the code under debugger means you need particular input data -- the one suitable for the part of the code you study. That can take time to get.
- drvdevd 10y agoAh I see. This is actually a very good point. One team I was on would constantly get hung up on this sort of issue. I believe a more general ability to just run a code review and study session would've saved lots of time.
- bschwindHN 10y agoI'm impressed at how the author of this website managed to take plain text content and literally make it slower to load than a 1080p 60FPS video. You really have to try to do something like that, bravo. I normally try to avoid commenting on stuff like this, but that's pretty much all I can say considering it took me actual minutes to load (from Japan). In lieu of the article's content, I'll spare readers the trouble and provide something better (and faster to load!): -Read lots of code There you go, you're well on your way to becoming a code-reading master!
- riffraff 10y agothe sad thing is The Wiki (this is the original one) used to be a simple text thingy, with instant load. I have no idea what happened, but I presume the new version just doesn't handle the HN effect well enough.
- shakna 10y ago> doesn't handle the HN effect well enough. Not at all. c2 now uses Federated Wiki, which for engineering reasons (I think), loads the entire wiki tree as a key part of the json that serves as the content for every page.
- jeremiep 10y agoLooks like new maintainers took control and decided the old codebase wasn't to their liking. Thing is, these changes come with loads of tradeoffs the newcomers usually are completely oblivious to and the new product is of lesser quality than the original, but they feel good about themselves now :)
- rejschaap 10y agoThe irony is that they also host an article about Over Engineering: http://wiki.c2.com/?OverEngineering http://wiki.c2.com/?OverEngineering I didn't wait for it to finish loading and actually read it though.
- bschwindHN 10y ago
- henrik_w 10y agoOn the subject of reading code, I thought this was a good article (basically, you can't read it like you read literature): http://www.gigamonkeys.com/code-reading/ http://www.gigamonkeys.com/code-reading/
- morbidhawk 10y agoI agree with the opinion that reading code is not like reading literature but I do find it useful to compare the 2 things. The book "The Psychology of Computer Programming" talks about how most good novelists actually read lots of novels while amateur novelists naively think they have enough inspiration without reading ideas written by others. And the same thing happens with programming, most really good programmers have read a lot of code in addition to writing code. I have found this to be the case for myself, the more code I read the more creative inspiration I have available when writing my own code.
- ensiferum 10y agoThe page doesn't load.
- SFJulie 10y agoI don't need to read the article to know in this case the best reading of their code for rendering plain text in HTML should be to have absolutely no code to read instead of any code they can have that might actually be brilliant. Lol.
- falava 10y agoWayback Machine archived copy: https://web.archive.org/web/20160709111543/http://c2.com/cgi/wiki?TipsForReadingCode https://web.archive.org/web/20160709111543/http://c2.com/cgi... The Wiki was remodeled recently: http://c2.com/wiki/closed.html?WelcomeVisitors http://c2.com/wiki/closed.html?WelcomeVisitors https://news.ycombinator.com/item?id=12715560 https://news.ycombinator.com/item?id=12715560
- napsterbr 10y ago"Remodeled" is an understatement. It was destroyed. Luckily we have the archive...
- falava 10y agoI think he needs some time to improve it. More explanations here: https://github.com/WardCunningham/remodeling https://github.com/WardCunningham/remodeling
- dom0 10y agoGiven the state and performance of the site the tag line "The original wiki rewritten as a single page application" is practically satire: It's a SPA now, of course performance was degraded by a factor 100+.
- halayli 10y agoThis article is basically saying use your brain.
- hashin 10y agoI am being curious here. Do anyone really print out the code so that the comprehension really increases? I can imagine all the benefits, but can someone give any first hand experience were this has actually helped them? Also, how better is it than the code map on Sublime Text or syntax colouring we have anyway?
- JoachimSchipper 10y agoI have done, for code that fits on 2-8 pages (basically a demonstration program). I used vim's ":hardcopy", so the printed code was already syntax-highlighted in the way I was used to. This did let me work on the code when away from my workstation, but I didn't otherwise find it very helpful. If you have lots of code, some form of code navigation is very helpful.
- tyingq 10y agoVim's :TOhtml can be handy if you need to see syntax highlighed source on a tablet or some other device that doesn't run vim.
- ekzy 10y agoI feel like most of the code I read at work should never be printed. Even if it is production code, it would make it very "real" and... embarrassing. Haha.
- chandler 10y agoSure, at a prior job we passed out hardcopies of a side-by-side diff before scheduling a peer review (including only the affected methods/functions). No syntax highlighting, but it was quite useful. Off the top of my head, the two main benefits are: 1) You free up your computer for the review 2) You get a different form factor to interact with. Regarding the first benefit, having a printout means you can use your computer to run the application, look up docs, and even (ironically) navigate the code in your IDE. As for the second, having a printout means you can spread out the pages to compare different bits of code, draw diagrams, connect arrows, circle bits of code for notes, etc. It was pretty useful.
- 10y ago
- spion 10y agoOne of the tricks I like to use to get up to speed quickly is making a hyperlinked dictionary / glossary (a wiki or a github page with anchor links work nicely). The biggest roadblock to understanding the code you read is not knowing the exact meaning of the terminology chosen by the original developers. Building the dictionary helps internalise it quickly, leading to much less confusion when reading. Terms present in the UI (or API docs) of the segment you wish to understand are a good starting point.
- mysterydip 10y agoYou might be interested in this single page app I made to handle similar use cases of automatically hyperlinking documentation: https://github.com/elusivegames/termify https://github.com/elusivegames/termify Sorry there's no live demo, but the readme should explain it easily enough.
- techbio 10y agoThanks for the link. I enjoyed the breakdown, and can see how it works, but wow that code would be easier to read split up over three php files and a template.
- mysterydip 10y agoDefinitely. The goal was something I could just drop in as one file to a folder of docs, hence the ugliness :)
- morbidhawk 10y agoI do something similar when reading terminology used in code but for a different reason. I like to steal the good name ideas and I keep them in a mind map I can later look at for naming inspiration.
- deleted 10y ago[deleted]
- pasbesoin 10y agoSo sad to see/experience c2 as a Javascript-dependent experience.
- dranka 10y agoI love the C2 wiki! Such a nice collection of discussions and ideas. When I encounter some predicament or some ideas pop into my mind about a particular subject I always check C2 to see what they have on it. Reading about reading code takes me back to when I came from the university as a young naive programmer to my first job at a multinational software company. The code base we were working in was huge and had been worked on by hundreds and hundreds of programmers. I was only used to my own smallish programs, the sanitized examples code examples presented in assignments or tutorials or at worst a code base hacked together by a handful of students over a few weeks. After the grace period at my new work I was given the assignment to add some new logic to an old piece of code. The code in question was a couple of modules adding up to a few thousand lines of code. The logic was heavy and tightly interconnected. I remember that it used exceptions as part of the control flow, I knew enough to know this was bad practice and scuffed a bit at it but in the same time I could see it was just not abuse out of ignorance, the control flow was kind of ingenious in its own twisted way. I tried to come to grips with it for a few days but it wouldn’t really fit in my head the whole thing. I was lucky to have a mentor of great quality, something as rare as and well accomplished academic turned to a pragmatic industrial programmer. I came to him and asked what I should do with this code. He said the code is mostly untouched since it was first developed some years ago, it has worked well and had only needed small touchups to fix some corner cases. He admitted himself had never fully took the effort to understand the code and he acknowledged that this assignment was given to me as a challenge of my abilities. He told me I should go and talk with the original author and so I did. The originator of the code was an old programmer and he light up when I told him I was investigating his code. I could tell he was a bit sentimental about the work he had put in. He told me this piece of code did not come easily, it was something he had been spending months on to get right. The problem was a complicated and the code had the right to show some complexity too. We talked a bit about the high level problem and then I asked him if he could give me an overview of the code but to my surprise he said no. The reason for this apart from the time passed was that it was written in a state of flow when he had been engulfed in the problem. He needed the right context to bring it back and the context was not there anymore. I can respect this. It is one of the best programming experiences, when you in the flow and are programming at a complexity level that are just at the limit of your ability. When you finally get it all in place the details drain away quickly and there is a satisfying emptiness when the complicity dislodges itself from your brain. He couldn’t give me any clarity in the problem at hand but he told me an anecdote about how hot code swapping in Erlang was a nice feature but he remembers how they used to do that in assembler way before Erlang was around. The way he talked about this cemented my view of him as a very clever programmer. I can always respect cleverness but it is not always what you want when you are handed down some arcane piece of code... I went back to my mentor and explained that I was not much wiser about the code. He sighed a bit and told me “just stare at the code until it makes sense”. At the time I was not so happy about this answer. I interpreted it as a kind of brush off response to my predicament. Later I realized that this advice embodied a kind of zen like wisdom. I went back to the code and did as I was told. For a couple of weeks I spent all my time with the code. I kind of blankly accepted the complexity. I scrolled the code up and down, jumping back and forth trying to puzzle the logic in my head. Advances was made in small increments as the logic slowly structured itself in my mind. Eventually I got in the flow and I made leaps of progress. I could see the point of the clever tricks my predecessor had left for me. I could tell he had enjoyed writing this code and it gave me satisfaction to unravel his thinking. This kind of programmer type is something I encounter infrequently; they should be admired for the accomplishments they can achieve when they are in the right state but also a bit resented for their masturbatory cleverness. Anyway, finally it clicked, I was on the bus or in the shower, I had finally accumulated all the information in my head and my background thought process had been churning away on it long enough. The code had become lucid. I did the code change and left some comments to try to help future challengers. [1] Since that experience I have deciphered a vast amount of legacy code. There is always the satisfaction when you get the ‘click’ and reach full understanding. Unfortunately, I have never had a repeat of the exact same fulfilling experience as above. Sadly, most code is not an expedition into a ingenuous madman’s legacy, it is more like shifting through a heap of old accumulated cruft and accidental complexity. Complexity not in any way tied to the complexity of the problem domain, rather built up from a horde of opportunistic coding by generic programmers trying to solve their bug report before the go to lunch, no passion, no pride for the craft, no regard for the next programmer that comes along and trying to untangle the mess they build upon. No hard feeling, they do not do it to spite you, they have no sadistic inclinations, they just want to get their work done and move on. They do not see the bigger picture of technical debt and they do not share your ideals of sound programming practices. To give them a passionate speech about these topics will just leave you looking at a blank stare. I have two pieces of advice when it comes to reading code. They seem a bit contradictory but they have been proven useful. The first advice is that when you approach an unfamiliar codebase do so without judgment. It is easy to get offended by bad code left around. It is like seeing someone left a mess in a public space, the lack of decency and respect annoy you, you might try to picture the degenerate who did this in your mind and you get riled up. Instead see just see it as it is. The code might be convoluted because it is working around a problem that is not existing anymore. The abstraction might have made perfect sense until some ignorant programmer came along years later and did a quick hack that left the original abstraction looking half-assed. Or what is usually the case: a small fix upon a small fix upon a small fix. Each one making sense from an opportunistic perspective but together they make up monstrous complexity. Just focus on the logic and accept that superfluous complexity is unavoidable in a legacy system. My second advice is that if you are coming back repeatedly to the same legacy code base then get to know the people who wrote it. If you see similar horrendous patterns or you find nuggets of slick quality code take some time and do some archaeologic investigation in your version control system to find out who the original author was. Later when you encounter more legacy code you can find the author and give the code a baseline of trust depending on what you have seen from them so far. This can help with the tricky code, if it was written by someone that warrants respect try to approach it again and understand why the trick is there, if a more clouded mind wrote it you can in most cases just assume the “trick” is ignorance and save you some time by just assuming you know the problem better than they do since that is most likely the case. My advice when it comes to writing code is no surprise. Simplicity is king. Simplicity of implementation trumps all until proven the other is needed beyond doubt. Yes, simplicity of implementation trumps performance. Simplicity trumps abstraction and reuse. The thing is true simplicity comes from fully understanding the problem at hand and usually does not appear until after a few iterations of possible solutions. Cleverness is masturbation, simplicity is the hard part. Push complexity away from the core. The Berkley approach is the way to go [2]. If you solve the complexity of your corner cases in the core of your program it might make the outer layers look slick but it is a false sense of simplicity. With a complicated core the complexity will seep out through all the outer layers even if they do or do not take advantage of all the features of the core. Yes, simplicity even trumps readability in this sense. [1] I like to think I did leave some comments at least… comments are not easy to write when the problem is lucid in mind. If you have the discipline, write down the questions you encounter during your investigation, when you reach enlightenment go back and answer them and add this as comments in the code. [2] https://web.stanford.edu/class/cs240/old/sp2014/readings/worse-is-better.html https://web.stanford.edu/class/cs240/old/sp2014/readings/wor... TLDR; Bla bla, keep it simple stupid