Categories
Mastering Development

Avoid returning writable references

I have a submission to do for software development and my professor urges us to not use return statements, that return object references.

The task is about a to-do list application, and I wonder if there is any easy way to not implement the todo-list as a object with such writable return references.
I managed to do this but since I use JDBC its just writing my tasks to the DB and the re-loading it into my list from the DB, which for me sounds like the same as just writing it directly in the list.

I have the lecture again next Thursday and will ask then, but maybe some of you has a clue.
In my opinion its completely justifiable to return a reference to the list, especially since the application changes a lot in scenes and windows.

Edit: Code attached.

Since I have not yet implemented it here is how I would do it.


public class TaskList {
    private static ArrayList<Task> taskList = new ArrayList<>();

    public  ArrayList<Task> getTaskList() {
        return taskList;
    }
}

Then I can get my TaskList in the different scenes like the calendar or overview by simply using the getter and modify the list.

Currently I have this code like that:

public class Calendar {


    private ArrayList<Task> usersTasks = new ArrayList<>();
    
    //Bunch of methods to draw my calendar

    private createTask(){

            Task createdTask = new Task(title,content,prio,color,duedate,today);

            //write task in mySQL db
            DatabaseHandler databaseHandler = new DatabaseHandler();
            databaseHandler.createTask(createdTask,user);
    }

    private getTasks(){

            usersTasks.clear();
            //new dbHandler to fetch as ResultSet and write in ArrayList userTasks

    }
}

I hope the snippets are explaining since I left a lot out. The example is for my calendar view.

So currently, when I create a task, I would write it in the DB, then clear my ArrayList and then re-fetch it.

Leave a Reply

Your email address will not be published. Required fields are marked *