coding-style

Making the case for code quality

Imagine you're working with developers who habitually write 300 line nested methods full of errors, possible errors, etc. etc. Applications regularly throw exceptions as a matter of course, and much time is spend hunting odd intermittent bugs. How do you go about making the case for better code and better standards? How do you make the...

Using "super" in C++

My style of coding includes the following idiom: class Derived : public Base { public : typedef Base super; // note that it could be hidden in protected/private section, instead // Etc. } ; This enables me to use "super" as an alias to Base, for example, in constructors: Derived(int i, int j) : super(i), J(j) { } ...

Ruby Memory Management

I have been using ruby for a while now and I find for bigger projects it can take up a fare amount of memory. What are ruby best practices for reducing memory usage? Please, let each answer have on "best practice" and let the community vote it up ...

Python style: multiple-line conditions in IFs

Hello, Sometimes I break long conditions in IFs to several lines. The most obvious way to do this is: if (cond1 == 'val1' and cond2 == 'val2' and cond3 == 'val3' and cond4 == 'val4'): do_something Isn't very very appealing visually, because the action blends with the conditions. However, it is the natural way using corr...

Your preferred C/C++ header policy for big projects?

When working on a big C/C++ project, do you have some specific rules regarding the #include within source or header files? For instance, we can imagine to follow one of these two excessive rules: #include are forbidden in .h files; it is up to each .c file to include all the headers it needs Each .h file should include all its dependa...

JavaScript style/optimization: String.indexOf() v. Regex.test()

I've recently come across this piece of JavaScript code: if (",>=,<=,<>,".indexOf("," + sCompOp + ",") != -1) I was intrigued, because to write this test I would have done: if (/(>=|<=|<>)/.test(sCompOp)) Is this just a stylistic difference, or does the author of the other code know something about optimization that I don't? Or per...

Comparison Functor Types vs. operator<

In the Google C++ Style Guide, the section on Operator Overloading recommends against overloading any operators ("except in rare, special circumstances"). Specifically, it recommends: In particular, do not overload operator== or operator< just so that your class can be used as a key in an STL container; instead, you should cr...

Switch Statement Fallthrough...should it be allowed?

For as long as I can remember I have avoided using switch statement fallthrough. Actually, I can't remember it ever entering my consciousness as a possible way to do things as it was drilled into my head early on that it was nothing more than a bug in the switch statement. However, today I ran across some code that uses it by design, whi...

Are there any style guides for HTML?

I have found that my HTML is, to be honest, very clunky. Small, simple pages are OK. But there comes a point when between indenting and the kinds of tags I have, it's impossible to keep lines short. Is there a W3C (or otherwise "official" or well accepted) formatting guide for clean, maintainable HTML? If not, what suggestions can the co...

Is Iterator initialization inside for loop considered bad style, and why?

Typically you will find STL code like this: for (SomeClass::SomeContainer::iterator Iter = m_SomeMemberContainerVar.begin(); Iter != m_SomeMemberContainerVar.end(); ++Iter) { } But we actually have the recommendation to write it like this: SomeClass::SomeContainer::iterator Iter = m_SomeMemberContainerVar.begin(); SomeClass::SomeCont...

make bad code good

I got myself reading a article regarding changing the existing bad code into a good one. For reference, this is the link to the article http://www.javaworld.com/jw-03-2001/jw-0323-badcode.html?page=1 It broadly talked about the following Add comments Re-Factoring Code Break large classes into smaller classes Break large functions in...

Boiler plate code replacement - is there anything bad about this code?

I've recently created these two (unrelated) methods to replace lots of boiler-plate code in my winforms application. As far as I can tell, they work ok, but I need some reassurance/advice on whether there are some problems I might be missing. (from memory) static class SafeInvoker { //Utility to avoid boiler-plate InvokeRequired co...

what is the difference between using the visitor design pattern or using an interface ?

What is the difference between applying the visitor design pattern to your code , and code like the following : interface Dointerface { public void perform(Object o); } public class T { private Dointerface d; private String s; public String getS() { return s; } public T(String s) { t...

Class member organization

What is the best way to sort class members? I'm in conflict with a team member about this. He suggests that we should sort the members alphabetically. I think it's better to organize in a semantic manner: important attributes first, related methods together, etc. What do you think? ...

Java: Triple Curly Bracing

I've been given some code with commenting unlike anything I've come across before: //{{{ Imports import imports; //}}} It is the same for each method block, //{{{ above the code block //}}} below the code block ALSO SEE: http://en.wikipedia.org/wiki/Folding_editor ...

What real-world projects would you suggest as code examples to study?

What real-world projects would you suggest looking through the sources? As I'm learning Java Swing, mucommander seems to be a decent example. The code is excessively commented though. EDIT: No shameless plugs plz :). ...

When is JavaScript's eval() not evil?

I'm writing some JavaScript to parse user-entered functions (for spreadsheet-like functionality). Having parsed the formula I could convert it into JavaScript and run eval() on it to yield the result. However, I've always shied away from using eval() if I can avoid it because it's evil (and, rightly or wrongly, I've always thought it is ...

Eclipse - generated method parameters final

Can Eclipse make parameters for generated methods (overwriting, implementing interface, etc.) final. If yes - how? If I'm not mistaken, then IntelliJ had an option for it. I could not something similar in Eclipse. This has become a part of my coding style and I am making parameters final manually, but there has to be a faster way :P ...

Name of method that lists all user groups where the current user is member of

Hello all, we had a heated discussion about a method name. We have a class User. There is property called "Groups" on the user. It contains all groups that contain the user directly. That's ok. What we have problem with, is the name of the method that would recursively list all user's groups and their "parent" groups and return list of...

C++ headers - separation between interface and implementation details

One of classes in my program uses some third-party library. Library object is a private member of my class: // My.h #include <3pheader.h> class My { ... private: 3pObject m_object; } The problem with this - any other unit in my program that uses My class should be configured to include 3p headers. Movi...