Welcome, Guest. Please login or register.

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

Description:

0 Members and 1 Guest are viewing this topic.

Offline falemagn

  • Sr. Member
  • ****
  • Join Date: May 2002
  • Posts: 269
    • Show all replies
    • http://www.aros.org/
Re: Learning C with the Amiga
« on: February 04, 2007, 04:55:12 AM »
Quote

lou_dias wrote:
Quote

mel_zoom wrote:
lou_dias:

I never quit once I set my mind on something. I dont think the language looks that bad just yet!

This one worked fine....

#include

int main(void)
{
  const char *result;
  ...
  result = "was funny";
  ...
  result = "backfired";
  ...
}



ah yes, another thing about C syntax that makes no sense

"oh but the address stays the same" ... oh but it still leads to confusion because a variable declared as a constant should remain a constant, otherwise, why call it a constant?


The const qualifier in const char *var doesn't mean that var is constant, rather that the data it points to is constant.

To get a constant pointer to char you write char *const var.
 

Offline falemagn

  • Sr. Member
  • ****
  • Join Date: May 2002
  • Posts: 269
    • Show all replies
    • http://www.aros.org/
Re: Learning C with the Amiga
« Reply #1 on: February 04, 2007, 05:08:00 AM »
Quote

I plan to learn c++ once I have a better understanding of c. At least I was told this was a good idea :)


C++ isn't strictly a superset of C, so that may actually give you a few headaches. But you do look skilled, so I'm quite sure you'll overcome all the bad sides of that. ;-)

Yet, any good C++ book, even an online one, will give you enough information about all the C you need to know to code good C++, so starting with C++ might be a good idea overall. Except, it won't help you much with the Amiga API, which is strictly C-based.

Statistically (but don't ask me to quote any numbers, don't have them at hand :-P) the majority of bad C++ programmers are the ones trying to use C++ as a "C with classes".


 

Offline falemagn

  • Sr. Member
  • ****
  • Join Date: May 2002
  • Posts: 269
    • Show all replies
    • http://www.aros.org/
Re: Learning C with the Amiga
« Reply #2 on: February 04, 2007, 12:41:41 PM »
Quote

Karlos wrote:

I should add that, IMNSHO, anybody that knows C and doesn't think it's worth making the jump to C++ either hasn't understood what OOP actually is and how it applies to any non-trivial project or hasn't realised just how much easier C++ can make many problems in C appear.


I must admit that the OO aspect of C++ isn't what I like most: I'm a fan of template metaprogramming, operator overloading and data abstraction, I just love how you can make C++ behave almost like any other language (albeit with limitations that hopefully C++0x will get rid of).

Quote

As for dealing with amiga APIs after migrating to C++, well that's where the fun is; inventing your own class libraries to wrap it all up for you ;-)


That may be fun, but it's tedious to reinvent the wheel each and every time. Would be nice to have a publicly available c++ foundation class library.
 

Offline falemagn

  • Sr. Member
  • ****
  • Join Date: May 2002
  • Posts: 269
    • Show all replies
    • http://www.aros.org/
Re: Learning C with the Amiga
« Reply #3 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 falemagn

  • Sr. Member
  • ****
  • Join Date: May 2002
  • Posts: 269
    • Show all replies
    • http://www.aros.org/
Re: Learning C with the Amiga
« Reply #4 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 falemagn

  • Sr. Member
  • ****
  • Join Date: May 2002
  • Posts: 269
    • Show all replies
    • http://www.aros.org/
Re: Learning C with the Amiga
« Reply #5 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 falemagn

  • Sr. Member
  • ****
  • Join Date: May 2002
  • Posts: 269
    • Show all replies
    • http://www.aros.org/
Re: Learning C with the Amiga
« Reply #6 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. :-)
 

Offline falemagn

  • Sr. Member
  • ****
  • Join Date: May 2002
  • Posts: 269
    • Show all replies
    • http://www.aros.org/
Re: Learning C with the Amiga
« Reply #7 on: February 12, 2007, 03:18:54 PM »
Guys,

To me it looks like Mel is clever enough to overcome all the little nitty-gritty details that may have put some of you off. Pointers, strings, arrays, are a non issue for some people, even if C is their first language. And something tells me C isn't the first language for Mel.

Besides, many of the issues that some of you guys have raised about C are just non existent in C++: C++'s got a pretty decent string class, bound-checked arrays (vector class) and pretty nice memory handling classes (boost's shared_ptr class comes to mind).

Mel, I don't know what kind of role and expertise you have in your company, but if you're even slightly interested in general Computer Science, with some math background, and are as fascinated by languages in general even just one third than I am, then once you've grasped the basics of C, C++ and the Boost Library will be your "paradise" (for lack of better term). :-)
 

Offline falemagn

  • Sr. Member
  • ****
  • Join Date: May 2002
  • Posts: 269
    • Show all replies
    • http://www.aros.org/
Re: Learning C with the Amiga
« Reply #8 on: February 13, 2007, 09:26:00 AM »
@ Cymric

Why would a text parser need to concatenate strings?
 

Offline falemagn

  • Sr. Member
  • ****
  • Join Date: May 2002
  • Posts: 269
    • Show all replies
    • http://www.aros.org/
Re: Learning C with the Amiga
« Reply #9 on: February 13, 2007, 01:39:46 PM »
Quote

lou_dias wrote:

So you've never built SQL statements on the fly based on parameters in a filtering selection screen for a program that generates a report?


And that's text parsing exactly in which way?

On the costructive side: yes, in those cases - which however have nothing to do with parsing, the standard C library string functions are a pain to use. However, you can use whichever string library suits you best, or even not use C but rather C++.