Welcome, Guest. Please login or register.

Author Topic: Learning C with the Amiga  (Read 32579 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline ethierbach

  • Newbie
  • *
  • Join Date: Nov 2006
  • Posts: 35
    • Show only replies by ethierbach
Re: Learning C with the Amiga
« Reply #104 from previous page: February 08, 2007, 09:41:14 PM »
Pardon me for jumping in here...

Don't mind too much about the "language wars"...but I hope you can take away one point from it all.  Once you have C and C++ well in hand, take some time to learn a completely different sort of language, one that pushes you to think in a different direction than C does.  Maybe one with strict typing like Pascal, or an interpreted one like Perl.  That will help you to separate specific language quirks from sound general programming principles.

Your take on C seems a lot like my wife's -- she regards the code as self-documenting, and considers comments to be a distraction.  I have a different view, probably colored by my experience with IBM System/370 Assembler.  I'd much rather read comments, to know what the programmer meant to do, than play computer and try to execute the code in my head.

Interestingly enough, one of my lame justifications for getting an Amiga was to get more comfortable with C, as I hope to get involved with some Linux or other kernel programming.  Hacking around with Amiga programming seemed much more interesting than the many other options I have.

-Ed-
 

Offline falemagn

  • Sr. Member
  • ****
  • Join Date: May 2002
  • Posts: 269
    • Show only replies by falemagn
    • http://www.aros.org/
Re: Learning C with the Amiga
« Reply #105 on: February 08, 2007, 11:34:53 PM »
Quote

Your take on C seems a lot like my wife's -- she regards the code as self-documenting, and considers comments to be a distraction. I have a different view, probably colored by my experience with IBM System/370 Assembler. I'd much rather read comments, to know what the programmer meant to do, than play computer and try to execute the code in my head.


I gotta agree with your wife there. The point is, you can write code that is self-documenting without requiring you to "play computer".

C is a language, English is a language too, but C is better at concisely expressing what in English would require verbose sentences.
Code: [Select]

/* Defines an integer variable named 'a' and assigns it the value 2 */
int a = 2;

Yes, it's quite an extreme example, but shows the point.

The only things that require comments are algorithms, but the more self-documenting code you are able to write, the better.
 

Offline CannonFodder

  • Hero Member
  • *****
  • Join Date: Sep 2003
  • Posts: 1115
    • Show only replies by CannonFodder
Re: Learning C with the Amiga
« Reply #106 on: February 09, 2007, 12:07:40 AM »
Quote

falemagn wrote:
Quote

Your take on C seems a lot like my wife's -- she regards the code as self-documenting, and considers comments to be a distraction. I have a different view, probably colored by my experience with IBM System/370 Assembler. I'd much rather read comments, to know what the programmer meant to do, than play computer and try to execute the code in my head.


I gotta agree with your wife there. The point is, you can write code that is self-documenting without requiring you to "play computer".

C is a language, English is a language too, but C is better at concisely expressing what in English would require verbose sentences.
Code: [Select]

/* Defines an integer variable named 'a' and assigns it the value 2 */
int a = 2;

Yes, it's quite an extreme example, but shows the point.

The only things that require comments are algorithms, but the more self-documenting code you are able to write, the better.


IMNSHO comments should clearly (and only) state the ¨why¨, the code itself should clearly state the ¨what¨ and the ¨how¨.

YMMV :-)
People are hostile to what they do not understand - Imam Ali ibn Abi Talib(AS)
 

Offline Hyperspeed

  • Hero Member
  • *****
  • Join Date: Jun 2004
  • Posts: 1749
    • Show only replies by Hyperspeed
Re: Learning C with the Amiga
« Reply #107 on: February 09, 2007, 02:07:30 AM »
The last thing I programmed was a stupid DOS "Hello world" type thing in Turbo Pascal on PCTask 3.

I'd like to learn C for the Amiga too but this thread is not doing beginners any good.

Maybe I should RTFM.

:-D

EDIT:
· Mel: Are you Karlos' secretary?
· Why does your Mazda link take me to a mammogram page? :inquisitive:

Answers on a postcard!
 

Offline ethierbach

  • Newbie
  • *
  • Join Date: Nov 2006
  • Posts: 35
    • Show only replies by ethierbach
Re: Learning C with the Amiga
« Reply #108 on: February 09, 2007, 02:52:03 AM »
@Hyperspeed:
I agree that this thread might not be the best starting place.  :-)   Here's a quick and easy set of tutorials that I found helpful:

http://einstein.drexel.edu/courses/CompPhys/General/C_basics/c_tutorial.html

