Slashdot Mirror


Code Reading: The Open Source Perspective

nazarijo writes "You can usually tell someone who's been writing a lot of code by how they write code. That may sound like a tautology, but it's got a deeper meaning than that. What editor they use, what idioms they use to avoid common pitfalls, and what organization patterns they employ all tell you what kind of programmer you're meeting. When you first start writing code, so many things are inconsistent and just plain wrong that it's almost embarrassing. I know that when I look over older code that I've written I feel sheepish about it. But how do you grow as a programmer, and what really makes a good programmer beyond language familiarity?" Read on for Nazario's review of Code Reading: The Open Source Perspective, a book which attempts to instill deeper knowledge about programming than just "knowing how." Code Reading: The Open Source Perspective author Diomidis Spinellis pages 499 publisher Addison-Weslet Longman rating 7 reviewer Jose Nazario ISBN 0201799405 summary A tour of large-scale development projects from code to organization

A few books are tackling this subject, including Coder to Developer and Programming Language Pragmatics. These books don't teach you much about a particular language in the way that an introductory text would. Instead, you grow as a skilled developer by studying them and learning from them. That's one of the key things that people are talking about lately, that to be a strong developer requires more than a working knowledge of a language. It requires a familiarity with the strengths, weaknesses, and core features of a language and the base libraries to be efficient.

Code Reading: The Open Source Perspective is one of these books in this small but growing library. In it, Diomidis Spinellis takes you through a large body of code and focuses on several languages, techniques, and facets of development that differentiate strong developers from weak ones. What I like about this book is how much it covers, how practical the information is, and how much Spinellis teaches you. You wont learn a language, which is the complaint of some people who read this book, but if you know one or two you'll be a better programmer.

Perhaps one of the most telling things about the book is that it draws heavily from NetBSD source code, and features over 600 examples to make the point. Examples are often annotated using NetBSD as a reference. This makes sense, because NetBSD is a large project that's relatively stable and mature. Everything from how to define a C structure consistently and sanely to UML diagrams and build systems are covered, making this truly a developer's book. However, even Windows and Mac OS X developers will benefit, despite the BSD focus.

Chapter 1 introduces some of the basic tenets of the book, namely that code is literature and should be read as such. All too often people only read code when they have a specific problem to solve or want to get an example of an API. Instead, if you read code frequently you'll always be learning things and improving your skills. Also, Spinellis discusses the lifecycle of code (including its genesis, maintenance, and reuse), which simply must be taken into account if code is to be good. Poorly skilled developers forget these things and just slap it together, never thinking ahead.

In Chapter 2, a number of concepts basic to any programming language are covered, including the basic flow-control units common to many languages. The book focuses on C, with additional coverage given using C++, Java, and a few other things thrown in for good measure. As such, these chapters -- in fact the whole book -- focuses on concepts common to these languages but absent in some other languages, like Scheme or LISP. One neat section is called "refactoring in the small." It illustrates the real value of the book nicely, in showing you various ways to organize your code and your thoughts for various effects. Oftentimes a book will only teach you one way (which doesn't always suit your needs), and Spinellis' examples do a nice job of escaping that trap, not just here but throughout the book.

Chapter 3, "Advanced C Data Types," focuses on some language-specific matters. These are pointers, structures, unions and dynamic memory allocation, things that most people who code in C may use but only some truly understand well. Again, a somewhat basic chapter, but useful nonetheless. Make sure you read it; chances are you'll learn a thing or two.

In Chapter 4, some basic data structures (vectors, matrices, stacks, queues, maps and hash tables, sets, lists, trees and graphs) are covered. This is an important chapter since it helps you see these structure in real-world use and also helps you understand when to chose one structure over another. While Knuth, CLRS, or other algorithms and data structures texts cover these, they often do so in isolation and at a theoretical level. While their coverage is short, it's to the point and usable by anyone with a modest understanding of C.

Chapter 5, "Advanced Control Flow," the last chapter that deals with actual programming information, is another useful one. Again, short but to the point, this chapter covers things like recursion, exceptions, parallelism, and signals, all topics that have warranted their own books (or major sections in other books) but which are covered in a single chapter here. Still, seeing them side-by-side and in the context of each other and in real-world use provides some justification for the compact presentation.

The remaining chapters of the book go well beyond a normal programming book and focus on projects. These chapters complement the first bunch nicely by focusing on the organization of your code and projects. Chapter 6 deals specifically with many of the commonly identified (but rarely taught) things like design techniques, project organization, build processes, revision control, and testing. A number of things that aren't covered include defining and managing requirements for a release and their specifications, basics on how to use autoconf and automake, and instead rips through a whole slew of topics quite quickly.

Chapter 7 is sure to be controversial for some people: it covers "Coding Standards and Conventions." Some people seem to be big fans of the "if it feels good, do it" style of programming, and instead of writing sane, usable code, what they produce is buggy and messy. This chapter teaches you tried and tested methods of naming files, indentation (and how to do so consistently using your editor to help), formatting, naming conventions (for variables, functions, and classes), as well as standards and processes. The style and standards are (as you would expect) based on NetBSD, which differ slightly from GNU and Linux standards, as well as commonly found Windows practices. However, I think you'll agree that the style is readable with minimal effort, and that goal, coupled to consistency, is paramount in any standard.

Chapter 8 introduces you to documentation, including the use of man pages, Doxygen, revision histories, and the like. Also included are hints at using diagrams for added value. One thing I don't like about this chapter is the opening quote, which sets a bad precedent. It blithely suggests that bad documentation is better than none, which is highly questionable. Misleading docs can be worse than no docs at all, since someone without docs will have to dig through the code in front of them to understand it. Someone with bad docs will rely on the docs and wonder what's broken when things go awry.

Chapter 9 focuses on code architecture, such as class hierarchies, module organization, and even core features like frameworks to chose. This chapter covers a lot of material, and is, despite its size, simply too terse on many of these subjects. It serves as a decent introduction, but doesn't go very far in some places, considering the importance of the material. However, like much of the book, it's a good introduction to the topics at hand.

Chapter 10 also features a lot of good things to know. Granted, you could pick them all up with a lot of hard work and scouring for information, but it's easier to have them presented to you in a cohesive format. The chapter discusses code reading tools, things that you use to help you dig around a large body of code. One you get over a few source files, even if you have well-organized code and interfaces, many changes can require that you inspect the data path. You can do this manually, or you can be assisted with tools. Tools like regular expressions, grep, your editor -- Spinellis shows you how to make use of all of them when you write code. A lot of tools I've never used (but have heard about) are featured, and their use is demonstrated, but of course many tools are simply ignored, focusing on popular ones that will work for most people.

Finally, all of the above is brought together in Chapter 11, "A Complete Example." A small tour of a large, complex piece of code is taken (34,000 lines of Java) as the author makes changes. It's unfortunately in Java, when so much of the book focused on C (why couldn't they have been consistent examples?), but it works. The example itself could have covered a few more things, such as a proper JUnit example, but overall I'm pleased with it.

Overall, Code Reading: The Open Source Perspective is ambitious and worthwhile, both as a complement to a bookshelf of study that includes The Practice of Programming and Design Patterns, and to someone who is growing tired of books on learning a language. At times it feels like the author promised more than he wound up delivering, but it serves as an introduction to a large number of topics. You wont learn a language, and you wont be able to get as much out of the book if you don't engage it with practice, but it's a useful book to get started on the road from being someone who knows a language or two to someone who is a developer, ready to contribute to a team and work on large projects. Never underestimate the skills required to be a good developer, because they go well beyond knowing how to use a language.

You can purchase Code Reading: The Open Source Perspective from bn.com. Slashdot welcomes readers' book reviews -- to see your own review here, read the book review guidelines, then visit the submission page.

