Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (736)
Games in Android Showcase (223)
games submitted by our members
Games in WIP (813)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Lockable variables  (Read 3875 times)
0 Members and 1 Guest are viewing this topic.
Offline CopyableCougar4
« Posted 2014-12-19 23:50:40 »

I was just thinking and I had an idea for a concept, but I wanted to get some input from other people before putting it to use.

Concept: lockable variables. You set a locked flag and then you can set it's value.

LockedVariable.java

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  
public class LockedVariable<T> {

   private T value;
   
   private boolean locked = false;
   
   public LockedVariable() {
     
   }
   
   public LockedVariable(T value) {
      this.value = value;
   }
   
   public void lock() {
      locked = true;
   }
   
   public void unlock() {
      locked = false;
   }
   
   public void setValue(T t) {
      if (!locked) value = t;
   }
   
   public T getValue() {
      return value;
   }
   
}


Any feedback on whether this idea is worth putting to practice would be appreciated Smiley

CopyableCougar4

Either wandering the forum or programming. Most likely the latter Smiley

Github: http://github.com/CopyableCougar4
Offline HeroesGraveDev

JGO Kernel


Medals: 382
Projects: 11
Exp: 4 years


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


« Reply #1 - Posted 2014-12-19 23:58:20 »

What's the point?

Offline CopyableCougar4
« Reply #2 - Posted 2014-12-19 23:59:13 »

For global constants, so that they could be kept from being set at certain times. It was just a thought, I just wanted some feedback to know if this idea is usable.

Either wandering the forum or programming. Most likely the latter Smiley

Github: http://github.com/CopyableCougar4
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline HeroesGraveDev

JGO Kernel


Medals: 382
Projects: 11
Exp: 4 years


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


« Reply #3 - Posted 2014-12-20 00:03:02 »

With reflection, nothing is immutable.

Offline CopyableCougar4
« Reply #4 - Posted 2014-12-20 00:04:47 »

Point taken, but the concept would be for variables where you wouldn't be using reflection (because the same programmer who made the lockable variables would not use reflection to set them).

Either wandering the forum or programming. Most likely the latter Smiley

Github: http://github.com/CopyableCougar4
Offline HeroesGraveDev

JGO Kernel


Medals: 382
Projects: 11
Exp: 4 years


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


« Reply #5 - Posted 2014-12-20 00:06:23 »

Quote
Point taken, but the concept would be for variables where you wouldn't be using reflection (because the same programmer who made the lockable variables would not use reflection to set them).

Well then why does the programmer need to use the locked variable in the first place? They would just know when not to modify the variable.

Offline Abuse

JGO Ninja


Medals: 60


falling into the abyss of reality


« Reply #6 - Posted 2014-12-20 00:07:16 »

For global constants

That's your problem right there.
Offline CopyableCougar4
« Reply #7 - Posted 2014-12-20 00:10:14 »

I guess the concept should be trashed, it seems like a programmer who knows their way around a given project should have no need for this idea.

Either wandering the forum or programming. Most likely the latter Smiley

Github: http://github.com/CopyableCougar4
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 485
Exp: 7 years



« Reply #8 - Posted 2014-12-20 00:21:23 »

Not to mention the fact that it doesn't tell you when you tried to set a value that you shouldn't have, it just fails silently, leading you on a wild goose chase until you remember about your weird extra global state hiding in your variables.
A programmer wants better abstractions if any are needed, not leakier ones.
Offline theagentd
« Reply #9 - Posted 2014-12-20 00:24:16 »

Uh, the final keyword? o_O

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

« JGO Bitwise Duke »


Medals: 485
Exp: 7 years



« Reply #10 - Posted 2014-12-20 00:26:52 »

That and defensive getters if you're really concerned.
I've been becoming more and more of a fan of the "immutable by default" tend in prog-langs recently. Makes life simpler.
Offline CopyableCougar4
« Reply #11 - Posted 2014-12-20 00:27:08 »

Uh, the final keyword? o_O

Well the original intent was one that could be locked an unlocked, where I believe final can only have the initial value (but if I'm wrong please tell me)

Either wandering the forum or programming. Most likely the latter Smiley

Github: http://github.com/CopyableCougar4
Offline Ashedragon

JGO Coder


Medals: 27
Projects: 1
Exp: 4 years


The best person you could possibly be is yourself.


« Reply #12 - Posted 2014-12-20 00:31:33 »

I can't see it being useful if users aren't going to be interacting with your codebase (scripting, etc).

boo
Offline The Lion King
« Reply #13 - Posted 2014-12-20 09:04:27 »

I understood this to be more like a Mutex. That has a lot more use if that's what you are trying to do, but it already exists so use that instead.

"You have to want it more than you want to breath, then you will be successful"
Offline trollwarrior1
« Reply #14 - Posted 2014-12-20 10:07:44 »

1  
2  
3  
4  
public void setValue(T t) {
     if (locked) throw new IllegalStateException("Value is locked!");
     value = t;
}


I have used something like this in my recent project. It has worked like a charm. When you begin writing big and complex abstracted code things like this are really helpful. Helped me solved some possible bugs without almost any effort at all. I just block some states and boom there you go you get an exception when you shouldn't have accessed the variable.
Offline Riven
Administrator

« JGO Overlord »


Medals: 1310
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #15 - Posted 2014-12-20 11:06:48 »

You can simply setup a breakpoint to determine/prohibit the execution of a certain piece of code.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Offline trollwarrior1
« Reply #16 - Posted 2014-12-20 11:12:28 »

I had a class "FlagWord" so I don't think break point is very viable in such situation.
Offline Riven
Administrator

« JGO Overlord »


Medals: 1310
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #17 - Posted 2014-12-20 12:42:20 »

Naturally.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 
cybrmynd (117 views)
2017-08-02 12:28:51

cybrmynd (139 views)
2017-08-02 12:19:43

cybrmynd (136 views)
2017-08-02 12:18:09

Sralse (152 views)
2017-07-25 17:13:48

Archive (619 views)
2017-04-27 17:45:51

buddyBro (730 views)
2017-04-05 03:38:00

CopyableCougar4 (1255 views)
2017-03-24 15:39:42

theagentd (1235 views)
2017-03-24 15:32:08

Rule (1213 views)
2017-03-19 12:43:22

Rule (1264 views)
2017-03-19 12:42:17
List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05

SF/X Libraries
by SkyAphid
2017-03-02 06:38:56

SF/X Libraries
by SkyAphid
2017-03-02 06:38:32

SF/X Libraries
by SkyAphid
2017-03-02 06:38:05

SF/X Libraries
by SkyAphid
2017-03-02 06:37:51
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!