#Checking whether having nested classes is a good idea

1 messages · Page 1 of 1 (latest)

cinder onyx
#

Hi there, if i have a class 'Coordinate' that stores the coordinates for a NoFlyZone which is another class, would it be good practice to have these two classes nested?

light atlasBOT
#

<@&987246399047479336> please have a look, thanks.

light atlasBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

iron geyser
#

It depends, I would say. If a class is only used by another class, e.g. to group recurring attributes internally, it might be a good idea to do so, in order to avoid pollution of the public namespace. But in case that the potential nested class represents a more general concept or this class is also exposed by the super class via getters/setters or other methods, I would tend to make them standalone.

For me "Coordinate" sounds like a more general concept, so I would assume it is a standalone class

cinder onyx
#
public record Coordinates(double longitude, double latitude) {

    @JsonCreator
    public Coordinates(double[] coordinates) {
        this(coordinates[0], coordinates[1]);
    }

} ```
light atlasBOT
cinder onyx
#
public class NoFlyZone {

    private String name;
    private ArrayList<Coordinates> coordinates;


    public NoFlyZone(@JsonProperty("name") String name, @JsonProperty("coordinates") ArrayList<Coordinates> coordinates){
        this.name = name;
        this.coordinates = coordinates;

    }

    public String getName() {
        return name;
    }

    public ArrayList<Coordinates> getCoordinates() {
        return coordinates;
    }```
light atlasBOT
# cinder onyx ```java public class NoFlyZone { private String name; private ArrayList...

Detected code, here are some useful tools:

Formatted code
public class NoFlyZone {
  private String name;
  private ArrayList<Coordinates> coordinates;
  public NoFlyZone(@JsonProperty("name") String name, @JsonProperty("coordinates") ArrayList<Coordinates> coordinates) {
    this .name = name;
    this .coordinates = coordinates;
  }
  public String getName() {
    return name;
  }
  public ArrayList<Coordinates> getCoordinates() {
    return coordinates;
  }
iron geyser
#

Since you return the Coordinates via getter, there is a high chance that another class need to use coordinates as well... Also pls return a List instead of an ArrayList... In your example I would tend to a standalone class+

cinder onyx
#

on whether to nest them or not

gilded needle
#

@cinder onyx please stop reposting the same question several times

cinder onyx