8

Anyone has a better way to calculate / display advancement % ?

I';m using that by habit for the past 15 years. But it's kind of boring to write every time.

Comments
  • 5
    Took me a moment to figure out the point of that p and pp until I realised that projects.Count may be much larger than 100 so that repeated messages with the same percentage could come up. :)
  • 2
    @Fast-Nop yep, the Count is around 27000.

    And there are 2 API calls for each.

    (It's a one time, discardable script, performances are not very important, but I still want to know where are we at)
  • 2
    @zlice It will be false every time a new item has been finished in that for loop BUT the displayed percentage number would stay the same because projects.Count is much larger than 100.

    Take projects.Count is 1000, you have finished 500 items, the percentage is 50%. Now item 501 gets finished within the for loop, that's 50.1%, but it's casted to int, so that's 50% again. Then the "if" is false so that you won't have 50% displayed again.

    It will only become true when item 510 has been finished because then it's 51%.
  • 0
    @zlice The times 100 is for converting from ratio to percentage, and for the reason of the if, see my above comments.
  • 2
    @Fast-Nop exactly.

    Basiclly that aproch worked for me for the past 15 years and I still can'tfind a better one :(
  • 0
    @zlice The +1 is for converting from offset, as in array index, to count, as in number of items done. You'll end up at exacly 100% because the last loop run has index = project.Count - 1, given that the loop condition is index < project.Count.
  • 1
    Imho:
    - "index" var is too verbose, "i" is fine, because everyone uses it
    - "p" and "pp" are hard to figure out

    Also, I would extract logging to separate function and pass in a lambda.

    projects.forEachLogging(
    (project) => {
    doSmth(project)
    }
    )

    PS: Syntax might not be correct, but you get the point.
  • 0
    @WildOrangutan projects.forEachLogging or something like that limits you in case of async and parallel operation.

    Here is an extract for same thing, but in mlti thread enviroment :
  • 0
    @zlice I think you are refering to the last comment using "one opercentage count"

    The original solution is 100% accurate. It will display advancement only if we get 1 more percantage (Rounded to int)

    Couldn't find a better way to deal with it in multi threads haha
  • 1
    This came to mind (I see it's kind of similiar to your comment above). It doesn't show the final 100%, and when projectCount is low it can skip numbers, but it will never repeat any number.
  • 1
    @WildOrangutan offtopic question.
    Why do you prefer i over index?
    Index is more descriptive of what it does, instead of i

    The reason I'm asking this, is that a long variable can always be autocompleted while a short one usually not so much.
  • 2
    Ugh. The naming makes me want to stab people.

    p is currentPercentage
    pP is previousPercentage

    Wouldn't a while loop be more simpel as it could integrate the calculation?

    The for loop seems kinda iffy to me.
  • 0
    @IntrusionCM pp = "current percantage"

    p is "last displayed percentage"

    In while you'll still need to have an a vraiable to count. The reason to use "for" is only because variable "progress" is in the loop
  • 2
    @thebiochemic just because "i" is so commonly used, that everyone knows what it is. That's why you can afford to shorten it, without impacting readability.

    You don't need to autocomplete single character variable tough :)

    I think code with less words reads faster. Tough there is a fine line, when it becomes hard to understand, like "pp" for example.

    The smaller the scope (e.g. function size), the shorter variable you can afford to have.

    I always try to name variables with single word. For example:

    // Too verbose
    var johnDeereTractor = new JohnDeereTractor()

    // My preference
    var tractor = new JohnDeereTractor()
  • 2
    @WildOrangutan agreed that code with less words reads better, but as you said only within reason.

    I also agree on using tractor instead of the full name as long as no ambiguity exists in all the related code whatsoever

    I deliberately will ignore the fact that i is popular. Because i is a name used by mathematicians, and as far as I'm aware, mathematicians never were good in naming their shit.

    to be fair, I do go as far as to say, you should not use abbreviations *unless* they are well established tech related components, such as json, html xml etc.

    You can hate me for that. But realistically this will make code more readable for people, who have just been thrown into your project.
  • 1
    Another way would be to use time instead of a current progression, for example run log every ≈second
Add Comment