It's not Amiga-specific, but enough to hopefully give a good flavor of the language.
-Ed-
 

Offline Hyperspeed

  • Hero Member
  • *****
  • Join Date: Jun 2004
  • Posts: 1749
    • Show only replies by Hyperspeed
Re: Learning C with the Amiga
« Reply #109 on: February 09, 2007, 04:56:51 AM »
ethierbach: Ahhh, now that's more like it. C for the mentally challenged!

:-D

I wonder what happened to Amiga-E, there was an article on it in Amiga Format once and Iconian was alledgedly programmed in E.

To be able to code Workbench apps like this would be really cool but they'd have to be super lean.

I long for the day when I can code in Assembly too; those apps/games always seem so damn fast.

EDIT:
I suppose GoldEd would be good for writing code on Amiga because it allows you to change the colours of the text. You'd be able to save work and have a mate highlight in red the crap that needs refining...

;-)
 

Offline AJCopland

Re: Learning C with the Amiga
« Reply #110 on: February 09, 2007, 09:25:51 AM »
@falemagn
Yeesh, yuck and no! :-x

That kinda code is itself a comment as it's a simple assignment, but what are you going to use it for and what is the number 2 representing? Bad commenting.

/* integer variable 'aIdx' is used in the following loop to iterate through mini-game actors updating them. Has initial value of 2 because first 2 entries are player 1 & player 2 */
int aIdx = 2;
for( ; aIdx < numMiniGameActors ; ++aIdx )
{
MiniGameActors[aIdx]->update(deltaTime);
}


That is a mildly spurious example, but it IS still a more valid example. Someone else will read your code, probably you, in 6 months time and be glad that you documented why you used the value 2. Also renamed 'a' to 'aIdx' to more clearly indicate that it is used as an index.

You're never writing code just for yourself, you're writing code for you in 6 months time as well. Not only that but it's good practice to comment why you did something so that when you are working on a group project you don't forget to do it for others who might be less experienced than you. (I see this lack of *why* comments everyday on the current project, it slows down work and is poor coding style).

Andy
Be Positive towards the Amiga community!
 

Offline falemagn

  • Sr. Member
  • ****
  • Join Date: May 2002
  • Posts: 269
    • Show only replies by falemagn
    • http://www.aros.org/
Re: Learning C with the Amiga
« Reply #111 on: February 09, 2007, 09:54:26 AM »
Quote

AJCopland wrote:
@falemagn
Yeesh, yuck and no! :-x

That kinda code is itself a comment as it's a simple assignment,


Wasn't that the point? Self-documenting code, indeed. You said it yourself. :-)

And yes, I admitted it was an extreme case, just to show the point. That doesn't mean that other, not so extreme cases, don't exist. In fact, read on...

Quote

but what are you going to use it for and what is the number 2 representing? Bad commenting.


That was all that had to be commented, an assignment. 2 represents 2, that's all. If I wanted 2 to mean something else than just 2, I wouldn't have used 2, but rather a named constant: again, self-documenting code. In fact, read on...

Quote

Code: [Select]

/* integer variable 'aIdx' is used in the following loop to
iterate through mini-game actors updating them. Has initial
value of 2 because first 2 entries are player 1 & player 2
*/
int aIdx = 2;
for( ; aIdx < numMiniGameActors ; ++aIdx )
{
    MiniGameActors[aIdx]->update(deltaTime);
}


That is a mildly spurious example, but it IS still a more valid example. Someone else will read your code, probably you, in 6 months time and be glad that you documented why you used the value 2. Also renamed 'a' to 'aIdx' to more clearly indicate that it is used as an index.


Problem is, commenting is superfluous in the above code.

1) "integer variable 'aIdx'" - the name of the variable is clear as it's clear it's an integer, no need to spell those in English.

2) "is used in the following loop to iterate through mini-game actors updating them" - the fact it's a loop it's pretty self evident, the fact it's looping through mini-game actors is evident too, by the name of the MiniGameActors array that is being indexed, and the fact that those actors are being updated is also obvious, for the mere fact that the update() method is being invoked on them.

3) "Has initial value of 2 because first 2 entries are player 1 & player 2" - so then, if that's what 2 means, why not state that explicitely in the code? You could have done it like this:

Code: [Select]


enum { numPlayers = 2; }

for(int aIdx = numPlayers; aIdx < numMiniGameActors; ++aIdx)
{
    MiniGameActors[aIdx]->update(deltaTime);
}


