 
                
                463 posts
                
                    
                        Location
                        Star Wars
                    
                
             
            
                Posted 24 May 2018 - 06:43 PM
                Imagine you have a class called Widget. Now you create a object from Widget, and you would append it to the global Widget.
local wid = Widget()
wid:resize(10, 10)
-- #  wid:getSize() would return the witdth and the height of wid
-- #  imagine globalWidget would be the screen
globalWidget:addChild(wid)
Now, you want to reposition the widget. What do you think is better and/or more intuitive? Please give a reason (e. g. just more intuitive or an real advantage).
globalWidget:repositionChild(wid, 5, 5)
-- #  Or
wid:reposition(5, 5)
I am not sure, because the position of an object is an information that doesn't belongs to that (it doesn't defines the state of this object).
Edited on 24 May 2018 - 05:40 PM
                
             
         
        
        
            
            
                
                     
                
                64 posts
                
                    
                        Location
                        C:\Minecraft\saves\stuff\computer
                    
                
             
            
                Posted 24 May 2018 - 06:55 PM
                I think wid:reposition is better.
                
             
         
        
        
            
            
                
                     
                
                463 posts
                
                    
                        Location
                        Star Wars
                    
                
             
            
                Posted 24 May 2018 - 07:08 PM
                I think wid:reposition is better.
Okay, but can you tell me please why? Is it just more intuitive or has it an advantage?
 
         
        
        
            
            
                
                     
                
                749 posts
                
                    
                        Location
                        BOO!!
                    
                
             
            
                Posted 24 May 2018 - 07:14 PM
                It has reliance on less variables
Plus, it is better off to leave an object to modify it's own internals, in case they are changed
                
                    Edited on 24 May 2018 - 05:14 PM
                
             
         
        
        
            
            
                
                     
                
                463 posts
                
                    
                        Location
                        Star Wars
                    
                
             
            
                Posted 24 May 2018 - 07:26 PM
                It has reliance on less variables
Plus, it is better off to leave an object to modify it's own internals, in case they are changed
It is a matter of perspective, because the parent object will hold the information for all pixels, to form the image. Or do you have another solution?
 
         
        
        
            
            
                
                     
                
                7083 posts
                
                    
                        Location
                        Tasmania (AU)
                    
                
             
            
                Posted 24 May 2018 - 11:16 PM
                I'd tell the child to move. Otherwise, what happens if you have children of children? You'd have to figure out parents of parents.
                
             
         
        
        
            
            
                
                     
                
                3057 posts
                
                    
                        Location
                        United States of America
                    
                
             
            
                Posted 24 May 2018 - 11:59 PM
                In my experience with existing frameworks (eg. Qt), each "layer" of objects will have its own reposition-like method.  That method shifts itself and all of its children.
                
             
         
        
        
            
            
                
                     
                
                463 posts
                
                    
                        Location
                        Star Wars
                    
                
             
            
                Posted 25 May 2018 - 07:48 AM
                I'd tell the child to move. Otherwise, what happens if you have children of children? You'd have to figure out parents of parents.
Think this is a good reason, i didn't thought in that way. Thank you!
 
         
        
        
            
            
                
                     
                
                749 posts
                
                    
                        Location
                        BOO!!
                    
                
             
            
                Posted 25 May 2018 - 12:24 PM
                Also, what if you change the parent of the child? Sometimes it is possible to do so.
                
             
         
        
        
            
            
                
                     
                
                797 posts
                
             
            
                Posted 25 May 2018 - 03:52 PM
                I'd actually make it configurable. Giving parents control over child positioning lets you do cool things like auto-arranging grids/lists, and HTML-like 'flow' layouts. Of course, sometimes you want to just say "put this here exactly" so that's why I'd not say either one is the best standalone solution.
                
             
         
        
        
            
            
                
                     
                
                749 posts
                
                    
                        Location
                        BOO!!
                    
                
             
            
                Posted 25 May 2018 - 04:15 PM
                But what if you're not making a layout manager? Then the other way is easier
What about a compromise: we do both
                
                    Edited on 25 May 2018 - 02:16 PM
                
             
         
        
        
            
            
                
                     
                
                327 posts
                
                    
                        Location
                        Julfander Squad Studio
                    
                
             
            
                Posted 25 May 2018 - 09:07 PM
                globalWidget:repositionChild(wid, 5, 5)
If I would implement the reposition function in the parent, I would do it like this:
globalWidget:getChild(wid):reposition(5, 5)
This would also fix the issue Bomb Bloke pointed out:
I'd tell the child to move. Otherwise, what happens if you have children of children? You'd have to figure out parents of parents.
Edited on 25 May 2018 - 07:07 PM
                
             
         
        
        
            
            
                
                     
                
                477 posts
                
                    
                        Location
                        Germany
                    
                
             
            
                Posted 25 May 2018 - 10:53 PM
                If I would implement the reposition function in the parent, I would do it like this:
globalWidget:getChild(wid):reposition(5, 5)
Tbh that doesn't really make sense because wid already is the child which was appended so globalWidget:getChild(wid) should return wid(If it returns anything else it would be pretty strange) and if it returns wid that would be the same as:
wid:reposition(5, 5)
 
         
        
        
            
            
                
                     
                
                327 posts
                
                    
                        Location
                        Julfander Squad Studio
                    
                
             
            
                Posted 26 May 2018 - 08:46 AM
                If I would implement the reposition function in the parent, I would do it like this:
globalWidget:getChild(wid):reposition(5, 5)
Tbh that doesn't really make sense because wid already is the child which was appended so globalWidget:getChild(wid) should return wid(If it returns anything else it would be pretty strange) and if it returns wid that would be the same as:
wid:reposition(5, 5)
Oh, I did something wrong there. I meant it like this:
globalWidget.wid = widget(1, 1)
globalWidget.wid:reposition(5, 5)