coding-style

C# Nested null checks in if statements

I have a question on programming style and C# language design in general, I'd love to know if there is a better way to do what I'm doing. If you have a complex data object, with properties that can be null but you want to check or operate on data if it is there, you cannot write a line like so if(Myobject.MyNestedObject != null || Myob...

C++ Style Convention: Parameter Names within Class Declaration

Hello. I'm a fairly new C++ programmer and I would like to hear the arguments for and against naming parameters within the class declaration. Here's an example: Student.h #ifndef STUDENT_H_ #define STUDENT_H_ #include <string> using namespace std; class Student { private: string name; unsigned int age; float h...

Do Scala libraries follow the same inverted domain convention for naming packages as Java?

I'm looking to write a small Scala library to get a feel for its Actor programming model. In the example code I've come across, some libraries use inverted domain (e.g. org.foo.bar) for packages and some do not (maybe just for brevity). Is it advisable for Scala libraries to use the same package naming conventions as Java? More ge...

Why are all-caps constant considered bad coding style?

Jeff finished his post talking about this, but I don't catch the idea. So, why do you think this is a bad coding style? EDIT: I, as a lot of you, don't think that it is a bad coding style. But Jeff is far better programmer than me, and his point of view turned on lights on my head to get answering if I was wrong. I was a Delphi devel...

How do you avoid redundancy in documentation comments?

We just started using StyleCop and the one thing I'm having a hard time with is the documentation requirements. I don't want to debate the usefulness of the tool, I'm just wondering if anyone has any guidelines or ways of thinking about documenting methods that makes the comments actually useful. I find that my comments often contain a l...

In Python, can you call an instance method of class A, but pass in an instance of class B?

In the interest of reusing some existing code that was defined as an instance method of a different class, I was tying to do something like the following: class Foo(object): def __init__(self): self.name = "Foo" def hello(self): print "Hello, I am " + self.name + "." class Bar(object): def __init__(self): self.name =...

One line functions in C?

What do you think about one line functions? Is it bad? One advantage I can think of is that it makes the code more comprehensive (if you choose a good name for it). For example: void addint(Set *S, int n) { (*S)[n/CHAR_SIZE] |= (unsigned char) pow(2, (CHAR_SIZE - 1) - (n % CHAR_SIZE)); } One disadvantage I can think of is that it...

Are there any free cmd-line scripts which can re-format PHP source-code?

I'm using a pre-commit hook to lint-check PHP source submitted by our staff, and everyone's really happy with the results. The staff have agreed it would be useful to have the code re-formatted to follow (customizable) style rules on commit. Are there any FOSS scripts which can do this from the command-line? ...

Why would you write something like this? (intentionally not using delete [] on an array)

I came across this kind of code once in a while - I suspect the creator is/was afraid that table delete would iterate over the table and "cost performance" (which imho will not be done either way)... is there any real benefit one might get/consider/imagine from not using the the table delete here? myClass** table = new myClass* [size]; ...

Learn proper programming syles?

Hello, Where would one find information or just rather good ideas on what is considered proper programming ethics or methodology of how to format the code, comments, or even variable names so that it is easier to read the code at a later time? Thanks! ...

Indenting #defines

Hi all, I know that #defines etc. are normally never indented. Why? I'm working in some code at the moment which has a horrible mixture of #defines, #ifdefs, #elses, #endifs, #etc. All these often mixed in with normal C code. The non-indenting of the #defines makes them hard to read. And the mixture of indented code with non-indented...

C++ iterators & loop optimization

I see a lot of c++ code that looks like this: for( const_iterator it = list.begin(), const_iterator ite = list.end(); it != ite; ++it) As opposed to the more concise version: for( const_iterator it = list.begin(); it != list.end(); ++it) Will there be any difference in speed between these two conventions? Naively the...

How to make developers follow coding standards?

How can I make developers follow coding standards? In our company: I've given documents and they don't have the patience to read it and follow it. I've tried telling them again and again "please do it this way" they nod their heads, but still do it the wrong way We're doing a project for the third time and still they don't seem to fo...

Algorithm for neatly indenting SQL statements (Python implementation would be nice)

I'd like to reformat some SQL statements that are a single string with newlines in to something that's much easier to read. I don't personally know of a good coding style for indenting SQL - how should nested queries / where clauses / left joins / etc by represented to maximise readability? Has anyone seen a pretty-printing algorithm t...

Most readable way to write simple conditional check

What would be the most readable/best way to write a multiple conditional check such as shown below? Two possibilities that I could think of (this is Java but the language really doesn't matter here): Option 1: boolean c1 = passwordField.getPassword().length > 0; boolean c2 = !stationIDTextField.getText().trim().isEmpty(); boo...

Char Array vs String: which is better for storing a set of letters

I need to store in a constant class 4 letter of a code. I can do: static final String CODE_LETTERS = "TRWAG"; or static final char[] CODE_LETTERS = {'T', 'R', 'W', 'A', 'G'}; After, I can obtain one of that characters in two ways: final char codeLetter = CODE_LETTERS.charAt(index); or final char codeLetter = CODE_LETTERS[index]...

Using empty parent class just to group other classes. Is there a better approach?

I have several classes that conceptually belong to one tier. They have no common properties which is why they have no need to inherit from some base class. I also have a few methods that deal with these classes. Some of them are templates of the following style: public SomeClass { public void SomeMethod<T> (T argument) where T : Ba...

Which coding style is more common?

In no way shape or form am i advertising/promoting my programming style, but as far as 'multiple variable declarations' are concerned, which case is more acceptable professionally and commonly: case 1: private $databaseURL = "localhost" ; private $databaseUName = "root" ; private $databasePWord = "" ; private $databa...

What's the most efficient way to determine whether an untrimmed string is empty in C#?

I have a string that may have whitespace characters around it and I want to check to see whether it is essentially empty. There are quite a few ways to do this: 1 if (myString.Trim().Length == 0) 2 if (myString.Trim() == "") 3 if (myString.Trim().Equals("")) 4 if (myString.Trim() == String.Empty) 5 if (myString.Trim().Equals(Strin...

To Do or Not to Do: One Line if Statements and Curly Braces

Duplicate: Why is it considered a bad practice to omit curly braces? Hey folks, Just curious on your thoughts about the below: if (someCondition) { callSomething(); } vs. if (someCondition) callSomething(); I only ask this because when I started using refactoring tools, the if without curly braces is what they ...