Tag Info

New answers tagged

0

I'd say that if you have any clout as a Java developer you should learn RoR if you want or need to. The end goal of a developer isn't to learn a particular technology, but rather to get themselves to a state where they understand what it means to solve problems with software using any given technology that's suited to the job. If you're a skilled developer, ...


2

The second construct that you use is called an anonymous class. It's both a declaration and instantiation of a class at the same time. Although ActionListener is an interface, you're creating a new instance of a class that implements that interface along with providing an implementation of its methods (in this case, there's only one method in the interface) ...


-1

If you are referring to the letters i,j,k as indices within a loop, that is a common practice in Java and other programming languages. Though there its probably better to use a for each style loop, such as for(User user: users) { doSomethingWithTheUser(user); } instead of for(int i=0; i<users.size(); i++) { doSomethingWithTheUser(users.get(i)); } ...


0

Is there any need for a rulebook object to be bound to a particular game state, or would it make more sense to have a rulebook object with a method that, given a game state, will report what moves are available from that state (and, having reported that, remember nothing about the state in question)? Unless there is something to be gained by having the ...


0

Can you create a dummy record in the database with the default values, then pass a flag to the edit page to indicate it should create instead of update on save? If it's not possible to pass a flag, would it be possible to give the dummy record a unique ID that you would then hard-code into the edit page to indicate that the record should be created instead ...


17

Properly naming things is hard. Very hard. If you look at it the other way, you can also take this to mean that properly named things are important. (Otherwise, why would you have spent the effort naming it?) But, sometimes, the names of things just aren't important. That's why we have stuff like anonymous functions ("lambdas"), for example: because ...


3

Reading the code in Kilian's answer, I don't think "there is a loop, and there is a variable, and it has type int, and it has a name, and the name is i, and it is initialised to 0, ...". I just think "loop..." where the three dots stand for unimportant details that I don't even think about. In my programmer's mind, that variable doesn't really exist, it is ...


-1

The proper way of dealing with this is to use interfaces. Instead of having two classes know about eachother, have each class implement an interface and reference that in the other class. Let's say you have class A and class B that need to reference eachother. Have class A implement interface A and class B implement interface B then you can reference ...


19

If your loop does nothing but use a variable for counting for(int i = 0; i < 10; i++) { System.out.println("Stop it! I really mean it!!"); } then yes, this is the best name you could use. Anything longer cannot possibly make the semantics any more obvious, but takes much longer to read. If the variable is used inside the loop, a meaningful ...


0

I believe the circular references and the god object in your case could be easily removed by separating the control of the game flow from the state and rule models of the game. By doing that you would probably gain a lot of flexibility and get rid of some needless complexity. I think you should have a controller ("a game master" if you wish) that controls ...


5

Game theory treats games as a list of previous moves (value types including who played them) and a function ValidMoves(previousMoves) I would try and follow this pattern for the non UI part of the game and treat things like board setup as moves. the UI can then be standard OO stuff with one way ref to the logic Update to condense comments Consider ...


10

Granted, circular dependencies are a questionable practice from a design point of view, but they are not prohibited, and from a purely technical point of view they are not even necessarily problematic, as you seem to regard them to be: they are perfectly legal in most scenarios, they are inevitable in some situations, and on some rare occasions they can even ...


4

The standard way of removing a circular reference between two classes in object oriented programming is to introduce an interface that can then be implemented by one of them. So in your case, you could have RuleBook referring to State which then refers to an InitialPositionProvider (which would be an interface implemented by RuleBook). This also makes ...


23

I've been wrestling with a problem in a Java project about circular references. Java isn't Python. Circular references do not cause any kind of problem in Java. Time spent eliminating perfectly natural circular references in Java is time wasted. I coded this up [...] but the problem is that it's full of circular references. I implemented it back ...


0

This is a common problem we encounter in our tools too. A nice UI solution we use is shown by a product by Telerek called Kendo. The grid can use a button to sync edits with the db inside the grid. Although the grid takes a professional license, you can try it out free and see if you like the style and follow the samples and build your own instead. ...


2

As stated in quite a few answers and comments, DTOs are appropriate and useful in some situations, especially in transferring data across boundaries (e.g. serializing to JSON to send through a web service). For the rest of this answer, I'll more or less ignore that and talk about domain classes, and how they can be designed to minimize (if not eliminate) ...


0

It seems slightly odd that Example.attributes can contain a mixture of AttributeA and AttributeB objects, but ExamplesAndMore.examples must contain a 'homogenous' (using that loosely) List of Example objects. How are these supposed to be used, for both Example and ExamplesAndMore classes? Is there also some kind of implied ordering/equivalence between the ...


1

The two cases/conditions, by my reading, clarify which real world and conceptual objects are meaningful candidates to model computational objects after; not whether an object or variable in-code is a computational object. And there's a large degree of subjectivity there -- it's all about whether you, the programmer, can recognize a meaningful state or ...


2

Is my understanding correct? No, since immutable objects (like strings in Java or C#, or tuples in Python), do not change their state over time, nevertheless they are perfectly valid examples for (computational) objects in any kind of OOP definition I know. However, those kind of objects can influence the state of other objects through interactions. ...


2

In SICP ยง 1.1.2, it says: A critical aspect of a programming language is the means it provides for using names to refer to computational objects. We say that the name identifies a variable whose value is the object. So a computational object is simply that which is pointed to by a variable. It can be an object, a first-class function, or even a ...


1

The issue of having getters and setters can be a matter of the fact that a class may be used in the business logic in one way but you may also have helper classes to serialize / deserialize the data from a database or file or other persistent storage. Due to the fact there are many ways to store / retrieve your data and you want to decouple the data objects ...


0

It seems to me that priority is not a property of a number type if priority is determined based on the priorities of the other number types, so I think the best approach would be to place the number types in a List. In other words, you have a List of Lists. The outer List determines the priority of the numbers contained within while the inner List ...


3

Your code doesn't allocate any objects, nevertheless it has O(n) space complexity. Whenever you call your method, the implementation will set up a call stack frame. At the least, this includes the return address, but in this case you will also need stack space for the parameters and any local variables. Even if a variable points only to null or to existing ...


0

Despite being a god object, if you always feel that the code you're writing is suitable for the role of BridgeConsole, then the problem is with the name! Rename it something more specific, and separate the other tasks into their respective classes. It is easier said than done, so perhaps it would be simply easier to rewrite the class from scratch. Before ...


0

Two alternative techniques to consider: Use a type with the Void generic argument removed. Instead of a Callable<Void>, use a Runnable. Instead of a SmurfMap<K, Void>, use (or make) a SmurfSet<K> Modify the clients to be null-friendly. For example, I know of no good alternative to Future<Void>. Guava's Futures.allAsList() ...


8

It appears from the comments and edits that your concern is more about duplicating code in multiple implementation classes. That has nothing to do with how many interfaces are implemented. Composite Pattern with Delegation If the implementations of each interface is generic then they should be classes that are delegated to using the Composite Pattern. The ...


1

If you don't need the values, don't use a map. Use a Set. It's a collection that contains no duplicate elements (by equals and hashCode) and has O(1) membership checking. If the code is impossible to structure in a way that uses a set, and it must use a map*, then of these options I'd make a singleton Unit type. I'd pick this because as a map value, it does ...


0

I would consider a class representing a JSON response to be a Data Contract. Keeping it separate from your DTOs and Domain Objects also keeps the clients consuming your service decoupled from the rest of your system.


0

It's about communicating your intent of how the object should be used. For example, if your method expects a Map object with a predictable iteration order: private Map<String, String> processOrderedMap(LinkedHashMap<String, String> input) { // ... } Then, if you absolutely need to tell callers of the above method that it too returns a Map ...


3

For me it seems pretty straightforward - as you say, you can divide the work beforehand to N chunks, each thread will pick up and process their assigned elements and put to pre-determined place in the array - threads effectively won't read/write same data. Two things though: You should preallocate all the arrays accessed by multiple threads before hand to ...


0

When you are submitting an HTML form, by default you cannot upload files. Even if the form contains an input for a file, the user will manage to select the file to be uploaded through the form, but the file will not be passed in the HTTP request at all. In order for this to be done, you have to set the encryption of the form to "multipart", so that all ...


2

No, a builder is not unheard of for building messages. Spring Integration does something similar with a Builder. Spring Integration messages have a general structure like yours: a payload and a header. There's the Message interface public interface Message<T> { T getPayLoad(); MessageHeaders getHeaders(); } The MessageHeaders class (your ...


2

How does one express this notion in natural everyday language? To make it short: class inheritance describes a family of objects. In language terms, think of nouns. Think of: vehicle, cars and bikes. interface inheritance describes a set of unrelated objects, sharing common behaviour. In language terms think of adjectives. Think of: able to ...


2

Extending a class is not necessarily "code inheritance". If A is an abstract base class or an interface, then making B a subclass of it means it only inherits the interface of A. If A is concrete, then B would inherit the interface and maybe some code, depending on what B chooses to override. So I believe the correct answer to your question is that ...


0

On the first sight I think it should stay in the Value Object because of cohesion and encapsulation principles - getChildMap() method implementation is intimately connected to the implementation of MyContainerVO object so it should stay inside this object without necessarily exposing the implementation details (childList). The problem here is that it's not ...


8

1) Where your manager is right Your manager wants a flexible architecture. To accomplish this, you have to design our code, that it is easily composed of pluggable components. What according to him is pluggable is obtaining object instances through Java Reflection That is a way to organize object creation and is how DI-frameworks do their job. ...


3

First of all, this is not a "problem" which needs "modelling". This is just a syntax issue. Secondly, why on earth would you even think of using a builder pattern for this? Is it perhaps because you read "Clean Code", where on page 35 the author classifies methods into four categories, depending on whether they accept zero, one, two, or the incredibly ...


1

A few benchmark runs might help establish why your applications are slow. Then a demonstration of a dramatic improvement by a change in methodology could be suggested.


1

It really depends on the structure of your neural network. For example this: Fig 1 can be modeled easily with 4 array lists since there is no interesting information in the connections. However, this: Fig 2 would require construction of an object graph. A key decision to make is which end of the arrow is knowledge of the other layer going to ...


0

The best way is to start with a linear algebra library that allows computation of: matrix multiplications and basic arithmetic (scaling, addition, subtraction), and matrix pseudo-inverse. (If you use a non-linear function, you might also need the partial derivative of that non-linear function - if I remember correct.) Then, you write a loop doing those ...


1

In addition to the other drawbacks that other answers have posited, another is that the Throwable mechanism is designed to be overbearing i.e., it short-circuits normal processing wherever present. Imagine the following public void doSomething(){ Foo it = new Foo(); Bar something = it.doesSomething(); //anything thrown here will cause a hard stop ...


1

Would it be abusive to create an event system that uses try-catch statements and throwables, which are really made for error handling? Abusive is an emotionally loaded term. And it is blatantly subjective ... unless you are measuring a standard that everyone concerned can agree is applicable. What could that standard be? It could be a particular ...


3

I don't understand how would something like that work. After a method throws an exception, it stops executing and permanently gives up control to some exception handler. But after a method raises an event, it gives up control to the event handler only temporarily, and then continues executing. I don't see how could you emulate temporarily giving up control ...


0

It's more than just whether you can mock a Brand object or not. First, I'll assume your Product is really a Data Access Object, and not a simple Java Bean/DTO/POJO. The real question to ask is, how long will this assertion hold? Assume 234 is the brand id of the brand in database. Will simple int values become long in the future, necessitating all ...


0

Sometimes I use option 4 Use the strategy pattern. Create a utility class with static methods that delegate implementation to an instance of pluggable interface. Code a static initializer that plugs in a concrete implementation. Plug in a mock implementation for testing. Something like this. public class DateUtil { public interface ...


4

For unit-testing the findByBrand method, there is no real difference between passing in a Brand object or just an ID. The difference is more relevant for the code calling findByBrand. In the large majority of cases, that code should already be dealing with Brand objects, so passing that as an argument to findByBrand makes the most sense. The only reason ...


0

Outputting results embedded in the TestScript Description seems excessive. If the results thus written are preventing the TestScript Description from being reused without an edit to remove them, then, the architecture is silly. Output specifications belong in the Description. The typical test pattern I've seen all my life is that the test has a name, that ...


0

Normally when you have to design the interface to be compatible with multiple platforms, the approach is to go with the lowest common denominator between the UI framework + components. So theoretically it is possible, provided that you only use the things that make sense and are supported in all the platforms that you want to port to. In practice, this ...


2

For the most part, you can write very Java-like code in JavaScript, but it's usually simpler not to. There are two pretty major factors that affect your design: dynamic typing and functional-style callbacks. Dynamic typing means you don't use interfaces, and you don't need to create a lot of those little classes that do nothing else but implement ...


0

Arguably, following the UPPERCASE naming convention violates DRY. (And YAGNI). e.g., in at your code example #2: "RED" and "red" are repeated. So, do you follow official convention or follow DRY? Your call. In my code, I will follow DRY. However, I will put in a comment on the Enum class, saying "not all uppercase because (explanation here)"



Top 50 recent answers are included