coding-style

OO Design - do you use public properties or private fields internally?

I'm working in C# 2.0, but this would apply to most object oriented languages. When I create classes with public properties that wrap private fields, I switch back & forth between whether I should use the property or field internally. Of course C# 3.0 makes this easier with auto-properties, but it could still apply. Does it matter? pub...

Reference equality performance difference? ((object)obj1 == (object)obj2) vs. object.ReferenceEquals( obj1, obj2 )

Is there extra overhead in using the object.ReferenceEquals method verses using ((object)obj1 == (object)obj2)? In the first case, there would be a static method call involved, and in both cases some form of casting to an object would be involved. Even if the compiler balances out those methods, what about inequality? (object)obj != n...

To remove #ifdef DEBUG parts for release or not?

Hello, When releasing source code for someone else to see, when coding style is not well defined (no pun intended) do you remove the #ifdef DEBUG parts? (that is the parts that are compiled only when DEBUG is defined) If I remove it, it makes the code looks better (or me look better - do I really want someone to know I've debugged, and...

How can I write Perl that doesn't look like C?

My co-workers complain that my Perl looks too much like C, which is natural since I program in C most of the time, and Perl just a bit. Here's my latest effort. I'm interest in Perl that is easy to understand. I'm a bit of a Perl critic, and have little tolerance for cryptic Perl. But with readability in mind, how could the following co...

Apply multiple negative regex to expression in Python

This question is similar to "How to concisely cascade through multiple regex statements in Python" except instead of matching one regular expression and doing something I need to make sure I do not match a bunch of regular expressions, and if no matches are found (aka I have valid data) then do something. I have found one way to do it bu...

C# coding standards for private member variables

I saw two common approaches for coding standards for private member variables: class Foo { private int _i; private string _id; } and class Foo { private int m_i; private string m_id; } I believe the latter is coming from C++. Also, many people specify type before the member variable: double m_dVal -- to indi...

Recommended coding style for setting property values in Objective-C

Let's say I have this interface: // .h @interface DataObject : NSObject { NSString* value; } @property (retain) NSString* value; @end // .m @implementation DataObject @synthetize value @end As far as I understand, the following two snippets are identical: DataObject *o = [[[DataObject alloc] init] autorelease]; [o setValue:...

negated equal vs. not equal

Lately, I've been reading some code that has if (! (a == b) ) instead of if ( a != b ) in some places. Obviously these are logically equivalent, but I'm wondering if there is any particular reason to use one over the other. Are there certain circumstances where one is preferable, or is it all just a matter of personal style? ...

The 80 column limit, still useful?

Related: While coding, how many columns do you format for? Is there a valid reason for enforcing a maximum width of 80 characters in a code file, this day and age? I mostly use C, however this question is language agnostic. Its also subjective, so I'll tag it as such. Many individual projects set their own various coding s...

Ratio of real code to supporting code

I'm finding only about 30% of my code actually solves problems, the rest is taken up by logging, tests, parameter checking, exceptions, error handling and so on. Do you find that in your code, and is there an IDE/Editor that allows you to hide code that's not interesting? OTOH are there languages which make the support code more managea...

Thoughts on try-catch blocks

What are your thoughts on code that looks like this: public void doSomething() { try { // actual code goes here } catch (Exception ex) { throw; } } The problem I see is the actual error is not handled, just throwing the exception in a different place. I find it more difficult to debug because i...

Boolean types

During code review I discovered many places of our C# code that looks like this: if(IsValid()) { return true; } else { return false; } or even "better": return (IsValid()? true : false); I always wondered why not just write the code like this: return IsValid(); This is the way I would write this code. I ain't questioni...

Performance/Style Question About Returning from a Method/Function

I found some code like this in a project I'm working on public SqlDataReader SomeMethod(int someParam) { // ... some code goes here SqlDataReader dataReader = m_command.ExecuteReader(CommandBehavior.CloseConnection); return dataReader; } I was wondering what is better, the original or below public SqlDataReader Some...

Best practice: setting HTML classes in a webapp

In every webapp I build, I come across this issue. I want to set classes on HTML elements conditionally. For example, sometimes the <div> for a post looks like: <div class="post">...</div> And sometimes it looks like this: <div class="post even recent replied_to author_is_admin">...</div> Each class after post is there because some...

Checking in of "commented out" code

Ok, here is something that has caused some friction at my current job and I really didn't expect it to. Organized in house software development is a new concept here and I have drawn up a first draft of some coding guidelines. I have proposed that "commented out" code should never be checked into the repository. The reason I have stated...

Internal typedefs in C++ - good style or bad style?

Something I have found myself doing often lately is declaring typedefs relevant to a particular class inside that class, i.e. class Lorem { typedef boost::shared_ptr<Lorem> ptr; typedef std::vector<Lorem::ptr> vector; // // ... // }; These types are then used elsewhere in the code: Lorem::vector lorems; Lorem::ptr lorem(...

C++ getters/setters coding style

Hi, I have been programming in C# for a while and now I want to brush up on my C++ skills. Having the class: class Foo { const std::string& name_; ... }; What would be the best approach (I only want to allow read access to the name_ field): use a getter method: inline const std::string& name() const { return name_; } make...

Do you use 'this' in front of instance variables?

When accessing instance variables or properties of a class from within the class itself, do you prepend them with "this."? ...

C++ friend classes

Hi, I realize that there are a lot of questions regarding friend classes in C++. My question, though, is tied to a specific scenario. Given the below code, is it appropriate to use friend in such a manner? class Software { friend class SoftwareProducer; SoftwareProducer* m_producer; int m_key; // Only producers can pro...

How many nested code blocks do you end up with in practice when coding?

I just finished writing a function that has ended up with nested code blocks something like the following: class ... { void Method() { while (...) { ... switch (...) { while (...) { switch (...) { if (...) { } } } } } } } Do you find this is stan...