Tag Info

Hot answers tagged

6

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. ...


6

The short answer is, of course, whether you want to break with naming conventions for what are essentially constants... Quoting from the JLS: Constant Names The names of constants in interface types should be, and final variables of class types may conventionally be, a sequence of one or more words, acronyms, or abbreviations, all uppercase, with ...


5

Trying to expand on Kasper's answer, it's easiest to rant against and eliminate setters. In a rather vague, handwaving (and hopefully humorous) argument: When would Customer.Name ever change? Seldom. Maybe they got married. Or went into witness protection. But in that case you'd also want to check on and possibly change their residence, next of kin, ...


5

As far as I can see, when you write: I made some changes which affected the expected array element position The advantage that you're focusing on is that in Method #1, the start position is only found in one place (the initial value of i) whereas in Method #2, that information is implicitly contained in all four array indexes. Seen this way, the ...


5

Your first method has 4 side effects (the pre-increments) while the second has none. In general you should avoid side-effects if you can. Furthermore, the second one is more clear about your intent. If I would see the first one in someone else's code I would ask myself if there was any reason for performing the pre-increments. So, stick with the second ...


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 ...


4

sun.misc.Unsafe isn't part of the Java standard. Thus, your very first task will be determining whether the Java implementation you will be running on the ARM chip supports it. If the answer to the above is yes, you have to ask yourself whether the I/O addresses you need to access are mapped within the JVM process. Unsafe lets you escape the usual Java ...


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 ...


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 ...


3

[Disclaimer: I'm not a UML jockey, so don't take my word for it.] I think, it would look like this. Some details omitted for brevity.


3

Sending enums between my Java code and a database or client app, I often end up reading and writing the enum values as strings. toString() is called implicitly when concatenating strings. Overriding toString() on some enums meant that that sometimes I could just "<input type='checkbox' value='" + MY_CONST1 + "'>" and sometimes I had to remember to ...


3

Don't modify ENUM that's just bad code smell. Let an enum be an enum and a string be a string. Instead, use a CamelCase converter for string values. throw new IllegalStateException("Light should not be " + CamelCase(color) + "."); There are many open source libraries that already solve this issue for Java. ...


3

The First method is horrible on my vision and it does not prevent "Exception". The same feelings I have when I see "magic numbers"! XD If possible, you need to change the architecture, something is wrong! Or You need to check it before to use it in the method! Or be right that those params always come with their values filled and in the same order! Did You ...


3

There's no direct way to do what you're describing. However, you have a few options which will hopefully achieve what you want: Have an interface which contains only the accessors, and not the mutators, and make sure your return type is of the interface rather than the concrete class. Have a way of cloning a class- this might be an external method, or a ...


3

You can make the objects immutable: public class Developer { private final String name; public Developer(String name){ this.name = name; } public String getName(){ return this.name; } } Then there will be no issue with returning the reference. Otherwise you can map each developer to a unmodifiable view of itself: ...


2

In the MVP pattern is the View is "dumb". It doesn't really do anything. The presenter acts as the controlling object in the heirarchy, while the Model stores the data and has business logic methods on it. That doesn't preclude the View from firing events, which are hooked by the Presenter to methods or properties on the model, or to other testable logic ...


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 ...


2

If you want to program a cheap (32 bits) ARM chip in Java (which is probably not a good idea) and if you accept to code against an old version of the Java programming language, you might consider using gcj, the Java ahead-of-time compiler inside some versions of GCC. (Beware, Java support in GCC seems phased out in 2015, or at least is becoming less ...


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 ...


1

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 ...


1

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 ...


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

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

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 ...


1

Chiming in: this is a very small example of the ongoing "Model-Based" vs "Model-Driven" engineering debate. The question is what is the purpose of your model? Model-Driven - you are trying to generate your code directly from your model. This was the thesis/goal of "Executable UML" and others. If you are working in this sort of team, you will need to study ...


1

I think first you should take a step back and think; what you are trying to communicate with the diagram, and to whom? If you absolutely have to do it via a class diagram then Nick's answer is a reasonable approach. However Class diagrams are usually used for showing static structure, creates seems like a behavioural/lifetime relation so is better suited to ...


1

I suppose that since my users will navigate between pages, the websocket handshake will have to be remade after each request, which might be bad because of the additional overhead (not so sure though..). Bad in what sense? Yes, there will be additional load on your server, but it probably won't be a big deal. Your main goal at this stage should be to ...


1

OOP is about encapsulating and hidding behavior inside objects. Objects are black boxes. This is a way to design thing. The asset is in many case one doesn't need to know the internal state of another component and is better to not have to know it. You can enforce that idea with mainly interfaces or inside an object with visibility and taking care only ...


1

I would actually... not go for both, but I'll pick yours over your co-worker for your simplified example. First things first, I tend to favor inlining values instead of doing one-off assignments. This improves code readability for me as I do not have to break my train of thought into multiple assignment statements, then read the line(s) of code where they ...



Only top voted, non community-wiki answers of a minimum length are eligible