Slashdot Mirror


Defining Useful Coding Practices?

markmcb writes "A NASA engineer recently wrote about his disappointment that despite having well-documented coding practices, 'clever' solutions still made the code he has to maintain hard to follow. This got me thinking about the overhead spent at my own company regarding our code. We too have best practices that are documented, but most seem to focus on the basics, e.g., comments, modularity, etc. While those things are good, they don't directly ensure that quality, maintainable code is written. As the author points out, an elegant one-liner coupled with a comment from a few revisions ago makes for a good headache. I'm curious what experience others have had with this, and if you've seen manageable practices that ultimately offer a lot of value to the next programmer down the line who will have to maintain the code."

59 of 477 comments (clear)

  1. Simple... by Anonymous Coward · · Score: 5, Insightful

    Simple, clever doesn't pay the bills, reliable and maintainable do. It's a cost benefit analysis: if that clever one-liner saves many man-hours of work, then probably a good idea. If it saves you two or three lines of code and all of an addition 45 seconds, probably not worth the blow to maintainability and readability. It's always a tradeoff...just have to decide which is more important in any given context, and at most companies, reliability and maintainability is king compared to a slight runtime increase or 45 seconds/3 lines shaved off.

    1. Re:Simple... by Coryoth · · Score: 4, Insightful

      Simple, clever doesn't pay the bills, reliable and maintainable do. It's a cost benefit analysis: if that clever one-liner saves many man-hours of work, then probably a good idea. If it saves you two or three lines of code and all of an addition 45 seconds, probably not worth the blow to maintainability and readability.

      I view it as a premature optimization issue. It's very rare indeed that you save much time in writing code by doing something cute or clever; usually it's a matter of trying to do something slickly to save cpu time or memory. Rather than prematurely optimize I find it is best to try and write things in the most clear straightforward fashion first (even if it's not the most efficient implementation) and then go back later and make things more efficient if it proves to be necessary (often it doesn't because of real bottlenecks elsewhere).

  2. multiple revision? by leuk_he · · Score: 2, Insightful

    As the author points out, an elegant one-liner coupled with a comment from a few revisions ago makes for a good headache.

    A oneline that had multiple revision should be completely rewritten, because if you manage to get multiple changes in one line it surely is a mess... somwhere.

    1. Re:multiple revision? by Toonol · · Score: 2, Insightful

      A oneline that had multiple revision should be completely rewritten, because if you manage to get multiple changes in one line it surely is a mess... somwhere.

      That makes me think of comparisons to sentence and paragraph structure. A sentence should enough words to express one thought, a paragraph enough sentences to express one idea. There are legitimate reasons to vary from those requirements at times, but in general it will improve your writing to adhere to those concepts. A lot of unreadable prose is generated by people that don't understand the function of paragraphs.

      An obvious analog is lines of code and function blocks. I write what many of you would probably consider verbose code, because I prefer a line of code to always do just one thing. I'll increment x on one line and set y=x on another line, if those are (in an abstract sense) two unrelated operations. It feels conceptually cleaner to me to keep each logical step separate.

      A one-liner seems to me to be reminiscent of one of those eighty-word sentences that you will read in a EULA... that are grammatically correct, legally precise, meticulously crafted, performs their function well, and yet is unreadable by a normal human. The lawyer that writes a sentence like that probably feels the same sense of pride that a programmer feels when they replace a twelve-line function with one clever one-liner.

  3. Literate Programming by John_Sauter · · Score: 3, Insightful

    To my mind, the best coding standard is Don Knuth's Literate Programming: http://www.literateprogramming.com/. Quoting:

    I believe that the time is ripe for significantly better documentation of programs, and that we can best achieve this by considering programs to be works of literature. Hence, my title: "Literate Programming."

    Let us change our traditional attitude to the construction of programs: Instead of imagining that our main task is to instruct a computer what to do, let us concentrate rather on explaining to human beings what we want a computer to do.

    The practitioner of literate programming can be regarded as an essayist, whose main concern is with exposition and excellence of style. Such an author, with thesaurus in hand, chooses the names of variables carefully and explains what each variable means. He or she strives for a program that is comprehensible because its concepts have been introduced in an order that is best for human understanding, using a mixture of formal and informal methods that reinforce each other.

    1. Re:Literate Programming by CortoMaltese · · Score: 3, Insightful
      Yup. I'd go as far as saying readability is more important than correctness; fixing or improving easy to understand programs is trivial compared to trying to decipher spaghetti code. You write code for other people, and that other people might be you a few years from now.

      I'm afraid you can't really force this attitude on people by using coding standards etc. though. I think it's something every coder needs to figure out for themselves. Like Pragmatic Programmer says, "care about your craft".

    2. Re:Literate Programming by joaommp · · Score: 3, Insightful

      Let us change our traditional attitude to the construction of programs: Instead of imagining that our main task is to instruct a computer what to do, let us concentrate rather on explaining to human beings what we want a computer to do.

      I'm sorry, but I can't help but disagree with this sentence. The purpose of creating a program really is to instruct the computer what to do. Not to explain other human beings what we want the computer to do.
      Computers don't do what we want, they do what we tell them to do. Success in programming a computer comes from telling it what we want it to do.
      One must not compromise the precision of the orders it gives to the computer in order to achieve readability by something/someone else than the computer. One should, however, try to make his code as readable and elegant as he possibly can without sacrificing precision in the message to the computer.
      If we program without prioritizing the computer, we may end up writing something that may look, to us and anyone else that reads it, like what we want the computer to do, but the computer my not take it that well...
      I do understand and advocate the need for excellence in coding practices. I even teach it. But as so, I am also the first to tell that we should allow the need to have others understand what we want to sacrifice the absolute necessity of having the computer understanding just as much.

    3. Re:Literate Programming by John_Sauter · · Score: 3, Insightful

      I do understand and advocate the need for excellence in coding practices. I even teach it. But as so, I am also the first to tell that we should NOT allow the need to have others understand what we want to sacrifice the absolute necessity of having the computer understanding just as much.

      I think you are misconstruing the intent of Literate Programming. It isn't Programming if you aren't telling the computer what to do. Programming becomes Literate Programming when a person, reading your code, is also able to understand what you are telling the computer to do.

      The first time I read a program, I ignore the code and look only at the comments. I hope to get a high-level understanding, so on the second read I can comprehend the details better. When I review my own code I do the same, and improve the comments if they don't convey an appropriate level of information.

    4. Re:Literate Programming by lophophore · · Score: 5, Insightful

      Many years ago, a very smart man explained to me that my customer is not the compiler. My customer is the next poor slob who has to work on the source code. Software maintenance is a fact of life. Someday, somebody is going to need to read and understand what is in your code.

      Ignore maintainability at your peril. If people on my team ignore maintainability, they become unemployed.

      Do you even know who Donald Knuth is? I'll take his advice over yours, any day.

      --
      there are 3 kinds of people:
      * those who can count
      * those who can't
    5. Re:Literate Programming by Alomex · · Score: 2, Insightful

      The purpose of creating a program really is to instruct the computer what to do. Not to explain other human beings what we want the computer to do.

      This is a very common misconception, we do want other humans to know what we want the computer to do, namely the poor saps who end up maintaining your crappy code.

      So if you think about it, there are two consumers of code: the compiler and the fellow who will maintain it for the next ten years. If you program with just one or the other in mind you are doing a darn poor job at writing code.

      One should, however, try to make his code as readable and elegant as he possibly can without sacrificing precision in the message to the computer.

      The computer doesn't complain if it has to work twice as hard to properly parse your code. So in the tradeoff between human and computer readability we must favor the human. Of course if we end up with incorrect code in the name of readability we have gone too far. I have yet to see code like this.

  4. What's worse that no documentation? by johnlcallaway · · Score: 4, Insightful

    Incorrect documentation.

    And the only correct documentation is the code itself. Anything else is a opinion and should be viewed accordingly.

    In other words, when it comes to reading documentation ... trust .. but verify!

    I don't know how many times over my 30 year career that I've read documentation and started work only to find out later that it hadn't been updated. The first standard in your documentation rules should be that all relevant documentation is created and updated before code goes into production. No excuses.

    If it doesn't have that .. then the documentation is unreliable at best, and dangerous at worst.

    --
    I rarely read replies, it's my opinion and if you thought about your opinion a little more, I'm OK with that.
  5. Review the Code by RAMMS+EIN · · Score: 4, Insightful

    Review the code that gets checked in (you are using version control, are you?). If it doesn't obviously do what it is supposed to do (including the case where it is not obvious what it is supposed to do), something is wrong.

    You can spend any amount of time writing best practices, but that doesn't ensure they are good, workable, and actually applied. In the end, what matters is if other people can understand the code. And you can measure that by just having them do that. As an added bonus, you get more people familiar with the code.

    --
    Please correct me if I got my facts wrong.
  6. Not Here! by Anonymous Coward · · Score: 1, Insightful

    I wouldn't ask here. Some of the worst code advice I've ever seen comes from Slashdot, especially from the idiots who think all programming is like their PC or Linux box. The worst are those that think Java is suitable for everything, or that what's good for the web is good for every situation.

  7. What is clear to one ... by WoodstockJeff · · Score: 3, Insightful

    I work in an environment where there are 4 programmers, of varying skill. What is "clear" to one is not necessarily clear to another, even if it is straight-forward coding. For example, I might program a loop to iterate through an array of data, extracting the important information, while another might explicitly write out each iteration, because he is less familiar with arrays. His code is quite clear - but inflexible, and subject to errors if he makes a required change to 19 of 20 copies of the calculations. Mine is quite clear, but subject to some obscuration if there's something special that has to be done in 3 of the 20 iterations.

    In the case of TFA, if the author were this other programmer, he might consider my code to be "too clever".

    Much was made of the use of "obscure" variables. Yes, that's one I dislike a lot, too... but I haven't made too much progress on that score in 25 years, with regard to others. When you're working on code that requires a lot of manipulation of a variable, typing a long, descriptive name 65 times is a bit of a PITA, and subject to its own bugs, when you misspell it a few times!

    1. Re:What is clear to one ... by Relic+of+the+Future · · Score: 2, Insightful

      typing a long, descriptive name 65 times is a bit of a PITA, and subject to its own bugs, when you misspell it a few times!

      Learn to use autocomplete.

      --
      Those who fail to understand communication protocols, are doomed to repeat them over port 80.
    2. Re:What is clear to one ... by James+Youngman · · Score: 3, Insightful

      Goodness, how did that person get hired as a programmer if they weren't already fully familiar with arrays? I wouldn't hire anybody as a programmer unless they were able to select an appropriate data structure for a problem, and explain why they selected that particular one. (Well, at my current employer, the bar is much higher than that, but I'm speaking in general).

    3. Re:What is clear to one ... by js_sebastian · · Score: 3, Insightful

      typing a long, descriptive name 65 times is a bit of a PITA, and subject to its own bugs, when you misspell it a few times!

      Learn to use autocomplete.

      If you have autocomplete then you can probably also hover your mouse over the variable to get a little window with the documentation for it... So use that, STFU, and let me use variable names I can type reliably and possibly even pronounce.

  8. Keep it simple by yog · · Score: 5, Insightful

    the op has it mostly right. There's little value in fancy show-off code that may earn a programmer some machismo points from like-minded colleagues but results in maintenance headaches down the line.

    Elegance is often misused to mean terse, clever code, but that is really far from what elegance ought to mean.

    I define good, elegant code to be code that is clear, well commented, self-explanatory, and easy to modify.

    I'm dealing right now with some "object-oriented" Perl programs that are nearly comment-free. Sure, eventually it'll start to look familiar and I'll know where to go to fix stuff, but it pisses me off at the programmer.

    I don't want people cursing and mocking my name after I've left a position, so I always strive to make my code as obvious as possible, at the cost of some high falutin' fanciness that just bogs people down and keeps a company from getting its business done efficiently.

    To me, the highest compliment is when a newbie says "I just read your {Java|Perl|C++|SQL} program and I was surprised at how easy it was to understand. I just wanted to thank you for that."

    --
    it's = "it is"; its = possessive. E.g., it's flapping its wings.
    1. Re:Keep it simple by UnknownSoldier · · Score: 5, Insightful

      > well commented,

      Many programmers don't understand that concept:

      * Code tells you 'how'
      * Comments tell you 'why'

      I have found this to be _especially_ important for bug-fixes. Very important to document why things were done a certain way to minimize implicit side effects.

    2. Re:Keep it simple by w3woody · · Score: 2, Insightful

      Absolutely spot-on. I would say, however, there is one exception: if you have code that is implementing a complicated algorithm, such as implementing insert or delete from a B-Tree using an algorithm pulled from a book like Knuth, or performing an algorithm found on Wikipedia, I'd document the original source of the algorithm. Thus while the code tells you "how", the comment tells you where you can find out more about "how".

  9. Lowest Common Denominator by Anonymous Coward · · Score: 0, Insightful

    Why should I write my code so that the lowest-common-denominator dull thud who took CSci just for the money can come along five years later and maintain it? I agree that good coding practices produce readable code. But too often this whole topic leads into the idea that all code should be McCode, meaning blah standards-conforming blocks, often not even approaching the best possible solution to a problem.

    Sorry. Not a priority. And no, I don't care that you won't hire me. You want an assembly line slinger. And will pay accordingly.

    1. Re:Lowest Common Denominator by tepples · · Score: 3, Insightful

      Why should I write my code so that the lowest-common-denominator dull thud who took CSci just for the money can come along five years later and maintain it?

      No, so that you can come along five years later and maintain it.

    2. Re:Lowest Common Denominator by middlemen · · Score: 2, Insightful

      Why should I write my code so that the lowest-common-denominator dull thud who took CSci just for the money can come along five years later and maintain it?

      No, so that you can come along five years later and maintain it.

      Or your future boss can outsource it to some peanut-salaried person in Eastern Europe or Asia or Latin America or Nigeria.

  10. communication is key, not just documentation by Kartoffel · · Score: 2, Insightful

    One person's clever, obscure trick is another person's common practice.

    Communicate with the other coders in your project. Write decent comments. TALK to the other coders. Cooperate and share ideas.

  11. Optimize all the time... for clarity by noidentity · · Score: 2, Insightful

    Optimization is fine, as long as you're "optimizing" the code to be more clear. This is a good way to redirect the energy that programmers often put into pointless performance optimization. Most understand that optimizing for one thing often de-optimizes somthing else, so they understand that you can't optimize for speed and clarity in many cases. Always looking for ways to make code clearer can become an enjoyable habit, as optimizing for speed is for many. Then you spend your idle moments eliminating many lines of code that you realize are unnecessary, bringing the code closer to its essence. My experience anyway.

  12. Rule of thumb by xororand · · Score: 4, Insightful

    "Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it."

  13. Re:Subs and functions by AB3A · · Score: 3, Insightful

    Functions have their place, Subroutines have their place. Global variables have their place. Static variables, Heap variables, and stack variables have their place. Even a Go To has it's place (in rare cases).

    The point is to use these tools in such a way that someone else can figure out what you're doing.

    On the other hand, as Albert Einstein is reputed to have said, things should be simplified as much as possible, but no farther. If the code is obtuse, sometimes it is because the programmer doesn't understand the background of what it is supposed to do. In other words, if you understand Digital Signal Processing, someone's code may look quite elegant. If you don't understand it, it looks obtuse.

    We'd all like to think that good programming is simply a matter of writing reasonably concise and well documented code. But it isn't. One needs an education on the subject being programmed. If you don't know the subject well enough, no amount of documentation is going to help you.

    --
    Nearly fifty percent of all graduates come from the bottom half of the class!
  14. All code is obscure to the incompetent. by Anonymous Coward · · Score: 0, Insightful

    for(ss = s->ss; ss; ss = ss->ss);

    Pretty standard way to traverse a linked list.

  15. Soem of the complaints aren't valid by tomhudson · · Score: 3, Insightful

    If you have a variable that is a rating of employees at an organization, organizationEmployeeRating is not a ridiculous name, while erating is.

    If you have a method that calculates the risk of an investment, name it calculateRiskInvestment(), not risk().

    erating is fine, especially if it's in a struct, to give you the context, or when it's first declared: int erating // employee rating

    risk() is also better. Since the author says its a method, it's in a class. Investment.risk() is a hell of a lot better than Investment.calculateRiskInvestment().

    The author is complaining because the code is all in c. Awww - c has different style conventions. Don't try to make it look like java.

    1. Re:Soem of the complaints aren't valid by AmberBlackCat · · Score: 3, Insightful

      "erating // employee rating" requires you to look in the comments of the declaration section in order to figure out what the variable does, whereas "organizationEmployeeRating" tells you what it is no matter where you see it in the code...

    2. Re:Soem of the complaints aren't valid by Anonymous Coward · · Score: 1, Insightful

      Exactly. Use ambiguous terms like erating if you want to ensure that other people are going to have to do global searches on erating in order to figure out what it means.

      One of the most insidious ways of introducing bugs is by loosely defining the exact the purpose and value range of a variable, and one of the first steps to reduce that problem is by using an unambiguous name. (That said, I wouldn't embed a variable's acceptable value range into its name -- that should be taken care of with unit tests and assertions, and noted in comments or docs.)

    3. Re:Soem of the complaints aren't valid by Graff · · Score: 3, Insightful

      risk() is also better. Since the author says its a method, it's in a class. Investment.risk() is a hell of a lot better than Investment.calculateRiskInvestment()

      At a glance, does Investment.risk():
      - return anything
      - change the state
      - perform validation

      Yes, Investment.calculateRiskInvestment() is overkill and redundant but Investment.risk() is not clear enough. A good compromise is Investment.calculateRisk() or Investment.getRisk(). You know what action it is taking and what it is acting upon.

      There is overkill in both directions, variables and method names that are too verbose and ones that say too little. The trick is finding a nice balance and, most importantly, having a very predictable and consistent naming convention.

    4. Re:Soem of the complaints aren't valid by tomhudson · · Score: 1, Insightful

      Riiiight ... you've never seen anyone compare a signed integer to an unsigned integer or vice versa ...

      ALWAYS look at the variable declaration and the initialization code.

    5. Re:Soem of the complaints aren't valid by Chees0rz · · Score: 2, Insightful

      This whole thread has a bunch of snotty nosed C programmers using the annoying, pretentious, absolute statement:

      "If you do/don't do X or Y, you shouldn't be touching code / aren't a programmer / should quit now / suck at life."

      Get a life. There are better ways to give advice than insulting the person while you do it.

  16. Re:Some of the complaints aren't valid by tomhudson · · Score: 1, Insightful
    And this hooter:

    The other side of C/C++ indirection is pointer logic. Pointers are powerful, but dangerous. For now I will just say try to stay away from statements like:

    int ***values;

    Instead, try C++'s STL Containers . First, the code is in C, not C++. Have fun porting the STL.

    Second, there's nothing wrong with int *** values; If you can;t figure it out, go back to java.

    and .. "OMG USE BRACKETS ALL THE TIME"

    if(test)
    do_something()

    to

    if(test)
    do_something()
    do_something_else()

    People get bitten by that once, then they learn. No brackets means a SINGLE statement. Brackets are optional, and not needed. Don't lcutter up your code because some n00b doesn't know how to read.

    I've seen people get bitten because they put brackets in - and because their indent style isn't consistent (they used 4 spaces instead of a hard tab, so when they cut-n-pasted code, "BAD THINGS(TM)" happened.

    Sure enough, the author cites Java coding conventions at the bottom of the article. c is NOT java. java is way to verbose, some of us like the clean lines of c, and think java should at least get a pre-processor so we can hide some of the typographical pollution. Even the original creator admits that the "everything is a class" model was a bad idea.

  17. Too MUCH of this by omb · · Score: 2, Insightful

    There is increasingly a meme that pretty code in a strongly typed language id OK, everything else is CRAP.

    Well no, neatly written code, in whatever, is good, as are good code and clear comments where they are needed, ie NOT,

    i = i++; //increment i

    Some of the worst code I have seen written is in C++, Java and C#. OO over the top, full of OO jargon, code that wanders around a thousand method calls without solving the problem, and TOO MUCH CODE, zillions of complex libraries and dependancies.

    Clarity, order and simplicity are the secret of writing good code, not language, tools or methodologies, and then it can be written in anything from Lisp to C#.

    1. Re:Too MUCH of this by Anonymous Coward · · Score: 1, Insightful

      i = i++; //increment i

      That code does not increment i. It's undefined. Depending on the implementation, it might increment i, leave i the same, or print out "forty-two" and abort.

  18. Teams by jlowery · · Score: 5, Insightful

    I've worked with teams a large percentage of (supposedly) hotshot programmers, and on teams with a similar percentage of mediocre talent. In my experience, it is the team with the so-so developers that deliver the more maintainable code. Why? I think it's because they know their limitations and are not afraid to "talk out the process" of writing code. They ask for feedback and opinion.

    What that leads to is a collaborative development process, where everyone has some idea of what the other is doing. And in these environments, for some reason, people take ownership and responsibility for their code, end-to-end.

    Contrast that with the hotshot teams: they know too much to ask for help, resent questioning of their implementation, mess around in other peoples domain "because they know better", and engage in heated arguments over trivial or religious matters. And in the final analysis, the lack of cooperation leads to disfunctional interfaces between components, idiosyncratic code, and incomplete functionality or low-quality performance.

    Whereas the so-so teams learn to collaborate to get things done, the hotshot teams rely on heroics: they take on too much, show exaggerated progress by declaring their code complete even though it fails edge cases, and then spend outrageous overtime fixing 'bugs' (or worse, they're too important to work on bugs, they do features, so the lesser mortals on the team get to clean up after them). Because bug fixing doesn't get scheduled up front, the schedule slides and more work hours are demanded to catch up (with decreasing effect).

    To my mind, the MAIN criteria for arriving at a maintainable codebase is OWNERSHIP, OPENNESS, and COLLABORATION. A collaborative team can do more in 40 hours than a heroic team can do in 60. They just don't look so impressive.

    --
    If you post it, they will read.
  19. Re:Linked list by TheSunborn · · Score: 3, Insightful

    While I don't understand why he did have a problem with seeing that this is a linked list traversel, I think he do have a point about fucked up naming, because in what implementation would you call the node to the next element ss instead of next, or nextNode.

  20. Code != documentation by mveloso · · Score: 4, Insightful

    As I've explained many times at work, code is not documentation.

    Code only tells you what it does. It doesn't say why it was done that way...and the "why is it doing this" is really almost always the problem.

  21. Treat comment bugs like code bugs by presidenteloco · · Score: 2, Insightful

    They are just as serious. Updating/"maintaining" a piece of code and not updating the comment
    should be made into close to a firing offense, after the suitable number of admonitions.

    The biggest weakness I find with code I review is that the programmers seem to be either
    inarticulate, lazy, smug, or exhibiting Aspergers syndrome (lack of empathy), in that they forget to
    include the most important comment for any method or class;

    The "What the h*ll is this?" comment that explains the gist of the method/class and why and
    in what contexts one should care about it.

    Also frequently missing is the considerate: "Mind the low headroom" comment.

    --

    Where are we going and why are we in a handbasket?
  22. elegant != clever by dsoltesz · · Score: 4, Insightful

    Elegant and clever are not the same thing.

    Elegant describes the algorithm or solution as using resources efficiently, having no unnecessary steps in getting to the solution, and easily readable to another programmer familiar with the subject at hand (as AB3A mentioned inre to DSP).

    Yes, if we use Webster's definition, there is a certain amount of "cleverness" in beautiful, elegant code. However, when we say "clever" we usually mean cutesy-clever, like mashing several traditional lines of code into a one-liner. That's fine for a geeky contest of "who can write this using the fewest number of characters," but (as most of us agree) does not have a place in professional code. One-liners and similar stunts might be clever, but are rarely elegant, and cause headaches for later maintainers of the code.

  23. A fool's errand by ClosedSource · · Score: 4, Insightful

    After you've been in this business for more than 20 years without giving it up to become a manager or guru, you may find that coding practices are orthogonal to code quality.

    Just do the best you can with the cards you've been given.

  24. Re:author seems somewhat confused and inexperience by Anonymous Coward · · Score: 4, Insightful

    That is bad code, or at least, hideously idiomatic.

    The variable names are apparently chosen for conciseness over legibility. The choice of 'ss' as both the loop variable and as an important field name appears deliberately confusing, especially when combined with its terseness. And the termination condition relies on the fact that a null pointer is treated as false in a boolean context - this is a non-obvious and largely arbitrary design choice in the language, and is arguably a type error.

    I've been in this game long enough to recognise a linked list traversal when I see one, but I still had to read it twice to be sure. If it had read "for (currentNode = list->firstNode; currentNode != NULL; currentNode = currentNode->nextNode)" I'd have saved valuable milliseconds. If it had gone all OO and used some iterator object in a while loop, instead of that horrible for construct, even better.

  25. Re:author seems somewhat confused and inexperience by hey · · Score: 5, Insightful

    You make some good points but

    for(ss = s->ss; ss; ss = ss->ss)

    could be better written

    for(p = s->next; p; p = p->next)

  26. Re:Some of the complaints aren't valid by Toonol · · Score: 2, Insightful

    Other than about the pointers, I tend to agree with the author rather than you on the points you mention. Which is fine, I suppose; we don't all need to program the same way, and it's probably good that we don't.

    But my thinking is that longer, more descriptive variable names are simply part of documentation; it can eliminate the need to look through the code and find the comment tied to the declaration. (Although I personally dislike the 'initial lower case, capitalize all subsequent words' standard.) As far as skipping the brackets on single line blocks after conditionals... I think that is ALWAYS a bad idea. It's a pointless, inconsistent shortcut that can cause a number of different mistakes, just for the convenience of saving two characters. Yes, you can learn to always watch for that; but that's a learned response to a problem that doesn't need to exist. Better to avoid the problem in the first place. Adding the brackets never hurts anything, it increases the consistency of your formatting, and reduces potential mistakes... that's exactly the sort of thing that belongs in a standard.

  27. b4 u were born, sonny... by airdrummer · · Score: 2, Insightful

    i worked on fortranIV code that had been migrated from fortranII that had been migrated from whatever language was used on an ibm drum-memory machine;-) there were variables named for drum memory locations, 4col...i wasn't about to change those;-)

    and those codes were written by the resident gas properties genius, who was still on the job;-) it was then i realized that the clarity of code is inversely proportional to the brilliance of the coder;-)

  28. FTFA: is called an external reference by Zero__Kelvin · · Score: 2, Insightful
    This time you got it right and added an external reference (FTFA), which is appropriate since the quote was outside of the scope. You see, when you write for readability you don't use variable names like erating and comments regarding code outside of the current scope include references to the source to which you are referring. I didn't really need you to make the point that people who think erating makes a great variable name don't know how to write for readability again, but thanks for really driving it home ;-)

    "Blame slashdot's crap UI and lack of editing capability. Other discussion boards allow for editing/correcting, why not this one?

    Yes, you are right. It is Slashdot's fault that you didn't preview before you posted, and it's the compilers fault when you don't #include the header file that resolves external references.

    "but I'm sure an Einstein like you can figure that out yourself. "

    You just run willy nilly with all the rules of writing, now don't you ;-)

    --
    Guns don't kill people; Physics kills people! - John Lithgow as Dick Solomon on Third Rock From The Sun
  29. Re:author seems somewhat confused and inexperience by noidentity · · Score: 2, Insightful

    He uses this code "for(ss = s->ss; ss; ss = ss->ss);" as an example of bad code

    I love how his example uses the most confusing variable names possible. Just using proper names should make it clear to almost anyone:

    for ( Item* p = foo->items; p != NULL; p = p->next )

  30. Re:author seems somewhat confused and inexperience by Anonymous Coward · · Score: 1, Insightful

    So you don't put up with "shit" like using widespread idioms and relying on standard language features? Sounds like a lovely place to work.

  31. Please pay attention to what Knuth said by Capt.Albatross · · Score: 2, Insightful

    Knuth is not suggesting one should be incorrect for the sake of maintainability, understandability, or anything else. In programming, if a solution is possible, there are infinitely many solutions, a few of them are easier to understand than most of them, and understanding is the key to verification and so to the writing of correct code.

  32. This is why we use Python by Sarusa · · Score: 2, Insightful

    Well - one of the reasons we use Python. I'm sure some people will consider this a troll, but it's true. Some languages encourage readability, some don't. You can write horrible, awful, unreadable Python and you can write elegant, readable, maintainable Perl (Calcium was the best I've seen in a real product), but neither is encouraged and you know what you tend to get out of your programmers.

    This means our code takes a bit longer to write up front than just banging it out in Ruby, but has the advantage that when we come back to it six months later it's still obvious what's going on (even if someone else wrote it) and it's easy to maintain.

    Ruby's great (and a close match for Python in niche and functionality, which is why I'm using it specifically), but our Ruby guys can't do that because they love to make things as concise as possible - it allows them to show how clever they are. Perl-style magic variables and punctuation everywhere. They sometimes rewrite functionality from scratch because it's easier than deciphering their (own!) old stuff, which at least unit tests help with. On the other side, Python's indentation generally limits how much you can cram into one line without gross misuse of lambdas, which frees you from the need to even try it.

    Perhaps this is self selection - maybe boring people like me who learned to value maintenance considerations (thanks to hard experience) gravitate towards easy maintenance languages, which exacerbates the effect. But I think still think your choice of language will strongly inform how maintainable your code will be; you can mandate code policies for any language, but how often (and for how long) are those actually enforced in practice?

  33. None of the complaints are valid. by mevets · · Score: 2, Insightful

    It is shorthand, and shorthand is contextual.

    If suddenly in the middle of a module to calculate the trajectory to return to earth, there is a reference to an out of scope notion like the employees rating; then a more descriptive name is very appropriate.

    If it is in a context which is doing computations about employee, department and company ratings, I would hope the person would be smart enough to use shorthand; otherwise all that annoyingly redundant crap just makes it more difficult to read and maintain.

    Why use sin() when it could be "RatioOfLengthOfFarSideOfRightTriangleToHypotenuse()"?

    How about "YCoordinateOfPointOnUnitCircleAtAngleFromOrigin()"?

  34. Re:author seems somewhat confused and inexperience by Anonymous Coward · · Score: 1, Insightful

    What happens, then, when a single structure needs to be contained in multiple lists?

    You might try to dismiss that as not a good idea, but I'm thinking in particular about some structures in the Linux kernel where this is the case...

  35. Re:Some of the complaints aren't valid by tomhudson · · Score: 2, Insightful

    You assume wrong. I've had to correct code because people coming from the Windows world NEVER turn on warnings. Many don't even know how to edit a make file. If there's no clicky thingee, they're lost.

    Anyone who cannot use make should not be writing c code. This is basic stuff. Then again, so is using ctags and fgrep.

    Ditto anyone coming from java. It's NOT the same, the mindset isn't the same, the conventions aren't the same, and someone with 10 years of java experience is totally useless when it comes to designing in c or c++. They try to make everything look like java, and they break things. And they keep on whining about needing garbage collection or smart pointers or the whole stl because they can't even write a simple linked list.

    If you can't write and manage a simple link list, a stack, a queue, and proper memory allocation in both c99 and c++, you should go back to java and leave c programming to c programmers. The guy who wrote the original article is a java programmer - look at the complaints and the references he provides.

  36. Re:Some of the complaints aren't valid by Anonymous Coward · · Score: 1, Insightful

    I look at the language standard. Look at the if() statement. It is followed by one line, and a semicolon. No parenthesis. Parenthesis allow you to group multiple lines (blockify) - and its' always possible to replace any block of code with a one-liner (either a function call, a macro, or even using the comma operator).

    Indent your code properly and you'll never have a problem with parenthesis - and your code will be more concise, thus easier to read.

    Adding brackets increases the visual pollution,

    WRONG.

    It's a familiar pattern that the brain can process much more quickly and correctly. And thus much easier to comprehend than your "on-again, off-again" use of braces that introduces two patterns when there only needs to be one.

    So, from a conceptual perspective, it's YOUR style that's more cluttered.

    and I really hate it when people put the opening brace on its own line - its a waste of a line.

    I'm sure that terabytes of storage would be wasted if you were to write all your code that way.

    NOT.

    Nice to see that you code to make your "folding editor" happy instead of the developers who have to maintain your code.

    Folding editors can handle braces at the end of the line just fine - why can't people? Concise can be clear as well.

    Well, let's see...

    Good fucking God. Because humans aren't "folding editors"!!!!!

    Because the human brain doesn't work the same way a computer does? Because a human brain sees and understands familar patterns faster and more correctly? Thereby making your code "sometimes there, sometimes not there" use of braces confusing at a fundamental level, and conceptually more cluttered than always using braces?

    Do you REALLY expect humans and "folding editors" to work the same?

    And YOU are trying to tell everyone how to write code?

    Wow.

    Just wow, someone who goes off on a rant about coding standards, and can't even defend his preferences without resorting to subjective characterizations like "cluttered"? And wonders why people can't understand code the way and editor can?

    Even nicer to see you post that under your real name. Now I know where to file your resume, if I ever see it.

  37. Re:Glad my code turned you inside-out, noob! by plover · · Score: 2, Insightful

    There is certainly a happy medium between clear code and inefficient code, but in general clear code wins on two counts. One is program correctness: when I'm reviewing clear code, I am more confident that it will do the right thing and won't cause a bug. The other is maintainability. How much time does a maintainer have to spend to understand the code in order to modify it?

    Both of those are cost avoidance strategies. It generally costs me more to have a developer messing around with complex code than it does in run-time cycles. And a bug or a crash is horribly expensive -- handling a bug means you've got phone calls to support, testing, writing up incident reports, fixing the bug, etc. A crash is way more expensive than inefficient code.

    I'm not saying "I like inefficient code." Far from it -- correctness also includes efficiency. Calling a database with a hundred individual queries from inside a loop because the developer doesn't understand how to process a multi-row recordset is horribly wrong, even though it may "look clear". But the correct version of the code doesn't have to read like a plate of spaghetti, either. Invoking the God of Efficiency doesn't magically give a developer license to write unreadable, unmaintainable code.

    Again, this comment is about code "in general." You also have to consider the problem domain. I work on a business application, with a user typing input and processing it in real time. If some rarely used path of the code makes the user stand there for an extra 62 nanoseconds, it's not a problem to me. I don't care as much about things that change response time by less than a human can perceive (8 milliseconds is the threshold for the response time of a human with extraordinary reaction times.) But if I were working on the primitives inside a graphics rendering engine, something that's going to be called a million times per frame, you can bet I'd value efficiency far higher than readability.

    --
    John
  38. Re:Some of the complaints aren't valid by tomhudson · · Score: 2, Insightful

    And proper indentation would have saved the day - no braces required.

    It's one thing python actually gets right.

    And your example just shows that a lot of people can't read code. If you're not dependent on braces to "clue you in", you'll spot the error immediately.

    Also, you NEVER should use tabs instead of spaces for indentation. What happens if someone happens to use an editor that expands tabs to something different so that code no longer lines up?

    That's a problem with YOUR editor, not mine. there's a TAB key on your keyboard for a reason.

    One of the reasons people try to use 4 spaces instead of an 8-space tab is because they let their code get fugly - WAY too many levels of indent and way too long variable names - and the only way they can see it on-screen without resorting to havig their code wrap every second line is to cheat - to only indent 4 spaces instead of a hard tab.

    They're too unimaginative to come up with concise descriptive variable, function, class, and method names, and to stupid to realize that when your code has too many levels of nesting, it's "broken by design" and a great place for bugs to breed..

    You always use the same editor? With nice, controllable GUI interfaces?

    Well, then you're nobody.

    Because until you're the guy on call who has to be able to fix things over a satellite link with 500 ms RTT and no bandwidth, you won't know why you have to write code so it's consistent no matter what editor you view it in.

    Arrogant little piss-ant, aren't we? I'm quite comfortable using vim on a remote box via ssh. I was writing code before the terms tui and gui were even in common use - we only had text screens. We had to code by pushing the bytes uphill, both ways, in a storm (well, not quite that bad, but monochrome amber monitors and hercules video cards give you an idea?). And in your example of "no bandwidth", a hard tab is better, since it's only 1 character. So who's the nobody? According to your definition, you failed since you sure wasted bandwidth. Like your examples - but at least you're consistent in that respect.

  39. Re:Some of the complaints aren't valid by tomhudson · · Score: 2, Insightful

    It's a familiar pattern that the brain can process much more quickly and correctly.

    ... only because that's all you've experienced. I have no problem parsing code that removes all superfluous braces around one-line statements, or for that matter, the classical empty for(); statement that does all the work inside the for() statement, so you terminate it with a semicolon, rather than what you would do - put some empty braces. The braces are only required for multi-line blocks in the language standard. If you can't read code that conforms to the standard, then you have the problem, not me.

    I guess you need to learn to be more fluent in the language. Next, you'll say that the comma operator should be banned because YOU might make a mistake in interpreting how it works. Or the hook operator. Or *gasp* those evil macros. And you'll also insist that we use that piece of shit called the stl when we're trying to make a multi-threaded app, and then wonder why performance is absolute crap.

    Good fucking God. Because humans aren't "folding editors"!!!!!

    Really? Next you'll say you can't read the first line of a paragraph, then skip to the next paragraph if you realize that what you're not looking at the right code. Do you move your lips when you read?

    Look, if YOU have a problem with concise code, that's your problem. Your style went out back in the early '90s. It's still taught that way because teachers are inevitably a decade behind the curve - at the minimum. Only old farts who can't adapt, the people who learned from them, people who are trying to increase their LOC count, and old koreans still put the opening brace on its' own line, or are so "un-fluent" in the language that they need to blockify everything, and can't handle something as simple as an if() without braces. What next - putting braces around the contents of case statements "just in case"? I've seen people do crap that, and it tells me one thing - they don't really know c. For them, pascal is probably a better option, since you HAVE to put begin and end ...

    Visual conciseness isn't a question of saving disk space - it's a question of making the code easier to read by reducing clutter. It works - problem is, you're so used to the crutch (yes, all those superfluous braces that the standard says aren't required ARE a crutch) of the clutter that you can't do without it. You're a crip. Admitting it is the first step in overcoming your handicap.