11 ms·
Some things I've learnt about programming
- asto 14y agoA large part of the post can be rewritten as "don't be lazy". 1. Don't be lazy and just do something that works without taking the time to learn why it works. 2. Don't be lazy and just stop when you have something that works. Go through the code again and see if you can make it better. 4. If you find yourself writing the same thing twice, don't be lazy and carry on, put the code in a single place and call it from where you need it. Or at least that's how I see it. I do all of the things I shouldn't do, largely because doing things the wrong way is so much easier! Edit: Rather than rewritten, I meant "falls under the general category of". The article was great!
- HarrietJones 14y agoThough point 4 should be rewritten as "If you find yourself writing the same thing twice, DO be lazy and carry on, put the code in a single place and call it from where you need it."
- asto 14y agoThere is usually an upfront cost of time and/or effort. The choice comes down to copy-pasting what you need everywhere or put effort into making a generic function that you can put in one place. If you are working against an impossible deadline, option 1 seems very enticing. In the long run, you are right, the effort is much less to maintain a well thought out codebase.
- HarrietJones 14y agoI don't really understand why people think that copy pasting takes less time than making a new function. Your function may need some refactoring to make sense, but just cutting that block of text out and then calling it in a function can usually be done in seconds. Copy Pasting: Copy the code. Paste in new place. Change variable names to fit in new place. New Function Cut the code. Place inside function declaration. create call to function in old and new places.
- Wilya 14y agoI doesn't usually happen like that for me. More likely, I stumble on a point where I have to reuse part of an old code, but with slightly different goals, or with some steps added/removed in the middle. Copy-pasting and just dropping the useless parts is easier than making a new function, because you have to think about how to make the new function apply to both cases (the easiest way is "well, I'll just put a switch in the parameters and if statements", but it doesn't really lead to better code).
- TeMPOraL 14y ago> New Function Cut the code. Place inside function declaration. create call to function in old and new places. And also: decouple code from it's context. Rename variables. Rename the function. Possibly add a new module for it. Moving repeated code to a function rarely just about relocating a piece of code. Quite often (especially if you have cross-file code repetition) you identify abstraction, which is a Serious Business (TM), requiring at least to think about where to introduce it, how to fit it into existing mental model of the program, and while we're at it, how to make it useful for code that you'll be writing next week, because it would be a waste not to do that now. It's not that difficult and it's quite rewarding, but it also takes significantly more time than just copy-pasting and carrying on.
- vlisivka 14y agoGood programmers are lazy.
- lifeisstillgood 14y agoThe hardest obstacle to not being "lazy" is deadlines Is it lazy to accept something that works, and meet the deadline? Is it OK as a craftsman to miss your deadlines, because you want to know why something works? These decisions are like a craft in themselves - sometimes we can just trust a library works. Sometimes, we need to understand more. And sometimes we need to re-negotiate deadlines. And sometimes we lose clients.
- gizzlon 14y ago1) and 2) seems to define lazy as "avoiding work now". Can't being lazy also involve avoiding work later? (see also: The first great virtue of a programmer)
- saraid216 14y agoWriting it like that would have been lazy, though.
- MindTwister 14y ago9. You count from 0
- pjmlp 14y agoQuite interesting
- mustafa0x 14y ago> Programming is much closer to a craft than a science or engineering discipline. It's a combination of skill and experience expressed through tools You seem be implying that the latter statement doesn't apply to the disciplines of science and engineering. "skill and experience expressed through tools" is highly important in both watchmaking and bridge building. I would advise anyone who says elsewise to reconsider. I understand your point, but why create a hugely false dichotomy between a craft discipline and the science and engineering disciplines? --- I strongly concur with points 2 and 6.
- HarrietJones 14y agoBridge Building vs Software Engineering http://www.codinghorror.com/blog/2005/05/bridges-software-engineering-and-god.html http://www.codinghorror.com/blog/2005/05/bridges-software-en... There is something different between software and other engineering methods. I'm not sure he expresses it properly, but it's definitely there.
- ObnoxiousJul 14y agoThe core of programming is creativity. No developers develop the same way, and even though there are some obvious ways, there seldom exists an absolute all cases best way. It is the thesis of the mythical month (F. Brooks) and I do like this theory since its corollary the "no silver bullets syndrome" is quite accurate. The essence of programming is creativity, thus no tools can improve software productivity in its essence. The problem with school, is studious dull boys with no imagination thinks they worth something in programming by incanting mantras of pseudo tech gibbish. They have a 90K$ loan, no gift, and they pollute the eco system because else, they become hobos. At least, most of them are hired as java, C++ or PHP developpers where they fit best.
- TeMPOraL 14y ago> The essence of programming is creativity, thus no tools can improve software productivity in its essence. Completely not true. Creativity is a fragile thing and anything that stands between you and expressing your thougths may break the creative process altogether. Good tools can also enhance the process[1], for example by allowing to see you the thing you're working on in realtime (see e.g. Bret Victor's "Inventing on Principle"). Also, software productivity is a function of both creativity AND being able to turn the idea into reality efficiently. Good tools do a great job on the second part. [1] I do have the feeling though that most of the creativity still happens on paper and/or whiteboard, not inside computer programs.
- raheemm 14y ago#7. Learn the layers. Is this even possible anymore? Seems like with apis, frameworks, there are layers upon layers just within code. Then you have the OS, the hardware, the network layer (whoa! 7 layers right there!)...
- gaius 14y agoWell, you are referring to Wheeler's Corollary to Lampson's Law, but it doesn't have to be like that. It's actually less work to just write the program you want in the base language, than trying to shoehorn it into a framework, or wrestle the framework around your code. I've recently rediscovered the joy of writing GEM apps in m68k ASM...
- someone13 14y agoFor people's information: "All problems in computer science can be solved by another level of indirection." - Butler Lampson And, from what limited sources I could find: "[above] ... Except for the problem of too many layers of indirection" - David Wheeler
- gaius 14y agoIt's a shame Wheeler isn't better known. He invented the subroutine back in the '50s. Calling a subroutine used to be called a "Wheeler jump" (relevant to #4 in TFA).
- pnathan 14y agoHis group with the EDSAC did some absolutely tremendous and groundbreaking work. Wilkes, Wheeler, and Gill wrote a book, "Preparation of programs for an electronic digital computer", that pretty much describes the core software engineering precepts we use today - in 1951. Gill wrote "The Diagnosis of Mistakes in Programmes on the EDSAC", which gives a good snip of what they were doing at the time. I think that it can be obtained for free online.
- 14y ago
- HarrietJones 14y agoGood article, though I don't agree with it all. It is harder to grow software than it is to initially build it. Preconceptions bite you on the ass, data structures don't allow for new features, side effects multiply. You don't need to learn the layers. In fact, if you're learning all the layers, you're probably an innefective coder. This is not to say that you shouldn't investigate the layers or have a poke around the layers. But, software's about reuse and reuse is about reusing other people's work via known interfaces without worrying overmuch about what goes on underneath the hood. I'm actually more of a debugger than a profiler, and as much as I'd like to believe that my way is as valid as his, I suspect that he's probably right on this and I'm probably wrong.
- kenbot 14y agoI find preconceptions bite ass way harder when you try to design big things all at once. If you grow them you can pivot and refactor more easily while they're small.
- sp332 14y agoGrowing sucks with large codebases. I worked with about a dozen people on a huge app, that would have been much easier to make if we had had a better idea of what we were building when we started instead of making stuff up as we went along.
- StavrosK 14y agoI never noticed until now, but I never use a debugger either. An employer urged me to start using one while I was working on their code, but it's just not natural for me. If I need to debug a crash, I just print the 1-2 variables of the state involved, see what's wrong, and fix it. Is there anyone who uses a debugger for more than inspecting state? EDIT: I guess lower level languages and more involved applications use debuggers much more extensively.
- borplk 14y agoI don't know about the nature of the problem you were working on but in my experience there are some cases where the problem is far too complicated to be fixed with debug print statements. I couldn't live without my debugger.
- jc79 14y agoFor example, when debugging operating system code running before printf (or printk) is available. I barely used debuggers on high level code, but in low level its an all different story.
- HarrietJones 14y agoI use a debugger. If you've got some input that gets transformed into unexpected output, it's great to follow the code along. Of course, placing debug output works too, be the debugger is usually faster in this situation. It's good for white box testing. It's also good for compiled languages. You know something is wrong, but you don't know why it's wrong.
- conductor 14y agoI can say the same for high level code, but I use debuggers when the source code is not available or I'm coding a device driver or something in assembly.
- jgrahamc 14y agoYes, I've done that sort of programming (embedded stuff) and debuggers and other exotic tools are very useful.
- defdac 14y ago"You have to ask yourself: "Do I understand why my program is doing X?"" "I almost never use a debugger." "And if your program is so complex that you need a debugger [simplify]" To me, being afraid of a debugger is like being afraid of actually knowing exactly what is going on - being lazy and just read logs and guessing what might have gone wrong, instead of letting the debugger scream in your face all the idiotic mistakes you have made. I would argue that using the debugger is being lazy in an intelligent way, instead of spending hours reading endless logs trying to puzzle together logic the debugger can show you directly.
- maigret 14y agoUsing logs wins against debuggers a few cases though: - Your program runs on a client system and logs help you understand or reproduce the system without having to do a remote session (which might be impossible on some firewalled envs). - Your code crashes without stack trace and you want to understand where to begin the search. Agree with your overall statement though.
- ObnoxiousJul 14y agologs beats debugers in asynchronuous mutiple workers/consumers context.
- einhverfr 14y agoI avoid debuggers too, not because I am afraid of them but because I find logging etc works better. However, the fact is that I wouldn't recommend being afraid of them. It is extremely helpful to know how to use a debugger. In fact I would say if you don't know how to use a debugger, you really have no reason to avoid one. The point of knowing the debugger in part, esp. with more dynamic languages, is to do better debugging in your head.
- AngryParsley 14y agoAgreed. Logs are good. They're effective. They're easy to use. You can filter, search, and aggregate them. Without printf()s and log statements, my world would be chaos and darkness. But a debugger gives you superpowers: break blah.c:180 run thread apply all bt You can stop time. You can get backtraces. You can see and modify every aspect of the process. Many debuggers even let you attach to a running process and do these things. Changing log statements means stopping and re-running your program. If startup time is large, this can hurt productivity. It's rare, but logs can mislead. With async stuff, logs don't always get printed out in the right order (hello, Node.js and Twisted). A debugger is crucial for figuring out that sort of unintuitive behavior.
- belial 14y ago> "Programming is much closer to a craft than a science or engineering discipline. It's a combination of skill and experience expressed through tools" I think the authors definition of Science, Engineering, and Programming is vague. What is Science? What is Engineering? And what is Programming? I don't know what kind of programming he is talking about, but we all know that designing Programming Languages is a science, and also programming compilers and interpreters... Well, how about Artificial Intelligence, isn't it also a Science? And they all imply heavy programming and optimization.
- thebigfatpanda 14y agodebuggers are time savers. one must write a unit test for a bug found before going to fix it. thus you don't need a debugger next time if things regress.
- wissler 14y agoTaking personal pride in not using a debugger is a bad idea. Sometimes it's the right tool for the job, and if your picking it up makes you feel dirty, you're only handicapping yourself.
- wpietri 14y agoBut sometimes it isn't the right tool for the job. In particular, it enables you to deal with a confusing, poorly factored code base. The right tools there are the ones that help you clean the mess up, rather than making the mess more tolerable.
- wissler 14y agoIf you have a rational argument that proves that debuggers are only useful on poorly designed code then I would certainly be interested in hearing it. It is true that the worse the code the more frequently you need to debug, but that's a quite different proposition.
- wpietri 14y agoI'm not saying they're only useful there. But we both agree that they're very helpful in understanding a bad codebase. That's because they make bad code easier to handle. Which for some people removes the incentive to clean it up. Basically, they use debuggers as crutches.
- bambax 14y agoA lot of those things apply to many human activities. I don't build bridges but I would be very surprised if an architect described his work as "pure science and no craft at all" (how would it be possible, then, to build beautiful / ugly bridges?) I do a little woodworking and have many tools; friends sometimes look at my shop and ask if I really need all that -- yes, I do. In the course of a project you get to use many different tools. You can get around to missing one but it takes exponentially longer to work with not the exact tool. (Same thing with photography). I'm learning to fly, and the most important word regarding human factors is "honesty". The way to fly is not to avoid mistakes, it's to detect them and minimize the consequences; if you feel you can do no wrong you'll eventually kill yourself.
- deleted 14y ago[deleted]
- einhverfr 14y agoThese are all golden lessons that people who think about writing code generally learn. One thing I would add though is that there are many times when there is time pressure and a kludge works. The right thing to do here is to document that it is a kludge so that if/when it bites you later you have a comment that attracts your attention to it. "I don't understand why this fixes the problem of X but this seems to work" is a perfectly good comment. It's great to admit in your comments what you don't know. (That's why questions relating to commenting are great interview questions IMO.) Finally, I think it's important in the process of simplification to periodically revisit and refactor old code to ensure it is consistent with the rest of the project. This should be an ongoing gradual task. Anyway, great article.
- AngryParsley 14y agoProgramming may be a craft, but researchers have published tons of studies about this craft. Many of these studies contradict anecdotal evidence. For example, copying code isn't as bad as you might think: http://www.neverworkintheory.org/?p=102 http://www.neverworkintheory.org/?p=102 Another example is TDD. People espouse the benefits, then some study comes along (http://www.neverworkintheory.org/?p=139 http://www.neverworkintheory.org/?p=139) saying the benefits are largely illusory and that code reviews are more effective. Instead of listening to the experts at programming, listen to the experts on programming. Read some studies about the effectiveness of various tools and methods. Try new things. Programming is a craft, and like many crafts it contains significant amounts of dogma passed from teacher to apprentice.
- wpietri 14y agoI agree one should try new things and relentlessly test one's own dogmas. But regarding TDD, one study of "junior and senior computer science students" (described in an article I have to pay $19 to read) doesn't make somebody an expert on anything relating to how experienced professional programmers handle code bases that last years to decades.
- AYBABTME 14y agoYou can't just say "This is bullshit 'cuz they're two students who don't know crap about the real world". Any John Doe can come and affirm whatever he wants on any matter, and as long as he has proper evidence to support his claims, you should not discard what he comse up with based only on the fact that he's a nobody, a student, the President or Donald Knuth. The only thing that matters is the evidence John Doe brings. But further than that, truth is they are not the first one to come up with those results. Following is a blatant selection of near verbatim quotes from Code Complete (the author makes a great job covering the subject). "Microsoft's applications division has found that it takes three hours to find and fix a defect by using code inspection, a one-step technique, and 12 hours to find and fix a defect by using testing, a two-step technique (Moore 1992)." "Collofello and Woodfield reported on a 700,000-line program built by over 400 developers (1989). They found that code reviews were several times as cost-effective as testin - a 1.38 return on investment vs. 0.17." "[...]the Software Engineering Laboratory found that code reading detected about 80 percent more faults per hour than testing (Basili and Selby 1987). " "A later study at IBM found that only 3.5 staff hours were needed to find each error when using code inspections, whereas 15-25 hours were needed to find each error through testing (Kaplan 1995)." Table 20-2. Defect-Detection Rates Removal Step Lowest Rate Modal Rate Highest Rate Informal design reviews 25% 35% 40% Formal design inspections 45% 55% 65% Informal code reviews 20% 25% 35% Formal code inspections 45% 60% 70% Modeling or prototyping 35% 65% 80% Personal desk-checking of code 20% 40% 60% Unit test 15% 30% 50% New function (component) test 20% 30% 35% Integration test 25% 35% 40% Regression test 15% 25% 30% System test 25% 40% 55% Low-volume beta test (<10 sites) 25% 35% 40% High-volume beta test (>1,000 sites) 60% 75% 85% Source: Adapted from Programming Productivity (Jones 1986a), "Software Defect-Removal Efficiency" (Jones 1996), and "What We Have Learned About Fighting Defects" (Shull et al. 2002). Really, I'm not saying that TDD is bad, not at all. What I'm saying is that this sentence : "one study of "junior and senior computer science students" (described in an article I have to pay $19 to read) doesn't make somebody an expert on anything" ... is really wrong. [Edit] Formatting nightmare
- bromagosa 14y agoThe debugger part doesn't look generic enough to me. As a Smalltalk programmer, I can only say usage of debuggers depends _a lot_ on which language you code in. In Smalltalk, you practically live inside the debugger. Also, if you are an ASM programmer, the debugger is indispensable.
- vbtemp 14y ago> 0. Programming is a craft not science or engineering Unless, of course, you are software engineer-ing. Flight guidance-and-control systems, among many other things, are are precisely engineered software systems. In a world of web-apps and mobile-apps, people tend to forget this kind of software exists. Sure, working on your web app, writing some JQuery widgets, or coding up some python scripts is a craft.
- MichaelGG 14y agoCan you explain more about how high quality engineering software projects do not rely on programmer's experience and intuition, but instead follow formulaic rules and achieve excellent? From my limited reading, it seems that most "mission critical" software is achieved by applying a lot of resources, especially testing, to the project. Not to mention having a very well-defined (and relatively unchanging?) problem space. Surely, if there were engineering principles that enabled folks to reliably create high quality software, we wouldn't see the horrible failure rates across all sorts of software projects.
- roma1n 14y agoFrom what I saw, most mission critical software is done in such a way that a single programmer can understand the whole system starting from microcode instructions up to the control parts. Lots of static analysis also helps, as well as systematic human review of any code, test, or test result. The best example I witnessed was an engine control system (FADEC software) written in a dialect of ADA called Spark. The code was so clear that it was self-explaining, and a requirements database could explain any given statement in the program. Spark has some nice properties (e.g. no recursion to make it possible to check stack depth limits statically...) So while more manpower is an important element, it is not the only one. Simplifying the problem space to an extreme is also essential.
- pnathan 14y agoI work in the area of critical infrastructure software. There's no special software engineering sauce that gets used. But there is a dedicated commitment to careful code review, and exhaustive testing, as well as a very rigorous process for defect handling. We have a dedication from the top down to ensure our stuff doesn't break our customers. We don't have a "software pirate ninja rock star" culture, we have a culture of careful work. Doesn't mean null pointers don't get accessed, or that weird code doesn't show up. It just means that hey, we worked real hard to ensure that these issues are minimized. Quality is a journey, and every day we have to work on it. This is a great article about how this sort of stuff is done and the kind of culture that you want to cultivate. It's a bit dated, but still solid. http://www.fastcompany.com/magazine/06/writestuff.html http://www.fastcompany.com/magazine/06/writestuff.html
- Tashtego 14y ago"I'm not young enough to know everything" Having recently started mentoring/managing the first really junior engineer on our team (self-taught, <1 year programming experience), boy does this ring true. Luckily I'm of the temperament to find the "advanced beginner" stage of learning more funny than annoying. I think it's possible to understand as little about your code when using loggers as when using debuggers, so I have a hard time agreeing with him there. I think his general point about having tools and knowing when to use them applies just as much to that as it does to language, so he contradicts himself.
- zethraeus 14y agoIt's nice to see number 5. >Be promiscuous with languages >I hate language wars. ... you're arguing about the wrong thing. It's easy to take this for granted, but it's a concept that is very important to stress to new coders. If you spend too much time focusing on one language you run the risk of the form becoming the logic . This is a dangerous place where your work can be better analogized to muscle memory than to logical thought. At least in a college environment, I think this lack of plasticity causes discomfort with different representations of similar logic - and so flame wars abound.
- alainbryden 14y agoI like that you starting numbering at 0 - very apropos.
- cryptide 14y ago>>a printf that's inserted that causes a program to stop crashing. Huh?
- salgernon 14y agoprintf can/does have side effects. Perhaps one of the parameters to the call is obscured by a macro, which has some other effect. Or perhaps stdout was redirected and the printf was required to prevent line buffering from hanging a downstream consumer. The point is, if it seems very unlikely that such and such could cause a problem, it probably isnt the true cause. (Printf itself isn't the fix, it is just involved in the fix)
- cryptide 14y agointeresting, thanks for the explanation.
- piotrSikora 14y agoprintf() introduces delay that is often enough to hide / "fix" race conditions.
- 10098 14y ago> I almost never use a debugger. I make sure my programs produce log output and I make sure to know what my programs do. I used to do precisely that. Sprinkle code with log messages, recompile and run. When I finally learned how to use gdb, my debugging productivity increased tenfold. I mean, just the ability to stop your program at any given point gives you an enormous advantage. You can not only examine the local state of your program, but also you can see how the state of systems outside of your program (e.g. database) changes, and all of this without polluting the code with tons of useless debug messages. Often when I had new ideas during bug hunts, to test my hypothesis without a debugger, I had to go back and add new logs, the recompile, then run (and make sure it reaches the same state as before!) - lots of wasted time. With a decent debugger it's as easy as typing an expression. And I don't think debuggers lead to lazy thinking. The process of finding the problem is the same whatever method you use - you analyze the code, have an idea about what could be wrong, change one thing, then see what happens. Debuggers just make it easier.
- plg 14y agoIs "learnt" a real word?
- tymekpavel 14y agoYes. It is used instead of "learned" in nearly every english-speaking country outside the U.S.