coding-style

Standalone java base java formatter

Is there a Java-based program(jar) that can format java code(indenting/spacing). If it would be configurable that would be great. ...

What's a good name for an Enum for Yes & No values

Background In a C# command-line app I'm writing, several of the parameters have "yes" and "no" as the possible values. I am storing their input using the Enum type shown below. enum YesNo { Yes, No } Which is fine - the code works. No problem there. NOTE: Yes, I could store these as bool (that's how it used to work). My ...

C# coding conventions

Duplicate of: Naming Conventions in C# Where can I find the most common and up to date coding conventions for C#? ...

if (condition) continue; OR if (!condition) { ... }? (style preference)

I know this is a matter of style, hence the subjective tag. I have a small piece of code, with two nested conditions. I could code it in two ways, and I'd like to see how more experienced developers think it should look like. Style 1: while (!String.IsNullOrEmpty(msg = reader.readMsg())) { RaiseMessageReceived(); if (parseMsg) ...

Tools to enforce a coding style for Javascript

I need to automatically check the style of javascript sources written by different people. Do you know of a good tool to do it? Integration with emacs would be a plus. Thank you in advance. ...

What are the pros and cons of putting as much logic as possible in a minimum(one-liners) piece of code?

Is it cool? IMO one-liners reduces the readability and makes debugging/understanding more difficult. ...

Java: What is the preferred Throwable to use in a private utility class constructor?

Effective Java (Second Edition), Item 4, discusses using private constructors to enforce noninstantiability. Here's the code sample from the book: public final class UtilityClass { private UtilityClass() { throw new AssertionError(); } } However, AssertionError doesn't seem like the right thing to throw. Nothing is bei...

Why might one also use a blank constructor?

I was reading some Java recently and came across something (an idiom?) new to me: in the program, classes with multiple constructors would also always include a blank constructor. For example: public class Genotype { private boolean bits[]; private int rating; private int length; private Random random; public Genotype() { ...

How do you style major and minor comments?

Sometimes I want to write a "major" comment to describe a large block of code and then write "minor" comments to describe a few of the lines within that block of code: // Major comment // Minor comment ... // Minor comment 2 ... The major comment looks strange without code directly beneath it, and you can't visually tell how much co...

Do you prefer "if (var)" or "if (var != 0)"?

I've been programming in C-derived languages for a couple of decades now. Somewhere along the line, I decided that I no longer wanted to write: if (var) // in C if ($var) # in Perl when what I meant was: if (var != 0) if (defined $var and $var ne '') I think part of it is that I have a strongly-typed brain and in my mind, "if...

Function and class documentation best practices for Python

I am looking for best practices for function/class/module documentation, i.e. comments in the code itself. Ideally I would like a comment template which is both human readable and consumable by Python documentation utilities. I have read the Python documentation on docstrings: http://docs.python.org/tutorial/controlflow.html. I underst...

In Java, why do people prepend fields with `this`?

When referencing class variables, why do people prepend it with this? I'm not talking about the case when this is used to disambiguate from method parameters, but rather when it seems unnecessary. Example: public class Person { private String name; public String toString() { return this.name; } } In toStr...

Indent style names

I've seen this questions here. I'm wondering if there exists an offical name for the following indent style: void fooBar(String s) { while (true) { // ... do something } } When the opening brace is in the same line as the control statement, the statments within are indented and the closing brace is on the same iden...

Python: Alter elements of a list

I have a list of booleans where occasionally I reset them all to false. After first writing the reset as: for b in bool_list: b = False I found it doesn't work. I spent a moment scratching my head, then remembered that of course it won't work since I'm only changing a reference to the bool, not its value. So I rewrote as: for i i...

Coding style checker for C

I'm working for a company that has strict coding style guidelines but no automatic tool to validate them. I've looked around and the only tools I could find were lint like tools that seem to be aimed at verifying what the code does, and preventing bugs and not at making sure the coding style is correct. What tool should we use, if at al...

Setting a boolean value based on an integer.

I found this statement is some old code and it took me a second to figure out... IsTestActive = (TestStateID == 1 ? true : false); Please correct me if I'm wrong but isn't this the same as this one?: IsTestActive = (TestStateID == 1); If it is, why would you ever want to use the first? Which one is more readable? (I think the latt...

How do you resolve the discrepancy between "StyleCop C# style" and "Framework Design Guidelines C# style"?

After going through the Appendix A, "C# Coding Style Conventions" of the great book "Framework Design Guidelines" (2nd edition from November 2008), I am quite confused as to what coding style is Microsoft using internally / recommending. The blog entry A Brief History Of C# Style claims: In fact, the differences between the "StyleCo...

CSS coding style

Are there any good CSS coding style/standards? ...

Are there any HTML coding conventions/style/standard

I mean to name ids, names, values, etc? ...

How to manage FxCop overwhelming reports

I've recently started using it. However, after running it against one of my company's largest project. It turns up mountains of problems. The list of problems was so overwhelming it would take days to find and fix some, if not all of the stuff. Now, I know that it's not very practical to fix everything FxCop tells you to fix. But as I ...