Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Sam's Teach Yourself C in 24 Hour...
Index -> Programming, C++ -> C++ Tutorials
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
wtd




PostPosted: Mon Nov 20, 2006 11:31 am   Post subject: 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:

code:
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.
Sponsor
Sponsor
Sponsor
sponsor
NikG




PostPosted: Mon Nov 20, 2006 1:01 pm   Post subject: (No subject)

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




PostPosted: Mon Nov 20, 2006 1:04 pm   Post subject: (No subject)

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.

code:
int result = x + y;


It's also possible to return the result of such an expression directly.

code:
return x + y;
NikG




PostPosted: Mon Nov 20, 2006 2:21 pm   Post subject: (No subject)

Ahh right, I should've seen that since it applies to any language.
ericfourfour




PostPosted: Mon Nov 20, 2006 6:51 pm   Post subject: (No subject)

Useless code is a common part of my everyday compsci class. Here are a few I've recently encountered.

Java:
static boolean getBoolean (boolean something)
{
    if (something == true)
    {
        return true;
    }
    return false;
}


This one might be a bit harder to catch.

Java:
static int[] fillArray (int[] nums)
{
    System.out.println ("Enter " + nums.length + " numbers.");
    get the integers and put them into the array
    return nums;
}
wtd




PostPosted: Mon Nov 20, 2006 7:18 pm   Post subject: (No subject)

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




PostPosted: Mon Nov 20, 2006 7:26 pm   Post subject: (No subject)

I didn't mean it in that way. Confused 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).

wtd wrote:
This is however not terribly apparent until one tackles a fucntional programming language

Perhaps I should pick up a functional programming language.
wtd




PostPosted: Mon Nov 20, 2006 10:53 pm   Post subject: (No subject)

Ah, I see.

And yes, you should. Smile

For what it's worth:

O'Caml:

code:
let integer_add x y = x + y


Or perhaps...

code:
let integer_add = (+)
Sponsor
Sponsor
Sponsor
sponsor
Null




PostPosted: Mon Nov 20, 2006 11:52 pm   Post subject: (No subject)

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




PostPosted: Tue Nov 21, 2006 11:25 am   Post subject: (No subject)

Null wrote:
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




PostPosted: Fri Nov 21, 2008 8:08 pm   Post subject: RE:Sam\'s Teach Yourself C in 24 Hour...

thankx for the tip bro, since i'm fairly new to C++ =)
michaelp




PostPosted: Fri Nov 21, 2008 8:33 pm   Post subject: (No subject)

ericfourfour @ Mon Nov 20, 2006 6:51 pm wrote:
Useless code is a common part of my everyday compsci class. Here are a few I've recently encountered.

Java:
static boolean getBoolean (boolean something)
{
    if (something == true)
    {
        return true;
    }
    return false;
}



It took me a while to figure why that is useless. Razz
wtd




PostPosted: Fri Nov 21, 2008 10:32 pm   Post subject: RE:Sam\'s Teach Yourself C in 24 Hour...

Do not necro post!
Display posts from previous:   
   Index -> Programming, C++ -> C++ Tutorials
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 13 Posts ]
Jump to:   


Style:  
Search: