Thought not required



  • I just recently got interested in vPython for doing graphics.  I thought I'd look at the code they had as examples of good form, and found this little gem:

        	if pfc.x > self.boidflock[aboid].x:
    pfc.x = (pfc.x - self.boidflock[aboid].x)*self.FACTOR
    if pfc.x < self.boidflock[aboid].x:
    pfc.x = (self.boidflock[aboid].x - pfc.x)*self.NEGFACTOR
    if pfc.y > self.boidflock[aboid].y:
    pfc.y = (pfc.y - self.boidflock[aboid].y)*self.FACTOR
    if pfc.y < self.boidflock[aboid].y:
    pfc.y = (self.boidflock[aboid].y - pfc.y)*self.NEGFACTOR
    if pfc.z > self.boidflock[aboid].z:
    pfc.z = (pfc.z - self.boidflock[aboid].z)*self.FACTOR
    if pfc.z < self.boidflock[aboid].z:
    pfc.z = (self.boidflock[aboid].z - pfc.z)*self.NEGFACTOR

    <font face="Times New Roman" size="4">I mean, it isn't terrible, but I probably would have just used
    <font size="2"><font face="Courier New">pfc = (pfc - self.boidflock[aboid]) * self.FACTOR</font></font>
    </font>



  • @dmwit said:

    I just recently got interested in vPython for doing graphics.  I thought I'd look at the code they had as examples of good form, and found this little gem:
        	if pfc.x > self.boidflock[aboid].x:
    pfc.x = (pfc.x - self.boidflock[aboid].x)*self.FACTOR
    if pfc.x < self.boidflock[aboid].x:
    pfc.x = (self.boidflock[aboid].x - pfc.x)*self.NEGFACTOR
    if pfc.y > self.boidflock[aboid].y:
    pfc.y = (pfc.y - self.boidflock[aboid].y)*self.FACTOR
    if pfc.y < self.boidflock[aboid].y:
    pfc.y = (self.boidflock[aboid].y - pfc.y)*self.NEGFACTOR
    if pfc.z > self.boidflock[aboid].z:
    pfc.z = (pfc.z - self.boidflock[aboid].z)*self.FACTOR
    if pfc.z < self.boidflock[aboid].z:
    pfc.z = (self.boidflock[aboid].z - pfc.z)*self.NEGFACTOR

    <FONT face="Times New Roman" size=4>I mean, it isn't terrible, but I probably would have just used
    <FONT size=2><FONT face="Courier New">pfc = (pfc - self.boidflock[aboid]) * self.FACTOR</FONT></FONT>
    </FONT>

    True, but the sample code in a lot of packages is not written for brevity; it's written to illustrate to a product-noobie how the thing works under-the-hood. Sample code is usually less than ideal for illustrative purposes.



  • But... self.NEGFACTOR ??



  • Some coding styles state that you can't assume that the elements in a structure will assign with an assignment of the structure. In other words,

    pfc = pfc - modification

    is not the same as:

    pfc.x = pfc.x - modification

    pfc.z = pfc.z - modification

    etc.

    You've missed the big bug in the example code: What happens when pfc.value is the same as self.boidflock[aboid]?



  • @dmwit said:

    But... self.NEGFACTOR ??


    It seems likely that self.NEGFACTOR does not equal -self.FACTOR. This sure looks like part of a flocking algorithm, where a bunch of boids are following a leader. Maybe they want to make sure that the boids stay behind the leader, so they put a heavier correction on one side than on the other.



  • Man, it really sucks when you report a WTF and everybody defends it, doesn't it?

    Personally, I prefer the example code...it is much more explicit, and exploiting relatively obscure syntactical decisions is a sure way to cause confusion.  Impress people by doing it well, not by doing it short.



  • No, self.NEGFACTOR is most definitely set explicitly to -1 * self.FACTOR. =(
    ~d


Log in to reply