coding-style

SQL coding style guide

I AM SO TIRED OF READING THE WHOLE SQL STATEMENT / STORED PROCEDURE IN FULL CAPS. WHAT THE HELL WERE THE INITIAL DEVELOPERS THINKING? What's the best style (in terms of cap, indentation, lines breaks, etc) to write SQL in? ...

Tabs versus spaces in Python programming

I have always used tabs for indentation when I do Python programming. But then I came across a question here on SO where someone pointed out that most Python programmers use spaces instead of tabs to minimize editor-to-editor mistakes. How does that make a different? Are there other reasons why you would use spaces instead of tabs for P...

Use continue or Checked Exceptions when checking and processing objects

I'm processing, let's say a list of "Document" objects. Before I record the processing of the document successful I first want to check a couple of things. Let's say, the file referring to the document should be present and something in the document should be present. Just two simple checks for the example but think about 8 more checks b...

What is your "favorite" anti pattern?

By favorite I mean the one that gets your goat the most, not the one you enjoy using the most. I'm fairly new to the concept of anti patterns and I'd like a list of do not do's. An explanation of why it's an antipattern and what problems it causes would be good too. ...

Good Haskell coding style of if/else control block?

I'm learning Haskell hope it could let me getting closer to functional programming, before learing it, I mostly use C-sytanx like languages, like C, Java or D Programming Language. I followed the tutorial on Wikibook, it goes well so far, I could understand most of them before the chapter "Simple input and output" But I do have a littl...

Is Programming Style important? How Important?

Last year I was troubleshooting a team member's code and it was lacking indents and comments. I was talking to him about it telling him it was not a good idea but he got offended. He was/is smarter than me or certainly more educated. Since then I found out he applied to Microsoft and when they had him do a doubly linked list implementa...

Should Python import statements always be at the top of a module?

PEP 08 states: Imports are always put at the top of the file, just after any module comments and docstrings, and before module globals and constants. However if the class/method/function that I am importing is only used in rare cases, surely it is more efficient to do the import when it is needed? Isn't this: class SomeClass(obje...

Design & Coding - top to bottom or bottom to top?

When coding, what in your experience is a better approach? Break the problem down into small enough pieces and then implement each piece. Break the problem down, but then implement using a top-down approach. Any other? ...

Should all public methods of an API be documented?

When writing "library" type classes, is it better practice to always write markup documentation (i.e. javadoc) in java or assume that the code can be "self-documenting"? For example, given the following method stub: /** * Copies all readable bytes from the provided input stream to the provided output * stream. The output stream will...

What is a sensible maximum number of characters per line of code?

Everybody's screen size and font are different, so while my code fits on my screen, it might scroll off the right edge of your screen. What should be the maximum length of a line of code? ...

Do you prefix your instance variable with 'this' in java ?

And... we have another QAW (Quality Assurance War) on our hand. After reading the more generic "What kind of prefix do you use for member variables?" question, I tried to argue with my QA department about the advantages to always add 'this' before instance (or member) variables in java code. They are not convinced. I know there is a si...

#ifdef vs #if - which is better/safer?

This may be a matter of style, but there's a bit of a divide in our dev team and I wondered if anyone else had any ideas on the matter... Basically, we have some debug print statements which we turn off during normal development. Personally I prefer to do the following: \\---- SomeSourceFile.cpp ---- #define DEBUG_ENABLED (0) ... So...

Are booleans as method arguments unacceptable?

A colleague of mine states that booleans as method arguments are not acceptable. They shall be replaced by enumerations. At first I did not see any benefit, but he gave me an example. What's easier to understand? file.writeData( data, true ); Or enum WriteMode { Append, Overwrite }; file.writeData( data, Append ); Now I got i...

What is the best approach to centralzing error messages in an application?

All throughout an application wherever error messages (or other user messages) are used I typically hard-code a string. Obviosly this can be really bad (especially when you may have to come back and localize an app). What is the best approach to centralize these strings? A static class? Constants? An XML File? Or a combination (like crea...

Pro/con: Initializing a variable in a conditional statement

In C++ you can initialize a variable in an if statement, like so: if (CThing* pThing = GetThing()) { } Why would one consider this bad or good style? What are the benefits and disadvantages? Personally i like this style because it limits the scope of the pThing variable, so it can never be used accidentally when it is NULL. However, ...

Are multiple return points from a method good or bad?

Given a coding standard with a maximum line limit of X characters, minimising deep nesting can avoid exceeding that length. Sometimes this leads to a code that looks like: if (fail test) { return(failed) } // continue ... return(success) In general, should a method only have a single return point, or are multiple ok? Ideally, plea...

Redundant code constructs

The most egregiously redundant code construct I often see involves using the code sequence if (condition) return true; else return false; instead of simply writing return (condition); I've seen this beginner error in all sorts of languages: from Pascal and C to PHP and Java. What other such constructs would you flag in a c...

Which Coding convention to follow for PHP?

Should I stick with Sun's Java code conventions for PHP code? ...

Which syntax is better for return value?

I've been doing a massive code review and one pattern I notice all over the place is this: public bool MethodName() { bool returnValue = false; if (expression) { // do something returnValue = MethodCall(); } else { // do something else returnValue = Expression; } return re...

What's the least useful comment you've ever seen?

We all know that commenting our code is an important part of coding style for making our code understandable to the next person who comes along, or even ourselves in 6 months or so. However, sometimes a comment just doesn't cut the mustard. I'm not talking about obvious jokes or vented frustraton, I'm talking about comments that appear...