coding-style

Are there any useful naming conventions for *your* constant/static variable in Cocoa?

I know that constants start with the k prefix, but does anyone have their own constant prefix, so they can easily get completion on their constants, instead of Apple's? I use a three letter prefix for my classes because of my company name, let's pretend it's OMG. I tried prefixing my constants with omgkConstantName but that isn't very s...

PHP return(value); vs return value;

Is there any difference between return($var); and return $var; other then wrapping it in parentheses? ...

How do you assign a variable with the result of a if..else block?

I had an argument with a colleague about the best way to assign a variable in an if..else block. His orignal code was : @products = if params[:category] Category.find(params[:category]).products else Product.all end I rewrote it this way : if params[:category] @products = Category.find(params[:category]).products else @produc...

how to tackle a new project

Hi, I have a question about best practice on how to tackle a new project, any project. When starting a new project how do you go about tackling the project, do you split it into sections, start writing code, draw up flow diagrams. I'm asking this question because I'm looking for advice on how I can start new projects so I can get going...

Is there a proper and wrong way to format CSS?

When I first started writing CSS, I was writing it in an expanded form div.class { margin: 10px 5px 3px; border: 1px solid #333; font-weight: bold; } .class .subclass { text-align:right; } but now I find myself writing css like this: (Example from code I'm actually writing now) .object1 {...

PHP Commercial Project Function define

Currently I am working with a commercial project with PHP. I think this question not really apply to PHP for all programming language, just want to discuss how your guys solve it. I work in MVC framework (CodeIgniter). all the database transaction code in model class. Previously, I seperate different search criteria with different fun...

Check request type in Django

While it is recommended to use the following construct to check whether request is POST, if request.method == 'POST': pass It is likely that people will find if request.POST: pass to be more elegant and concise. Are there any reasons not to use it, apart from personal preference? ...

CSS Brace Styles

I'm unable to figure how the standard (or just popular) brace style names apply to CSS. Here're all the brace styles: /* one - pico? */ selector { property: value; property: value; } /* two - pico extra */ selector { property: value; /* Start Properties on Newline */ property: value; } /* three - horstmann? */ sel...

best-practice on C header files with #ifndef #define #endif

what is concerned best practice regarding the following "pattern"? #ifndef BLAFOO_H #define BLAFOO_H /* ... * ... */ #endif /* BLAFOO_H */ how should i name the header in the #define directive? i've seen all from said BLAFOO_H to __BLAFOO_H to _BLAFOO_H_ etc.. ...

elegant way to extract values from array

Something that bugs me for a long time: I want to convert this Array: // $article['Tags'] array(3) { [0] => array(2) { ["id"] => string(4) "1" ["tag"] => string(5) "tag1" }, [1] => array(2) { ["id"] => string(4) "2" ["tag"] => string(5) "tag2" }, [2] => array(2) { ["id"] => string(4) "3" ["tag"] => str...

Factory vs instance constructors

I can't think of any reasons why one is better than the other. Compare these two implementations: public class MyClass { public MyClass(string fileName) { // some code... } } as opposed to: public class MyClass { private MyClass(){} public static MyClass Create(string fileName) { // some code....

Best practice - When to evaluate conditionals of function execution

If I have a function called from a few places, and it requires some condition to be met for anything it does to execute, where should that condition be checked? In my case, it's for drawing - if the mouse button is held down, then execute the drawing logic (this is being done in the mouse movement handler for when you drag.) Option one ...

pythonic way to do something N times

Hey! Every day I love python more and more. Today, I was writing some code like: for i in xrange(N): do_something() I had to do something N times. But each time didn't depend on the value of i (index variable). I realized that I was creating a variable I never used (i), and I thought "There surely is a more pythonic way of doing...

Contractor changing code style

I currently support an application at work that was originally written by a team of four but has now reduced to just me. We recently got a contractor in to look at some performance issues while I'm occupied with other things. While the contractor has appeared to do a good job with the performance, they have also gone through large amoun...

Why is there excessive use of whitespaces in expressions in most sample code?

A statement that could be written as: foo=(bar*5)+baz; is usually written in sample code (documentation, tutorials etc) as: foo = ( bar * 5 ) + baz; This appears to require extra work and seems counter-productive to me. Is there a rational reason for this? Is this a good coding practice, or just for sample code? (The reason I ask ...

Checkstyle for Python

Is there an application similar to Java's Checkstyle for Python? By which I mean, I tool that analyzes Python code, and can be run as part of continuous integration (e.g. CruiseControl or Hudson). After analyzing it should produce an online accessible report which outlines any problems found in the code. Thank you, ...

java style for long throws exception list

What's the Java style for formatting a long throws list? Let's say I have this: public void some() throws IOException, ClassNotFoundException, NoSuchMethodException,InvocationTargetException, IllegalAccessException { } Should it be: public void some() throws IOException, ClassNotFoundException, ...

Should I test == 1 or != 0?

Hey. I was coding here the other day, writing a couple of if statements with integers that are always either zero or one. I asked myself: Should test against 1 or 0? For example, given an int n, if I want to test if it's "true", should I use n == 1 or n != 0? Is there any difference at all? Please don't answer with stuff regarding t...

Should a connect method return a value?

I was looking at some code I've inherited and I couldn't decided if I like a bit of code. Basically, there is a method that looks like the following: bool Connect(connection parameters){...} It returns true if it connects successfully, false otherwise. I've written code like that in the past, but now, when I see this method I don't ...

How to explain to a developer that adding extra if - else if conditions is not a good way to "improve" readability?

Recently I've bumped into the following C++ code: if (a) { f(); } else if (b) { f(); } else if (c) { f(); } Where a, b and c are all different conditions, and they are not very short. I tried to change the code to: if (a || b || c) { f(); } But the author opposed saying that my change will decrease readability of the code....