Help coderanch get a
new server
by contributing to the fundraiser

Christopher Schneider

Greenhorn
+ Follow
since Aug 10, 2011
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by Christopher Schneider

The date would only come from the client if the date is supplied by the client via a request. Otherwise, it's coming from the server.
4 years ago
I think I can add a little more information here:

I'm not sure that the name of the method is critical so much as the @Bean annotation is



In this example, no, it doesn't matter since there's an explicit name in the @Bean annotation.

Naming does matter with this, however, as beans always have names, even if they're not declared manually:



In this case, the bean's name is the method name. With that, it's generally just simpler to just let Spring do the naming do the work for you via method name. So an equivalent to the previous example is:



As for the behavior in question, I believe this is what happens, but can't find the specific documentation. I recognize that's basically what is being asked here, but I'm struggling to find explicit documentation on what the load order is here.

1. Spring scans for components
2. Finds a @Component and @Configuration - loads beans from @Configuration
3. Sees there is already a component named "businessBean" so does not load this bean. This would essentially be a bean override.

That makes sense to me, at least, knowing what I know about Spring's general behavior and having written code with beans intended to be overridden by name. Basically, the first bean with a given name wins.

There was a general discussion about this behavior and that it was not desired behavior here: https://github.com/spring-projects/spring-boot/issues/13609
As of Spring Boot 2.1.0, overriding a bean by name requires setting a property, so on Spring Boot 2.1.0 at least, I would expect this to cause a startup failure.
4 years ago
Just a few things to add here, since it seems to me that there's just a lot of resistance to using well established tech with dated conceptions around Javascript.

The primary concern I have is this: It sounds like the approach here is to execute any arbitrary Javascript returned from a server. Hopefully you account for the XSS implications here, but I obviously don't know your application.

With that out of the way, I just had a few comments:

Frameworks such as React were essentially built to do what I think you want to do. Re-usable components. There is a fairly steep learning curve, however, but that's really the case with any new framework. At my employer, we've written complex administration tools as well as customer facing applications using Vue (Similar conceptually). I'm not advocating for or against using one of these frameworks, as, like I said, there is a learning curve, but know that they're generic enough to make pretty much anything you'd want.

I'm currently solving a classpath problem lol.



This is what dependency management (like Maven) is for :-) I write Java every day, and I would never write a Java program without some sort of dependency management. Even on personal projects. I can't imagine manually adding libraries to the classpath for a considerably sized project. I've done it before on smaller projects, and it's just not worth the effort.

While there is a bit to learn with Maven, it's pretty minimal. It also has the nice side effect of encouraging adding dependencies by making it much easier. Since you seem to have an aversion to what you call "proprietary" code (Which is incorrect) I want to emphasize this isn't a bad thing. Dependencies can be something as simple as Apache StringUtils (Which I use daily) for String operations, or something much more complex, like Spring. Either way, these are often well tested in production and can significantly speed up development.


4 years ago


I think you're getting ahead of yourself. Before you look at an advanced concept like generics, you should understand constructors.

When you instantiate an object, you pass it parameters. You are passing parameters named String str and Integer id.

The two issues here are that they do not exist (the reason for "can't find symbol String.") and they have spaces.

The proper way to do this would be:



Here, I am passing a string literal, "baseball", and an integer, 1. These are defined values, whereas your previous code passes undefined values to the constructor.
4 years ago

what was the point of implementing it of the data that is added will be the size of tha HashMap



I don't understand what your question is. Do you mean the buckets themselves are of a similar size to a HashMap? In the case of small maps, this is a trivial amount of memory. In the case of large maps, trees are used and there are more elements per bucket, so this is also trivial when compared to the memory consumed by the many elements in the map.


4 years ago
Decided to write a new utility. I have some old code that does exactly what I need for the new utility.

I wrote this code a couple months ago. Who needs to document, right? I've got some pretty clever looking C code, but no idea what it does and I wrote it. A small header on top of the function describing what it does would have saved me tons of time.

Ugh... I wouldn't do this at work, and I'd curse the person who left me this code. Apparently I have no problem doing it to myself...

From now on, I am required to document my own code. I don't care if I know what it does now.
8 years ago
I missed this, but this is a perfect example on how you should refactor. This class:



