coding-style

How should I check if a flag is set in a flags enum?

Of the two methods below, which do you prefer to read? Is there another (better?) way to check if a flag is set? bool CheckFlag(FooFlag fooFlag) { return fooFlag == (this.Foo & fooFlag); } And bool CheckFlag(FooFlag fooFlag) { return (this.Foo & fooFlag) != 0; } Please vote up the method you prefer. ...

Named blocks to limit variable scope: good idea?

For years, I've been using named blocks to limit the scope of temporary variables. I've never seen this done anywhere else, which makes me wonder if this is a bad idea. Especially since the Eclipse IDE flags these as warnings by default. I've used this to good effect, I think, in my own code. But since it is un-idiomatic to the point...

How do you tell someone they're writing bad code?

I've been working with a small group of people on a coding project for fun. It's an organized and fairly cohesive group. The people I work with all have various skill sets related to programming, but some of them use older or outright wrong methods, such as excessive global variables, poor naming conventions, and other things. While t...

C++: "std::endl" vs "\n"

Many C++ books contain example code like this... std::cout << "Test line" << std::endl; ...so I've always done that too. But I've seen a lot of code from working developers like this instead: std::cout << "Test line\n"; Is there a technical reason to prefer one over the other, or is it just a matter of coding style? ...

Is constant-comparison-variable personal taste, or is there an objective reason for it?

For all of my programming life, I've always used the following notation style in my code: n < 10 "".length == 0 ptr != NULL etc. In other words, I put the variable on the left of the comparison, and the constant on the right. Recently, I've noticed - here on stackoverflow and it some third party code - people doing the opposite: 10 ...

Break statements In the real world

Been having a discussion on whirlpool about using break statements in for loops. I have been taught and also read elsewhere that break statements should only be used with switch statements and with while loops on rare occasions. My understanding is that you should only use for loops when you know the number of times that you want to lo...

Placement of instance variable declarations

I've seen some developers put instance variable declarations at the end of classes though I mostly see them placed at the top. The only reasons I can think of for doing this are stylistic preference or maybe it somehow makes them easier to work with in an IDE. Is there a more legitimate reason for choosing this style? ...

When to use a private constant?

Is it right to use a private constant in the following situation: Say I have a game with a lives variable and a startingLives variable. At the start of the game I set the lives variable to equal the startingLives variable. This is how I would normally do it: private var lives:int = 0; private var startingLives:int = 3; private functio...

Reducing coding standards 'brainwashing' duration and effort of new hires.

At my current job we're pretty strict about code quality and coding standards. All new hires go through a 'brainwashing' period in which time senior developers coach them to write (hopefully) better code. The code review process is meticulous and it normally halves the productivity of the developers doing the review. And sometimes the '...

Best approach with dynamic classes using Python globals()

Hi! I'm working on a web application that will return a variable set of modules depending on user input. Each module is a Python class with a constructor that accepts a single parameter and has an '.html' property that contains the output. Pulling the class dynamically from the global namespace works: result = globals()[classname](para...

Infinite loops - top or bottom?

In the spirit of questions like Do your loops test at the top or bottom?: Which style do you use for an infinite loop, and why? while (true) { } do { } while (true); for (;;) { } label: ... goto label; ...

Why use infinite loops?

Another poster asked about preferred syntax for infinite loops. A follow-up question: Why do you use infinite loops in your code? I typically see a construct like this: for (;;) { int scoped_variable = getSomeValue(); if (scoped_variable == some_value) { break; } } Which lets you get around not being able to see the value o...

Open file, read it, process, and write back - shortest method in Python

Hello, I want to do some basic filtering on a file. Read it, do processing, write it back. I'm not looking for "golfing", but want the simplest and most elegant method to achieve this. I came up with: from __future__ import with_statement filename = "..." # or sys.argv... with open(filename) as f: new_txt = # ...some translatio...

Automating code review and refactoring

I'm trying to do automated code review and refactoring with a tool. Something in the lines of FxCop or ReSharper. Looking for tool suggestions that meet these requirements: Low learning curve. Targeted at C# (and maybe VB.NET). Let's me easily add or customize rules (and understands entire CodeDOM). Well documented. FREE or dirt cheap...

What is the advantage of this peculiar formatting?

I've seen this format used for comma-delimited lists in some C++ code (although this could apply to any language): void function( int a , int b , int c ) I was wondering why would someone use that over a more common format such as: void function (int a, int b, in...

How to find out about the accepted coding styles

Where to read up on (the best-practice in) source code formatting, for individual languages i.e. what are the conventions for naming variables etc., for indentation and where to place braces, how and where to include comments, etc. For example for C Kernighan and Ritchie's book The C Programming Language and Linus Torvalds' Linux kerne...

To foo bar, or not to foo bar: that is the question.

This was something originally discussed during a presentation given by Charles Brian Quinn of the Big Nerd Ranch at acts_as_conference. He was discussing what he had learned from instructing a Ruby on Rails Bootcamp to many people both new to programming and new to Rails. One particular slide that stood out was along the lines of never...

Best way to implement file access in C#

Scenario - I need to access an HTML template to generate a e-mail from my Business Logic Layer. It is a class library contains a sub folder that contains the file. When I tried the following code in a unit test: string FilePath = string.Format(@"{0}\templates\MyFile.htm", Environment.CurrentDirectory); string FilePath1 = string.Format...

Vim 80 column layout concerns

I feel like the way I do 80-column indication in Vim is incorrect: set columns=80. At times I also set textwidth but I like to be able to see and anticipate line overflow with the set columns alternative. This has some unfortunate side effects -- I can't set number for fear of splitting between files that have different orders of line n...

What is the most frustrating programming style you've encountered?

When it comes to coding style I'm a pretty relaxed programmer. I'm not firmly dug into a particular coding style. I'd prefer a consistent overall style in a large code base but I'm not going to sweat every little detail of how the code is formatted. Still there are some coding styles that drive me crazy. No matter what I can't look a...