Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7620559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T03:58:08+00:00 2026-05-31T03:58:08+00:00

Lets say I have some sort of graphical animation code where I have two

  • 0

Lets say I have some sort of graphical animation code where I have two classes: Sprite and SpriteAnimator. The SpriteAnimator is responsible for moving the sprites at regular intervals. The Sprite has a property which can lock it from movement though.

I first implemented this use case like this:

public class Sprite
{
    public bool Locked;
    public void MoveTo(int x, int y){}
}

public class SpriteAnimator
{
    private List<Sprite> Sprites;
    public void DoMovement()
    {     
        foreach (Sprite sprite in Sprites)
        {
            if (!sprite.Locked) MoveTo(newX, newY); 
        }
    }
}

…but then I remembered Tell-don’t ask principle, and I feel I’m asking about the state, making a decision and then tell them what to do – just like the principle forbids me. So I recode to this:

public class Sprite
{
    private bool Locked;
    public void MoveIfNotLockedTo(int x, int y) { ... }
}


public class SpriteAnimator
{
      private List<Sprite> Sprites;
    public void DoMovement()
    {     
        foreach (Sprite sprite in Sprites)
        {
            MoveIfNotLockedTo(newX, newY);
        }
    }
}

..but is this actually better code? I’m not sure how I feel about method names containg the word “If”.

There is also a third option – where the Controller takes ownership of the locked-states of the sprites. Something like this:

public class Sprite
{
    public void Move(int x, int y) { ... }
}


public class SpriteAnimator
{
  private List<Sprite> Sprites;
  private List<Sprite> LockedSprites;

    public void DoMovement()
    {     
        foreach (Sprite sprite in Sprites)
        {
            if (!LockedSprites.Contains(sprite) MoveTo(newX, newY);
        }
    }
}

…but this has a performance impact as I get O(N^2) loop.

So what do you guys think? Is it time to be pragmatic and choose option #1 which I feel is the best but violates the tell-don’t-ask-principle?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T03:58:10+00:00Added an answer on May 31, 2026 at 3:58 am

    Based on the principle, it looks like the second option is the way to go.

    Tell the object what you want. Let it figure out how to do it.

    Don’t worry too much about the function’s name, better yet, if you’re afraid of not remembering how that function works, just comment the function properly so it would be easily maintainable by you, or someone else.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say I have a view, myView, a view controller, myViewController, and some sort
Lets say we have this interface: interface IVehicle { ... } And some classes
Lets say I have some simple classes for a twitter-like app: User Post A
Lets say we have some basic AR model. class User < ActiveRecord::Base attr_accessible :firstname,
Lets say I have one row with three columns - some buttons on left
Lets say I have a simple 1D array with 10-20 entries. Some will be
I have a name in a database, lets say its DFectuoso and some legacy
Let's say I have some code like this: <html> <head><title>Title</title></head> <body> <?php if (!$someCondition){
Let's say I have some code like this if(isset($_GET['foo'])) //do something if(isset($_GET['bar'])) //do something
Let's say I have some Java code: public class SomeClass { static { private

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.