Coding style is a set of guidelines that helps readability and understanding of the source code.
67
votes
11answers
4k views
Never use Strings in Java?
I stumbled upon a blog entry discouraging the use of Strings in Java for making your code lack semantics, suggesting that you should use thin wrapper classes instead. This is the before and after ...
70
votes
10answers
13k views
Why do most of us use 'i' as a loop counter variable?
Has anyone thought about why so many of us repeat this same pattern using the same variable names?
for (int i = 0; i < foo; i++) {
// ...
}
It seems most code I've ever looked at uses i, j, ...
42
votes
11answers
10k views
What is the ideal length of a method?
In object-oriented programming, there is no exact rule on the maximum length of a method , but I still found these two qutes somewhat contradicting each other, so I would like to hear what you think.
...
1
vote
3answers
136 views
Accepting a numerical range in a function call
I have encountered two ways of doing it:
void foo(int from, int to); /* 'from' inclusive, 'to' exclusive */
void foo(int startIndex, int rangelength);
Has one style historically been preferred ...
3
votes
3answers
152 views
Organization of DLL linked functions
This is a code organization question.
I got my basic code working but when I expand it, it will be terrible. I have a DLL which I don't have a .lib for. Therefore I have to use the whole ...
1
vote
1answer
81 views
Is there an easy way to type in common math symbols?
Disclaimer: I'm sure someone is going to moan about easy-of-use, for the purpose of this question consider readability to be the only factor that matters
So I found this site that converts to easting ...
74
votes
16answers
18k views
What is the benefit of not using Hungarian notation?
One of the things I struggle with is not using Hungarian notation. I don't want to have to go to the variable definition just to see what type it is. When a project gets extensive, it's nice to be ...
1
vote
1answer
112 views
The purpose of using a constants pool for immutable constants
Originally posted at stackoverflow.com/q/23961260
I come across the following code with a lot of frequency:
if (myArray.length == Constants.ZERO_INT)
or
if (myString != null && ...
30
votes
3answers
858 views
Origins of code indentation
I am interested in finding out who introduced code indentation, as well as when and where it was introduced.
It seems so critical to code comprehension, but it was not universal. Most Fortran and ...
1
vote
3answers
118 views
Use unnamed object to invoke method or not?
If I have a class with only only public method. When I use this class, is it good to use unnamed object to invoke its method?
normal:
TaxFileParser tax_parser(tax_file_name);
auto content = ...
2
votes
4answers
125 views
Should our team order Javascript class methods/properties consistently? If so, how?
As our team is growing I've noticed that different developers put their class methods in different orders. For instance:
var Foo = Backbone.Model.extend({
someVar: {},
initialize: ...
4
votes
6answers
2k views
sizeof style: sizeof(type) or sizeof variable?
I've seen two styles of using sizeof for memory-related operations (such as in memset or malloc):
sizeof(type), and
sizeof variable or sizeof(variable)
Which one would you prefer, or would you use ...
2
votes
4answers
836 views
Identify this programming style
Some of the legacy code I've inherited uses the fact that C# supports multiple assignment to write code like:
void DisableControls()
{
ddlStore.Enabled =
ddlProgram.Enabled ...
-1
votes
1answer
221 views
_variable - why that underscore at the beginning? [closed]
Sometimes I see people writing variable names like this:
int _variable;
IMO That's really ugly. What's that about?
51
votes
15answers
10k views
What's wrong with circular references?
I was involved in a programming discussion today where I made some statements that basically assumed axiomatically that circular references (between modules, classes, whatever) are generally bad. ...
3
votes
4answers
468 views
Is lack of whitespace a sign of a cargo cult programmer? [closed]
Eric Lippert has written about cargo cult programming, and Steve McConnell has tackled the subject from a Software Engineering point of view.
Eric provides a fairly succinct description:
They ...
8
votes
6answers
888 views
Should my team use some common well-regarded coding standard as a basis for its own?
The R&D team I'm in has decided to adopt a coding standard. We have only recently formed, and have too little code and common coding time of our own to base our standards/conventions document on ...
16
votes
7answers
959 views
Should I write compact code or code with lots of spaces? [duplicate]
I have two friends that have completely different schools of thought on how to lay out their code.
The first says that code should be well-indented and use lots of spaces and to name variables ...
73
votes
8answers
5k views
Should you always pass the bare minimum data needed into a function in cases like this
Let's say I have a function IsAdmin that checks whether a user is an admin. Let's also say that the admin checking is done by matching user id, name and password against some sort of rule (not ...
10
votes
5answers
674 views
Evolution in coding standards, how do you deal with them?
How do you deal with evolution in the coding standards / style guide in a project for the existing code base? Let's say someone on your team discovered a better way of object instantiation in the ...
0
votes
2answers
258 views
How to enforce good coding style in team? [duplicate]
Even if I don't like enforcing people to do things (and I believe that it may decline the productivity and cause anger), I really want to enforce good coding style.
Is there a way to set up ...
1
vote
2answers
122 views
Objective-C style: Do I implement factory methods or init methods?
I'm new to Objective-C programming, and creating various classes for an iOS application I'm working on.
When creating objects, it seems like many classes in the built-in frameworks use the "static ...
14
votes
6answers
923 views
Most readable way to format long if conditions? [closed]
Long winding if conditions should be avoided if at all possible, yet sometimes we all end up writing them. Even if it's a very simple condition, the involved statements are sometimes simply very ...
1
vote
4answers
443 views
What is “using namespace” pollution?
I was looking at the google coding guide [here] and they do not recommend that one use the using namespace ornamespace:function` - if I did not misinterpret it.
Does this apply to std as well? ...
4
votes
2answers
198 views
Using a closure to avoid code duplication in Python
Sometimes I find myself wanting to run the same code from a few different spots in the same function. Say I have some function func1, and I want to do the same thing from a few different spots in ...
6
votes
6answers
498 views
Commenting regular expressions
Are there any common practises for commenting the regular expressions: inline comments referring different part of RegEx or general comment for all expression?
2
votes
1answer
85 views
equivalence in callback and non-callback javascript
I'm pretty sure that the following two snippets are equivalent but I wanted to double check:
CALLBACK STYLE
function foo(input, callback) {
// do stuff with input
callback();
}
function ...
22
votes
18answers
2k views
Why write clean, refactored code? [duplicate]
This is a question I've been asking myself for a long time. Thought of throwing out it to you.
From my experience of working on several Java based projects, I've seen tons of codes which we call ...
13
votes
2answers
529 views
Creating nested functions for purely aesthetic reasons?
I've always wondered what other programmers think about the idea of creating pure aesthetic functions.
Say I have a function that processes a chunk of data: Function ProcessBigData. Say I need ...
6
votes
6answers
2k views
Best practice in setting return value (use else or?)
Whenever you want to return a value from a method, but whatever you return depends on some other value, you typically use branching:
int calculateSomething() {
if (a == b) {
return x;
} else ...
-2
votes
1answer
107 views
Is there a named antipattern for unclear API not exposing the requirements? [closed]
In the source code I'm evaluating (jarjar), there exists java code that can be used like this:
JarJarTask fixture = new JarJarTask();
fixture.addConfiguredRule(new Rule());
fixture.execute();
Which ...
-2
votes
6answers
185 views
What is the coding standard for a long parameter list? [closed]
I am trying to code a constructor and the class needs a few variables to get it running. This method creates a log file containing a subset of a source file's entries based on a key and possible ...
14
votes
2answers
10k views
What is the difference between K&R and One True Brace Style (1TBS) styles?
I have read the Wikipedia article on Indent Styles, but I still don't understand. What is the difference between K&R and 1TBS?
1
vote
1answer
216 views
How to break these long C++ lines in a neat way? [closed]
I'm on my first bigger C++ project and find that I have some really long lines. My goal is to break them to 79 columns, but I do not really know how to do this in a neat way. Are there some guidelines ...
24
votes
5answers
3k views
Why is it bad to write something in language X as if you're writing a program in language Y in terms of using a shared coding paradigm [closed]
A while ago, I asked a question on SO about something written in C++, but instead of getting an answer to the problem at hand, the comments went all crazy on my coding style, even when I indicated ...
69
votes
22answers
16k views
Where do you declare variables? The top of a method or when you need them? [closed]
I am in sort of a dilemma (in a geekish way of course).
I love to declare variables at the beginning of my methods, and usually order them in some logical way.
The problem is, when the list gets ...
35
votes
14answers
5k views
How important is it to clean up someone else's code when faced with a tight deadline? [closed]
(I'm talking about HTML / CSS code (not programming languages) but I think we also face the same issue as with programmers.)
I am the senior front-end designer in a team and I often have to re-work ...
-1
votes
3answers
168 views
Should I always use “is” as prefix for boolean variables? [closed]
Should I always use is as prefix for boolean variables? What about booleans that indicate something in past? Should I write isInitialized or wasInitialized? Should I write for properties IsManyMembers ...
16
votes
5answers
2k views
How can you decompose a constructor?
Lets say I have a Enemy class, and the constructor would look something like:
public Enemy(String name, float width, float height, Vector2 position,
float speed, int maxHp, int ...
0
votes
0answers
58 views
Check some value between each function call
Can you recommend a nice way of checking a particular value between calls to a set of functions?
E.g. something like this in Python (might not be terribly 'Pythonic'):
self.error_code = 0 # this ...
0
votes
1answer
59 views
Preferred Scala code style for several embedded brace sets
I was wondering the correct way to format a situation such as this, where I have a def, a foreach and an if clause all nested, requiring braces.
Currently I find it hard to read, and there is no ...
32
votes
11answers
4k views
Is it okay to make coding style changes on an open source project that doesn't follow best practices?
Recently, I came across a number of open source Ruby (or majority of it was Ruby) projects on GitHub that when checked with a code analyzing tool like Rubocop, create a lot of offenses.
Now, most of ...
0
votes
1answer
40 views
How using tab affects commands like diff
In the page at http://pear.php.net/manual/en/standards.indenting.php, it is advised to use space instead of tab as it causes issue with diff, patches. Here is the excerpt from the page.
Use an ...
1
vote
2answers
123 views
Approach to coding algorithms to minimize rework [closed]
I find myself reworking my code quite a bit, especially when learning to write algorithms that involve keeping track of multiple attributes and labels.
I feel that having the pseudo-code written up, ...
-4
votes
2answers
258 views
How to really understand programming? [duplicate]
I have started to learn to program. I am interested in it and dont mind how long it takes to learn. But I am using books to start out and I find that there are some things I get and some things I have ...
19
votes
11answers
1k views
Are long functions acceptable if they have internal structure?
When dealing with complicated algorithms in languages with support for nested functions (such as Python and D) I often write huge functions (because the algorithm is complicated) but mitigate this by ...
15
votes
12answers
2k views
Teaching myself, as a physicist, to become a better programmer
I've always liked physics, and I've always liked coding, so when I got the offer for a PhD position doing numerical physics (details are not relevant, it's mostly parallel programming for a cluster) ...
2
votes
1answer
50 views
When should I pass setting-like value as class' variable and when as an assoc. array?
Following my other question, is there a general rule of thumb, when we should pass a setting-like value, that controls class' behavior (for example displayed texts) as as class' constant or variable, ...
1
vote
1answer
86 views
When should I pass value as class variable and when as a method argument?
Is there a general rule of thumb, when we should pass a value as as class variable and when as a method argument? Or is it just a choice of the developer?
For example -- are there any reasons, why ...
0
votes
3answers
100 views
Long vs short scripts? Big vs small scripts? [closed]
As a programmer, I have always wondered whether it is preferable to write (a) short modular functions that are each stored in their own script (i.e., file) or (b) long comprehensive scripts that ...