• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Re: How to make Metronome?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Re: How to make Metronome?


  • Subject: Re: Re: How to make Metronome?
  • From: Wagner Truppel <email@hidden>
  • Date: Sat, 2 Sep 2006 19:22:44 -0700

<snip>
- (IBAction)slide:(id)sender{
sixty = 60;
beats = [view floatValue];
bpm = sixty / beats;
bpmtimer = [NSTimer scheduledTimerWithTimeInterval:bpm target:self selector:@selector(handletimer:) userInfo:nil repeats:YES];
[view setFloatValue:bpm];
}
<snip>

sixty = 60; ?????

So... if you later (for some reason) needed that changed to, say, 45, you'd do a global search and replace and replace that with

fortyfive = 45; ?

Haven't you heard of symbolic constants and #define? Additionally, while you're at it, how about a better name, something like

#define SECONDS_PER_MINUTE     60

I don't mean to personally criticize the author of the piece of code above, but only to point out that giving symbolic names to "magic numbers" in one's code, while generally a good practice, only makes sense if the names are more meaningful than the numbers themselves. Sometimes it's not even necessary to give a number a name, if (a) it's already crystal-clear what the number means in the context of where it's being used *and* (b) the number will never have to be changed. Moreover, if you *are* going to give a "magic number" a symbolic name, do so using a constant, not a variable.

This reminded me of a comment a student in one of the C++ classes I taught once had in his code:

return result; // return result

Now, I realize that this post is off-topic and I apologize for that, but I believe good practices are important (especially in software engineering, where people have to read each other's code all the time) and there was an opportunity not to be missed here, to help others improve their skills.

Wagner
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Prev by Date: Re: How to make Metronome?
  • Next by Date: Re: Question about categories
  • Previous by thread: Re: How to make Metronome?
  • Next by thread: Re: How to make Metronome?
  • Index(es):
    • Date
    • Thread