Logically, think about setName(). What should a method called setName() do? It should set the name. It shouldn't accept user input, do validation, or anything like that. All it should do is set the name. If you want to validate input, create a method called validateName(). If you want to get input from the user, make a method called getUserInput().
9 years ago
Holy smokes!

I'd like to help, but I can't figure out what's going on here. You have a method that is 90 lines of code, and that is far, far too long. Your first step, before anything else, is refactor. Some things you definitely need to fix before you progress:

1. Methods should be kept short and do one thing. As an example, on line 56:

You might extract the code beyond that if statement into a method called scratchHorses().
2. You don't need to call System.out.println() to print a new line. This:

can be shortened to

3. Class names need to start with uppercase letters, methods with lowercase letters. e.g. thoroughbred should be Thoroughbred.
4. Give meaningful variable names. Don't do this:

If you can clean up this code, I'd be happy to help. Ideally, a method should only be 10-20 lines.
9 years ago
I think this may help. Looking at your MyGeneric class:



You should remember that T can be anything. With that code, I could theoretically do this:



There is no method getWhatAmI() defined for an Integer class, however, so you'll receive a compile time error.
9 years ago
I've been thinking a bit about the use of enumerations versus an interface, and when one or the other is preferable. I'm looking at this from a has-a perspective.

In this hypothetical situation, I have a Delivery class. The Delivery has a method by which it is delivered. Let's say the delivery methods are Ground and Air. At this point, nothing differentiates Ground from Air delivery and they're essentially just tags. This is all the specification calls for.

The way I see it, I have two options (If there's a third I don't see, I'd like to hear that, too):

1. Have an interface, something like DeliveryType, and have Ground and Air implement that interface.
2. Have an enum containing Ground and Air.


Assuming there's not a third, better, option, I would most definitely lean toward option #1. If I want to add another new delivery type(e.g. SpaceShip or Boat), I make a new class and implement the interface. If I want to add functionality specific to a single delivery type, I can do that as well even though there is currently no unique functionality.

My reasoning is that unless I know for sure that something is never going to change, I'm not going to use an enum. Even though the refactor with an enum would be easy, I find zero refactoring preferable to easy refactoring.

Thoughts?
9 years ago
I copy/pasted the code into an IDE, and there are many problems with the code as is.

First, are these two classes in separate files? If not, they need to be. The class name needs to match the file name, or you can use inner classes. I would recommend that you put the classes in separate files.

The Toy class is fine as-is. It's only holding data: getters and setters. The ToyDemo is where everything breaks down. You have no default constructor for Toy, so on line 10 you cannot instantiate a new Toy(). Remember, your constructor for a Toy is:



Therefore, when you say it doesn't work because expects a name, category, price, and discount. You instantiated a toy correctly on lines 15, 16, and 17:



because they match the constructor with a name, category, price, and discount.

There may be confusion here with the default constructor. If you do not supply a constructor, the compiler generates a default constructor that accepts no arguments. However, you've created a constructor for Toy, so no default constructor is generated.

Now the elephant in the room is line 13:


I have absolutely no idea what's going on here. What are you trying to do?
9 years ago
Some initial observations:

-I don't understand the point of

It's a poor variable name even in practice code since I have no idea what its purpose is at a glance.
If you're just trying to get a Regex pattern working, make it simple.

-Putting spaces between your = signs makes things much easier to read.


I don't think there's anything wrong with your regular expression. You are not using Matcher.find() correctly, though.
9 years ago
Hello folks, back again. The book I'm reading had me create a simple clock program, and it works fine.

However, when it displays the time, if the minutes are below 10, it doesn't show a zero in front, and it doesn't look right. e.g. if it's 10:09, it shows up at 10:9 in the window. It's not a big deal, and I'm still moving on to the next chapter of the book, but I'd like to know how to do this anyway. Here's the code (irrelevant information excluded):

13 years ago

Campbell Ritchie wrote:. . . and welcome to the Ranch

I personally would prefer to use a book like Head First Java™. Make sure to use the 2nd edition, and you can get a second-hand copy at a very reasonable price.



Thanks for the responses. Going to have to look it over some more... Need some caffeine

As for HFJ, I already tried that book, and it wasn't for me.
13 years ago