Tag Info

New answers tagged

0

Adding an unused parameter is confusing. People might call that method assuming this feature will work. I wouldn't add it. It is trivial to later add it using a refactoring and mechanically fixing up the call sites. In a statically typed language this is easy to do. Not necessary to eagerly add it. If there is a reason to have that parameter you can ...


0

There is not enough information to answer this question. It depends on what getFooList actually does, and how it does it. Here is an obvious example of a method which should support an additional parameter, used or not. void CapitalizeSubstring (String capitalize_me, int start_index); Implementing a method that operates on a collection where you can ...


1

Background There are three places where final can show up in Java: Classes (JLS 8.1.1.2) Methods (JLS 8.4.3.3) Fields (JLS 8.3.1.2) Making a class final prevents all subclassing of the class. Making a method final prevents subclasses of the method from overriding it. Making a field final prevents it from being changed later. Misconceptions There are ...


1

No design principle is absolute, so while I mostly agree with the other answers, I thought I'd play devil's advocate and discuss some conditions under which I would consider accepting your colleague's solution: If this is a public API, and you anticipate the feature being useful to third-party developers, even if you don't use it internally. If it has ...


2

You don't need it now, so don't add it. If you need it later, extend the interface: public interface IEventGetter { public List<FooType> getFooList(String fooName, Date start) throws Exception; .... } public interface IBoundedEventGetter extends IEventGetter { public List<FooType> getFooList(String fooName, Date start, ...


11

From a software engineering perspective, I believe the proper solution for this kind of problems is in the builder pattern. This is definitely a link from 'guru' authors for your colleague http://en.wikipedia.org/wiki/Builder_pattern. In the builder pattern, user creates an object that contains the parameters. This parameter container will then be passed ...


18

but he keeps on insisting that a lot of work will have to be done if we implement the use of "end" date some time later and have to adapt all the code then. (some time later) public class EventGetter implements IEventGetter { private static final Date IMPLIED_END_DATE_ASOF_20140711 = new Date(Long.MAX_VALUE); // ??? @Override public ...


29

Invite him to learn about YAGNI. The Rationale part of Wikipedia page may be particularly interesting here: According to those who advocate the YAGNI approach, the temptation to write code that is not necessary at the moment, but might be in the future, has the following disadvantages: The time spent is taken from adding, testing or improving the ...


6

[Are there] "respected" coding gurus that we can link him to [to persuade him]? Appeals to authority are not particularly convincing; better to present an argument that is valid no matter who said it. Here's a reductio ad absurdum that should persuade or demonstrate that your cow-orker is stuck on being "right" regardless of sensibility: What you ...


5

When you call method_2, you are also implicitly calling multAdd. As per the definition of multAdd, you are printing the result of (a*b) + c). Once that method_2 is done with the call to multAdd, the execution will proceed with the remainder of the code in your method. Eventually, it will reach the print statement (similar to what you have in the multAdd ...


1

Another advantage to String being final is that it ensures predictable results, just like immutability. String, though a class, is meant to be treated in a few scenarios, like a value type (the compiler even supports it via String blah = "test"). Therefore, if you make a string with a certain value, you expect it to have certain behavior that is inherit to ...


4

JetBrains bundles the version of the JDK that the IDE needs with it. This is true for all of the different IDEs that they make. The reason for this is to make sure that they are running with the JDK that the IDE is designed for. If you look in the location where the IDE is installed (IDE_HOME) you will find the JRE that it is bundled with in the JRE ...


1

The only way to get access to all those systems is to earn the TRUST of their creators/owners. And I can tell you right now that you're not going to get that trust as a one person hobbyist or startup with no background in the field. That's highly sensitive private and financial information you're talking about, data that needs to be seriously protected. Your ...


4

Large software products start out as small software products and feature creep from there until it does everything the customer wants it to. Rarely are large products made ex nihilo. Start small - very small - and increment. You write the smallest simplest thing you can that works in the realm. Inventory tracking for example. Just write some code that ...


0

kevin's answer was a good start to know how we can deal with regular expression from a theoritical point of view, which would help building a java library that let users generate String based on a given regex . Here is a what i've build , a java library that provide many features for using regex to generate String (random generation ,generate String based ...


3

If you're using a logging framework like Log4j, you can use a nested diagnostic context (NDC) to store the file name at the point it is available, then retrieve from the NDC at the time you log the messages. Logj4 NDC This can be even more useful because you can actually write a log4j appender that handles log messages specially based on the NDC content. ...


0

In all honesty, I like your answer best. The return array should be the same size as the input array, so you can easily determine which inputs resulted in which outputs. You can also loop through the results array and check to see if the exception is null or not, and if not, then process the result. Another way to do this would be to pass in an array that ...


1

Sorry to hear things aren't necessarily how things are going how you thought they would in college. Let me start by saying you're on the right track. While a degree can be helpful when getting a programming job I've found that experience and getting a good portfolio out there for potential employers to view can be equally as impressive. If you're seriously ...


0

Since you are already using Maven, you may consider giving the Exec Plugin a shot. I would rather avoid custom runs scripts if I were you unless the startup and shutdown logic is really complex, since writing them properly may be much harder than it seems, and you can run into issues with cross-platform or even cross-machine compatibility. If you need lots ...


2

It sound like you are running some Java programs in a sequence and basically you were using Ant to call java as java -jar myApplication.jar ... Normally, I see this done using shell scripts. They are simple, small, and very good for redirecting output to files, other programs, etc... If you want something that is more cross-platform compatible, you could ...


1

If I were you I would simply keep a place holder in a separate file. If you are looking for alternatives though, you could place a special character directly in the log. Like a flag/number as the first line of the log that would update after reading every line. If you are working with several files, you could check each file for that first line flag or ...


0

One of the ways I can think is to store the line number of the previous read and then start from that position, something like this and keep reading until eof. public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader (new FileReader("Profiles.ini")); String info = ""; int startLine = 17; int endLine = 22; //EOF in ...


4

The problem is with this line: for(rate = 0.05; rate <= .10; rate++) As you've written that loop, here is what you're telling the computer to do: initialize rate to 0.05 check if rate is below .10 do the things in the for loop increase rate by 1 (new value: 1.05) check if rate is below .10 it isn't so stop the loop. Instead that line ...


1

I would recommend the following: Implement the data layer using an established ORM framework. My favorite is myBatis, but JPA and Hibernate are definite contenders. Wrap the data layer with a Java API that supports only business operations (not CRUD operations)- e.g. Read customer details, enrol new customer, cancel customer, place order etc. That way you ...


1

Few benefits of using GWT that I think of ( more details read my blog http://www.pandurangpatil.com/2012/09/benefits-of-using-gwt.html) As GWT client application is written in Java, one get an opportunity to catch syntactical mistakes at compile time because of the same (Though it doesn't support all the JRE classes as those features are not supported by ...


1

Return an array of Optional. That aside, you might want to consider better collections than arrays; they're quite cumbersome, especially since they don't implement Iterable or Collection. EDIT: For catching the type of the error, your Result<T> is a good starting point, but I wouldn't expose both fields like that (I assume you only did that to get the ...


1

The error is thrown from Double.parseDouble, in other words outside of your code, by the time setHourlyPayRate gets called you know it's a valid double. The conversion from string to double is not your problem™. If you really want to throw your custom exception then you need to wrap the conversion in a try catch and rethrow the custom exception: String ...


4

Catch the exception at the level where you do have the file name, incorporate the file name into the error message, and rethrow. This is a classic example of why exceptions are designed the way they are; they propagate up the call stack to the next available catch clause, where new information can be added to them where it is available. This avoids the ...


0

The simple apporach is to just fail when one of the bars fail. But if you want to process the entire list of bars, then you will need a more complex object that contains arrays like: SuccessfulFoos[] FailedBars[] FailedBars would be a composite object and would encapsulate the error information as well. SuccessfulFoos would just be a Foo since no ...


1

A good solution here would be the abstract factory pattern. Essentially, there is an abstract base factory with a series of abstract methods for building certain abstract classes. Then, you would provide a concrete subclass of this factory implementing the methods for a certain library. Take this example: public class ScheduleCreator { public ...


0

Your question is generic, so the answer is when it helps. However, I can name two cases where I find myself using default modifiers often. Firstly, they are handy when you want to test a private method but don't want to make it available for child classes or outside of package. Another use case is to refactor a nested class into a separate file while ...


0

The default access modifier makes the member visible throughout the entire package. This makes using it on a member that's mutable in any way only marginally better than a global variable. The only possible use I can think of for it is to create constants that you can use as an implementation detail within a package. If you later decide to get rid of or ...


2

The default access is otherwise known as package level private or protected... your choice, it doesn't really have a term, but its useful to think of it that way. If you dig within the source for java.lang you will often see it - allowing other classes within java lang to be able to access some of the internals of this class without exposing those internals ...


1

A UUID is a standardised representation of a 128-bit number. As noted by AndyBursh, the representation is 32 hexadecimal digits, split into groups and separated by dashes. Nothing else is a proper UUID, it would just be a string. The point of a UUID is that it's "universally unique". If I just pick a string that I like the look of, then there's no ...


7

No. A UUID has a particular, standardised format. From the wiki article on the subject: In its canonical form, a UUID is represented by 32 lowercase hexadecimal digits, displayed in five groups separated by hyphens, in the form 8-4-4-4-12 for a total of 36 characters (32 alphanumeric characters and four hyphens).


1

Your UI should tell the database layer what operation to perform. In the UI, you will obviously have two buttons, one which says "Create new Person" and the other saying, "Edit this Person". So you already know what action needs to be performed. When you invoke the UI Dialog, pass in this state (Add or Edit) to the dialog. Then, based on the value of this ...


0

I think what you are looking for is the finally clause. This will allow you to run some other code, even in the event of an exception. boolean importantFlag = false; //Default value try { importantFlag = getFlag(); } catch (Exception e) { //rethrow exception } finally { //This will run whether or not there is an exception. ...


0

I'm not sure I understand the question, but here's my take on it. In these cases you might want to create a wrapper function that calls the unsafe function safely, and always returns a value. Falling back on a default value if there is an exception. function boolean getFlagSafely(boolean defValue) { try { return getFlag(); } catch(Exception) ...


-2

If you're actually in Java, finalize the variable. boolean foo() { final boolean importantFlag; try { importantFlag = getFlag(); } catch (Exception e) { throw new RuntimeException(e); } return importantFlag; } This will compile and run safely. If you, however, removed the throw statement from the catch block, ...


-1

When dealing with exceptions, it usually pays to have as much code as possible inside the try block. Not just the function itself that could throw, but also everything that uses the return value from the function. In your case, I would write it as: try { boolean importantFlag; importantFlag = getFlag(); doSomeStuff(importantFlag); // more ...


0

Two options that I used in my last project: maven assembly plugin: this plugin can generate for you a "package" that contains the libraries you need and a generated shell script to execute the program. Another option I like a lot is to generate a "fat jar" with all the libs merged in a big .jar. This simplifies a lot the distribution and executing of java ...


0

In a perfect world shutting down your Ant program would also shut down your Java programs. It sounds like you need an explicit shutdown procedure. Ant is great for writing platform-independent scripts. I've never heard of a long-lived Ant process before. In my experience the Ant program runs, does its job, and exits. I would suggest setting spawn=true ...


2

tl;dr - I think you're going to want to inject state objects at each level of state as appropriate. Reason being, as your application scales and you add in more controllers you're going to want to control which controllers talk with which other ones. And the first approach you suggested will position you better to support that. To help illustrate ...


4

See also: Lower-bounded wildcards the answers to the question java generics super keyword on Stackoverflow. A digression. Feel free to skip. For programmers who are also familiar with C#, a similar feature exists: Covariance and Contravariance in Generics The terms, covariance and contravariance, can be traced back to the early discussions ...


5

List<? super RationalNumber> is a list of some type X that is a superclass of RationalNumber. Your assumption is that RationalNumber is a subclass of Number, so the possible candidates for X are RationalNumber, Number, or Object. The most restrictive possibility is that X is RationalNumber itself. A List<RationalNumber> can contain only ...


3

As you copied parts of the other library into your library, the authors/copyright holders of the parts you copied must also be mentioned as (joint) copyright holders of your library. For that reason, neither option is correct. A correct way to provide the right copyright and license informations is Copyright 2014 <original author names>, <new ...


1

I would recommend using composition instead of inheritance in this scenario. Also, I would reverse the model to match the logical flow of control. A HumanState can contain member variables for the HandStates. In this way, you have a logical composition of elements. HumanState -> --> contains HandState x2 --> contains HeadState x1 --> ...


1

I would add to Kilian's answer that one big difference between Java and C# is that C# designers control not only the language, but also the standard IDE. Adding something like extension methods and partial classes could be a nightmare in development/control versioning if the IDEs would not properly support it. Since you are expected to be able to compile ...


2

In my experience that depends on how much you need to do once you caught the exception. If you need to just propagate it further after closing some resources - there is no need to make your own exceptions. If you need to do some complex work depending on what failed and how - custom exceptions are your friend. If you just use a single ...


12

Several reasons: C# came later than Java; version 1 was a blatant rip-off of Java 1.4, so it pretty much had everything Java had at that point. But then C# developed much faster than Java, because it was an exciting new platform (and had an utterly brilliant driver in Anders Hejlsberg, the father of Turbo Pascal). That allowed them to avoid all the ...



Top 50 recent answers are included