coding-style

Ruby coding style guidelines

Is there a document for Ruby that describes some preferred conventions for whitespace, indentation and other style issues? I found Python's PEP 8 to be very helpful and am looking for something similar for Ruby. ...

Function-wide exception handling in c++ - is it a bad style?

There is a try-catch thing about functions, which I think sometimes may be quite useful: bool function() try { //do something } catch(exception_type & t) { //do something } So the first part of the question: is this style considered bad in general case? And the concrete example I used this approach in: We had project with q...

Has any research been done to scientifically evaluate various coding standards?

As we all know, in the software development world there is a huge amount of passionate debate about coding standards: brackets on the same line or new line, when to indent and by how much, tabs or spaces, etc. Has anyone ever done any actual scientific research to determine what coding standards produce the "best" code (where "best" c...

Does malloc() allocate a contiguous block of memory?

I have a piece of code written by a very old school programmer :-) . it goes something like this typedef struct ts_request { ts_request_buffer_header_def header; char package[1]; } ts_request_def; ts_request_buffer_def* request_buffer = malloc(sizeof(ts_request_def) + (2 * 1024 * 1024)); the programm...

C# - whether or not to prefix private members with "m_"

I know it's a silly question but still: we're having a world war going here about whether or not to prefix private members with " _ " or " m_ ". group1 says "...it's the new microsoft suggestion for .net to camelCase your private menmbers and not put any /m prefix..". group2 (my group) says "...it's easier to see & understand that we're ...

Should I always/ever/never initialize object fields to default values?

Code styling question here. I looked at this question which asks if the .NET CLR will really always initialize field values. (The answer is yes) But it strikes me that I'm not clear that it's a always a good idea to have it do this. My thinking is that if I see a declaration like this: int myBlorgleCount = 0; I have a pretty goo...

Is it more important to get the problem done or to write programs that are easy to follow?

A guy I work with is outstanding at programming. He gets the job done quick and always has an answer to any question you may have. However, I was recently going through some of his code and found it to be really sloppy. He uses variable names like ulc and td, and he mixes instance variable declarations between method declarations. I ask...

Using 'this': where is good and where is not

I like to use 'this' statement for all non-local variables: for properties, for class variables, etc. I do this for code easy reading, easy understanding where from this variable has got. object someVar; object SomeProperty { get; set } void SomeMethod(object arg1, object arg2) { this.SomeProperty = arg1; this.someVar = arg2; } ...

Using implicitly typed local variables

I just installed a trial version of ReSharper and one of the first things I noticed is that it always suggests to replace explicitly typed local variables with implicitly typed ones, e.g: public string SomeMethod(int aParam) { int aNumber = SomeOtherMethod(aParam); // should be changed to: var aNumber = SomeOtherMethod(aParam); } I...

(0 == variable) or (null == obj): An outdated practice in C#?

Exact duplicate: Why does one often see “null != variable” instead of “variable != null” in C#? I have seen senior developers using syntaxes mentioned in the title. Is there a need for specifying a constant first in .NET? (as opposed to in C/C++ world) ...

What's a good way to structure variable nested loops?

Suppose you're working in a language with variable length arrays (e.g. with A[i] for all i in 1..A.length) and have to write a routine that takes n (n : 1..8) variable length arrays of items in a variable length array of length n, and needs to call a procedure with every possible length n array of items where the first is chosen from the...

What's the deal with a leading underscore in PHP class methods?

While looking over various PHP libraries I've noticed that a lot of people choose to prefix some class methods with a single underscore, such as public function _foo() ...instead of... public function foo() I realize that ultimately this comes down to personal preference, but I was wondering if anyone had some insight into where th...

Is there any reason to write terse code in Java/C#/C++?

Did you ever find yourself writing terse code in Java, C# or C++? If so, why? Do you think there are any situations in which this should be acceptable, given the situations in which these languages are used? ...

Private property called @this that returns this

I know that prefixing a C# string literal with @ marks it as a verbatim string literal, but what purpose would this serve if it was prefixed to the name of a private property? This came up in some code I was maintaining... public class JourneyBuilder { // ... private JourneyBuilder @this { get { return this; } ...

Should We Use Long-Name Or Short-Name in JavaScript Coding?

There is a discussion about JavaScript coding in my work group. Some people argues that we should use long-name for better readability; the others believes that short-name should be favored to same bits-on-wire. Generally, it is about coding convention. One side believes that identifier such as "fAutoAdjustWidth" is OK, while others pre...

Is it appropiate to break down a long line into two operations?

Tidying up some code that has what I consider to be a confusing line break structure: return CommonContext.HttpWebService.DownloadXml(configuration.MethodUrl(APIMethods.CharacterSheet), postData); If it were on one line it would clearly to be to long to be readable. As it...

How can I use the DRY principle to in ASP.NET MVC to refactor this code?

I have several methods in one of my controllers that does this: ViewData["Customers"] = LoadCustomers(); ViewData["Employees"] = LoadEmployees(); ViewData["Statuses"] = LoadStatuses(); etc...... Here is LoadCustomers(), but LoadEmployees, LoadStatuses and all the others are virtually the exact same logic: private static SelectLis...

What is a clean, pythonic way to have multiple constructors in Python?

I can't find a definitive answer for this. AFAIK, you can't have multiple __init__ functions in a Python class. So what is a good way to solve this problem? Suppose I have an class called Cheese with the number_of_holes property. How can I have two ways of creating cheese-objects... one that takes a number of holes like this: parmesa...

Outdated coding practices

As I do my coding I sometimes wonder if I'm doing things the best way or just the way it's always been done. Does what I'm doing make sense anymore? For example, declaring all your variables at the top of the function. If I try to declare it twice or below where I start using it my IDE will bark at me at design time - so what's the big ...

Do you use (bare) C# code blocks?

I've been programming in C# for...a while now. I coded a routine recently and it occurred to me that it was the first time (that I could recall) that I deliberately used bare code blocks (i.e. with no preceding control-flow statement). The code looks vaguely like this: //... var output = source.GetRawOutput(); { var fooItems = Foo...