Reputation
789
Top tag
Next privilege 1,000 Rep.
See votes, expandable usercard
Badges
4 11
Newest
 Yearling
Impact
~16k people reached

  • 0 posts edited
  • 0 helpful flags
  • 45 votes cast
Feb
27
comment How to handle shutdown/startup of a Java application that relies on real time events?
Please also note that you will likely lost some of the events: anything between your clients and the server could fail. In order to keep as much information as possible, I would simply (redundantly and) persistently store each event as fast as possible after reception, and perform some computations "from time to time". In such a way, if your computing process/server/handler fails, you can always compute all derivated values again from your "history". Redundant events will then be handled by simply ignore them.
Feb
20
comment How to save tuplas values, to later search on it
"I wouldn't even mention Hashtable in any context apart from history or uber-legacy code." The OP mentioned Hashtable, so I discussed it. Plus, by no mean not(A) => not(B) implies A => B. Also, Hashtable ensures thread safety, but in a suboptimal way. So it is definitively not a bad solution. I added a link in my answer for a detailed comparison between Hashtable, ConcurrentHashMap, and Collections.synchronizedMap.
Jan
15
comment Why would I need `equals` if I have already `hashcode`?
Wow, my mistake. I was actually (erroneously) remembering Chapter 3 of Effective Java. So a Rational class must implement hashCode in such a way that two Rational objects representing the same value but having different numerators and denominators (and therefore having potentially a different behavior) have the same hash. OR two Rational objects representing the same value with different numerators and denumerators must not be equal. Right?
Jan
15
comment Why would I need `equals` if I have already `hashcode`?
In addition to the risk of collision or the impossibility to produce a decent hash, there is also the fact that two objects having different hashes may be logically equals, because some objects properties may be not relevant to determine this equality. So even if same hashes (and no collision) should implies equality, different hashes does not implies inequality.
Jan
5
comment Refactoring a long method which is based on large number of switch cases
The problem is that the ability to maintain the switch will decrease as the number of cases increases: you are getting a longer method each time a new option is inserted. Conversely, the method() body will have the same length and complexity, independently of the number of available options. And each option manages a single, isolated, and therefore more easily maintainable behavior. Plus, again, a switch places a option-based behavior in something that is not an option, but has or receive an option.
Jan
5
comment Refactoring a long method which is based on large number of switch cases
You also have to take into account the fact that enum+switch structures tend to make you option-based logic spread all over the code, instead of concentrate it in a single point. It's a violation of the open-closed principle.
Nov
13
comment What is the best way to handle two very similar projects with Git?
@SlavaKnyazev Actually, this is used by Github for handling many repositories more efficiently: a blob is only stored once for all Github projects. This is particularly efficient for fork projects and typically common files such as license files.
Nov
12
comment What is the best way to handle two very similar projects with Git?
If you use different repositories, you could be interested in shared clones which allow you to share common objects and therefore spare disk spare.
Sep
25
comment How to make a datatype for something that represents either itself or two other things
@DocBrown Please place my sentence in context. codebreaker is only looking for a solution involving cards and "subcards". I tell that if you want to play with sub-sub-cards, sub-sub-sub-cards, etc (which AFAIK doesn't make sense in this context), the Composite DP allows you to deal with the recursive nature of your structure without any extra complexity (wrt using it for a 2-level structure). Also, IMHO, Composite DP just describes what he wants: a simple card, and a complex card made of two simple ones. A complex card is not just a bunch of cards, but may have extra behaviour.
Sep
25
comment How to make a datatype for something that represents either itself or two other things
I don't see why having two Card instances makes my class a decorator. According to your own link, a decorator adds features to a single object, and is itself an instance of the same class/interface than this object. While, according to your other link, a composite allows the owning of multiple objects of the same class/interface. But ultimately the words don't matter, and only the idea is great.
Sep
25
comment How to make a datatype for something that represents either itself or two other things
For further exploration, the use of a Visitor for exploiting methods specific to a subclass is sometimes called Multiple Dispatch. Double Dispatch could be an interesting solution to the problem.
Sep
16
comment Creating a better mouse dragger function
For test automation realism is out of topic. But a quadratic Bezier curve should do the job. You probably even could ignore most of the 30 points pers second.
Aug
28
comment Should a new type be created though it only wraps a single field?
In addition to resist to changes, classes/interfaces with a single value may be used to get a stronger type safety in the program: "No, this is not a simple string, this is actually a species id". It makes easier the understanding of parameters: "This method is waiting for a species id, not a simple string" and therefore limits the risk to misuse a method. It also makes sense when your programming style involves type based behaviour (for instance, with pattern matching).
Jun
8
comment Maintaining State without assignment
Sure. Please look at my edit.
May
30
comment How do you avoid getters and setters?
As the question is tagged as a Java question, please note that Java conventions require you to start your methods' names with a lower case.
Jan
9
comment How to randomly get or find a sub-tree (include root node) from a given tree that contains n leaf-nodes
Count the number of leaf nodes in the given tree. If this number is greater or equal to n, return the tree itself. Otherwise, there is no subtree with at least n leaf nodes.
Jan
7
comment Why would a program require a specific minimum number of CPU cores?
Are you sure the problem is really and directly related to the number of cores? Maybe the mentioned game is partially based on a feature only (correctly) provided by CPU with at least 4 cores?
Oct
24
comment What can one do to improve readability of math-oriented code in C#, Java, and similar?
Yes, in general, and by definition, a DSL may be useful when you work in a specific domain. However, if this DSL doesn't exist, or if it doesn't fit the needs, you have to maintain it, which may be problematic. Also, for the specific question ("How can I use the sin, cos, … methods/functions without writing the Math class every time"), a DSL is maybe an oversized solution.
Jul
7
comment Automatically create or update object in database
I would not ask anything to the database directly from the UI, since it's a clear violation of the MVC design pattern. Even if you don't intend to strictly respect it, consider the fact that you will probably want to access to the database without using the GUI. In OOP, I would also avoid to pass states such as Add or Edit. Prefer the use of inheritance or, generally even better, composition to specialise the GUI's behaviour. In this context, you could consider the ActionListener as the Strategy of a Strategy Design Pattern.
Nov
18
comment Ordering if conditions for efficiency and clean code
I don't think it's foolhardy, in the worse case you spend time for no perceptible speed improvement. But, indeed, generally tests are not so heterogeneous that a such "optimisation" is interesting.