
-----------------------------------
wtd
Mon Nov 20, 2006 11:31 am

Sam's Teach Yourself C in 24 Hour...
-----------------------------------
... fails in the first chapter.

Call me crazy, but I don't trust a book that encourages* programmers to do things like this:

int integer_add( int x, int y )
{
   int result;
   result = x + y;
   return result;
}

* And by encouraged I mean demonstrates.  One has to be exceptionally careful when teaching those new to programming.  They have no sense of what is and is not ugly, because it's all new, and it's all wonderful.

-----------------------------------
NikG
Mon Nov 20, 2006 1:01 pm


-----------------------------------
For those of us not very quick (or maybe just me), can you explain what's wrong with that example? (other than being terribly useless...)

(Also, I should mention I'm not a c/c++ programmer so please don't yell at me for not knowing an obvious answer)

-----------------------------------
wtd
Mon Nov 20, 2006 1:04 pm


-----------------------------------
There is no need to declare the variable, then separate initialize it, then return it.

For one thing, it's quite possible to combine declaration and initialization.

int result = x + y;

It's also possible to return the result of such an expression directly.

return x + y;

-----------------------------------
NikG
Mon Nov 20, 2006 2:21 pm


-----------------------------------
Ahh right, I should've seen that since it applies to any language.

-----------------------------------
ericfourfour
Mon Nov 20, 2006 6:51 pm


-----------------------------------
Useless code is a common part of my everyday compsci class. Here are a few I've recently encountered.

static boolean getBoolean (boolean something)
{
    if (something == true)
    {
        return true;
    }
    return false;
}

This one might be a bit harder to catch.

static int

-----------------------------------
wtd
Mon Nov 20, 2006 7:18 pm


-----------------------------------
Actually, it is far from useless to define a function as a wrapper for standard mathematical operators.  This is however not terribly apparent until one tackles a fucntional programming language, and tries to apply the lessons learned in it back to languages like C and C++.

-----------------------------------
ericfourfour
Mon Nov 20, 2006 7:26 pm


-----------------------------------
I didn't mean it in that way. :? I was talking about the extra unneeded code. In my first example it the code could have been shrunk to one line (return something). In the second example it was returning the array (there is no need to return the array because it is a pointer).

This is however not terribly apparent until one tackles a fucntional programming language
Perhaps I should pick up a functional programming language.

-----------------------------------
wtd
Mon Nov 20, 2006 10:53 pm


-----------------------------------
Ah, I see.  

And yes, you should.  :)

For what it's worth:

O'Caml:

let integer_add x y = x + y

Or perhaps...

let integer_add = (+)

-----------------------------------
Null
Mon Nov 20, 2006 11:52 pm


-----------------------------------
wtd, in this case I disagree with you. 

I think the clearly demonstrated creation of a variable, an assignment of value to that variable, and finally a return statement are much clearer to a newbie then a single return statement. Understanding how things happen are very relevant in languages like C. The programmer is in complete control.

I only hold that opinion if the author then explains that there widely used shortcuts to accomplish the same thing (and then demonstrates them).

-----------------------------------
wtd
Tue Nov 21, 2006 11:25 am


-----------------------------------
I think the clearly demonstrated creation of a variable, an assignment of value to that variable, and finally a return statement are much clearer to a newbie then a single return statement. Understanding how things happen are very relevant in languages like C. The programmer is in complete control.

I only hold that opinion if the author then explains that there widely used shortcuts to accomplish the same thing (and then demonstrates them).

You are correct.  However, in this case the author does not spend more than a sentence or two talking about the variable declaration.

The author also does not demonstrate that such a pattern is not strictly necessary.  This type of code is pervasive.

-----------------------------------
kiran_n444
Fri Nov 21, 2008 8:08 pm

RE:Sam\'s Teach Yourself C in 24 Hour...
-----------------------------------
thankx for the tip bro, since i'm fairly new to C++ =)

-----------------------------------
michaelp
Fri Nov 21, 2008 8:33 pm


-----------------------------------
Useless code is a common part of my everyday compsci class. Here are a few I've recently encountered.

static boolean getBoolean (boolean something)
{
    if (something == true)
    {
        return true;
    }
    return false;
}


It took me a while to figure why that is useless. :P

-----------------------------------
wtd
Fri Nov 21, 2008 10:32 pm

RE:Sam\'s Teach Yourself C in 24 Hour...
-----------------------------------
Do not necro post!
