The most egregiously redundant code construct I often see involves using the code sequence
if (condition)
return true;
else
return false;
instead of simply writing
return (condition);
I've seen this beginner error in all sorts of languages: from Pascal and C to PHP and Java. What other such constructs would you flag in a code review?
Code Complete says it is good practice to always use block identifiers, both for clarity and as a defensive measure.
Since reading that book, I've been doing that religiously. Sometimes it seems excessive though, as in the case below.
Is Steve McConnell right to insist on always using block identifiers? Which of these would you use?
//naughty…
I see a lot of c++ code that looks like this:
for( const_iterator it = list.begin(),
const_iterator ite = list.end();
it != ite; ++it)
As opposed to the more concise version:
for( const_iterator it = list.begin();
it != list.end(); ++it)
Will there be any difference in speed between these two conventions? Naively the first…
If I need to check multiple conditions, which is the preferred way with respect to performance
if( CND1 && CND2 && CND3 && CND4)
{
}
else
{
}
or
if(CND1)
{
if(CND2)
{
if(CND3)
{
if(CND4)
{
}
}
}
}
Hello,
i often work on private projects using the WinApi, and as you might know, it has thousands of named and typedefed structs like MEMORY_BASIC_INFORMATION.
I will stick to this one in my question, what still is preferred, or better when you want to name a variable of this type. Is there some kind of style guide for this case?
For…
I just hope the following doesn't seem to you like redundant jabber :)
Anyway, there is that:
for (p = fmt; *p; p++) {
if (*p != '%') {
putchar(*p);
continue;
}
switch (*++p) {
/* Some cases here */
...
}
}
And I wondered why the writer (Kernighan / Ritchie) used the continue in the if…
I have always wondered what's the best way to achieve this task. In most web based applications you have to provide search options on many different criteria. Based on what criteria is chosen behind the scene you modify your SQL. Generally, this is how I tend to go about it:-
Have a base SQL template.
In the base template have…
Consider this OCaml code:
let coupe_inter i j cases =
let lcases = Array.length cases in
let low,_,_ = cases.(i)
and _,high,_ = cases.(j) in
low,high,
Array.sub cases i (j-i+1),
case_append (Array.sub cases 0 i) (Array.sub cases (j+1) (lcases-(j+1)))
Why the expression let ... and ... in is used in place of a let…
When I first started writing CSS, I was writing it in an expanded form
div.class {
margin: 10px 5px 3px;
border: 1px solid #333;
font-weight: bold;
}
.class .subclass {
text-align:right;
}
but now I find myself writing css like this: (Example from code I'm actually writing now)
…
When naming a boolean, or a function returning a boolean it's usual to prefix with 'is' e.g.
isPointerNull
isShapeSquare
What about when refering to multiple items, should it be:
arePointersNull or isPointersNull
areShapesNull or isShapesNull
I can see arguments for both; is offers consistency and perhaps slightly…
I have a source code of a library which has a lot of strange IF, ELSE, FOR, etc. macros for all common C-keywords instead of using just usual if,else,for,while keywords. These macros are defined like this:
#define IF( a) if( increment_if(), a)
where increment_if() function is defined so:
static __inline void…
I'm creating a large PHP project and I've a trivial doubt about how to proceed.
Assume we got a class books, in this class I've the method ReturnInfo:
function ReturnInfo($id) {
if( is_numeric($id) ) {
$query = "SELECT * FROM books WHERE id='" . $id . "' LIMIT 1;";
if( $row =…
I've been working on a foundational c++ library for some time now, and there are a variety of ideas I've had that could really simplify the code writing and managing process. One of these is the concept of introducing some macros to help simplify statements that appear very often, but are a bit…
If you extend SQLiteOpenHelper, for the Constructor you have to use a Context. I am wondering if there is a way to leave this out, and be able to work with database tables without a Context.
Or at least be least restrictive, I mean a way of project/class structure that will make history the…
What is the general opinion on the 2nd indentation method below.
// Normal indentation
a.Value = "foobar";
ab.Checked = false;
foo.Value = "foobar";
foobar.Checked = true;
// Spaces before the dot to align the properties/methods
a .Value = "foobar";
ab .Checked =…
What's your opinion about using #region folding using application semantic, instead of folding for "syntax".
For example:
#region Application Loop
#region User Management
#region This Kinf of stuffs
instead of
#region Private Routines
#region Public Properties
#region ThisRoutine //…
I'm learning C++. For me, my programming style is just what looks the best; it doesn't seem to follow the rules of any one particular style. Here's an example
void f(int x){ //no space between close-paren and bracket
if (!x){
cout << "x is non-zero\n";
} //closing…
Consider this flow structure which I happen to use often:
if ( hasPosts() ) {
while ( hasPosts() ) {
displayNextPost();
}
} else {
displayNoPostsContent();
}
Are there any programming languages which have an optional else clause for while, which is to be run if…
I have created a protocol that my classes need to implement, and then factored out some common functionality into a base class, so I did this:
@protocol MyProtocol
- (void) foo;
- (void) bar;
@end
@interface Base <MyProtocol>
@end
@interface Derived_1 : Base
@end
@interface…
Eclipse's default template for new types (Window Preferences Code Style Code Templates New Java Files) looks like this:
${filecomment}
${package_declaration}
${typecomment}
${type_declaration}
Creating a new class, it'll look something like this:
package pkg;
import…
I have kind of got used to it now but in the past if i had to work on a piece of code someone else had written with the brackets placed in this way I would have to go through and change it all. Why would anybody do this, I can't think of a single advantage but lots of…
I am just working on a project where the library has an object with the property color, however being British I always use colour when writing variables and properties.
I also just found some legacy code where the British developer used color in a variable name.
Is…