Making an array that accepts only a string and instances of a class?

Summary

The problem at hand is to create an array that only accepts strings and instances of a DialogueCommand class in a Unity system for dialogue. The current implementation uses an object array, which can lead to errors and confusion as it allows any data type.

Root Cause

The root cause of this issue is the use of an object array, which is not type-safe. This allows any object to be added to the array, rather than restricting it to only strings and DialogueCommand instances. The causes of this issue include:

  • Lack of type safety in the current implementation
  • Inability to restrict the object array to specific data types
  • Need for a more robust and maintainable solution

Why This Happens in Real Systems

This issue occurs in real systems due to:

  • Inadequate design: Failing to consider the need for type safety and restrictions on data types
  • Overuse of object arrays: Relying too heavily on object arrays without considering the potential consequences
  • Insufficient testing: Not thoroughly testing the system to identify potential issues

Real-World Impact

The real-world impact of this issue includes:

  • Errors and exceptions: Allowing incorrect data types to be added to the array can lead to errors and exceptions
  • Confusion and maintenance issues: The lack of type safety can make the system more difficult to maintain and understand
  • Security vulnerabilities: In some cases, the lack of type safety can even lead to security vulnerabilities

Example or Code

public abstract class DialogueCommand { }

public class SetImage : DialogueCommand { 
    public string Image { get; set; } 
    public SetImage(string image) { Image = image; } 
}

public class DialogueLine {
    public string Text { get; set; }
    public DialogueCommand[] Commands { get; set; }

    public DialogueLine(string text, params DialogueCommand[] commands) {
        Text = text;
        Commands = commands;
    }
}

public DialogueLine[] CreateDialogueLines() {
    return new DialogueLine[] {
        new DialogueLine("Example text", new SetImage("Image1")),
        new DialogueLine("Another line", new SetImage("Image2"), new SetImage("Image3"))
    };
}

How Senior Engineers Fix It

Senior engineers fix this issue by:

  • Using generic collections: Instead of object arrays, using generic collections such as List or T[] to restrict the data types
  • Creating custom classes: Designing custom classes, such as DialogueLine, to hold the string and DialogueCommand instances
  • Implementing type safety: Ensuring that the system is type-safe and restricts the data types to only strings and DialogueCommand instances

Why Juniors Miss It

Junior engineers may miss this issue due to:

  • Lack of experience: Not having enough experience with type safety and generic collections
  • Insufficient knowledge: Not being familiar with the potential consequences of using object arrays
  • Rushing to implement: Focusing too much on getting the system working, rather than taking the time to design a robust and maintainable solution

Leave a Comment