In the case of languages that support single decision and action without brackets, such as the following example:
if (var == true)
doSomething();
What is the preferred way of writing this? Should brackets always be used, or should their usage be left as a preference of the individual developer? Additionally, does this practice dep...
Excluding Whitespace, BrainF*ck (and all those other languages not designed for practical usage), and assembly, what do you think is the most difficult real programming language to write readable code in, and why?
I find that I'm very comfortable reading code with C/C++ style braces and brackets. I can easily scan a file for method and...
How do you write code that is easily read by other people and who have had no hand in writing any part of it?
...
I've been really enjoying Python programming lately. I come from a background of a strong love for C-based coding, where everything is perhaps more complicated than it should be (but puts hair on your chest, at least). So switching from C to Python for more complex things that don't require tons of speed has been more of a boon than a ba...
I'm working on a tool that will output an interface and a couple classes implementing that interface. My output isn't particularly complicated, so it's not going to be hard to make the output conform to our normal code formatting standards. But this got me thinking: how human-readable does auto-generated code need to be? When should e...
experimenting with Cockburn use cases in code
I was writing some complicated UI code. I decided to employ Cockburn use cases with fish,kite,and sea levels (discussed by Martin Fowler in his book 'UML Distilled'). I wrapped Cockburn use cases in static C# objects so that I could test logical conditions against static constants which r...
Sometimes I find myself in the situation where I want to execute several sequential commands like such:
try:
foo(a, b)
except Exception, e:
baz(e)
try:
bar(c, d)
except Exception, e:
baz(e)
...
This same pattern occurs when exceptions simply need to be ignored.
This feels redundant and the excessive syntax causes it t...
I miss it so much (used it a lot in C#). can you do it in C++?
...
If I'm deep in a nest of loops I'm wondering which of these is more efficient:
if (!isset($array[$key])) $array[$key] = $val;
or
$array[$key] = $val;
The second form is much more desirable as far as readable code goes. In reality the names are longer and the array is multidimensional. So the first form ends up looking pretty gnarly...
I am always wondering what should I write for inline code documentation? I always end up with one liners "This function does x" or a 4 paragraph essay describing every micro detail of the function. I am trying to find a balance that provides good documentation without being to verbose. So if you could replay with the one thing that yo...
If you had to iterate through a loop 7 times, would you use:
for (int i = 0; i < 7; i++)
or:
for (int i = 0; i <= 6; i++)
There are two considerations:
performance
readability
For performance I'm assuming Java or C#. Does it matter if "less than" or "less than or equal to" is used? If you have insight for a different languag...
Oftentimes a developer will be faced with a choice between two possible ways to solve a problem -- one that is idiomatic and readable, and another that is less intuitive, but may perform better. For example, in C-based languages, there are two ways to multiply a number by 2:
int SimpleMultiplyBy2(int x)
{
return x * 2;
}
and
in...
I have these two pieces of code, wich one is more readable?
foreach
decimal technicalPremium = 0;
foreach (Risk risk in risks)
{
technicalPremium = technicalPremium + risk.TechnicalPremium;
}
return technicalPremium;
linq
return risks.Sum(risk => risk.TechnicalPremium);
...
Coming from a Ruby background, I'm used to writing all my code using classes with methods and such. I do know javascript quite well, but I'm new to jQuery and its best practices.
Obviously there are a million ways to simulate classes in javascript. But, what is the actual jQuery community REALLY using in the real world?
Specific exampl...
I am not sure what the best practices are here, but I often see abbreviated variable names especially when the scope is small. So (to use simple Ruby examples) instead of def add_location(name, coordinates), I see things like def add_loc(name, coord)—and I might even see something like def add_loc(n, x, y). I imagine that longer names mi...
What's the best way to format this for readability?
if (strpos($file, '.jpg',1) && file_exists("$thumbsdir/$file") == false || strpos($file, '.gif',1) && file_exists("$thumbsdir/$file") == false || strpos($file, '.png',1) && file_exists("$thumbsdir/$file") == false) {
createThumb("$gallerydir/$file", "$thumbsdir/$file",$thumbsize);
...
UPDATE: Thanks to everyone for the responses. I didn't realize document.write() was deprecated. Add a another notch to the learning column. I'll be taking the advice posted here, but leave the original question so that the answers given make sense in context of the original question.
I'm in the process of coding some rather long write...
I have a large base of code to play with written in two programming languages [C++,OTCL], it is not well documented and I have to modify parts and add new modules. I found that reading the code is the best practice [the mailing list= many questions+very very few answers].
Do you have any tips for making my reading more effective ?
Ha...
There seem to be so many color wheel, color picker, and color matcher web apps out there, where you give one color and the they'll find a couple of other colors that will create a harmonic layout when being used in combination. However most of them focus on background colors only and any text printed on each background color (if text is ...
I often use this code pattern:
while(true) {
//do something
if(<some condition>) {
break;
}
}
Another programmer told me that this was bad practice and that I should replace it with the more standard:
while(!<some condition>) {
//do something
}
His reasoning was that you could "forget the break" too easily...