2

I have a class which is used to get transfer data from the one application to another and then also to update if changes were made.

public class Data {
    private String name;
    private String number;
    private String info;

    ... getters/setters...
}

Let's say name and number will be updated if you change them but e.g. info is not. What's the best way to tell programmers in the future that this is intended so they can recognize it immediately?

Update: It's encoded as a JSON file and when I get it back I don't care about the info field anymore. It could be empty

ave4496
  • 2,950
  • 3
  • 21
  • 48
  • 1
    I'm thinking the word you're looking for is _persistence_. `name` and `number` are persistent, `info` is not. – S.L. Barth is on codidact.com Jan 19 '17 at 13:10
  • what options do I have except writing a comment next to the field? – ave4496 Jan 19 '17 at 13:10
  • If you were using Hibernate, then @Transient annotation would fit – Cristian Meneses Jan 19 '17 at 13:11
  • 3
    You are talking about sending these informations to another application, if you are using a webservice you should document the WS and write all the needed informations in the documentation rather than in a Bean. If using a DB, I would write that as a comment inside the method which updates the DB. The bean itself has nothing to do with the saved data, it's just a container. The method that saves/manipulates the data needs to explain what it's doing and why. – BackSlash Jan 19 '17 at 13:12

5 Answers5

2

You can create your custom annotation, specific to your application. If you are using any framework like Hibernate you can use @transient.

Rohit Gulati
  • 542
  • 3
  • 15
  • 2
    But OP never talked about DB nor Hibernate – BackSlash Jan 19 '17 at 13:13
  • To inform other progammers also annotation can be helpful like override, functional interface. I just gave the example of db :) @BackSlash – Rohit Gulati Jan 19 '17 at 13:19
  • 1
    OP did mention a custom annotation. Otherwise just a comment `// transient` would be useful - most people should understand it and the comparison with Hibernate, even if it's not being used, is valid. – vikingsteve Jan 19 '17 at 13:25
  • I talked about a DB but it actually used between applications – ave4496 Jan 19 '17 at 13:36
0

Start here https://docs.oracle.com/javaee/6/tutorial/doc/bnbpz.html

Almost any programmer seing a POJO like this will know that behaviour is what you explained....

@Table(name = "data")
public class Data {
    @Id
    @Column(name = "name")
    private String name;

    @Column(name = "number")
    private String number;

    private String info;

    ... getters/setters...
}

UPDATE: It's encoded as a JSON file and when I get it back I don't care about the info field anymore. It could be empty

Graham
  • 7,431
  • 18
  • 59
  • 84
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
0

Probably not the correct way, but if you are just talking about "informing" other programmers, you could simply put the transient keyword on your info field.

But of course, that would be really "informal"; as it would probably not at all affect how your framework is dealing with your fields.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • But since he said _What's the best way to tell programmers in the future that this is intended so they can recognize it immediately_, Programmers should read the documentation. – AxelH Jan 19 '17 at 13:30
0

I would use serialisation combined with the transient keyword

What is object serialization?

import java.io.*;
import java.util.*;

// This class implements "Serializable" to let the system know
// it's ok to do it. You as programmer are aware of that.
public class SerializationSample implements Serializable {

    // These attributes conform the "value" of the object.

    // These two will be serialized;
    private String aString = "The value of that string";
    private int    someInteger = 0;

    // But this won't since it is marked as transient.
    private transient List<File> unInterestingLongLongList;
Community
  • 1
  • 1
Tschallacka
  • 27,901
  • 14
  • 88
  • 133
0

There's no indication in your file that name or number are being persisted.

If you are going to put behavior into the file in some durable way, this isn't just a file, it's a representation of an object, where data and the related behavior live as one. Write a method clarifying the intent.

public boolean isStorable() {
    boolean isOk = true;
    isOk &= (name != null && name.length() > 0);
    isOk &= (number > 0);
    return isOk;
}

Makes it clear that not every one of these items contribute to being able to store the object, and that not every value within these items contribute to a valid storage state.

It also makes it clear that this object permits invalid states within its private data. That's a code smell that could indicate a design flaw. Perhaps you should look into whether that is a design flaw, and if it is, then fix it.

Edwin Buck
  • 69,361
  • 7
  • 100
  • 138
  • A bunch of fields with a bunch of do-nothing getters and setters is not a true Object Oriented Programming object in Java because it lacks any behavior. It is a C-style data structure. They are sometimes useful to have in Java, and you are describing the correct way to implement them, but if you implement such a thing, keep in mind that you also lose out on the benefits of OOP for this part of your program. – Edwin Buck Jan 19 '17 at 13:25
  • Also note that a lot of very useful frameworks, technologies, and tools are not idealistically OOP embracing. If you are storing with a specific persistence technology in mind, then embrace the conventions of that technology. For example, if persisting with JPA, then put in the annotations indicating which fields are persisted, to which database tables and columns. People will be able to make sense of your markup, because the framework will educate them as to its meaning. – Edwin Buck Jan 19 '17 at 13:34