coding-style

Appending to NSString (immutable)?

Just trying a few things out and noticed that this works, it does compile, but I just wanted to check if it would be considered good practice or something to be avoided? NSString *fileName = @"image"; fileName = [fileName stringByAppendingString:@".png"]; NSLog(@"TEST : %@", fileName); OUTPUT: TEST : image.png Might be better writt...

When should I use Scala's Array instead of one of the other collections?

This is more a question of style and preference but here goes: when should I use scala.Array? I use List all the time and occasionally run into Seq, Map and the like, but I've never used nor seen Array in the wild. Is it just there for Java compatibility? Am I missing a common use-case? ...

What do you do to write better code?

What do you do to write better code? Concentrate more? Read more books? My method is reading, asking. What is your method of writing better code? ...

which is better, using a nullable or a boolean return+out parameter

Lets say I have a function that needs to return some integer value. but it can also fail, and I need to know when it does. Which is the better way? public int? DoSomethingWonderful() or public bool DoSomethingWonderful(out int parameter) this is probably more of a style question, but I'm still curious which option people would tak...

Multithreaded Java server: allowing one thread to access another one

Hopefully the code itself explains the issue here: class Server { public void main() { // ... ServerSocket serverSocket = new ServerSocket(PORT); while (true) { Socket socket = serverSocket.accept(); Thread thread = new Thread(new Session(socket)); thread.start(); } // .. } public...

Recommendations for naming C# classes/methods intended to replace existing APIs.

Long explanation aside, I have a situation where I need to basically re-implement a .NET framework class in order to extend the behavior in a manner that is not compatible with an inheritance or composition/delegation strategy. The question is not a matter of whether the course of action I am to take is what you would do, or recommend, i...

Const correctness in C++ operator overloading returns

Hi! I'm a little confused as to why I've been told to return const foo from a binary operator in c++ instead of just foo. I've been reading Bruce Eckel's "Thinking in C++", and in the chapter on operator overloading, he says that "by making the return value [of an over-loading binary operator] const, you state that only a const member f...

Server side Javascript best practices?

We have a CMS built on Java and it has Mozilla Rhino for the server side JS. At the moment the JS code base is small but growing. Before it is too late and code has become a horrible mess I want to introduce some best practices and coding style. Obviously the name space control is pretty important. But how about other best practices - e...

Is there a well-established naming convention for PHP namespaces?

So far, I've seen many different naming conventions used for PHP namespaces. Some people use PascalCase\Just\Like\For\Classes, some use underscored\lower_case\names, some even use the Java convention for package names: com\domain\project\package. The question is very simple -- can any of these (or other) conventions be called well-establ...

Ruby Style: How to check whether a nested hash element exists

Consider a "person" stored in a hash. Two examples are: fred = {:person => {:name => "Fred", :spouse => "Wilma", :children => {:child => {:name => "Pebbles"}}}} slate = {:person => {:name => "Mr. Slate", :spouse => "Mrs. Slate"}} If the "person" doesn't have any children, the "chilren" element is not present. So, for Mr. Slate, we ca...

Is it OK for an abstract base class have non-abstract methods?

An abstract base class (interface class) usually has all its member functions abstract. However, I have several cases where member functions consisting of calls to the abstract methods of the interface are used. I can implement them in a derived-but-still-abstract class, or I can implemented the methods as non-abstract, non-virtual meth...

Review my Django Model - Need lots of suggestions.

Hi all, I am pulling a variety of information sources to build up a profile of a person. Once I do this I want to the flexibility to look at a person in a different ways. I don't have a lot of expierience in django so I would like a critique (be gentle) of my model. Admittedly even as I coded this I'm thinking redundancy (against DRY...

Should I use new Type() or just Type() for calling a constructor

Both syntaxes are equivalent (at least I suppose they are). let o1 = new Object() or let o2 = Object() Which way do you use more often? What about readability issues? ...

Logical value of an assignment in C

while (curr_data[1] != (unsigned int)NULL && ((curr_ptr = (void*)curr_data[1]) || 1)) Two part question. What will (curr_ptr = (void*)curr_data[1]) evaluate to, logically. TRUE? Also, I know its rather hack-ish, but is the while statement legal C? I would have to go through great contortions to put the assignment elsewhere in the...

Is it acceptable to have useless code?

I see that some programmers add code that, after all, does not do anything useful. For instance (C#): [Serializable] class Foo { // ... string SerializeMe() { return new XmlSerializer(typeof(this)).Serialize(this).ToString(); // serialize to xml (syntax wrong, not important here) } } The class is marked as Serializ...

Examples of Spartan Programming in C#

I am interested in reading examples of code in C# that makes use of the Spartan Programming philosophy. Can you please provide a link to any open source project or online code sample that follows this coding style? ...

Long code blocks inside if statements or for loops

This is a cross language question on coding style. I have to work with a lot of code that has very long code blocks, sometimes hundreds of lines, inside if statements or for loops. The code is procedural. Code like the following if(condition){ //hundreds of lines of code }else if{ //hundreds of lines of code } else { //hun...

C style printf/scanf

I've seen, here and elsewhere, many questions that, to get input data, use something like this: ... printf("What's your name? "); scanf("%s",name); ... This is very reminiscent of the old BASIC days (INPUT for those who remember it). The majority of those questions, if not all, are from people just learning C and are homeworks or...

Pyjamas import statements

I'm starting to use Pyjamas and I'm running into some annoyances. I have to import a lot of stuff to make a script work well. For example, to make a button I need to first from pyjamas.ui.Button import Button and then I can use Button. Note that import pyjamas.ui.Button and then using Button.Button doesn't work (results in error...

Is Google's "Go" language multi-value return statement an alternative to exceptions?

It seems to me Google's alternatives to exceptions are GO: multi-value return "return val, err;" GO, C++: nil checks (early return) GO, C++: "handle the damn error" (my term) C++: assert(expression) Is multi-value return useful enough to act as an alternative? Why are "asserts" considered alternatives? Does Google think it O.K. if a...