4 Replies - 7768 Views - Last Post: 10 May 2013 - 09:16 AM

#1 Julyuary  Icon User is offline

  • D.I.C Head

Reputation: 38
  • View blog
  • Posts: 106
  • Joined: 20-December 11

Is This a Helper Class? Good or Bad?

Posted 09 May 2013 - 08:53 AM

So I'm working on a rhythm game, and there are a few objects that are accessed by different classes in the game. At first I tried passing things around by reference; that didn't go so well. I thought it'd be convenient to make a simple static class that allows easy access to these objects for any class. I'd like to know if this is a "helper class," and if so, is this a good thing? Here's the class in question;

    static class SongManager
    {
        private static readonly SongLoader songLoader = new SongLoader();
        private static List<SongTrack> songList = new List<SongTrack>();
        private static List<Texture2D> imageList = new List<Texture2D>();
        private static List<OggSong> musicList = new List<OggSong>();
        public static int CurrentSongIndex
        {
            get;
            set;
        }
        public static List<SongTrack> SongList
        {
            get { return songList; }
            set { songList = value; }
        }
        public static List<Texture2D> ImageList
        {
            get { return imageList; }
            set { imageList = value; }
        }
        public static List<OggSong> MusicList
        {
            get { return musicList; }
            set { musicList = value; }
        }
        public static void GetSongs(GraphicsDevice graphicsDevice)
        {
            songLoader.LoadSongs(ref songList, ref imageList, ref musicList, graphicsDevice);
        }
}


Is there a proper use of helper classes? I've done some googling and all I've seen is that it's generally discouraged. If so, are there any alternatives? I can't see how it's affecting my design badly now, but it may be in the long run. I also have an instance of it being used here...

        private void PositionNotes()
        {
            foreach (Note note in SongManager.SongList[SongManager.CurrentSongIndex].TopNotes)
            {
                float distanceFactor = (SongManager.SongList[SongManager.CurrentSongIndex].Speed * 60)
                    * (note.Occurance / 100);
                note.Position.X = distanceFactor + 140;
            }
            foreach (Note note in SongManager.SongList[SongManager.CurrentSongIndex].MiddleNotes)
            {
                float distanceFactor = (SongManager.SongList[SongManager.CurrentSongIndex].Speed * 60)
                    * (note.Occurance / 100);
                note.Position.X = distanceFactor + 140;
            }
            foreach (Note note in SongManager.SongList[SongManager.CurrentSongIndex].BottomNotes)
            {
                float distanceFactor = (SongManager.SongList[SongManager.CurrentSongIndex].Speed * 60)
                    * (note.Occurance / 100);
                note.Position.X = distanceFactor + 140;
            }
        }

One thing I do dislike about it currently is that it makes my lines a lot longer and what I'm referencing isn't local.

Is This A Good Question/Topic? 0
  • +

Replies To: Is This a Helper Class? Good or Bad?

#2 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5800
  • View blog
  • Posts: 12,636
  • Joined: 16-October 07

Re: Is This a Helper Class? Good or Bad?

Posted 09 May 2013 - 10:47 AM

First quick rewrite:
        
private void PositionNotes() { PositionNotes(SongManager.SongList[SongManager.CurrentSongIndex]); }

private void PositionNotes(SongTrack st) {
	foreach (Note note in st.TopNotes) {
		float distanceFactor = (st.Speed * 60) * (note.Occurance / 100);
		note.Position.X = distanceFactor + 140;
	}
	foreach (Note note in st.MiddleNotes) {
		float distanceFactor = (st.Speed * 60) * (note.Occurance / 100);
		note.Position.X = distanceFactor + 140;
	}
	foreach (Note note in st.BottomNotes) {
		float distanceFactor = (st.Speed * 60) * (note.Occurance / 100);
		note.Position.X = distanceFactor + 140;
	}
}



Second:
        
private void PositionNote(SongTrack st, Node note) {
	float distanceFactor = (st.Speed * 60) * (note.Occurance / 100);
	note.Position.X = distanceFactor + 140;
}

private void PositionNotes(SongTrack st) {
	foreach (Note note in st.TopNotes) { PositionNote(st, note); }
	foreach (Note note in st.MiddleNotes) {	PositionNote(st, note); }
	foreach (Note note in st.BottomNotes) { PositionNote(st, note); }
}



Last one
private void PositionNote(SongTrack st, Node note) {
	float distanceFactor = (st.Speed * 60) * (note.Occurance / 100);
	note.Position.X = distanceFactor + 140;
}

private void PositionNotes(IEnumerable<Note> notes) {
	foreach (Note note in notes) { PositionNote(st, note); }
}

private void PositionNotes(SongTrack st) {
	PositionNotes(st.TopNotes);
	PositionNotes(st.MiddleNotes);
	PositionNotes(st.BottomNotes);
}



Yes, giant globals in the sky are bad. Keep the collections in a single object, the one that calls everything, your main form. Pass what you need.
Was This Post Helpful? 1
  • +
  • -

#3 Julyuary  Icon User is offline

  • D.I.C Head

Reputation: 38
  • View blog
  • Posts: 106
  • Joined: 20-December 11

Re: Is This a Helper Class? Good or Bad?

Posted 09 May 2013 - 11:21 AM

That definitely looks a lot better than my original code, but are there any cases where helper classes are appropriate?
Was This Post Helpful? 0
  • +
  • -

#4 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4463
  • View blog
  • Posts: 7,777
  • Joined: 08-June 10

Re: Is This a Helper Class? Good or Bad?

Posted 09 May 2013 - 04:15 PM

You seem to be stuck on the idea of a "helper class". It's a really simple term:

http://en.wikipedia....ki/Helper_class

Quote

Helper Class is a programming technique in object-oriented programming. Helper classes are a term given to classes that are used to assist in providing some functionality, though that functionality isn't the main goal of the application.


That's all it is. Yes, they're frequently appropriate.
Was This Post Helpful? 1
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5800
  • View blog
  • Posts: 12,636
  • Joined: 16-October 07

Re: Is This a Helper Class? Good or Bad?

Posted 10 May 2013 - 09:16 AM

Helper class can mean many things. Basically, in OO, it's when you have methods that can to be used by more than one type of object and don't seem to cleanly belong to any particular object. For instance, I'll put something like standard number formats in a "helper" class.

What a helper class is NOT is a dumping ground for global mutable variables and shared state. That's called a bad idea. However, if required, it's often called a Singleton. Note, it is rarely required. Note, also, that static is rarely required.

Exposing an entire collection globally invalidates even the intended benefit of a singleton. If any part of the program can manipulate a collection without constraint, then you have spaghetti code on a epic scale.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1