coding-style

Style: objective c and token concatenation

This is a style question: Because Apple reserves the "_" privatization for its keywords, I was thinking of something along the lines of the following: #import <Cocoa/Cocoa.h> #define _(name) pvt_##name @interface SFMeasureViewController : NSViewController { @private NSTextField *_(label); } @property (retain) IBOutlet NSTex...

Using spaces before and after the `=` sign of HTML element attribute

I am wondering if there's a "best" approach to having a space before and after the equal sign in a HTML page when you write it down. It seems like no one is using this, but for me it seems fairly natural coming from programming languages that have this printed in their basic code style. So, is there any standard that you have to not use ...

How do I declare the version number for a Perl module?

I thought I knew how to declare version numbers for modules. But after reading the article "$VERSION Confusion" at Modern Perl Books, a Modern Perl Blog; I'm now more confused than I started. (Ignorance was indeed bliss.) Not that I have hangups about "perfect" code but I'm just really curious why such a trivial matter apparently has no ...

Best way to force a Java class to be loaded

What is the best and cleanest way to do this? Specifically, I need some code in a static initializer block to run in that class, but I'd like to make this as clean-looking as possible. ...

If 'else' is about to happen anyway should it be declared or not?

Possible Duplicate: Should else be kept or dropped in cases where its not needed? When a = 0 This: var foo = function() { if (a != 0) return true return false } Or this: var bar = function() { if (a != 0) return true else return false } ...

Stylistic Issue - to separate functions or not?

I'd like to know if there is any consensus on what is better practice in this general statement type. if(CouldDoSomething()) DoThatThing(); else WriteErrors(GetTheErrorsThatWouldHaveResulted()); This may require twice as much work .as you have to check for potential problems, then either do the thing you wanted to do or go b...

Would you write this code the same way using OOP PHP ?

Hello everyone, I'm trying to go oop with one of my scripts. It's a contact script which deals with encodings when sending an email through jQuery's ajax function. I wanted to make the user able to use the same script with two forms in the same page and making it an easy job. Now I've made a prototype of how it's going to be rewritte...

Documenting getters and setters

For simple getters/setters, like the one below, what's the best way to document it? public float getPrice() { return price; } I'm pretty strict about coding standards, so my IDE warns me about any undocumented public/protected methods. Option 1: /** * Get the price field. * * @return */ Option 2: /** * @return Price */...

Best Practice: if(foo== false) or if(!foo)

Possible Duplicate: What is the preferred way to write boolean expressions in Java Today, me and my colleague raked up an argument. Which is a better way to use the boolean variables in Java code along with if statements. boolean foo=true //1. if(foo == false) // do something else // do something else //2. if(!fo...

x or y: acceptable idiom, or obfuscation?

I have to extract values from a variable that may be None, with some defaults in mind. I first wrote this code: if self.maxTiles is None: maxX, maxY = 2, 2 else: maxX, maxY = self.maxTiles Then I realized I could shorten it to: maxX, maxY = self.maxTiles if self.maxTiles is not None else (2, 2) But then I realized this migh...

return value style question

Lets say I have the following code public static string GetXMLValue() { XDocument settingsFile = XDocument.Load("Settings.xml"); return settingsFile.Element("Settings").Element("GenericValue").Value; } It simply reads an XML Settings file and returns the GenericValue value. It can't be any simpler than that. Now my question ...

Reduce casting and better styling

Consider the following (nasty) code: /// <summary> /// Calls matching process error code on response.Code /// </summary> /// <param name="response">Actually will be of type Response or extend it</param> /// <returns>true for successful response, false otherwise</returns> private static bool ProcessErrorCode(objec...

Java Coding Conventions: Getters & Setters

Why is it convention to place getters and setters after constructors within classes? I would rather see them placed immediately after class fields, before the constructors, in order to see which of the private fields are accessible via getter & setter methods. Especially if the methods' bodies are single return or assignment statements...

Premature optimization in Java: when to use "x = foo.getX()" vs simply "foo.getX()"

When I find myself calling the same getter method multiple times, should this be considered a problem? Is it better to [always] assign to a local variable and call only once? I'm sure the answer of course is "it depends". I'm more concerned about the simpler case where the getter is simply a "pass-along-the-value-of-a-private-variable...

Which combination of tags should I use?

In my form I have 3 styles of text: styleA (size 16 and bold, more like a header) styleB (size 16, normal weight, the normal text) and styleC (size 14 and italic, a note at the end). Should styleA and styleB use <h?> tags and styleC use a <p> tag? Or should styleA use an <h?> tag, styleB use a <p> tag, and styleC use a <small> tag? Or i...

An interesting detail about variable name

I have read tutorials all over the web with different kinds of tutorials specified on game (however, this turns out to be pretty general). Are there any reasons to why many developers name their variables like: mContext For me it is default to just name it "context" or something similar. Are there any reasons why the "m" are before?...

Whats the cleanest way to code logic for search, create if not found, search again

I have been writing code like the following enough a lot lately. I don't like the duplicate code in the else block. Is there some obvious thing I'm missing? I pondered 'goto' but abandoned it when I saw an infinite loop possibility. I know the obvious thing to do is create a separate function. The reason I hesitate is because, like ...

Is it simpler to group tests or to keep them separate?

I just had a discussion with a colleague where we disagreed about which of the following snippets was simpler: public boolean foo(int x, int y) { if (x < 0) return false; if (y < 0) return false; // more stuff below } OR public boolean foo(int x, int y) { if (x < 0 || y < 0) return false; /...

Is this Macro Abuse?

I was reverse engineering some code and came across this... /************************************************************************/ /* */ /* MACRO CHECK_FREAD */ /* ...

When to use 'if…else if' and when to use

1) I know how if…else if statements work, but in the next example both methods are identical as far as the resulting value is concerned. So does it matter which of the two methods I use or should I always go for one that is semantically closest to what the code is trying to do ( here I’m guessing that semantically the two methods are qui...