Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (799)
Games in Android Showcase (237)
games submitted by our members
Games in WIP (865)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1] 2
  ignore  |  Print  
  simplifying getter/setter classes  (Read 15448 times)
0 Members and 1 Guest are viewing this topic.
Offline ags1

JGO Kernel


Medals: 367
Projects: 7


Make code not war!


« Posted 2014-10-25 23:58:56 »

I really hate Java beans and their screenfuls of boiilerplate code. I had an idea to get rid of this that I'm calling Matrix Methods:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
public class Bond {
    private double price, yield, spread;
   
    public {att} get{att}() {
        return {att(price, yield, spread)};
    }

    public {att} get{att}Bps() {
        return {att(price, yield, spread)} * 10000.0;
    }

    public {att} get{att}Percentage() {
        return {att(price, yield, spread)} * 100.0;
    }

}

Offline princec

« JGO Spiffy Duke »


Medals: 1126
Projects: 3
Exp: 20 years


Eh? Who? What? ... Me?


« Reply #1 - Posted 2014-10-26 00:16:30 »

Don't use JavaBeans then. They are a travesty of OOP design.

Here's a tip: if all you're doing with properties is getting and setting them ... use the dot operator. That's what it's for. Just remember that properties are often used in a way that is basically breaking the whole idea of OOP design ("encapsulation").

Cas Smiley

Offline ags1

JGO Kernel


Medals: 367
Projects: 7


Make code not war!


« Reply #2 - Posted 2014-10-26 10:15:11 »

The idea is this is more generalized than just getters and setters, I'm only using them as a simplke example...

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline KevinWorkman

« JGO Plugged Duke »


Medals: 288
Projects: 12
Exp: 12 years


HappyCoding.io - Coding Tutorials!


« Reply #3 - Posted 2014-11-05 14:00:58 »

I'm a little confused- are you trying to propose a feature that should be added to the language itself?

If beans really bother you, you might consider taking a page out of the Calendar class's book:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
import java.util.HashMap;
import java.util.Map;

public class Bond {

   public static enum BondField{
      price, yield, spread;
   }

   private Map<BondField, Double> fieldMap = new HashMap<BondField, Double>();
   {
      fieldMap.put(BondField.price, 0.0);
      fieldMap.put(BondField.yield, 0.0);
      fieldMap.put(BondField.spread, 0.0);
   }


   public double get(BondField field) {
      return fieldMap.get(field);
   }

   public void set(BondField field, double d){
      fieldMap.put(field, d);
   }

   public double getBps(BondField field) {
      return fieldMap.get(field) * 10000.0;
   }

   public double getPercentage(BondField field) {
      return fieldMap.get(field) * 100.0;
   }
}


Or you could convert the class into an immutable class, then add methods that return modified versions of the class, along with static helper methods for converting the fields to the information your want:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
public class Bond {
   
   public final double price;
   public final double yield;
   public final double spread;

   public Bond(double price, double yield, double spread){
      this.price = price;
      this.yield = yield;
      this.spread = spread;
   }

   public Bond withPrice(double price){
      return new Bond(price, this.yield, this.spread);
   }
   
   public Bond withYield(double yield){
      return new Bond(this.price, yield, this.spread);
   }
   
   public Bond withSpread(double price){
      return new Bond(this.price, this.yield, spread);
   }

   public static double bpsOf(double d) {
      return d * 10000.0;
   }

   public static double percentageOf(double d) {
      return d * 100.0;
   }
}


So there are other approaches, but simply using a bean is probably better for readability and maintainability.

HappyCoding.io - Coding Tutorials!
Happy Coding forum - Come say hello!
Offline atombrot
« Reply #4 - Posted 2014-11-05 14:50:23 »

Have a look at lombok. I think lombok introduces some nice code sugar. Auto generated getters and setters are part of lombok if I remember correctly.
Offline Roquen

JGO Kernel


Medals: 518



« Reply #5 - Posted 2014-11-06 06:03:50 »

Or better yet, since you're not writing a long lifetime public API used by thousands that are hampered by API changes...don't use getter/settters.
Offline richierich
« Reply #6 - Posted 2014-11-06 09:44:18 »

I always start with an empty class definition, and add functions as they're needed. I think it's bad form for code to contain stuff that's never used - aside from the obvious code bloat it can be confusing because it suggests there might reasonable use cases when there aren't.
Offline ags1

JGO Kernel


Medals: 367
Projects: 7


Make code not war!


« Reply #7 - Posted 2014-11-06 12:36:46 »

I was thinking about this for my work, not strictly for gaming. It seems to me to be a neat way to write code once - don't repeat code, write metacode instead.

