coding-style

Criticising code

What is the best way to point out flaws in a developers code? Stuff they've been working on a while and though you'd love to not say anything, it's bad code and something has to be said. How do you go about it without them getting angry at you?...

[Team] Coding Styles

[Assuming you write code on a team] Does your team actually agree on a coding style standard? Independent of what language you are using, I'm curious to know A) do you agree and B) are the styles something you created as a group or did you decide to use a "third party's" style guide? Our team uses C# primarily, and we've taken to using...

Do you name controls on forms using the same convention as a private variable?

For some reason I never see this done. Is there a reason why not? For instance I like _blah for private variables, and at least in Windows Forms controls are by default private member variables, but I can't remember ever seeing them named that way. In the case that I am creating/storing control objects in local variables within a memb...

php: access array value on the fly

in php, i often need to map a variable using an array ... but i can not seem to be able to do this in a one liner. c.f. example: // the following results in an error: echo array('a','b','c')[$key]; // this works, using an unnecessary variable: $variable = array('a','b','c'); echo $variable[$key]; this is a minor problem, but it keeps...

C# String output: format or concat?

Let's say that you want to output or concat strings, what style do you prefer: var p = new { FirstName = "Bill", LastName = "Gates" }; Console.WriteLine("{0} {1}", p.FirstName, p.LastName); Console.WriteLine(p.FirstName + " " + p.LastName); Do you rather use format or do you simply concat strings? What is you...

Batch code indenters and beautifiers

Does anyone here know of good batch file code indenters or beautifiers? Specifically for PHP, JS and SGML-languages. Preferably with options as to style. ...

What constitutes beautiful code?

For anyone that is passionate about software development, I'm of the opinion that you should always strive to write beautiful code; however, is there a clear definition of what beautiful code really is? On one hand, I see elegant code being something that's easy to read while simultaneously solving the problem at hand in the most effici...

How many lines of code is too many?

One thing that occasionally drives me crazy is reading another person's functions that span 5 vertical monitor lengths, or .cpp files that are over 2000 lines long. For readability, wouldn't it be better to break a 1000 line function into many smaller sub-functions called by one function? Shouldn't a class implementation not span an in...

How do you make wrong code look wrong? What patterns do you use to avoid semantic errors?

Ever since I first made the mistake of doing an assignment in an if I've always written my ifs like this: if (CONST == variable) { to avoid the common (at least for me) mistake of doing this: if (variable = CONST) { //WRONG, assigning 0 to variable And since I read Joel Spolsky's essay Making Wrong Code Look Wrong I've been trying ...

C++ cast syntax styles

A question related to Regular cast vs. static_cast vs. dynamic_cast: What cast syntax style do you prefer in C++? C-style cast syntax: (int)foo C++-style cast syntax: static_cast<int>(foo) constructor syntax: int(foo) They may not translate to exactly the same instructions (do they?) but their effect should be the same (right?). If...

Algorithm to format text to Pascal or camel casing

Using this question as the base is there an alogrithm or coding example to change some text to Pascal or Camel casing. For example: mynameisfred becomes Camel: myNameIsFred Pascal: MyNameIsFred ...

How to keep a "things done" count in a recursive algorithm in Java?

I have a recursive algorithm which steps through a string, character by character, and parses it to create a tree-like structure. I want to be able to keep track of the character index the parser is currently at (for error messages as much as anything else) but am not keen on implementing something like a tuple to handle multiple return...

Should a function have only one return statement?

Are there good reasons why it's a better practice to have only one return statement in a function? Or is it okay to return from a function as soon as it is logically correct to do so, meaning there may be many return statements in the function? ...

How many constructor arguments is too many?

Let's say you have a class called Customer, which contains the following fields: UserName Email First Name Last Name Let's also say that according to your business logic, all Customer objects must have these four properties defined. Now, we can do this pretty easily by forcing the constructor to specify each of these properties. Bu...

how do you settle tabs vs spaces in the office?

We have some grumpy developers in the office, and like the perrenial bikeshed, everyone wants to have their say. Some languages do try to mandate one style over the other (python), others are just free form. I'm having a hard time getting consensus here, and people are doing spurious edits on the codebase. Is there some sort of techni...

What does 'foo' really mean?

I hope this qualifies as a programming question, as in any programming tutorial, you eventually come across 'foo' in the code examples. (yeah, right?) what does 'foo' really mean? If it is meant to mean nothing, when did it begin to be used so? Cheers ...

Single quotes vs. double quotes in Python

According to the documentation, they're pretty much interchangeable. Is there a stylistic reason to use one over the other? ...

Should I use multiple assemblies for an isolated ASP.NET web application?

Coming from a corporate IT environment, the standard was always creating a class library project for each layer, Business Logic, Data Access, and sometimes greater isolation of specific types. Now that I am working on my own web application project, I don't see a real need to isolate my code in this fashion. I don't have multiple app...

Best way to write a conversion function

Let's say that I'm writing a function to convert between temperature scales. I want to support at least Celsius, Fahrenheit, and Kelvin. Is it better to pass the source scale and target scale as separate parameters of the function, or some sort of combined parameter? Example 1 - separate parameters: function convertTemperature("celsius"...

In C# (or any language) what is/are your favourite way of removing repetition?

I've just coded a 700 line class. Awful. I hang my head in shame. It's as opposite to DRY as a British summer. It's full of cut and paste with minor tweaks here and there. This makes it's a prime candidate for refactoring. Before I embark on this, I'd thought I'd ask when you have lots of repetition, what are the first refactoring op...