coding-style

Most crucial elements in a light-weight C++ coding standard

I've been involved in developing coding standards which were quite elaborate. My own experience is that it was hard to enforce if you don't have proper processes to maintain it and strategies to uphold it. Now I'm working in, and leading, an environment even less probable to have processes and follow-up strategies in quite a while. Stil...

When did single quotes in HTML become so popular?

Recently I've been seeing a lot of this: <a href='http://widget-site-example.com/example.html'&gt; <img src='http://widget-site-example.com/ross.jpg' alt='Ross&#39;s Widget' /> </a> Is it even valid to use single quotes in HTML? As I've highlighted above it's also problematic because you have to escape apostrophes. ...

When to Use Double or Single Quotes in JavaScript

console.log("double"); vs console.log('single'); I see more and more JavaScript libraries out there using single quotes when handling strings. What are the reasons to use one over the other? I thought they're pretty much interchangeable. ...

Straw Poll - K&R vs BSD

No holy wars please - (ultimately a standardised and consistently-observed house-style on a project always wins out whatever is chosen), but I am genuinely interested in the preferences of people for K&R style formatting: public bool CompareObjects(object first, object second) { if (first == second) { return true; } else...

Which coding style you use for ternary operator?

I keep it in single line, if it's short. Lately I've been using this style for longer or nested ternary operator expressions. A contrived example: $value = ( $a == $b ) ? 'true value # 1' : ( $a == $c ) ? 'true value # 2' : 'false value'; Personally which style you use, or find ...

Should we declare a public constructor when the class is declared as package private?

I think in this case there is no need to declare a public constructor since the class is not accessible outside the package anyway. But is there some hidden impact when the class has only package private constructor? ...

Do you consider this technique "BAD"?

Sometimes you need to skip execution of part of a method under certain non-critical error conditions. You can use exceptions for that, but exceptions generally are not recommended in normal application logic, only for abnormal situations. So I do a trick like this: do { bool isGood = true; .... some code if(!isGood) b...

Case (or switch) in a for loop or for loop in a case (or switch)?

Can it be known in general whether or not placing a case within a for loop will result in bad assembly. I'm interested mainly in Delphi, but this is an interesting programming question, both in terms of style and performance. Here are my codez! case ResultList.CompareType of TextCompareType: begin LastGoodIndex := -1; ...

Should I catch exceptions thrown when closing java.sql.Connection

Connection.close() may throw SqlException, but I have always assumed that it is safe to ignore any such exceptions (and I have never seen code that does not ignore them). Normally I would write: try{ connection.close(); }catch(Exception e) {} Or try{ connection.close(); }catch(Exception e) { logger.log(e.getMessag...

What is the correct naming notation for classes, functions, variables etc in c#?

I'm a web developer with no formal computing background behind me, I've been writing code now some years now, but every time I need to create a new class / function / variable, I spend about two minutes just deciding on a name and then how to type it. For instance, if I write a function to sum up a bunch of numbers. Should I call it Su...

What to put at the top of source files in large projects?

It is necessary to put copyright information and some other stuff on top of every source file in large enterprise projects. What is your preferred template, what should be included/excluded? I am more or less thinking of this: /** * This file is part of blabla. * Created: * Changes: * * * * $Id$ * Copyright (C) YEARS BlaB Com...

Would it be bad form to put braces on the same line as the statement for single line "if" statements?

So I know it's considered somewhat good practice to always include curly braces for if, for, etc even though they're optional if there is only one following statement, for the reason that it's easier to accidentally do something like: if(something == true) DoSomething(); DoSomethingElse(); when quickly editing code if you don'...

Should you use the private access modifier if it's redundant?

Given that these two examples are equivalent, which do you think is preferrable? Without explicit modifier public class MyClass { string name = "james"; public string Name { get { return name; } set { name = value; } } void SomeMethod() { ... } } With explicit modifier public class MyClass { ...

What Delphi coding standards document(s) do you follow?

What Delphi coding standards document(s) do you follow? Our company is looking at putting some better coding standards in place, to improve our code’s readability, reviewability, and maintainability. We’ve come across CodeGear’s “Object Pascal Style Guide”, but it hasn’t been touched in quite a while and I imagine a number of people ha...

defensive coding practices

Ever since I first wrote if ($a = 5) { # do something with $a, e.g. print "$a"; } and went through the normal puzzling session of why is the result always true why is $a always 5 until I realized, I'd assigned 5 to $a, instead of performing a comparison. So I decided to write that kind of condition above as if (5 == $a...

Tab versus space indentation in C#

I sometimes find myself discussing this issue with other C# developers and especially if we use different styles. I can see the advantage of tab indentation allowing different developers to browse the code with their favorite indent size. Nonetheless, I long ago went for two space indentation in my C# code and have stuck with it ever sin...

Why does one often see "null != variable" instead of "variable != null" in C#?

Hi, In c#, is there any difference in the excecution speed for the order in which you state the condition? if (null != variable) ... if (variable != null) ... Since recently, I saw the first one quite often, and it caught my attention since I was used to the second one. If there is no difference, what is the advantage of the first o...

SQL Statement indentation good practice

Hi, What is the accepted practice for indenting SQL statements? For example, consider the following SQL statement: SELECT column1, column2 FROM table1 WHERE column3 IN ( SELECT TOP(1) column4 FROM table2 INNER JOIN table3 ON table2.column1 = table3.column1 ) How should this be indented? Many thanks. ...

Use of else after a return or break from a function or loop

This is a matter of style I've considered for a while, and I'm curious of others thoughts. The two pieces of code below are logically equivalent, which in your opinion is better style and why?: // no else some_function(): if ( my_var == 0 ) then: return true print "hello: " + my_var return false // with else some_...

Do you group private fields or put them with their property?

I've seen (and used) on various projects this layout, with a group of fields followed by a group of properties: private int MyIntField; private string MyStringField; public int MyInt { get { return MyIntField; } set { MyIntField = value; } } public string MyString { get { return MyStringField; } set { MyStringFiel...