@richierich: it would still be possible for an IDE to warn if variations of the metamethod are not used.

If this was a Java language feature, I would definitely use it.

Offline KevinWorkman

« JGO Plugged Duke »


Medals: 288
Projects: 12
Exp: 12 years


HappyCoding.io - Coding Tutorials!


« Reply #8 - Posted 2014-11-06 15:14:32 »

For this to be usable, you'd have to have the ability to include or uninclude particular attributes, which would probably mean annotations.

And at that point, why not just use lombok? It's actually less verbose than your solution.

HappyCoding.io - Coding Tutorials!
Happy Coding forum - Come say hello!
Offline princec

« JGO Spiffy Duke »


Medals: 1126
Projects: 3
Exp: 20 years


Eh? Who? What? ... Me?


« Reply #9 - Posted 2014-11-06 15:46:13 »

I say again... Beans are a giant facepalm. Don't use them, or their idioms. View "properties" as you would an embarrassing disease of the genitals: best tell as few people about it as possible.

Cas Smiley

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline cylab

JGO Kernel


Medals: 195



« Reply #10 - Posted 2014-11-06 15:58:03 »

Well, using Beans and their idioms depend on the context. If you work in any kind of Enterprise Java job, there is simply no way to avoid them...

Mathias - I Know What [you] Did Last Summer!
Offline princec

« JGO Spiffy Duke »


Medals: 1126
Projects: 3
Exp: 20 years


Eh? Who? What? ... Me?


« Reply #11 - Posted 2014-11-06 16:15:53 »

Ah, but that is a hell especially reserved for the mediocre  persecutioncomplex

Cas Smiley

Offline ags1

JGO Kernel


Medals: 367
Projects: 7


Make code not war!


« Reply #12 - Posted 2014-11-06 16:29:11 »

