2

I am using Scala for my example but I believe this probably could apply anywhere.

Say I am using a class somewhere in my project that reads in data from a live stream of events which monitors where a player is in a 2D game at any given moment.

case class Player(player_id: String, time: Long, age: Int, color: String, position_x: Int, position_y: Int)

I then take this live data and store it in a database.

Now somewhere else in a different part of my project I want to collect a series of data related to the player and run some calculations on it involving the player_id, time, position_x, and position_y fields. But because my Player class has age, and color I will have to set these to 0, and null respectively as I do not care about those fields. So it is providing to be unnecessarily bulk (in this example its not much data but in my actual project my class has many other attributes that I would need to ignore and set which increases the memory load unnecessarily)

What would be the best practice for this then or alternative?

Would I create a new class like

case class PlayerLimitedData(player_id: String, time: Long, position_x: Int, position_y: int) 

I could see this could easily get out of control in other places in my project where I need to run statistics where I just need a view piece of information about a player and not all the information.

2

This is a classical trade-off situation. Shall we reuse something which is already there, but does not fit perfectly, or shall we create something which may fit better, but may come for the price of duplicating some code or "reinventing the wheel"?

IMHO sometimes creating a new class is the best option, sometimes using the old class (and ignoring the attributes you don't need) is better. Here is a list of things I would recommend to take into account:

  • "how much existing code can you reuse by using the existing class" vs. "how much new code will you have to write by using a new class" (and may that additional code increase future maintenance efforts)?

  • will reusing the old class introduce an unwanted dependency or coupling into your program, which you can avoid by creating a new class?

  • will you really save memory by using a new class, and will this make a notable difference? Or will the difference for the memory load be negligible?

  • will creating a new class allow you to create a more generic solution for your specific use case, because you make exactly the required attributes available for a mechanism like reflection? Or is there no such benefit to expect?

  • will the new class give you a better abstraction for the specific use case in the context of that "different part of your project"?

Note most of these question are just rules-of-thumb, often there is no clear "right" or "best" solution to this, for many cases, this can be matter of personal taste and style.

0

The problem with null (and zero) is when it's not obvious what it means. Worse, it can seem obvious when it's not.

Null is so completely dependent on it's context for meaning that simply adding another idea into the abstraction it lives in changes what people might assume it means.

The real problem is that null represents an idea without a name. When that causes confusion, change things to give the idea a name.

It is dangerously easy to assume nulls meaning is obvious to others when it's obvious to you. Don't assume. Be suspicious and paranoid. Ask someone else. Not just when you create null. Any time you change the stuff around it.

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.