coding-style

Catching specific vs. generic exceptions in c#

This question comes from a code analysis run against an object I've created. The analysis says that I should catch a more specific exception type than just the basic Exception. Do you find yourself using just catching the generic Exception or attempting to catch a specific Exception and defaulting to a generic Exception using multiple c...

Are do-while-false loops common?

A while back I switched the way I handled c style errors. I found a lot of my code looked like this: int errorCode = 0; errorCode = doSomething(); if (errorCode == 0) { errorCode = doSomethingElse(); } ... if (errorCode == 0) { errorCode = doSomethingElseNew(); } But recently I've been writing it like this: int errorCode = ...

Show trailing whitespace on emacs only on non-empty lines

Right now I'm using: (setq show-trailing-whitespace t) In my .emacs to show trailing whitespace for my CC mode. I can't seem to figure out how to have it not show the whitespace font for whitespace only lines. Empty lines separating indenting code are sometimes indented at the code level and sometimes not indented at all, and I don't...

How to code in professional manner?

I was wondering, how a program can be written in a perfect professional style. Many times it happens that we write a very good program/code which gives the accurate output.One might use the best algorithm to solve the given problem. But for a person who is reading your code for his/her reference, it becomes difficult to unders...

Python bracket convention

What do you think is the convention that is mostly used when writing dictionary literals in the code? I'll write one possible convention as an answer. ...

Are there any plugins for Visual Studio which automatically formats code how I like?

When I open any code file, whether something i've written or something from another developer, I want it to automatically format it with my preference of bracing, indentation, line spacing, etc.. Ideally, when saving a file to disk, it would only save the formatting for code/lines i've touched. It would still display the rest of the cod...

PHP Coding styles return; in switch/case

Hi , we're trying to implement new coding style guidelines for our team, the php codesniffer is printing an warning on switch case statements when no "break" is found like: switch ($foo) { case 1: return 1; case 2: return 2; default: return 3; } is there any good reason to use : switch ($fo...

CSS: camelCase vs under_score

There is much to read out there concerning this old question. Most languages seem to have their preferred style - and everythings ok with this. But what about this question of style in the CSS context? Both is correct and looks fine: someContainerContent vs. some_container_content What do you think? Which is your and what do you th...

Which do you prefer for interfaces: T[], IEnumerable<T>, IList<T>, or other?

Ok, I'm hoping the community at large will aid us in solving a workplace debate that has been ongoing for a while. This has to do with defining interfaces that either accept or return lists of some type. There are several ways of doing this: public interface Foo { Bar[] Bars { get; } IEnumerable<Bar> Bars { get; } ICollection<Bar> ...

Migrating from one C language to another, change Style?

I find myself in conflict, regarding which code style i should follow when using a different c language. Currently i am doing work (different projects) in c++, c# and objective-c I noticed there is a lot of discrepancy in the conventions basic frameworks follow. Generally, i dont think it's a bad idea to adhere to these convetions, as ...

Coding-style: How to improve coding-styles and standards at a company

What are the best ways to improve the standards of coding-styles at a company? Let's use C# as an example here. I guess there are many differences between developers that need to be taken into consideration. The concrete ones could be education, experience and past programming-languages. How does one justify that something is right ove...

Using Objective-C Blocks

Today I was experimenting with Objective-C's blocks so I thought I'd be clever and add to NSArray a few functional-style collection methods that I've seen in other languages: @interface NSArray (FunWithBlocks) - (NSArray *)collect:(id (^)(id obj))block; - (NSArray *)select:(BOOL (^)(id obj))block; - (NSArray *)flattenedArray; @end The...

How careful are you with your return types in Objective-C?

Say you have a method that returns a newly generated NSArray instance that is built internally with an NSMutableArray. Do you always do something like this: - (NSArray *)someArray { NSMutableArray *mutableArray = [[NSMutableArray new] autorelease]; // do stuff... return [NSArray arrayWithArray:mutableArray]; // .. or [[muta...

Code style with Annotations

I can't make up my mind between @MyAnnotation public void foo(){} and @MyAnnotation public void foo(){} Is there a best-practice emerging? ...

Space between line-comment character(s) and start of actual comment

I realize that this rule might differ from one company's coding standards to another, but in general, which is preferred? With a space after the line-comment: int foo = Bar(quux + 1); // compensate for quux being off by 1 foo = Bar(quux + 1) # compensate for quux being off by 1 2. No space after the line comment: int foo = B...

How to specify type for bulk list assignments

I have the following code that looks like this [a,b,c,d] = ["a","b","c","d"] The compiler reports the warning: Warning: Definition but no type signature for 'a' Inferred type: a :: [Char] How to silence the warning and specify the type for this expression? ...

Is it code-smelly to have empty classes in the middle of a class hierarchy?

I sometimes end up with a class hierarchy where I have an abstract base class with some common functionality and a couple of implementing classes that fall into two (rarely more) groups which I want to treat differently in some cases. An example would be an abstract tree node class and different branch and leaf implementations where I wa...

When is it advisable to use a ret_val variable?

I have seen conflicting advice on whether the following code is better def function(): ret_val = 0 if some_condition(): ret_val = 2 else: ret_val = 3 return ret_val or whether this is better: def function(): if some_condition(): return 2 else: return 3 This is a simple example...

In Java, when should I use an abstract method in an interface?

I have the following interface in Java public interface IFoo { public abstract void foo(); public void bar(); } What is the difference between foo() and bar()? When should I use abstract? Both seem to accomplish what I want unless I'm missing something subtle? Update Duplicate of Why would one declare a Java interface method...

Is it better to use the link tag or the style tag to import CSS?

Or is it just a personal preference thing? What I'm getting at is, is there a specific benefit to using either method? <link href="main.css" rel="stylesheet" type="text/css"> versus <style type="text/css"> @import url('main.css'); </style> ...