coding-style

HTML 5: Is it <br> <br/> or <br />?

I've tried checking other answers, but I'm still confused--especially after seeing W3schools HTML 5 reference. I thought HTML 4.01 was supposed to "allow" single-tags to just be <img> and <br>. Then XHTML came along with <img /> and <br /> (where someone said that the space is there for older browsers). Now I'm wondering how I'm suppos...

Best Practices for Code Formatting on Large Projects

I maintain the build for a large J2EE/Maven/Hudson/Perforce project with about 20 developers spread across the world. The solution in-place for code formatting is to format the codebase using jalopy when the developer runs the build, thereby ensuring any code which has not been formatted gets formatted prior to check-in. There main pro...

Should I make my python code less fool-proof to improve readability?

I try to make my code fool-proof, but I've noticed that it takes a lot of time to type things out and it takes more time to read the code. Instead of: class TextServer(object): def __init__(self, text_values): self.text_values = text_values # <more code> # <more methods> I tend to write this: class TextServer...

Is assignment in a conditional clause good ruby style?

In order to write more concisely, rather than do this: test_value = method_call_that_might_return_nil() if test_value do_something_with test_value end I've been assigning in the conditional: if test_value = method_call_that_might_return_nil() do_something_with test_value end Is this bad style? The still-more-concise syntax: do...

Where should I deallocate memory within functions?

I'm writing a shell in C. While I don't expect many other people to use it, I'd like to practice writing maintainable and well-organized code. I've noticed the following pattern in a number of my functions, so before it solidifies, I'd like it to be fully vetted. As an example, consider the following function: int foo(int param...) {...

Events or abstract methods, best practices

A subclass needs to know when particular events occur withing its superclass, but there are more than one ways for the superclass to break the news. Here are 2: dispatch an event call an abstract method which the subclass could eventually override I was wondering if best practices would recommend one of the approaches over the other....

When should a C function return newly allocated memory?

In a response elsewhere, I found the following snippet: In general it is nicer in C to have the caller allocate memory, not the callee - hence why strcpy is a "nicer" function, in my opinion, than strdup. I can see how this is a valid pattern, but why might it be considered nicer? Are there advantages to following this patter...

Function before form or form before function?

What do you believe in? As an incomplete basis for a good product - would you prefer a mess of code that is horrible to look at but works perfectly for what its supposed to do, or a beautiful set of well organized classes (or something else if OO doesn't float your boat) but have buggy functionality that still needs a lot of work? If yo...

Good Haskell coding standards

Could someone provide a link to a good coding standard for Haskell? I've found this and this, but they are far from comprehensive. Not to mention that the HaskellWiki one includes such "gems" as "use classes with care" and "defining symbolic infix identifiers should be left to library writers only." ...

In C#, what's the best way to spread a single-line string literal across multiple source lines?

Suppose that you have a lengthy string (> 80 characters) that you want to spread across multiple source lines, but don't want to include any newline characters. One option is to concatenate substrings: string longString = "Lorem ipsum dolor sit amet, consectetur adipisicing" + " elit, sed do eiusmod tempor incididunt ut labore et d...

?: operator PHP

I saw this today in some PHP code. $items = $items ?: $this->_handle->result('next', $this->_result, $this); What is the ?: doing? Is it a Ternary operator without a return true value? A PHP 5.3 thing? I tried some test code but got syntax errors. ...

PHP - Over-commenting?

I recently started working on a small CMS. I usually develop .NET applications in C#, and I'm very used to commenting my fields and methods. My friend told me earler that having comments in PHP scripts is quite bad, since PHP is dynamic and parsed when requested, so having to parse the comments will take longer. Does this statement ap...

Reference function or create a new function that only calls another?

def a(something): return something*something #Case I - referencing b = a #Case II - creating a new function to call the first def b(something): return a(something) Which is better style? Are there drawbacks to either? ...

empty lines in functions/methods

Hi there. At our company's Xmas party we had almost a physical fight over one issue: whether or not to allow empty lines in a function/method in our code (c/c++/c#). This is all about just single blank line, for example imagine code like: private void LoadData() { if (!loaded) return; Dat...

Question about functional OOP style in JavaScript

I prefer to use functional OOP style for my code (similar to the module pattern) because it helps me to avoid the "new" keyword and all problems with the scope of "this" keyword in callbacks. But I've run into a few minor issues with it. I would like to use the following code to create a class. namespace.myClass = function(){ var sel...

two-way list comparison in C# unit test

In my C# unit tests, I often query for a list of rows based on a list of IDs. I then want to ensure that 1) for all the IDs, there was at least one row found that has that ID and 2) for all the returned rows, each row has an ID that is in the list of IDs to find. Here is how I usually ensure that: Assert.IsTrue(ids.All( id => resu...

Is there a safe way of using eval to unthaw Data::Dumper output in Perl?

I have an object that uses freezes data as such: sub frozen_data { my $self = shift; $Data::Dumper::Indent = 0; $Data::Dumper::Terse = 1; return Data::Dumper->Dump( [ $self->{_DATA}, ] ); } and a corresponding thaw: sub thaw_data { my ($self) = @_; $self->{_DATA} = eval $self->{DATA}; } this seems to work...

What is the recommended coding style for PowerShell?

Is there any recommended coding style how to write PowerShell scripts? It's not about how to structure the code (how many functions, if to use module, ...). It's about 'how to write the code so that it is readable'. In programming languages there are some recommended coding styles (what to indent, how to indent - spaces/tabs, where to ...

How to express "never" with java.util.Date?

I have a class with the fields "deletionDate" and "experiationDate" which could both be undefined, what would mean that the object is whether deleted nor has an expiration date. My first approach was: private Date deletionDate = null; // null means not deleted Having the book "Clean Code" in mind I remember to better use expressive ...

invoking pylint programmatically

I'd like to invoke the pylint checker, limited to the Error signalling part, as part of my unit testing. so I checked the pylint executable script, got to the pylint.lint.Run helper class and there I got lost in a quite long __init__ function, ending with a call to sys.exit(). anybody ever tried and managed to do so? the dream-plan wo...