There, no need to write any comment, the code is indeed self-documenting. In fact, looking at this code for the 1st time I can say that this is a loop going through mini game actors so that they are updated taking into consideration the time elapsed since last time they were updated. There are 2 (human?) players in the game which are considered to be "actors" too, for the sake of code simplicity, but that don't need to be updated, as they are not controlled by the AI, and those 2 players are the 1st and 2nd element of the MiniGameActors array, therefore we skip them when updating the rest.

Quote

You're never writing code just for yourself, you're writing code for you in 6 months time as well. Not only that but it's good practice to comment why you did something so that when you are working on a group project you don't forget to do it for others who might be less experienced than you. (I see this lack of *why* comments everyday on the current project, it slows down work and is poor coding style).


I'm a professional programmer, highly rewarded for my work too, been programming since I was 6. Don't assume I don't know what I'm talking about, thanks. ;-)
 

Offline AJCopland

Re: Learning C with the Amiga
« Reply #112 on: February 09, 2007, 10:04:00 AM »
@falemagn
I didn't mean to sound like you don't know what you're talking about, I'm merely at work and brevity matters when I'm replying. Sorry.

No my example wasn't ideal because I literally copied your example and expanded on it ever so slightly. However as you say you've been coding since you were 6. We have coders who have just graduated and hadn't touched a compiler until they hit Uni'.

Code wholly without comments won't do. We have a good set of guidelines here about what to comment and why (comment the purpose and usage for a class but not how for example), but I find your guidance that comments are pointless doesn't reflect what the less experienced coders have to deal with. That's why I suggest that over-commenting is better than under commenting.

Over-comment and everyone gets the bloody idea whether they want to or not. Under-comment and there's a chance that someone won't when they really really need to.

Andy
Be Positive towards the Amiga community!
 

Offline Jupp3

  • Sr. Member
  • ****
  • Join Date: Mar 2002
  • Posts: 364
    • Show only replies by Jupp3
    • http://jupp3.amigafin.org
Re: Learning C with the Amiga
« Reply #113 on: February 09, 2007, 11:28:45 AM »
Quote
Question 1

Is there a way that a function can return more than one value simultaneously? Its obvious from the function definition syntax so far that you return a single value or nothing at all (void).

I read the thread quite quickly, and I'm not sure if this was already mentioned. At least there were some talks about pointers to structs, which are quite widely used.

Anyway, you can give function pointers to a variable.

For example:
void ChangeXandY(int * x, int * y, int xdiff, int ydiff)
{
    *x+=xdiff;
    *y+=ydiff;
}
int main(void)
{
    int x=4;
    int y=5;
    ChangeXandY(&x, &y, 1, 2);
    return x+y;
}

Doesn't make much sense, but demonstrates one way to do what you asked. Might not be the best way to achieve ">1 return values", but it's good-to-know, not least becouse it's used often in f.ex. OpenGL and SDL.

Also if you use pointer as argument but don't want to change the data, you can add "const" so that the compiler will warn you if you try to do that ;-)

With struct, you can just do like this:
void somefunc(struct somestruct * data)
{
    int x;
    x=data->intvalue;
}

Oh, if you end up using structs a lot (and even if you don't), it's a good idea to learn typedef early on, that would have saved me lots of code editing later on :-)
You can do f.ex.
typedef struct
{
    float x;
    float y;
    float z;
}Point3D;

And later in code, instead of doing something like this:
struct Point3D point;

you can do this:
Point3D point;

At least I find it more "clean" way to "say the same thing in C code"
 

Offline Louis Dias

Re: Learning C with the Amiga
« Reply #114 on: February 09, 2007, 12:21:39 PM »
Code: [Select]
Structure Point
   Dim X as Double
   Dim Y as Double
   Dim Z as Double
   Dim Name as String
End Structure

Sub Main()
   Dim Position as Point
   Dim Speed as Double = 1.0

   Position = SetInitialPosition(&quot;Orbit 1&quot;)

   For Angle as Integer = 0 to 359  'Default stepping is 1
      Position = Plot(Position, Speed, Angle)
      DrawIt(Position)
   Next Angle
End Sub

Function SetInitialPosition(ByVal Name as String) as Point
   Dim Vector as New Point

   Vector.X = 1
   Vector.Y = 0
   Vector.Z = 0
   Vector.Name = Name

   Return Vector  'here you can alternately do this:  SetIntitialPostion = Vector
End Function

Function Plot([b]ByVal[/b] aPoint as Point, ByVal Velocity as Double, ByVal Theta as Integer) as Point
...
End Function


The PLOT function above, the structure is passed by value as well.  However, I could have written it to pass by reference like this:

Function Plot(ByRef aPoint as Point, ByVal Velocity as Double, ByVal Theta as Integer) as Point
...
End Function