99 of 464 comments (clear)

  1. Consistency and good comments by Neil+Blender · · Score: 4, Insightful

    When I see that develop in someone, then I know they are going to the next level. When I don't see it after time, I know they will never evolve.

    1. Re:Consistency and good comments by Arimatheus · · Score: 5, Interesting

      Pseudocode.

      The other day I was looking at some code a peer of mine wrote and noticed a few places where there were comments with no code associated with them, he (like me and I'm sure a few others) builds the frame of his project and then fills in the code. I think it really helps to think the project through before hand, build your comments and then write.

      And oddly, you'll notice that a lot of the coders who do this know a large multitude of languages and probably developed this habit over the course of doing personal projects, it helps you quantifiably say how far along you are in the project, remember what direction you were taking with the project (because of course we NEVER let there go a ~3 week gap in personal projects :-P ) and allows you to look at the fundabmentals of a project without forcing yourself into a language before you can address where your language of choice might fail.

      At least that's the way I see it.

      --
      OEÉæÁÄZÝÈA OEÉæé_CX
    2. Re:Consistency and good comments by shufler · · Score: 4, Insightful

      builds the frame of his project and then fills in the code. I think it really helps to think the project through before hand, build your comments and then write.

      And oddly, you'll notice that a lot of the coders who do this know a large multitude of languages

      This isn't odd at all. I do it myself. And I know why I do it too -- the language you use is inconsequential. If you "write" a script, you can probably use a host of languages to do the task. The key is knowing what you want to do.

      Over the years I've sat and listened to many "debates" (arguments, really) about which language was better than another. I've always sat back and thought, "Who cares?" Once you can reach a point where you can visualise the solution (this includes pseudocoding it out, or at least listing what the program has to do at this point), selecting a language is irrelevant, other than considering what languages are available to you.

      I'm not saying different languages don't have their strengths and weaknesses, and I'm espcially not saying they are all the same. What I am saying is that once you "write" what the program needs to do, use a programming language is as easy as literally looking up syntax and function names in a book.

      This is also how proper language translators operate. They don't know entire languages, but they instead form exactly what they need to say before translating. New languages become second nature after repeated use, it's always just a matter of knowing what you want before hand.

    3. Re:Consistency and good comments by fireman+sam · · Score: 2, Insightful

      Personally, I prefer to have my code self commented. Meaningful variable names, no "smart arse" tricks, good clear layout.

      If you have to explain what a function is to do, it has not been coded very well.

      --
      it is only after a long journey that you know the strength of the horse.
    4. Re:Consistency and good comments by msgregory@earthlink. · · Score: 3, Interesting

      I go the opposite route. I write the code and then a week later go in and write the comments. Doing it this way gives you time to get some perspective on the code, so when you go back to do the comments, you can get a sense of what's clear about the code and what isn't. So it not only helps you write more pertinent comments, it also forces you to think through the code again with a little emotional distance from it, which helps in finding flaws. Actually, it's best to read code and rewrite comments on a continual basis. You can find a lot of bugs by doing this.

    5. Re:Consistency and good comments by dtrent · · Score: 5, Insightful

      Personally, I prefer to have my code self commented. Meaningful variable names, no "smart arse" tricks, good clear layout. If you have to explain what a function is to do, it has not been coded very well.

      Yeah but your function doesn't live in a vacuum does it? How about you write some lines up there at the top to put it in context for when I have to fix it next year.

      Your Cubemate.

    6. Re:Consistency and good comments by Bill+Dog · · Score: 3, Insightful
      You sound as if you work in the sysadmin side of the house. This is probably totally appropriate for your tasks, where you may typically need modest-sized scripts to piece together some things to, say, process a log file.

      But note that there's a whole other world out there, and be careful not to fall into the PHBism of assuming that anything you don't know about must be simple. On the software engineering side, for large-scale projects, the choice of languages is not at all inconsequential, and can affect maintainability, quality, and performance. We're not handed a pseudocode design that spells everything out, and we only have to translate it into the actual language syntax. A requirement may say such-and-such shall appear alphabetically. You know what the program "needs to do", but there are choices of sorting algorithms, and data structures/containers, that have different qualities, that should be evaluated and chosen deliberately for the task at hand.

      --
      Attention zealots and haters: 00100 00100
    7. Re:Consistency and good comments by AhBeeDoi · · Score: 3, Funny

      I always seem to end up with your code before the week is up.

    8. Re:Consistency and good comments by drsmithy · · Score: 4, Insightful
      Yup, I do it all the time. But then I go through and delete all the comments, because when I'm finished, the final code explains what's going on much more eloquently than any comment I could write about it.

      But does it explain *why* ?

    9. Re:Consistency and good comments by CoderBob · · Score: 2, Interesting

      Not everything is coded in OOP. And what about when I'm using your code as a library? Maybe it wasn't intended to be a library, and there is something concerning system state between function calls, or little things like is it a blocking function or not, etc. that I might need to know, now that The Pointy Haired Boss wants me to use it in my code.

      A few lines at the top of each function/procedure/class at least tell the other guy what the known effects of the function are would be nice. I don't want to have to look into your specific code unless something goes wrong. I want to be able to call foo() with baz and bar as parameters, and know that if baz happens to be of the format "xxx.xxx.xxx.xxx" instead of as a long int, it will be translated to a long int for the socket library (This is especially useful in such languages as Python, PHP, etc, that are not strongly typed). And I'd rather not have to take the time to dig through your code to make sure you're doing it.

      I'm not saying comment the process, I'm saying comment the functionality. Preconditions, postconditions, undefined behavior, etc. The best part is, there are tools out there that can rip those out to generate paper docs later, if you want to have a nifty reference. "Oh! You need a function that gives pigs wings? I have one of those- its in file whenPigsFly.py. Here's the doc for the function, its got all you need to know."

  2. Very true. by gimpynerd · · Score: 3, Insightful

    This is very true. While learning a certain language I always have many inconsistanies in my code. In fact I always want to go back and change it as I progress. It just shows that experince is much more than talent.

    1. Re:Very true. by superpulpsicle · · Score: 5, Insightful

      Personally I hate wasting time writing a function to do something only to find out 10 days later... the language already comes with a library that does the exact same thing. Experience do count there.

  3. comments by PoopJuggler · · Score: 5, Insightful

    You can tell a great deal about the maturity of a programmer by the quantity, and quality, of comments.

    1. Re:comments by Rosco+P.+Coltrane · · Score: 5, Funny

      Funny, my colleague posted many good comments here while on the job and he finally got sacked...

      --
      "A door is what a dog is perpetually on the wrong side of" - Ogden Nash
    2. Re:comments by Suicyco · · Score: 4, Insightful

      This is absolutely true. The more you have worked on past software, horrible code, etc. the more you value proper commenting and documentation. Hence, the higher quality you produce, with full knowledge that it WILL be useful someday. Many times I spend just as much time carefully crafting comments and documentation, formatting the code, etc. so it reads as cleanly as possible.

      I suppose it also holds that once you have surpassed the threshold of super-duper quality commenting, you get to the point of such awesome code that it documents itself, so you come full circle.

    3. Re:comments by Mumpsman · · Score: 3, Insightful

      Actually, what you can tell about a programmer from comments is the ability and understanding of his/her direct supervisor.
      Understanding the value of good code is one thing, but insulating your developers from top-down pressure and unreasonable deadlines which force them to "slap it together" allows programmers to prosper.
      If your shop is producing crap code it's because your management is crap.

      --
      No battles to the death are recalled. Mumpsman can hit to attack and cause brainsmashing.
    4. Re:comments by nkh · · Score: 2, Interesting

      with full knowledge that it WILL be useful someday.

      That's why you need Literate Programming! A very good book for all the family ;)

    5. Re:Comments by jallen02 · · Score: 2, Insightful

      It all really depends. Ultimately I feel that you will learn to write code that explains itself to a large degree. There are two times I ask them to make comments.

      1.) The first time to comment is when you are doing something you know are not standard. Bitwise shifts to multiply, things you know are sub-optimal etc. Exceptional things that are not done how they shold be done, or are typically done.

      2.) When you want to explain how a relatively large block of code is working, or an entire classes purposes. Explaining a method or a class. Or a block of code to give an idea of flow.

      Another thing that I feel improves comment quality is to force them to be complete sentences. Try and keep the sentences brief and complete so that it clearly illustrates what you are trying to say without repeating. The "Do not Repeat Yourself" DRY principle applies to written communication as well as code.

      If you always refer back to these simple rules you will generally have code that is easier to maintain and that the comments don't go out of date on as quickly. I really think you could write an entire book on commenting code and naming variables and organizing code flow. Good Luck and happy coding!

      I will agree that developers go through phases in how and when they comment, but having someone lay out guidelines and telling you when to comment and giving you a few concerete examples makes a huge difference. For me its always about being a good leader and doing what I say. I have seen programmers come from mediocre/average just following a few simple "rules". Commenting is a huge part of it. Because if you can comment at an abstract/high level you can prove you know what the code should be doing. And if you write comments before you write the code.... ;-)

      Jeremy

    6. Re:Comments by xee · · Score: 3, Interesting

      There's some nice calculus lurking in there. Something about approaching a number from either side. Oh wait! You might be saying there's an ideal amount of commenting and in large numbers programmers tend to converge on that amount. Interesting. This would make a good research topic.

      --
      Oh shit! I forgot to click "Post Anonymously"...
    7. Re:comments by bobaferret · · Score: 2, Funny

      Unlike everywhere else, here we're waiting for key-release instead of key-press because the operating environment has a race condition and posts them out of order! Go Sun!

      I have this one word for word in my own code. Kinda spooky to see it here, but also nice to know it's not just me.

  4. Literature by 14erCleaner · · Score: 4, Funny
    Chapter 1 introduces some of the basic tenets of the book, namely that code is literature and should be read as such.

    Unfortunately, some experienced programmers write like ee cummings, and others like avant-guard poets.

    --
    Have you read my blog lately?
    1. Re:Literature by Geoffreyerffoeg · · Score: 4, Funny

      HRESULT hReply = authEECummings (atTheLeast) vhlpDidNot vrbUse adjHungarian nNotation;

  5. Hmm not sure by Rosco+P.+Coltrane · · Score: 4, Funny

    You can usually tell someone who's been writing a lot of code by how they write code.

    I looked at the slashcode once and I'm fairly sure Taco worked on debugging serial port line noise before starting /.

    --
    "A door is what a dog is perpetually on the wrong side of" - Ogden Nash
  6. Comments by The+Amazing+Fish+Boy · · Score: 5, Insightful

    The amount of comments in code is interesting. It kind of starts at an extreme, then moves slowly to the middle, happy land.

    If you started programming, you either chose to comment a lot, or not at all. "A lot", because you were never sure what your code was doing, and always needed the reference. Or "not at all", because you could make sense of the code very well.

    Then you start your first big project. For the big commenter, he realizes after getting quite a bit of work done that a lot of time was "wasted" on comments. ("// This line increments the 'i' variable by 1".) Further, these comments seem way too obvious. For the non commenter, they get lost easily and wish they commented more.

    So it goes in the opposite direction from when it started. The big commenter comments way less, and the non commenter comments way more.

    Then it repeats. Eventually the programmer gets the happy medium of the right amount of comments.

    This has been my experience, in my own code, and checking other people's code. Thoughts?

  7. Re:I don't understand by cperciva · · Score: 3, Insightful

    ... you're saying that there's more to it than just getting it done?

    Yes. Getting it done right.

  8. On the topic by advb89 · · Score: 2, Funny

    Speaking of which, I was recently browsing sourceforge, and happened to notice this project which MUST be a good one since its home page has an error screen:

    Fatal error: Cannot redeclare fecha2mysql() in /home/groups/c/cp/cphp/htdocs/funciones.php on line 4

    Not to mention they misspelled 'functions'!
    This project can be found here, and there homepage here.
    Oh yeah, if you happen to be reading this, agustindondo, no offense intended :)

    --
    <overrated>Insert Sig Here</overrated>
    1. Re:On the topic by Jason+Ford · · Score: 2, Informative

      Hmmm, it could be that agustindondo is a Spanish speaker. 'Funciones' is Spanish for, you guessed it, 'functions.' Oh, and 'fecha' is 'date' en español.

      --
      I did not become a vegetarian for my health, I did it for the health of the chickens. --Isaac Bashevis Singer
    2. Re:On the topic by javaxman · · Score: 2, Funny
      Unfortunatly as i mentioned before i have a c++ in spanish

      See, and here I was giving you the benefit of the doubt and thinking you were either being funny or trolling, but... well, unfortunatly you were just being dense. I hope you check your spelling in your own code better than you checked it in that last post, eh? Now I'm back to thinking you're funny again... if not intentionally.

  9. I believe that what you are trying to say is that: by adolfojp · · Score: 3, Insightful

    The smart man learns from his mistakes ...

    ... but the wise man learns from other peoples mistakes.

    I guess that I am just kind of average :-P.

    Cheers,
    Adolfo

  10. Use comments only when needed by c0d3r · · Score: 5, Insightful

    I disagree when writing Java and C#. These languages are inherintley readable if you write clean, understandable code with good variable names. Comments should be used whey you implement something that you can't understand by reading the code. Its quickker for me to read clean code, and comments often get in the way and get outdated and hurt more than they help. Maintainance is the Iceburg.

    1. Re:Use comments only when needed by enjo13 · · Score: 3, Insightful

      You couldn't be more correct. Comments are often to blame for maintainibility problems, as dangling comments can absolutely destroy the readibility of code.

      I do beleive you can judge a programmer by the quality and quantity of comments.. A good programmer comments only when the code NEEDS commenting. No more, no less.

      --
      Turn s60 photos into awesome videos with mScrapbook for all S60 3rd edition phones!
    2. Re:Use comments only when needed by Anonymous Coward · · Score: 5, Insightful

      Just because it's obvious what your code is doing doesn't mean it's obvious why it's doing it. Commenting is still important.

    3. Re:Use comments only when needed by Viking+Coder · · Score: 4, Insightful

      Sure - everything works great if you are fortunate enough to start with clean code, and keep your code clean.

      But the rest of us work for a living. You know, for bosses who don't "Get It." =) We have to fight for the time to keep code clean, because that time could be spent adding features instead. It can be a hard battle.

      When you're in that kind of environment, comments can save your life. Or at least your ass.

      --
      Education is the silver bullet.
    4. Re:Use comments only when needed by 14erCleaner · · Score: 3, Insightful
      Its quickker for me to read clean code

      Maintainance is the Iceburg.

      It's really hard for me to nitpick spelling, but I can't help myself, so please forgive me...

      A good first step in writing clean, maintainable code is to be self-aware. Many experienced programmers don't realize that they write crappy code. Honest code reviews can help, but most people don't take criticism well, and negative style comments are ignored.

      (Of course, my code is perfect... :)

      --
      Have you read my blog lately?
    5. Re:Use comments only when needed by AuMatar · · Score: 4, Insightful

      Whats clear to you isn't clear to me, and vice versa. Whats clear to you now won't be clear to you in 6 months, much less 12. Thats why comments are good- in 6-12 months you will understand the comment, while you might not the code. And a thrid party is far more likely to get the comment than the code.

      As for comments getting outdated- thats why you update comments when you update code. If you don't do that, you're a poor programmer. Changing the code and not the comment is doing half the job.

      --
      I still have more fans than freaks. WTF is wrong with you people?
    6. Re:Use comments only when needed by killjoe · · Score: 4, Insightful

      COmments are best when they explain WHY you are doing something, not just explaining WHAT you are doing.

      For example a comment that says "we have to make exceptions for clients whose names start with A because sales promised them a price cut" is very useful when somebody is scratching their head asking why there is a goofy if statement sitting there.

      --
      evil is as evil does
    7. Re:Use comments only when needed by Suicyco · · Score: 4, Insightful

      That is not the commenting at fault, it is the sloppy programmers who do not update them. Code NEEDS commenting, period. In any decent sized code base, it is not at all clear WHY things are being done at times. So you can read the code, any programmer should understand the syntax and conventions being used. That is not important. What is important is a concise, clean and understandable code structure with good comments. You can NEVER guess the level of future people reading this code. Just spell it out, what is the harm? Do not be sloppy, do not be arrogant and simply engineer good clean well documented code.

      The attitude that commenting is only something to be done if something isn't obvious to YOU is totally wrong and results in poor quality.

      The problem with not doing this, is if its not totally obvious WHY something is being done, it may get changed to something "better" which may break things. Perhaps that particularly ugly sort algorithm needs to be there because of extensive profiling done on the code. If that isn't spelled out, somebody may stroll along and think you were an idiot for obviously using the wrong sort and ruin the performance. For instance.

      ALL code NEEDS commenting at ALL times.

      I can always judge a programmer by the quality and quantity of the comments. A good programmer doesn't presume to know when the code NEEDS commenting based on his totally biased opinion. It always NEEDS it. A good programmer knows what to say to make things as clear as possible.

    8. Re:Use comments only when needed by jerky · · Score: 4, Insightful
      Just write it as
      if (pw_status == ADMIN_LOCKED) {
      and leave out the comment.
    9. Re:Use comments only when needed by boris_the_hacker · · Score: 4, Insightful

      First rule: Nothing is obvious.
      Second rule: Nothing is easy.

      I was in a talk the other day and someone was explaining his research. He used the words obvious and easy more times than I have fingers, just on the first slide!

      Afterwards I took him to one side and explained to him that most things are obvious and easy when you have been working on them for 2 years. He agreed and re-worked his approach.

      It is the same with code, it is never obvious nor easy if you are not familiar with it.

      The code I write is clean, uses meaningful identifiers, remains conistent in both structure and layout (I try to be anal about it). However, going back to some code I wrote last year, I wouldn't have stood a chance of understanding it if it wasn't for the comment explaining the algorithm in the code. This isn't because it is bad code, but because it was complicated and doing intelligent tasks (I would guess complicated and intelligent tasks covers most code).

      Comments are required and are useful for understanding code, providing a bigger picture. Good comments compliment well good code. This is the same for any language.

      You can have bad comments, or too many comments, but if you follow the same set of basic rules for you comments that you do your code, things should be fine.

      --
      chris at darkrock dot co dot uk
      http colon slash slash www dot darkrock dot co dot uk
    10. Re:Use comments only when needed by God!+Awful+2 · · Score: 3, Interesting

      ALL code NEEDS commenting at ALL times.

      You, sir, are correct. The developers I work "with" who never write comments (and in fact argue against them), are the same ones who believe that all code not written by them is a big kludge and needs to be rewritten, and they are also the same ones who are always introducing subtle flaws because they modify code without understanding all the consequences.

      I also need to take issue with the submitter's comment that inaccurate documentation (or comments) is worse than none at all. At least a document/comment tells you what someone *thought* the code was supposed to do.

      An incorrect comment is verifiably false. Once you discover that the code doesn't match the comment, you can make an educated guess about which one is correct. On the other hand, when you are faced with suspicious code and no comments at all, you typically have no idea whether the code is broken or whether you just don't understand what it was intended to do (or in many cases *both*).

      -a

    11. Re:Use comments only when needed by p3d0 · · Score: 2, Insightful

      Others have said why comments are important, so I'll just say this: if someone says "you can tell a great deal about the maturity of a programmer by the quantity, and quality, of comments" and you reply "nah, I don't think they're that important", what do you think that says about you?

      --
      Patrick Doyle
      I mod down every jackass who puts his moderation policy in his sig. Oh, wait a sec....
  11. Coding style by tjwhaynes · · Score: 5, Insightful
    After a while working on very large projects (>1Gb source code) I've begun to find that certain things which looked fine for personal projects are serious problems when you scale up.

    Most critical is managing complexity. Large, complex functions are bad - they have more bugs, they are harder to maintain, harder to bug fix (a change has more likelyhood of breaking something else). If a function has grown beyond a hundred lines of (real) code, it is almost certainly too large. If it has more than 4 levels of nesting, it is too large.

    Comments also matter. It's easy to code a couple of thousand lines of fresh code over a weekend if you get in the groove. It is almost impossible to unpick it one year later if you didn't comment it as you go.

    Variable and function names should be expressive. No single letter variable names! No obscure combinations of letters like words with no vowels (fnct could be function or function control, or even Function Numerical Constant Type). And personally I find that reverse Hungarian notation can be more trouble than it's worth. puiAnnoying!

    Build in automatic checks on everything. If a pointer to a function should never be null, check it and stop if it is. If a variable should only have values 1 -> maxIterations, then check it. If you (or anyone else) ever breaks that assumption, the code will flag it for you.

    Beyond that, nothing beats good design, especially designs where extending the original work is easy. So many designs end up as tangle knots of conflicts because they ended up trying to solve problems that the original code base never envisaged.

    Cheers,
    Toby Haynes

    --
    Anything I post is strictly my own thoughts and doesn't necessarily have anything to do with the opinions of IBM.
    1. Re:Coding style by excesspwr · · Score: 4, Insightful

      You point out some very common problems my colleagues and myself have run into and discussed many times.

      Just to add some more to what you are stating. I've come to the realization that I must determine the final size of a project before starting. If, for instance, a program starts out as a simple Perl script for parsing an input file and later ends with a request for a GUI to be added and additional functionality I'm better off starting over and rethinking the final design then just pasting on the necessary changes to meet the new requirements. Otherwise, the project, although still small and maintainable over a week or two is complete chaos when revisited in say a month or two.

      Comments do matter, however; it does seem over commenting is just as much of a problem as no comments or under commenting. I have been handed projects from long gone developers that have had more comments than code. While it did give insight into what they were doing it became hard to find the code amongst all the comments, even with color coding turned on. It's an artistic poetry of having the right thing to say and saying it correctly. Which I am far from doing in this post.

      The automatic checks on everything that you mention, exception handling and such, should be designed into the project from the start. Grafting it on after the fact is a nightmare and I have pity for anyone that is asked to do this and especially with code that is not theirs.

      A good approach to design, I believe, trumps everything though. If you can hash out truly usable requiremets and implement a good personal software process and stick to it documenting as you go the end product always seems to turn out many notches above par even for small projects. However, none of this ever seems to occur where I work. Half baked requirements and a lack of time to properly design and implement seem to be the norm.

    2. Re:Coding style by AuMatar · · Score: 4, Insightful

      [quote]Most critical is managing complexity. Large, complex functions are bad - they have more bugs, they are harder to maintain, harder to bug fix (a change has more likelyhood of breaking something else). If a function has grown beyond a hundred lines of (real) code, it is almost certainly too large. If it has more than 4 levels of nesting, it is too large.[/quote]

      The problem with hard fast rules like that is they're frequently not right. Take a state machine for example. A simple one with 6 or 7 states will go over 100 lines, and will go over 4 nestings. Heck, you'll take one up with the loop and one with the switch alone. You can break it into functions and hide some of the nesting in functions, but when doing that you frequently end up with functions that don't make a lot of sense by themselves.

      A better rule than size IMHO is the one logical operation rule. A function should do one logical operation (read a file, write a file, run a state machine, calculate something, etc). They should contain all the logic needed to do that operation plus any debug and error handling that makes sense at that level of code. They should be broken up into subfunctions ONLY if those subfunctions themselves follow the logical operation rule, and only if that logical operation makes sense outside of the context of the higher function.

      Take the state machine example. If it needs to wait on a semaphore before each iteration, the wait should be its own function- waiting on a semaphore is a logical operation. The logic for each state should NOT be separate functions, they are part of the state machine and make no sense without the whole of the machine for context. Breaking them out results in harder to read code, even if you do lose the nesting. But if in case RUNNING_MOTOR you issue a stop command to the motor that probably makes sense to be its own function- how you actually stop the motor isn't part of the core logic of the machine.

      I guess my point is- lines of code isn't the enemy, some things are complex and need many lines to do. Nesting isn't the enemy, some things require many loops/ifs. The enemy is a lack of clear separation of functionality and lack of clear abstraction between parts. If you have separation and abstraction, it tends to hit the optimal readability. If you don't, it will fail either because you broke it up too much (too little context) or too little (too much context).

      --
      I still have more fans than freaks. WTF is wrong with you people?
    3. Re:Coding style by bloosqr · · Score: 2, Insightful

      Actually this is one of the great things about C++. If one codes in a subset of C++ (C style,
      w/ local functions) and adds in auto nulled
      pointers and auto bounds checking via templates
      and auto memory leak checking on assignments (via
      templates as well) such that those modes are on only as a compile time option you get the best of both worlds, error checking when you need it and when you are absolutely convinced that the code will work, you can turn it off and get C/Fortran
      type performance..

      -bloo

    4. Re:Coding style by waveclaw · · Score: 2, Interesting
      The problem with hard fast rules like that is they're frequently not right.

      While I'm sure you've seen McCabe Metrics and the evil that is KLOC, hard and fast rules like
      If a function has grown beyond a hundred lines of (real) code, it is almost certainly too large. If it has more than 4 levels of nesting it is too large.

      are actually well-tested and well-known observations about the complexity of software code when read and written. Remember that unlike a computer, which knowns only its current state, a human can only track so many multiple states at once.

      Take a state machine for example. A simple one with 6 or 7 states will go over 100 lines, and will go over 4 nestings.

      Ah, the switch based state machine. You remind me of my college days learning formal automata for AI and compilers. If I saw a huge switch statement in the middle of parser code, I can be sure of how little training and experience the coder had when he/she wrote that. There are alternatives and they are so much more elegant.

      Swtich-based state matchers were a poor design then, and are a poor design now. By using maps, state tables, or even hashes your state machine can be 50-100 lines with thousands of states and easily readable code. Of course, the documentation of state transitions becomes very important. This information will be forced from the switch/case statement labels and into secondary documentation, such as *gasp* pictures of your state transition network. So, your state names will not be readily visible in the code (unless you are very good at reading sparse matrices and compressed linked-lists.) The compact and simple table-based code will be easier to maintain than a monstrous switch.

      Take the state machine example. If it needs to wait on a semaphore before each iteration, the wait should be its own function- waiting on a semaphore is a logical operation. The logic for each state should NOT be separate functions, they are part of the state machine and make no sense without the whole of the machine for context.

      From formal automata theory we know this to be incorrect. It sounds right, but leads to statements such as the intermingling of different functions as proposed here. Each state is independent of all other states. Each state does have incoming transitions and outgoing transitions. These transitions are completely isolated from the internal facts of those state from which they originate or terminate.

      Mealy machines perform actions only on transition. Moore machines perform actions only while in a state. Hybrids exist that do some of one and some of another. Even simple automata such as those show can have no well-defined state- or transition-based actions. None of these require implementations in code that break the 100-line and 4-depth rule. All of them can be implemented with a table and a transition function containing *gasp* no switch statements.

      There are a slew of Computer Science courses that delve deeper into a topic such as state machines and code readability. (I'm certain they must be terribly boring and useless to be overlooked by so many professional coders.) But, state transition is just as well known as other logic operations like array index math and -dare I say- waiting on a semaphore. These courses cover a lot of techniques, but all that have implementations that benefit from the 100-line and 4-depth rule above.
      --

      "You cannot have a General Will unless you have shared experiences. You cannot be fair to people you don't know."
    5. Re:Coding style by dutky · · Score: 4, Informative
      AuMatar wrote
      The problem with hard fast rules like that is they're frequently not right. Take a state machine for example. A simple one with 6 or 7 states will go over 100 lines, and will go over 4 nestings. Heck, you'll take one up with the loop and one with the switch alone.

      If you are coding state machines with switch statements, then you don't know what you're doing.

      The state transitions, transition functions and accepting states should be stored in tables (2-dimensional arrays) and the entire state machine is then coded in about a dozen lines:

      int next_state[NSTATES][NTOKENS]={{...}...};
      int state_trans[NSTATES][NTOKENS]={{...}...};
      int accept[NSTATES]={...};
      int retval[NSTATES]={...};
      int (*trans_func[NFUNCS])(...)={...}; /* syntax may be wrong */

      int state_machine(...)
      {
      int st = 0, tok = 0, err = 0;

      while(accept[st] == 0)
      {
      tok = get_token();
      err = trans_func[state_trans[st][tok]](...);
      if(err)
      return err;
      st = next_state[st][tok];
      }

      return retval[st];
      }

      All the real work is in the state tables and the transition functions referenced in the transfunc array. I've found it a hell of a lot easier to get the state tables right than to find all the mistakes in a giant, nested switch statement. In most circumstaces the state tables can be constructed from the state diagram by inspection. For anything big enough that you can't construct the state diagram by hand, there are automated tools.

      I guess my point is- lines of code isn't the enemy, some things are complex and need many lines to do. Nesting isn't the enemy, some things require many loops/ifs. The enemy is a lack of clear separation of functionality and lack of clear abstraction between parts.

      Lines of code or nesting depth may not be the enemy, but they're no ally either. It can be quite difficult to specify to a diverse team what is meant and expected by clear abstraction and separation of functionality, but almost anyone can wrap their brain around a LOC or nesting depth limit. If you don't want to make hard and fast rules, you can use these limits as warning signs during the code review.

      On the average you can say something like: "good code shouldn't have functions longer than two pages (120 lines) and no more than 4 or 5 levels of nesting" and hand out indulgences where needed. At least you might get some of the lower functioning members of the team to think before they code a 5000-line monstrosity.

  12. QA by Anonymous Coward · · Score: 2, Informative

    I'm doing QA on all work delivered by contractors that is on our standing offer list (yes, I work for the government... and yes, there are a few good people here).

    First thing you notice is that they hardly ever read the statement of work before they start coding. The whole application becomes a hack.

    Second thing is the structure of the code. Indentation? What a heck is that? Some try doing indentation by just using the tab key. That looks great for someone who has the tab size set to six... after 13 levels (extreme, but I've seen it), you can't see the freaking code. My advice to someone who's in charge for a codebase... Standardize indentation, as well as the tab setting.

    Third thing you notice is that they've changed the structure of the database that you provided to them. Oh, great!.. There goes the lean structure, just so that the coder could perform a quick hack by duplicating the data throughout all the tables.

    If you want code to be maintainable, do QA, and do it good!

  13. language bias detected by anomalous+cohort · · Score: 3, Insightful
    In Chapter 2, a number of concepts basic to any programming language are covered, including the basic flow-control units common to many languages. The book focuses on C, with additional coverage given using C++, Java, and a few other things thrown in for good measure. As such, these chapters -- in fact the whole book -- focuses on concepts common to these languages but absent in some other languages, like Scheme or LISP.

    Hmmm. What concepts, particularly what basic flow control concepts, are present in C, C++, or Java yet absent in Scheme or Lisp?

    1. Re:language bias detected by KenSeymour · · Score: 4, Funny

      I would say the concept of getting paid for programming.

      --
      "We can't solve problems by using the same kind of thinking we used when we created them." -- Albert Einstein
    2. Re:language bias detected by Jerf · · Score: 4, Insightful

      The author is probably referring to the way Scheme and LISP are "missing" static type checking, in much the same way I am "missing" a hole in the side of my head.

      The claim that something is missing would be more compelling if they weren't "missing" it on purpose, and the trend wasn't swinging in the dynamically-typed (or, as I think of it, "value typed" as opposed to "variable typed"; whatever you do it's wrong to call it "weakly typed") language's favor anyhow.

      (Variable-based-typing's only hope is that the type-inferencing languages penetrate into the mainstream before the recoginition that variable-based-typing isn't free does, that being the fundamental misconception keeping it afloat. (Once you ack. the obvious point that it is not free, and is in fact quite expensive, both in actual effort and in massively underestimated opportunity costs, you are suddenly able to consider it in terms of costs and benefits, and the cost/benefit analysis generally does not work out in its favor when done dispassionately, though there are exceptions.))

    3. Re:language bias detected by d1v1d3byz3r0 · · Score: 2, Insightful

      If they don't exist, you can always build them in. That's the beauty of Lisp.

      Off the top of my head, I can't think of any major flow control mechanisms missing from native Lisp. If they exist, I'm pretty sure there was a good reason that they don't exist in Lisp.

    4. Re:language bias detected by benja · · Score: 2, Interesting

      Umh -- your comment would make sense if the author had indeed written missing, rather than the neutral absent. I can't detect the bias in saying that C includes concepts LISP doesn't and vice versa.

    5. Re:language bias detected by Jerf · · Score: 2, Informative

      I've been working in Perl and Python for years now, with large products running on very big scales, tens of thousands of users running mission-critical stuff, day in, day out, that sort of thing.

      I will let you know when this is an actual problem, rather than stories to scare the kiddies with. So far, while I can clearly see the costs (which, if you have never left the 'safety' of variable-based typing languages behind, you can't), I'm yet to see the benefits on any but the most extreme jobs.

      I've picked up quite a list of people I've made this promise to, but I don't bother actually keeping track, because it isn't going to happen. They keep promising doom, and I keep coding four or five times faster than they do, and that's just the linear speed-up, to say nothing of the improved re-factoring I get and the general benefits of flexibility. Fair trade, I suppose.

  14. Amen by MHobbit · · Score: 2, Interesting

    Amen.

    I've been coding for a couple of years now, and always have been using comments. Mind you, I only started using a lot of them when I started with PHP code and other open-source, interpreted languages.

    Right now, I'm still a better C coder than PHP, and you can tell by what approach I take to solve various things.

    --
    Debugging? Klingons do not debug. Bugs are good for building character in the user.
  15. "Code is its own documentation, right?" by guitaristx · · Score: 5, Funny

    From someone who's had a hand in dealing with function pointers named StupidSuckingGlobalCallbackFunction, trust me, the subject of this post is very, very, very wrong.

    --
    I pity the foo that isn't metasyntactic
  16. Learning from decades-old code by shoppa · · Score: 4, Informative
    I've had many opportunities to work with code that has evolved over several decades. There are two common patterns:
    1. Project was originally a quick hack. It lives well past its prime, gets modded extensively to handle changes going on in the real world (new devices, competition, etc.). Abstractions are added where necessary. Some hacky ugliness lives at fringes, but after umpteen releases and way too much backwards compatibility customers are still buying it.
    2. Project was a grandiose dream by an analyst. Before any functionality exists, everything is abstracted to the max. 10-inch thick binders full of API's are published before the product actually does anything. The abstractions usually turn out to be wrong, and after many years (and little functionality) either the abstractions get twisted around at great expense to reach functionality, or the project dies in heaving paroxysms.
    . Reading the experienced coder's comments is always good. They know the history and want to pass on the lessons learned to whoever will look.
  17. Re:I don't understand by nacturation · · Score: 2, Funny

    Ah, grasshopper... your code may get things done, but will someone in Soviet Russia who has to parse through your unreadable code get done by it?

    --
    Want to improve your Karma? Instead of "Post Anonymously", try the "Post Humously" option.
  18. comments by defile · · Score: 5, Insightful

    If the expression itself requires a comment, then it is not being expressed clearly enough. It should be rewritten.

    If the expression is clear, but does not make sense in the context, appears suboptimal, or otherwise thwarts the reader's expectations, THEN I'll comment.

    Real world project comments...

    • We're using this byte[] array wrapper because Java won't hash identical byte arrays to the same value.
    • This is in fact a bubble sort. I'd call qsort(), but it's nowhere to be found and I'm not going to implement it myself just for this. Additionally, it doesn't matter since it sorts at most 100 records, and it's called very rarely.
    • Why yes, it IS a giant waste of space to represent an 8-bit value in a 16-bit quantity. But someone thought it was a good idea to leave out unsigned.
    • Unlike everywhere else, here we're waiting for key-release instead of key-press because the operating environment has a race condition and posts them out of order! Go Sun!
  19. The art of code by achacha · · Score: 2, Insightful

    There is a lot of truth to coding style that is not necessarily related to open source. Seasoned programmers work very hard to keep naming, patterns and even formatting consistent. It makes it easier to read.

    The open source angle is that the code will be viewed by many people and thus there is a greater detail to presentation, it's a form of ego stroking and seems to work quite well.

    I do code reviews all the time and I can tell a novice developer from someone who has done it for a while, it's a hard thing to explain, it's like a difference between good and bad art. And just like with art, you can have decent artists and you can have great artists with very little in-between. Go figure :)

    1. Re:The art of code by cratermoon · · Score: 2, Insightful
      It's not that hard to quantify the differences between a novice's code and an expert's. Here are some.
      1. Novice code is longer. There will be three statements when one will suffice.
      2. Novice code has duplication. You'll see the same thing done two ever-so-slightly different ways in two different place. An expert will abstract out the common code and vary on the thing that changes.
      3. A novice will write redundant conditions and handlers where an expert will be concise. To give a Java example, in a catch clause, checking that the exception is not null (!)
      4. Novices will code for all sort of imaginative things that *might* occur in some speculative future. Expert code will contain nothing that is not pertinent to the task at hand.
  20. MOD PARENT UP by guitaristx · · Score: 2, Interesting

    Believe me, this is ever so often the truth. I can't count the number of times where I thought "WTF was this programmer thinking?" while maintaining legacy code, only to find out that the offending code, after tracing through the authors and check-in logs, was driven solely by a manager cracking the whip for a quick-and-dirty solution.

    --
    I pity the foo that isn't metasyntactic
  21. Bad docs better than none by Acius · · Score: 2, Insightful

    Having worked on some terribly buggy code with buggy docs, I'm going to suggest that even bad docs are better than none. Sure, you get burned once or twice, but you quickly learn not to trust the docs as canon. And while the docs often don't tell you what the code does, it tells you what the developer WANTED it to do, which is golden information when you're trying to debug.

    --
    Acius the unfamous
  22. Re:Code format by Cthefuture · · Score: 3, Insightful

    I have to say that I agree with you to a certain degree.

    I'll go one more step though. It's about white space. You can almost always recognize a good programmer by how clean the code looks because of proper use of white space. If the code is all crammed together and cluttered looking then you know that programmer sucks. White space does not make your code slower guys!

    Note that I said this is a way to recognize a good programmers. Often experience plays a role but I see a ton of fairly experienced programmers that write ass crappy code.

    I see a lot of people saying comments are also a sign but I have to disagree. Well written code is easy to read and figure out. I would much rather have nicely written code than comments. (Note that it is my job to fix other people's code so I have a lot of experience with this)

    --
    The ratio of people to cake is too big
  23. What about COBOL? by HeighYew · · Score: 2, Funny

    I write COBOL code. Since COBOL is just like writing in English, why should I need to comment?

    ... on second thought...

    Hooray for self-documenting code!!! :)

    --
    There are 10 kinds of people in the world, those who understand binary and those who don't...what about the other 8?
  24. Comments need structure by yintercept · · Score: 3, Informative

    It seems to me that documation needs a structure. Structures like records of revision and design documents are useful. Free form comments in the code turn into white noise. Since I am far more interested in what a piece of code does, I pretty much ignore all documentation in the code. It seems like the majority of documentation in code becomes obsolete as people modify the code. For example, if a person has a problematic part in their code, they add copious notes document. When the problem finally clarifies in their mind and the find the code, they end up leaving obsolete documentation that just confuses people at a later date.

    Personally, I think a person should document the interface, maintain a record of revision. The code itself should only be documented when the code is doing something out of the ordinary.

    So, when I am learning a language, I put a great deal of notes in the code. As I learn the typical flow of the programs, I write less documentation in the code. When I have a strong feel for the language and reading the code is more informative than reading notes, my notes all but disappear. I've noticed many other programmers seem to have the same tendency. They write less documentation in code and they learn the language.

    Having supported a large number of applications. I greatly appreciate good design documention, but discard notes in the code.

  25. Indeed! by kraksmokr · · Score: 2, Insightful

    After spending many years as a programmer, and writing thousands of lines of code, I have learned so much about coding that these days I find myself not writing code at all, or very little code.

    As a young freshly trained programmer, you are walking around with a hammer, and a lot of things look like nails.

    You think you are a badass because of the power and precision with which you hit the nails.

    Then one day and you see you are building a house. And there are a lot of other people whose work is instrumental to getting the house built.

    So after a while you start telling other people how to hit their nails, and before you know it, you are building entire tracts of homes.

    1. Re:Indeed! by sylvandb · · Score: 2, Insightful

      After spending many years as a programmer, and writing thousands of lines of code, I have learned so much about coding that these days I find myself not writing code at all, or very little code. ...
      So after a while you start telling other people how to hit their nails, and before you know it, you are building entire tracts of homes.


      But that isn't the end. Suddenly you find yourself hating the way everyone else hits those nails (just when someone starts to get good they leave) and the subsequent lack of quality* to which your name is attached. You come to realize that either you care about lots of houses with your name attached, or you care about the quality of each house with your name attached. It cannot be both.

      Now you choose. Do it yourself and do it right; or just take all you can, while you can, and move on up the management ladder. What do you value?

      * - see "Zen and the Art of Motorcycle Maintenance" by Pirsig

      sdb

  26. my technique by SQLz · · Score: 2, Informative

    I'm a scripter (php, python, perl, java etc) and I work with other scripters. I notice a big difference in code depending on the age of the programmer and what programming language they started with.

    My technique is pretty much opposite from anyone elses where I worl, although, I don't know of this is a good thing or not.

    I start off with making tons of functions. I go crazy with functions and usually put them into related objects/packages with static methods just for organization purposes....so I quickly know where to look when I want to edit the behavior. I always maintain the PerlDOC/PHPDoc/JavaDOC comments even when I first start out because my IDE reads and pops up nice little hints when I forget arguments but other than that I don't comment too much unless I'm doing something dicey. Once I've coded enough, I start to see the places where I can refactor and abstract a lot of the functions away and weed out any inconsistancies really quick.

    Others tend to make one gigantic script with little or any functions and tons of global structures and arrays then refactor it to a more sane style later on.

    Their moto is "first get it working, then get it working right". My reply is "that doesn't mean you throw good programming practice out the window.";

  27. Re:I don't understand by TheSpoom · · Score: 2, Interesting

    Damn right. My new job involves making modifications to a Russian-developed PHP shopping cart, and while it's decently programmed in some places, others are *hellish* to try to work through and/or edit. /me keeps telling himself that it's all for the experience... ;^)

    --
    It's better to vote for what you want and not get it than to vote for what you don't want and get it.
    - E. Debs
  28. What I do as a programmer by Anthony+Boyd · · Score: 3, Informative

    As a PHP/Perl/JavaScript/HTML developer, here are some of the things I think I do well:

    • I use liberal comments in most of my projects. I always try to use /* and */ to make multiline comments that are easy to quickly add to without worrying about a lot of pretty formatting.
    • I have recently started to play with PHPDoc to create self-documenting code.
    • When creating any character(s) implying "open" I immediately create the "closed" character(s) too. For example, I type "if () { }" and then fill it in. As I write this, my LI tags are all already typed, I am just filling in text now.
    • I use text editors with syntax highlighting, such as HTML-Kit -- no drag & drop GUIs.
    • I use tools like WinMerge, Subversion (only a little, not so good at it) and ReplaceEm to maintain large codebases.

    Where I fail at coding:

    • I know OOP, but it isn't natural for me, so I'm still a procedural boy, even when Object-Oriented Programming might help.
    • I have no idea what vectors and matrixes are.
    • I'm self-taught, my schooling is as an English major. So I have absolutely no Computer Science education behind what I do. While I try to do well, my solution to a deep and complicated problem is going to be basic compared to a guy who spent years of his life learning algorithms.
    1. Re:What I do as a programmer by mr_z_beeblebrox · · Score: 2, Funny

      I use liberal comments in most of my projects

      Where precisely does something like "The Bush administration has no right to detain American Citizens as enemy combatants" fit into your projects. I guess it is better (IMHO) than commenting conservatively.

  29. Objective-C by bonch · · Score: 2, Insightful

    One of my favorite aspects of Objective-C is the way methods are self-documenting. "[Object doThis:something toThis:something]" is easy to figure out.

  30. Chapter 7 is sure to be controversial by Anonymous Coward · · Score: 3, Funny
    Chapter 7 is sure to be controversial for some people: it covers "Coding Standards and Conventions."

    I don't understand

    why it would be so
    controversial.

  31. Re:Code format by kclittle · · Score: 2, Insightful

    This style
    {
    is better by far
    {
    because the braces 'belong' to
    the following block of code
    }
    not the
    {
    preceeding guard statement
    }
    }

    Sincerely,
    An OFP (Old Fart Progammer)

    --
    Generally, bash is superior to python in those environments where python is not installed.
  32. Re:Code format by Decaff · · Score: 4, Insightful

    All I gotta say is that if you use {
    this {
    style {
    of indention
    }
    }
    }

    You are a newbie.


    No way! I have been coding for 30 years...

    It makes more sense to code

    if (test) {
    statement1
    } else {
    statement2
    }

    because the eye can immediately see from the 'if' and 'else' statements alone that a block of conditional code follows. Having the braces on the same line also marks out the lines as special: flow control code. I find that

    if (test)
    {
    statement1
    }
    else
    {
    statement2
    }

    simply adds pointless extra lines to code, with no gain in readability.

  33. Comments: they are documentation in the source by wintermute42 · · Score: 3, Insightful

    I've tried to work on projects where we developed new software as much as possible. However, taken all together I think I've probably spent many months of aggregate time reading other peoples code, modifying it, adding new features and fixing bugs. I hate doing this work, not only because of poorly designed software but because most software I've worked on is almost entirely undocumented (e.g., no comments and no external documentation).

    Some posters have mentioned comments and documentation in this thread. But in my opinion, it has not been discussed in strong enough terms. For a long rant on this topic, see my essay Software and Documentation. I'll summarize some of the points I try to make in this essay here.

    There were only a few people in this thread who wrote that they think that software source code is self documenting. This is a good sign. In the past this was a widely held view. Perhaps it is becoming less popular. Perhaps pigs are becoming airborne.

    To those of you who wrote that your code is self documenting: you are simply lazy and unprofessional.

    Sure I can understand what well written code does. But I cannot understand why it does what it does. I can show you three hundred lines of wavelet signal processing code. You can immediately understand what the code is doing. But you will have no clue what-so-ever about why it does what it does unless you understand the thinking behind the code. The same is true of any complex software.

    Another problem is that even though I might understand what components do and perhaps even why they are doing what they do, getting an overall understanding of the architecture of a large application can be difficult and time consuming.

    By not writing comments in your code and, for large applications, providing external documentation on the architecture, you are saving your time and effort, but forcing others to spend effort that you could have saved them.

    Even if you have no consideration for anyone else who must maintain the application after you write it, comments will help you find bugs in your own code. Again and again I have found that when I explain my code in a comment I am forced to look over it again. In doing this I have found that the code does not do what I intended or that I left something out.

    When it comes to documentation there is rarely enough. Even if you make an effort to document your code, when you are in the middle of it you will not document things that you find obvious. When you return later, these obvious issues will have been forgotten and are not obscure.

    Documentation must be maintained along with the code. And it should be extended where it is incomplete or unclear. English (or what ever your language is): it's another kind of software.

    Developers seem to fall into two camps: those who believe documentation is important and who do it and those who do not. I've managed software groups where I mentioned documentation in people's performace reviews and it still did not good. So far I have not been able to get someone who does not document to do it.

    I have thought that perhaps one solution would be to hold code reviews where one of the major features that was looked at was understandability in the code. But I have not experimented enough to know if this will actually get people to document their software.

    Literate programming not only involves writing well designed and well informed algorithms. It also includes writing documentation that explains these algorithms.

  34. Nonsense. by dwheeler · · Score: 4, Informative
    I'll use whatever the indentation style of a current project is, and carry on. If you like that style, then go ahead, use it. I'll go along, too, if you're the project lead.

    But many "newbies" such as Kernighan, Ritchie, and Torvalds all highly recommend the One True Brace (OTB) style. It's the one used in the K&R's C book, among other things. In other words, some of the people MOST exerienced with C use this style.

    There are serious advantages to the OTB style. In particular, it eliminates useless white space so that you can actually see more vertical text simultaneously -- even with big screens that's helpful.

    If you want to use a different style, go ahead! If you're the lead, I'll gladly use your style. But in programs I lead, I'll continue to use OTB and expect others to follow suit. Oh, and I've been using C since 1985, so !newbie.

    --
    - David A. Wheeler (see my Secure Programming HOWTO)
  35. some newbies by r00t · · Score: 4, Informative
    authors of 1999 ISO C standard (section 6.8.3 example 3, etc.)

    Linus Torvalds (Linux kernel)

    W. Richard Stevens (Advanced Programming in the UNIX Environment and UNIX Network Programming)

    Brian W. Kernighan (C Programming Language co-author)

    Dennis Ritchie (C Programming Language co-author)

    Gamma, Helm, Johnson, and Vlissides (Design Patterns)

    Hmmm. You are a newbie. Kernighan and Ritchie wrote the book on C, literally, and the ISO C standard still uses their style. The opening brace is only by itself when starting a function body. In all other cases, it shares the line.

    1. Re:some newbies by corysama · · Score: 2, Interesting

      It is my understanding that it wasn't even K&R's choice to lay out the code that way. It was a demand from the publisher to save paper. It may be a urban legend, but assuming it's true it makes me laugh when I see programmers get zealous defending a standard designed by a bean counter who was just trying to trim his dead tree budget.

  36. Obligatory quote by jagne · · Score: 5, Insightful

    From Structure and Interpretation of Computer Programs by Abelson and Sussman:

    "Programs should be written for people to read, and only incidentally for machines to execute."

    1. Re:Obligatory quote by lexluther · · Score: 2, Informative

      This is a Knuth quote. "Programs are meant to be read by humans and only incidentally for computers to execute".

  37. joe kicks ass by r00t · · Score: 2, Funny

    WordStar kicks ass, and joe is like WordStar.
    Therefore, joe kicks ass. Alan Cox uses joe.

    Unlike vi, joe is easy to operate over all sorts
    of defective terminal emulators. For example, vi
    users are screwed w/o the escape key. I've seen
    that twice, on a real DECstation (OK, you could
    use the F11 key to get an escape...) and on a
    telnet program for the Mac.

    No problem for joe!

    Don't get me started on emacs...

  38. Re:Indentation by AuMatar · · Score: 2, Insightful

    So you need guidelines to make a bad design decision work. This is what is generally called a "hack". The real solution is to not give semantic meaning to whitespace in the first place.

    --
    I still have more fans than freaks. WTF is wrong with you people?
  39. Vetenari by Nefarious+Wheel · · Score: 2, Funny
    ...namely that code is literature and should be read as such...

    Ah, like Vetinari, The Patrician ... he enjoyed reading music. He didn't want to play it, or listen to all that noise, merely to read it.

    --
    Do not mock my vision of impractical footwear
  40. Consistency, good comments, and short code by Grax · · Score: 4, Insightful

    Both consistency and good comments are good traits. I find understandable function and variable names are essential as well.

    I believe that language familiarity has little to nothing to do with being a good programmer. A more familiar programmer can program faster but programming principles remain the same across all languages.

    Smaller, simpler code is inherently less likely to contain bugs, runs faster, and is easier to troubleshoot.

    If programmer A develops 1000 lines of code per day and programmer B develops 10 lines of code per day and both programs do the same thing I will pick programmer B every time (normal lines of code, not contest entries with runon lines and things)

  41. Commenting in Eyecandy, ERP Modules & Space Cr by Qbertino · · Score: 2, Insightful

    When I programm ERP Modules (in Python) I start with a large comment on what the module is supposed to do. I actually started doing this quite from the beginning. I write a litte summary of what the module does and comment heavily throughout the source. I even have debugging sessions were I only work on comments - often detailing them further.
    I also do a lot of other programming (ActionScript rich media and stuff). I don't do opening or other comments there that much.
    Why that?
    If a multimedia app doesn't work I get an e-mail or a call asking to check into it within the next 24 hrs. The customer is anoyed or says that this comes bad with the end-user. We talk a little, I et to work and fix it within the next 10 hrs.
    If a ERP module doesn't do what it's supposed to, I get a call on my mobile, with the CEO at the other end telling me that he's losing 200 Euro an hour because order processing has gone haywire.
    This is a good point to end all discussion wether commenting is good or bad. When you are debugging a piece of code that keeps 3 people breathing you shure as well want it to be well commented.
    The Lockheed Software Group, the people who write code for the space shuttle and other things, take this to the all-out extreme. They write EVERYTHING in a sort of human readable meta code, meta-comment THAT and then tranfer that to real code. They've had 3 bugs in 25 years - so I've heard. One of them being a counter problem that once would have nearly locked down a com-dish on an orbiter and prevented it from following it's target on earth after having rotated a certain amount. Figure: A millisecond of timing error in their code has the orbiter off course by 3 miles. I hear their comment line/code line ratio is about 7 to 1.

    So much for "overcommenting is unprofessional".

    --
    We suffer more in our imagination than in reality. - Seneca
  42. Re:Other useful words and phrases in Spanish... by Grax · · Score: 2, Funny

    I am a mix of Cuban-American-Scottish-English-Irish-etc. I applied for minority status for Cuban-American-Scottish-English-Irish-etc's but they told me that there wasn't enough of us to form our own minority.

    To all the other Cuban-American-Scottish-English-Irish-etc's out there I say "stand with me. power to the Cuban-American-Scottish-English-Irish-etc's"

  43. Call everything by its right name by blackhedd · · Score: 4, Insightful

    To think of code as literature is actually a non-obvious and striking idea.
    The essence of literature is communication. I've been writing C and C++ for years, constantly fine-tuning and refining code and documentation, line by line, paragraph by paragraph. And how apropos to realize that the point is to express the thought so clearly and concisely that any reader (including yourself three weeks from now) will grasp it as close to immediately as possible.
    That means you have to think clearly in order to code clearly. This matters more than almost anything else.
    Too much documentation is just as bad as too little, since it obscures the intent of the code. How much is just enough? It takes years of thoughtful practice to know that.
    Everyone is posting their favorite rules about coding standards. Most young programmers apply standards as rigid as they are incomplete. I have only two rules:
    1) Call everything by its right name. Functions are verbs, variables are nouns. The name you give a object should express what it is or does, no more and no less, and be readable in English (or whatever your spoken language is). If you add something to a function that doesn't fit the function's name, it either belongs in a different function or else you misnamed the function in the first place. This takes constant review of your code, from a literary point of view.
    2) Rules are made to be broken. All great writers unhesitatingly break the rules in order to communicate more clearly.

    Languages are for communicating, and computer languages are no different. Keep that in mind at all times, and you'll become a wizard.

  44. It's not wrong. by Chemisor · · Score: 2, Informative

    > dealing with function pointers named StupidSuckingGlobalCallbackFunction

    When you have functions named like that, don't write documentation for them. Rename them. It takes a lot less effort to write code so its meaning is obvious then to write documentation explaining why you didn't do that.

  45. Re:dating yourself by Chemisor · · Score: 2, Funny

    > (I know I'm dating myself here!)

    Don't worry, we all do.

  46. Re: Don't bash hungarian by gidds · · Score: 2, Interesting
    Agreed. Full Hungarian notation is an abomination. A variable's type has no business being in its name; not only does that preclude changing the type at some point, but the type is usually clear from the context anyway.

    Whether the scope should be there probably depends on the language and system. In C, like you I've found p_ (for function parameters), g_ (for system-wide globals) and m_ (for module-level variables) useful; also s_ (for 4GL screen fields). That way, you know where to look for the definition, so you can find anything else out there. (Local variables get no prefix.)

    In Java, though, I find scope is usually obvious. Methods tend to be short enough that parameters are usually obvious; class names should start in upper case, which should make them obvious (and you can track them down from import statements); fields in other classes are always obvious from context; and there are no globals to worry about. The only confusion is between local variables and class fields; for a while I used a single underscore prefix for fields, but I really don't find it necessary.

    I'm sure that other languages have their own needs.

    While we're here, might as well repeat my own principle for meaningful variable names: The length of a variable name should be directly proportional to its scope. A variable that's only used for a couple of lines after it's defined is best kept to a few characters; but one that's used throughout the system should have a name long enough to make its function clear.

    --

    Ceterum censeo subscriptionem esse delendam.

  47. reading rule of thumb by recharged95 · · Score: 2, Insightful
    You'll notice good code is when you're reading the code and see comments at points when you question (yourself) the control flow from an architectural perspective. Then the developer's comments explain why he/she chose that path (i.e. the solution/algorithm/paradigm) and the pros&cons of his/her choice.

    Hence, simple algorithms will likely have no comments in the implementation.

    More of the time, comments that "supplement" the code usually add to the confusion for most advance developers.

  48. Worse than pointless by bluGill · · Score: 2, Interesting

    or simple counters I find that using i or j or similar works quite well. After all, everybody understands a for loop with an i index -- calling it array_index_counter or some-such is (IMHO) pointless.

    No, array_index_counter is worse than pointless, using i or j is recognizable to any good programmer, no though required. Using array_index_counter implies that there is something other than a loop index going on with the variable and causes all good programmers to pause for a moment to figure out what that is. (Only to be annoyed when they realize it is just a loop index)

    There is one other program with descriptive loop indexes: they take up too much space. The example you gave it 19 letters, and in a typical C for statement will be repeated three times. That works out to 57 letters. Add in a few for the other required parts, and indentation, and you are longer than the typical terminal line, and all hope of easy readability is lost. (Unless you expand the window, but that assumes you do all your editing in a graphical environment, which isn't always true)

  49. Re:Code format by Kwil · · Score: 2, Interesting
    I've only seen this done a few places, but I find the format
    if(test)
    { statement1
    }
    else
    { statement2
    }
    works very well.

    Especially when you get into nested..
    if(test1)
    { statement1
    statement2
    statement3
    if (test2)
    { statement 4
    statement 5
    }
    statement6
    }
    Bringing the brace down, but not adding the extra line conserves the space lost with the latter method and keeps the connection between the test and the block, but at the same time makes visual "chunking" of the code blocks that much easier by having the open and closing braces in the same column.
    --

    That Jesus Christ guy is getting some terrible lag... it took him 3 days to respawn! -NJ CoolBreeze

  50. Re:Blogging entails responsibilities... by zootread · · Score: 2, Funny

    We might not lend ourselves well to sympathetic portrals in commercials and other media, but every night we can come home in a shiny expensive car, to a nice furnished apartment, and eat some pussy before going to bed.

    Eat some pussy before going to bed? You sound pretty whipped to me. I either get my dick sucked by my girl or stick my dick in a pussy and blow a giant load in her mouth or pussy (and sometimes in her ass). I never ever have to eat her pussy. Eating pussy is for lesbians.

    I think the idea that geeks can't get laid is a huge misconception. Eventually some woman gives us a chance, and after years of masturbating we have such good sexual control and technique that we're able to make her cum like she's never cum before. That's what happened to me anyways. Having said that, though, having someone to fuck is really not that big of a deal, I don't know why people act like it is and put down people who can't get laid. Yes, sex can be really fucking good, but it doesn't improve you as a person to be getting it. If you were a stupid fuck before getting laid, you're a stupid fuck after getting laid.

    And yes, I am one of those open source hippies. But I get paid for leveraging open source technologies. And I get laid. Boo-yah!

    --
    Zoot!
  51. comments are overrated by menem · · Score: 2, Insightful

    Have you ever been in a code review that was a comment review? Where the reviewers, unwilling or unable to find problems in the code, nitpicked about how the comments looked like? Too many managers and second tier programmers spend their time worrying about the details of the comments and coding style because that is all they understand.

    What good are great comments if the code doesn't work? Sure, comments matter, but all of the comments in the world aren't going to make buggy software run better. It will just make the next programmer understand how screwed up the original programmer was.

  52. Read Code, *NOT* Comments! by andr0meda · · Score: 2, Interesting


    There is a lot of emphasis on comments here, and while I agree that the commenting style and opportunity seized by a programmer gives away a lot about the insight of programming, I just felt it necessary to add that commenting is not the holy grail either, and certainly not the final and decissive way to judge a programmer. Comments have the annoying property to become outdated. Especially when code ownership is blurry, comments in code tend to become obsolete or unapplicable. Despite all good intentions of the programmer.

    It is better to 'read the code' instead. And if you 'can`t read the code', it means that the code sucks! Plain and simple. Then it`s time to refactor, and make sure that you can read the code as if it tells you a story about what happens with data, and why. Real good programmers know how to abstract and describe this process using architectural elements and design patterns, and without the need for a lot of extra commenting.

    There is no algorithm that is so complex that it can not be put in a more or less readable form in code. Use abstractions and propper long names for classes, variables and fucntions. Describe with code what you are doing, and use small comments in-between to explain the progression within the algorithm, but refrain from writing large texts and function header comment blocks or whatever. They will become incorrect and misleading, and they are a drag to keep updated.

    The only reason where I could think of using extensive comment sections in your code is to feed document system code parsers like Doxygen or Doc++ etc.., but that is only usefull if you`re writing an API, and when you know that 90% of that API is stable and will not change much.

    --
    With great power comes great electricity bills.
  53. Re:What about version control? by Diomidis+Spinellis · · Score: 2, Interesting
    I discuss revision control systems in Chapter 6 of the Code Reading book, titled Tackling Large Projects. I also present revision control in the elective course Software Comprehension and Maintenance I am teaching. In that course students have to contribute to an open source project, in order to pass the course.

    You are right, version control should be part of "programming" education, and should probably be taught in a software engineering course.

    Diomidis