so it's official, I'm mediocre :-(

Offline KevinWorkman

« JGO Plugged Duke »


Medals: 288
Projects: 12
Exp: 12 years


HappyCoding.io - Coding Tutorials!


« Reply #13 - Posted 2014-11-06 16:33:45 »

The mediocre, and people who want to be employable in 5 years...  Tongue

HappyCoding.io - Coding Tutorials!
Happy Coding forum - Come say hello!
Offline princec

« JGO Spiffy Duke »


Medals: 1126
Projects: 3
Exp: 20 years


Eh? Who? What? ... Me?


« Reply #14 - Posted 2014-11-06 19:32:54 »

There are other jobs besides making little boxes for drones to send numbers into databases...

Cas Smiley

Offline KevinWorkman

« JGO Plugged Duke »


Medals: 288
Projects: 12
Exp: 12 years


HappyCoding.io - Coding Tutorials!


« Reply #15 - Posted 2014-11-06 19:38:48 »

There are other jobs besides making little boxes for drones to send numbers into databases...

Cas Smiley

By "enterprise" I assumed we meant JavaEE or more generally, server-side Java. That's where all the (Java) jobs are going- ain't nobody hiring front-end Java developers anymore. Those jobs are all going to javascript kids.

Sure, we can still hobby away at game development or Android development, and a lucky minority of us can make money that way. But for most Java developers, we either have to learn JavaEE (and accept that it uses beans all over the place), or learn javascript.

If by "enterprise" we meant something else, then I take back my statement. But there's plenty more to server-side Java than little boxes and databases.

HappyCoding.io - Coding Tutorials!
Happy Coding forum - Come say hello!
Offline HeroesGraveDev

JGO Kernel


Medals: 382
Projects: 11
Exp: 4 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #16 - Posted 2014-11-06 19:45:56 »

I'd take my chances with C++ rather than get into enterprise Java. Roll Eyes

Offline KevinWorkman

« JGO Plugged Duke »


Medals: 288
Projects: 12
Exp: 12 years


HappyCoding.io - Coding Tutorials!


« Reply #17 - Posted 2014-11-06 19:47:07 »

Fair enough. I've been dabbling in JavaEE and Spring for a little while now, and I actually like it a lot more than I thought I would.

Now if only I could make anything I do look pretty...

(I guess I have the same problem in game development, heh)

HappyCoding.io - Coding Tutorials!
Happy Coding forum - Come say hello!
Offline princec

« JGO Spiffy Duke »


Medals: 1126
Projects: 3
Exp: 20 years


Eh? Who? What? ... Me?


« Reply #18 - Posted 2014-11-06 19:50:46 »

By "enterprise" I assumed we meant JavaEE or more generally, server-side Java. That's where all the (Java) jobs are going- ain't nobody hiring front-end Java developers anymore. Those jobs are all going to javascript kids.
Still quite a lot of front-end Java work around as far as I can see, though not as much.

Cas Smiley

Offline KevinWorkman

« JGO Plugged Duke »


Medals: 288
Projects: 12
Exp: 12 years


HappyCoding.io - Coding Tutorials!


« Reply #19 - Posted 2014-11-06 21:04:33 »

Still quite a lot of front-end Java work around as far as I can see, though not as much.

My day job is a Swing (and now GWT) developer. But whenever I peruse job offerings, most of them are for Spring or JavaEE.

I would love to work as a Swing (or JavaFX) developer the rest of my life, but I don't see that happening.

HappyCoding.io - Coding Tutorials!
Happy Coding forum - Come say hello!
Offline nsigma
« Reply #20 - Posted 2014-11-06 21:11:18 »

Still quite a lot of front-end Java work around as far as I can see, though not as much.

Judging from NetBeans RCP mailing lists, etc. there's no shortage of enterprise desktop applications being developed.  I assume other platforms (Eclipse?) may be similar. (my usage of the NetBeans platform for Praxis LIVE is definitely atypical usage. Wink )

Praxis LIVE - hybrid visual IDE for (live) creative coding
Offline ags1

JGO Kernel


Medals: 367
Projects: 7


Make code not war!


« Reply #21 - Posted 2014-11-06 21:14:02 »

I'm enterprise java, but all the work is straight J2SE. All our frameworks are in-house and half the devs are C# / C++.

Anything EJB, Spring, WS or whatever is firmly legacy code.

Offline cylab

JGO Kernel


Medals: 195



« Reply #22 - Posted 2014-11-06 23:30:32 »

I would love to work as a Swing (or JavaFX) developer the rest of my life, but I don't see that happening.
Well, it won't happen with any technology nowerdays...  Shocked

Mathias - I Know What [you] Did Last Summer!
Offline Slyth2727
« Reply #23 - Posted 2014-11-07 00:28:35 »

C# has some really nice getter setter stuff going on. If only Java would take a little more from C#... *mumble*operator overloads*mumble*
Offline pitbuller
« Reply #24 - Posted 2014-11-07 00:33:06 »

Still quite a lot of front-end Java work around as far as I can see, though not as much.
I would love to work as a Swing (or JavaFX) developer the rest of my life.

That would sound extremely depressing.
Offline Roquen

JGO Kernel


Medals: 518



« Reply #25 - Posted 2014-11-07 05:55:29 »

If you work in any kind of Enterprise Java job, there is simply no way to avoid them...
But they're not rocket science.  Shouldn't be something you need to practice.
Offline basil_

« JGO Bitwise Duke »


Medals: 418
Exp: 13 years



« Reply #26 - Posted 2014-11-07 08:33:36 »

If you work in any kind of Enterprise Java job, there is simply no way to avoid them...
But they're not rocket science.  Shouldn't be something you need to practice.
editor-code-collapse
Offline cylab

JGO Kernel


Medals: 195



« Reply #27 - Posted 2014-11-07 08:56:44 »

If you work in any kind of Enterprise Java job, there is simply no way to avoid them...
But they're not rocket science.  Shouldn't be something you need to practice.
Yeah, actually you can just let them generate and refactor by any decent IDE.

Mathias - I Know What [you] Did Last Summer!
Offline princec

« JGO Spiffy Duke »


Medals: 1126
Projects: 3
Exp: 20 years


Eh? Who? What? ... Me?


« Reply #28 - Posted 2014-11-07 09:04:31 »

There's a theory that says that when a computer is able to write your code for you, either your code is unnecessary ... or you are.

Cas Smiley

Offline Roquen

JGO Kernel


Medals: 518



« Reply #29 - Posted 2014-11-07 10:54:49 »

I think some of you are missing my point...you don't need to learn how to write a getter/setter.  It's obvious.  Like you don't need to practice 4 space indentation (assuming you like say 2).  Or naming variables like this:  a_varaiable_name.  Someone requires you do so, then you can without practice.  Or you're in trouble.
Pages: [1] 2
  ignore  |  Print  
 
 

 
Riven (198 views)
2019-09-04 15:33:17

hadezbladez (4909 views)
2018-11-16 13:46:03

hadezbladez (1810 views)
2018-11-16 13:41:33

hadezbladez (5183 views)
2018-11-16 13:35:35

hadezbladez (1023 views)
2018-11-16 13:32:03

EgonOlsen (4387 views)
2018-06-10 19:43:48

EgonOlsen (5228 views)
2018-06-10 19:43:44

EgonOlsen (2971 views)
2018-06-10 19:43:20

DesertCoockie (3874 views)
2018-05-13 18:23:11

nelsongames (4305 views)
2018-04-24 18:15:36
Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!