Slashdot Mirror


What Workplace Coding Practices Do You Use?

Agent_9191 asks: "Recently I've been promoted to what essentially amounts to a project lead role for every project we do, in house. Since my company has run for the past 35+ years with no form of central IT department, there has been no standards put into place for developers to abide by. One of my tasks is to set up standards in how projects will be implemented and produced. Right now I'm more concerned about trying to set up coding standards, so that any developer can jump into any part of a project and be able to figure out what's going on, without wasting a couple hours just to figure out the code. I've come across some documents in this area from a few sources (of course can't remember them off the top of my head). What practices/standards do you use in your workplace?"

15 of 682 comments (clear)

  1. Joel on Software by Marxist+Hacker+42 · · Score: 4, Informative

    Has several excellent articles on the subject This is about as good of a starting place as any.

    --
    SJW: a person who perceives an injustice, and while correcting it, commits a greater injustice.
  2. Re:Comments by mopslik · · Score: 5, Informative

    ...be sure that they make them good comments.

    And make sure they update comments if changes necessitate it. There's nothing worse than reading through a function's description, complete with well-documented inputs/outputs/conditions/etc. and finding out that those things no longer apply because somebody changed a 1 to a 2.

  3. code complete has some good things to say by blackcoot · · Score: 5, Informative

    on this particular subject. i believe code complete 2 came out "reasonably recently". that said, were this my task, i'd say the following:

    1) document things thoroughly using a tool like doxygen. there is no excuse for interfaces not to be thoroughly documented
    2) adopt a standard naming convention. in java, this is easy -- just use the default. in other languages, you'll probably have to make your own up.
    3) pick an indentation style. it really doesn't matter which since tools like indent can convert between them almost painlessly. all code that goes into the repository is run through indent to put it into a standard format
    4) require that code compile cleanly with no warnings at the most anal retentive compiler settings before it can be checked in unless there are good reasons to ignore the compiler warnings
    5) average devs are only able to commit to the "head" fork (or equivalent in your sccs). the code is not committed to the "real" fork until it passes whatever tests you have
    6) incorporate tools like valgrind into your testing cycle --- they should come back largely clean. if they don't, things need to be fixed unless there's a really good reason not to.
    7) people who check in code which breaks cvs or, upon a code review, are found to not sufficiently adhere to your guidelines owe their dev group donuts.

  4. Re:Comments by dorkygeek · · Score: 5, Informative
    For example:
    if x==456 then //checks for conditional x and executes code if x is true
    - this is not a good comment
    if x==456 then //checks to see if x is equal to 456. If it is, then the code within is executed
    -this is a good, easy to understand comment.

    Is this supposed to be a joke??! Both of them are worst comments, because they only formulate in english what the code already says by itself. Everyone can see that this is an if-statement, everyone is able to identify the condition, and everyone knows the semantics of an if-statement.

    A good comment is not describing what is done (since everybody can see that from the code itself), a good comment describes why something is done, or what the overall objective of the statement is.

    For example:

    if (x == 456) { // Check if step motor reached final position. If yes, halt motor, otherwise step forward.

    This is ways more useful. Even more useful would be to already use self-describing symbol names in the code itself, like

    if (currentPosition == finalPosition) { ...
    --
    Windows is like decaf - it tastes like the real thing, but it won't get you through the day.
  5. #defun is sooo 70s... by PaulBu · · Score: 3, Informative

    enum is your friend!

    enum {LOAD_DATA, SAVE_DATA, DESTROY_DATA} ...

    Paul B.
    P.S. And of course my all-caps enum values were considered too lame by lameness filter... ;-(

  6. Ahahahahaaa... heh... Snrrrrrkkk. Kidding, right? by pla · · Score: 5, Informative

    without wasting a couple hours just to figure out the code.

    A couple hours???

    Look, no offense, but you either only deal in "toy" code, or you have such high expectation that you will fail, and quite spectacularly.

    A new coder, even an experienced one, takes days or even weeks after coming into an existing project before he can contribute anything but the most trivial of changes. For a truly massive project, or one that requires intimate domain-specific knowledge in a niche industry, extend that to months.

    If you can find a way to get an unfamiliar newcomer up to speed on any "real" project in a matter of hours, consider your talents wasted in your current position.

  7. Re:Comments by naibas · · Score: 5, Informative
    Both of them are worst comments, because they only formulate in english what the code already says by itself. Everyone can see that this is an if-statement, everyone is able to identify the condition, and everyone knows the semantics of an if-statement.

    A good comment is not describing what is done (since everybody can see that from the code itself), a good comment describes why something is done, or what the overall objective of the statement is.

    Amen to that.

    In addition to the original comments being redundant, there's also the issue of the code and the comments getting out of sync...

    The company I work for just wrote up a formal coding standard, which includes everything from a guide to our internal hungarian notation, indentation guidelines, and even which C++ features/paradigms are supported, frowned upon, or not allowed. All the coders got a chance to send in feedback before it was finalized, and we even ended up with a list of recommended reading on the subject, including:

    • Sutter & Alexandrescu's C++ Coding Standards,
    • Meyer's Effective C++,
    • Meyer's More Effective C++, and
    • McConnell's Code Complete.

    The idea is to keep the code readable and maintainable with the least amount of re-invention of the wheel. With good coding practices, it's easier to avoid bugs in your own code and spot them in others (reviews are also a big plus on both counts). And it gets any religious battles out of the way up front, so you don't have to waste time bickering later on.

  8. For .Net, here is what we use by cyberjessy · · Score: 4, Informative

    C# - The C# Coding Style Guide, Mike Krueger(SharpDevelop). This is probably the most widely used one (Novell). It largely agrees with Microsoft's internal coding standards, with a few exceptions.
        VB - .Net Coding Standards, part of the SDK. This is not comprehensive though, like the C# doc mentioned above.

    Version Control -
        Server: Subversion + Apache
        Client: Tortoise SVN (Excellent) [We also use Perforce, CVS, VSS(Commercial apps)]
    Continuous Integration - Cruise Control.Net
    Intranet, Knowledge Management - DotNetNuke (www.dotnetnuke.com)
    Project Management - dotProject (PHP) (www.dotproject.com), MS Project
    Unit Testing - NUnit (www.nunit.org)

    --
    Life is just a conviction.
  9. Code Complete by pigwin32 · · Score: 3, Informative

    Steve McConnell's Code Complete is an excellent source for coding standards and a good read for any developer. I don't agree with everything in the book but it is comprehensive. Ignore that it's published by Microsoft Press, it's a good one.

  10. Re:Comments lie by Lehk228 · · Score: 5, Informative
    Comments lie. Code never lies.
    I beg to differ
    --
    Snowden and Manning are heroes.
  11. Pragmatic by dividius · · Score: 3, Informative

    Read and apply the Pragmatic Programmer.

  12. Domain Driven Design by Symbiot · · Score: 3, Informative

    If you want code that can be readily understood you need something other than coding conventions. Coding conventions only make the code all look the same. You can get almost all of the benefit (without most of the arguments) by saying only that each file must be written with the same coding convention throughout. The code will get prettier and prettier with tighter conventions and developers will waste less time reformatting each others code. But it won't do a thing to make your project more understandable.

    For that you need an understandable design and the best advice I've ever seen for that is in Eric Evens' "Domain Driven Design" http://domaindrivendesign.org/. The advice there will work for both Agile and non-Agile projects and its core themes are pretty much unavoidable truths about how to write code for a project that is also written about the project: use language that comes from the projects domain, insulate code from each domain or sub-domain from the rest of the world, keep each method at the same level of abstraction (that's big) and make implicit concepts explicit, to name a few. The key is for your developers to consider themselves to be authors and to strive to keep each little piece of code they write on-topic. Not only will it be easier for new developers to come up to speed but the code will work a heck of a lot better too.

  13. Re:Comments by shinghei · · Score: 3, Informative

    But the generated document is only as good as the comment itself, isn't it? Correct me if I am wrong, I don't think any of the tools that you mentioned can generate any intelligent comment. What good is the comment "The openSocket method throws a SocketException"?

  14. Re:Comments by JulesLt · · Score: 3, Informative

    Which is why we would say :
    1) Don't use variable names like x, use something that indicates the meaning of the variable - so maybe in this case lErrorStatus
    2) Don't hardcode magic values, use a meaningful constant - which also means only one point in code needs to be changed if we switch CPU and find there is a different on-fire code

    Then we get

    if lErrorStatus == CPU_ON_FIRE then

    Which I think is pretty self-explanatory.

    One thing that isn't clear yet is whether this bit of code is sitting in a little function or part of a big block - i.e. what it's going to do on the 'then'.
    Now at some future point we might find ourselves needing to check CPU_GETTING_QUITE_HOT. So we will still have to impact all occurrences of
    lErrorStatus == CPU_ON_FIRE to add an extra test.
    This leads to

    3) Encapsulate tests in meaningfully named BOOLEAN functions.

    I'd say it is swings and roundabouts as to whether (3) is worth the effort.

    If the _cpu_on_fire(ErrorStatus) test is just done once, then we've had to go to a lot of effort to encapsulate a single line of code (as the expression is already boolean). If it is used >1 times it is definitely worth wrapping, ditto if the expression is complex - just to make sure developers get into the habit, as much as to protect against any change. (I think it's better to get into a slightly more time-consuming habit of doing the right thing and break it occasionally, than into the habit of doing the quickest thing and having to think to do the right thing).

    The other principle that points to (3) is the one that blocks of code should be 'readable' and if possible a block should fit on one page - i.e. when you have an
    if . . then . . .else . . end structure and the bodies of all the elements are large then each should be a procedure/method itself.
    Again, _cpu_on_fire(ErrorStatus) isn't a good example as we'd be replacing one line with one line.

    However, if you have a standard logging mechanism (say log4j), and template code for functions/methods, you can use this encapsulation to record the fact that the test occurred, input and output values, rather than leaving it to the individual developer to remember to add logging before their 'if' and on each conditional path.

    --
    'Capitalists of the world, unite! Oh ... you have' (League Against Tedium)
  15. looong functions by namekuseijin · · Score: 4, Informative

    "I once came across a ~1400 line function"

    This is so fucking wrong and wicked the programmer who did it should go straight to hell. I'm sure there's no functional coesion in there: most likely there are many disparate tasks that should be each in its own function and called from there. I'm sure there is a lot of cut-n-paste in there that should be each in its own function and called from there.

    I'm sure you can guess where i'm willing to get to... more important than hungarian notation, comments or documentation in PDF format is abiding for these 2 simple rules: KISS -- keep it simple, stupid -- and DRY -- don't repeat yourself. Once you do it, coding and reading code is a lot easier.

    So, my advice:
    * Give meaningful names to important, global, business rules variables ( local variables like i or c are ok, since they are mostly irrelevant ) or functions/methods/procedures/subroutines
    * Write short, highly coesive functions/methods/procedures/subroutines
    * Stop the cut-n-paste madness! If you do it a lot, it's obvious the copied code if begging to be parametrized and be given a name. Programmers altering your original code will be thankful
    * Write modular code, not a plain, huge, stupid monolithic wall of letters. Even in languages with no namespace support ( C/PHP etc ) a good naming convention for functions of a certain module/header can do wonders...

    * and please: meaningful names don't mean phrase-like names like thisLocalVariableIsCool. Conciseness go a long way towards good readability...

    --
    I don't feel like it...