0

I created the following method to use rate safely.
(Sometimes rate can become invalid value like INFINITY, NAN, or out of 0-1)

-(double)XXXX:(double)rate
    if (rate >= 1) {
         return 1;
    } else if (rate <= 0) {
        return 0;
    } else if (0 <= rate && rate <= 1) {
        return rate;
    } else {
        return 0;
    }
}

What should I name this method?

EDIT:
I use rate to display progress of time with UISlider, UIProgress, or just NString(XX %).

Usage of the method are:

rate = [objectOrClass XXXX: currentTime / totalTime];
rate = [objectOrClass XXXX:(currentTime + additionalTime) / totalTime];

I also use it to calculate currentTime from rate:

currentTime = [objectOrClass XXXX:rate] * totalTime;
js_
  • 4,671
  • 6
  • 44
  • 61
  • I'm new to objective-c. So I'm not good at naming rules of it yet. – js_ Jul 13 '12 at 10:27
  • Same name as you would in other languages - what would the user who specified this algorithm call it? – mmmmmm Jul 13 '12 at 10:29
  • - (double)returnRate:(double)rate – Nitish Jul 13 '12 at 10:30
  • @Mark In other language, method name is `verb+Noun`. But in objective-c, method name is `noun` without `verb` like `stringWithFormat` or `imageNamed`. So `-(double)rate:(double)rate` is good name for objective-c? – js_ Jul 13 '12 at 10:35
  • ratio is not what the user calls this - a ratio is in effect one number dived by another - also in any language ratio is a bad name as ratio of what? – mmmmmm Jul 13 '12 at 10:37
  • pushViewController: has a verb :) name the method the way you will easily remember it. – janusfidel Jul 13 '12 at 10:37
  • I don't know why this question was voted for close. I for one like to follow the naming conventions of the language I use, and I don't find it subjective in any way. ObjC has a convention for naming these methods, as already pointed out. – Krumelur Jul 13 '12 at 10:46
  • @Krumelur I agree with you. I feel that more people want to close questions because they don't like the questions these days than a year ago. – js_ Jul 13 '12 at 10:54
  • We can write any name as method name that is our convenience and understanding purpose. – Prasad G Jul 13 '12 at 10:55
  • @js_: How and where did you call -(double)XXXX:(double)rate method? – Prasad G Jul 13 '12 at 11:08
  • @PrasadG thanks. I'd like to follow the objective-c's rule to write code. I added usage of the method to the question. – js_ Jul 13 '12 at 11:53
  • 1
    This would probably fit http://programmers.stackexchange.com/ better – James Webster Jul 13 '12 at 11:54
  • @JamesWebster thanks for good advice. i think so, too. i'll post questions there next time i want to ask this kind of question. – js_ Jul 13 '12 at 12:13

3 Answers3

1
static inline float RateAligned(float rate) {
    return MAX(.0f, MIN(1.0f, rate));
}
Max O
  • 997
  • 8
  • 13
  • thanks. I didn't know `static inline`. I tested the function and it works. I like short code. But `MAX(.0f, NAN) returns 0.0` but `MAX(NAN, .0f) returns NAN`. Is it safe to assume that `MAX(.0f, NAN) returns 0.0`. Or is it implementation-dependent? – js_ Jul 13 '12 at 13:30
0
-(double) adjustRate:(double)rate;
Pavel Reznikov
  • 2,968
  • 1
  • 18
  • 17
0
When totalTime is zero  you will get INFINITY.

When totalTime and currentTime both are zeros you will get NAN.

When (currentTime + additionalTime) and currentTime both are zeros you will get NAN.

I think it will be helpful to you.

Prasad G
  • 6,702
  • 7
  • 42
  • 65