This example isn't good because it alludes to designing a "game" where global variables are favored over proper programming design for the sake of speed.

Oops, wrong language!  :flame:
 

Offline koaftder

  • Hero Member
  • *****
  • Join Date: Apr 2004
  • Posts: 2116
    • Show only replies by koaftder
    • http://koft.net
Re: Learning C with the Amiga
« Reply #115 on: February 09, 2007, 01:07:09 PM »
Why post vb.net stuff in a thread about C? If you want to go over some vb.net stuff, why not do it in a vb.net thread? I'm game. I could use some vb.net review as I have yet to actually move over to that environment ( and there are lots of us out there ).
 

Offline falemagn

  • Sr. Member
  • ****
  • Join Date: May 2002
  • Posts: 269
    • Show only replies by falemagn
    • http://www.aros.org/
Re: Learning C with the Amiga
« Reply #116 on: February 09, 2007, 01:13:27 PM »
Quote

AJCopland wrote:

[...]

No my example wasn't ideal because I literally copied your example and expanded on it ever so slightly. However as you say you've been coding since you were 6. We have coders who have just graduated and hadn't touched a compiler until they hit Uni'.


But that's beside the point. The code you showed has become self-documenting once I reworked it the way I showed, can you dispute that? Your English comment was totally redundant.

Quote

Code wholly without comments won't do.


Well, the piece of code that you've shown doesn't require a comment, do you disagree?

I myself stated that there's place for comments, particularly to describe how does an algorithm function, or where the code itself isn't able to convey all the information necessary to understand why the code was written how it was written and what it does. But the occurrence of the latter situation can be reduced to a minimum by using a few simple rules:

1) give variables and functions and all other kind of symbols a meangiful name, apt at describing what that symbol is meant to do;

2) don't use literal constant numbers if those numbers have a meaning attached, use named constants instead;

3) factor your code in such a way that common patterns are implemented by using either functions or preprocessor macros with a meaningful name: the higher level your code becomes, the more explicative it gets.

Quote

We have a good set of guidelines here about what to comment and why (comment the purpose and usage for a class but not how for example), but I find your guidance that comments are pointless doesn't reflect what the less experienced coders have to deal with.


I never said that comments are pointless, nor I think they are.

Quote

That's why I suggest that over-commenting is better than under commenting.

Over-comment and everyone gets the bloody idea whether they want to or not. Under-comment and there's a chance that someone won't when they really really need to.


I gotta disagree. English is a language much more difficult than C, if you're not good at expressing yourself properly in C, I very much doubt that you're going to do a better job at English. I'm not talking about you specifically, of course, this is rather a general rule that I've come to deduce by years of experience and looking at other people's code and comments.

What's more, too many comments clutter code. I want to see the code, I've grown a sense of aestethic towards code, so much that even if code I write works, and does so well, I keep adjusting it till it looks "beautiful", in the same sense as a well written and thought out mathematical equation can be "beautiful".

Self-descriptive code makes code beautiful.
 

Offline Louis Dias

Re: Learning C with the Amiga
« Reply #117 on: February 09, 2007, 01:19:56 PM »
Quote

koaftder wrote:
Why post vb.net stuff in a thread about C? If you want to go over some vb.net stuff, why not do it in a vb.net thread? I'm game. I could use some vb.net review as I have yet to actually move over to that environment ( and there are lots of us out there ).


If you need to know anything about vb, feel free to PM me.

Well, mostly, people here talk about how C is powerful and clear(cough) and blah blah blah, self-documenting(cough cough), blah blah, well I posted some VB code for comparison.  Let's leave it at that.  
 

Offline Louis Dias

Re: Learning C with the Amiga
« Reply #118 on: February 09, 2007, 01:23:54 PM »
falemagn wrote:
Quote

Self-descriptive code makes code beautiful.


I agree.  I know you weren't addressing me, but VB code is VERY self-descriptive.  C, not so much.  Please reference my code.  No flames intended.
 

Offline falemagn

  • Sr. Member
  • ****
  • Join Date: May 2002
  • Posts: 269
    • Show only replies by falemagn
    • http://www.aros.org/
Re: Learning C with the Amiga
« Reply #119 on: February 09, 2007, 01:39:31 PM »
Quote

lou_dias wrote:
falemagn wrote:
Quote

Self-descriptive code makes code beautiful.


I agree.  I know you weren't addressing me, but VB code is VERY self-descriptive.  C, not so much.  Please reference my code.  No flames intended.


I gently refuse to comment on that in this thread. Feel free to open another thread. :-)