|
geeves posted:Also Java's own documentation follows this model as well. https://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html That java example requires the scope change because they are using the variable in the catch/finally clause. If you need to do that, you are forced to declare it outside. His example was not however, so it shouldn't be done. Why would you do that for the PreparedStatement and not for the String that creates the query? Or any variable for that sense? You should only move things out of scope when its required. Zaphod42 posted:Why do you have to continue to talk down to me dismissively like I've never heard of improving code readability? Everything I've posted in this thread suggests I know exactly what you're talking about. Where we disagree is over how big a deal this is to readability; not whether readability matters period. Very arrogant attitude. Because Zaphod42 posted:Saving a line doesn't matter. Who cares. It sounds like you don't care about code readability, or you wouldn't have said this was a waste of time when the entire purpose of it was to improve code readability! I just can't give someone credit about code readability when they openly dismiss something that improves code readability. I literally see no advantage to just 'leaving it because who cares' other than laziness.
|
# ? Nov 16, 2016 22:37 |
|
|
# ? Jun 13, 2024 06:22 |
Thanks for the help, everyone. After changing the table name and ensuring I include the properly encoded ' symbols, it works OK now. Just in time for me to run into a new error. I'm working on a Java web application, and when I attempt to reference jQuery / Bootstrap in the head, the styles doesn't seem to be applied to my page. E.g: code:
|
|
# ? Nov 16, 2016 23:01 |
|
At my job, we recently had a bug in a production application and I'm interested for ideas as to how we can attempt to prevent this in the future. Here's some example code to illustrate the bug:code:
We are not terribly sophisticated when it comes to automated testing. I write unit tests that provide good coverage for my code, but I'm kind of in the middle of the totem pole and don't set the culture/standards/expectations. So as a whole we really, really lack in unit testing and way too much of our code is not covered by any automated tests. That being said, I'm not sure how you would unit test this, or even if that is appropriate. Previously, I've used H2 to setup a simple in-memory database to unit test stuff that reads from a database, but I don't know how I would test something that calls an Oracle DB stored procedure. My best guess is that this would be the domain of integration testing and that you'd setup a temporary database and run some higher level (than unit) tests. Again, we're not terribly sophisticated in this sort of thing, so I don't even know how that would be done or what sort of tools/libraries/frameworks would be involved. TLDR: How do you appropriately test whatever bit of Java code that interacts with your database, in particular calling Oracle stored procedures?
|
# ? Nov 16, 2016 23:04 |
|
FateFree posted:It sounds like you don't care about code readability, or you wouldn't have said this was a waste of time when the entire purpose of it was to improve code readability! I just can't give someone credit about code readability when they openly dismiss something that improves code readability. I literally see no advantage to just 'leaving it because who cares' other than laziness. My point in saying that line was to say there's no functional performance increase, so readability is the only concern. So in my opinion it wasn't a readability issue. I was just crossing off that potential reason since we hadn't yet established that it was for code readability. That doesn't mean I don't care about readability. venutolo posted:The developer never put it in for code review and I have no idea how QA didn't catch that the wrong end values were being written to the database. For the purposes of my question, let's assume that the code was reviewed but this small defect was missed. And let's assume that I want to catch this before it even goes to QA. So what sort of (automated) process might catch this thing? You need to write a regressions test suite, with lots of sanity checking on each individual component as well as the overall operation. This can be a ton of test cases, but once you get the regressions suite going you can just add tests to it as you go. Although this in particular is tough. All I can think of is adding a bunch of regressions tests that do sanity checking on the database. Yeah you'd definitely want to set up an example database, you should have a testing image with data, and you can have test cases that insert data, run tests, then clean up the data afterwards. So query for all kinds of data and see if anything is in any incorrect states after running each process that you're testing, like I guess you could query for start = end and throw flags if any data has the same values, and then also query for the expected results in the database and make sure they actually ended up there and nothing's missing. Its gonna be hard to think of everything though. For stored procedures all I can think of is generating new data, running the stored procedure, and then doing sanity testing on the database afterwards to make sure its all in the proper state. And just doing code review on the procedure itself to double check everything. Zaphod42 fucked around with this message at 00:49 on Nov 17, 2016 |
# ? Nov 17, 2016 00:44 |
|
venutolo posted:TLDR: How do you appropriately test whatever bit of Java code that interacts with your database, in particular calling Oracle stored procedures? You don't. I mean, you don't need to. All you should do here is to mock JDBCTemplate and verify that the update method gets called with the parameters you expect (in this case, the appropriate query string, and appropriate parameters). A relatively decent mocking library is jMock. However, be careful with mocking things. You mock things because method X has a hard dependency on some service that you don't want to test at that time. Question is: can you do the test without that dependency? Can you refactor the code so that the test is testing what you need without mocking things? In this particular case the answer is no, you really need to check what parameters are sent to the jdbcTemplate. But when you find yourself mocking a bazillion services to test one method, you should really consider refactoring the method. Don't ask how I know this . edit: Mocking things is not bad in and of itself. Abusing it, is. Show restraint when exercising your mocking powers. Volguus fucked around with this message at 02:46 on Nov 17, 2016 |
# ? Nov 17, 2016 02:24 |
|
Ornithology posted:
No , you don't. If the CSS-es are downloaded they will be applied, no questions asked. You should check if they are downloaded though and applied via the developer tools in the browser you're using.
|
# ? Nov 17, 2016 02:40 |
Volguus posted:No , you don't. If the CSS-es are downloaded they will be applied, no questions asked. You should check if they are downloaded though and applied via the developer tools in the browser you're using. Sorry guys, I guess I was just being dumb or something but after messing around with different browsers and a bunch of other crap it ended up working in the end. It always seems I'm posting here with questions that aren't even legitimate issues.. god help me when I get to a real problem or two.
|
|
# ? Nov 17, 2016 04:39 |
|
Ornithology posted:Sorry guys, I guess I was just being dumb or something but after messing around with different browsers and a bunch of other crap it ended up working in the end. It always seems I'm posting here with questions that aren't even legitimate issues.. god help me when I get to a real problem or two. Nah, the 'what... why???' stuff is real programming. Learning to track down the source of a problem and then work out what the hell is going on is fundamental, because there's always something. It'll help when you run into bigger problems
|
# ? Nov 17, 2016 07:26 |
Does anyone know of a good resource for learning how to create a Spring Web MVC project using Maven? The documentation for this seems to be all over the place and either doesn't correspond to the newest versions or is vague and has unexplained differences in file structure. I even downloaded the Spring Tool Suite IDE to see if it would help, but it looks like it doesn't even have a way to create an empty Web MVC project. Instead, I tried using this project provided in the spring.io documentation files as a starting point (https://github.com/spring-guides/gs-serving-web-content/tree/master/complete). However, for some reason it is missing important files like web.xml. I can't find any declaration of a view resolver anywhere and if I try to return jsp pages instead of html then I end up with 404 or 500 errors. I then found this sample project (https://github.com/spring-projects/spring-mvc-showcase) which looked more promising as it includes many more basic features and actually has important files/folders like web.xml and WEB-INF. However, when I try to run the project there's like 50 errors about missing files or incorrect references.... fun. Anyway, if someone can suggest a guide or point me to a basic project structure diagram or something, I'd really appreciate the help. Bonus points for the same, but for Hibernate.
|
|
# ? Nov 23, 2016 06:02 |
|
About functional programming in Java 8. I was doing a very simple loop:code:
code:
Is this maybe a bad example for functional stuff and what are the actual advantages of doing things in this way?
|
# ? Nov 23, 2016 09:46 |
|
John F Bennett posted:About functional programming in Java 8. I was doing a very simple loop: That's not functional programming, it's just a bad example of lambdas. Which are actually really cool. However a bigger problem in your code is that you are concatenating strings in a loop. In Java that means copying the whole string, every time, so that's O(n^2) complexity: very slow if your inventory has many items. Use a StringBuilder or StringBuffer that are designed for that very purpose. A better example might be code:
code:
pigdog fucked around with this message at 17:40 on Nov 23, 2016 |
# ? Nov 23, 2016 11:01 |
|
Thanks for the tip! I will play around with this tonight. I know about StringBuilder, but as the inventory will never contain more than 10 items, I decided to use the short version. This code will also only be run once, at the beginning of the program. But I will do it anyway, just for doing it the right way.
|
# ? Nov 23, 2016 11:08 |
|
pigdog posted:That's not functional programming, it's just a bad example of lambdas. Which are actually really cool. Map/reduce is pretty functional! e- oh you're saying it can be written better. Yeah IDEs have some weird suggestions sometimes John F Bennett posted:My IDE suggested replacing it by this thing: It's not really the best example considering how simple your loop is, but it basically says
The idea is that you're describing what you want to do, instead of how to do it. You manipulate a stream of data with a kind of pipeline of functions that shape the end result. For more complicated processes, your intent is clearer (because you can just read the steps instead of working out what the code is doing), there's less chance of making a mistake (because you're using a set of basic functions that have been implemented for you, and you're not managing state), and you can get other benefits like parallelism and lazy evaluation for free If you want a sort of bad analogy, it's like how a basic for loop gives you advantages over a while loop where you're managing a counter and checking an exit condition yourself. And how a for each gives you advantages over that, just handing you elements from an iterable so you don't have to pull them out with an index yourself. There's definitely a learning curve to functional stuff, and it's not always the best tool for the job, but it's definitely worth learning - especially since you seem to see it more and more, so at least being able to read it is important It gives you a new perspective too, it's neat baka kaba fucked around with this message at 11:44 on Nov 23, 2016 |
# ? Nov 23, 2016 11:40 |
|
If I want to do an action 5 times, is there a fancier way in modern Java than an old school for loop?
|
# ? Nov 23, 2016 13:55 |
|
smackfu posted:If I want to do an action 5 times, is there a fancier way in modern Java than an old school for loop? Not that I'm aware of in Java 8. There are sillier ways that may look fancier at first glance: Java code:
I'd write the loop once in some static utility if you really want this: Java code:
|
# ? Nov 23, 2016 16:49 |
|
The Laplace Demon posted:I'd write the loop once in some static utility if you really want this: Isn't that just a longer, janitored version of IntStream.range(0,5).forEach(this::action)?
|
# ? Nov 23, 2016 17:49 |
|
IntStream.forEach() takes an IntConsumer, though, so you can't just pass this::action if it doesn't accept an integer parameter. Now you're jumping through hoops to get the signatures to fall in line and you'd be better off just using a normal loop.
|
# ? Nov 23, 2016 18:20 |
|
Oh yeah I'm not saying do it, but if you're going to create a method that takes a runnable to run it in a for loop so you can call it with a method reference, you may as well just do the range thing
|
# ? Nov 23, 2016 19:22 |
|
But you only have to write that function once, compared to writing IntStream.range(0,5).forEach(ignored -> this.action()) every time. (You can't use the method reference in that line if it doesn't accept an integer parameter.)
|
# ? Nov 23, 2016 19:37 |
|
Yeah I just twigged the Runnable = no-args functional interface thing, which is weird because I just learned that the other day You could still use a lambda though. If you have use for a utility function then great, if it's an occasional thing then the range / forEach style isn't much longer than calling the custom function. I probably wouldn't do it, but it's not that bad
|
# ? Nov 23, 2016 19:56 |
|
baka kaba posted:You could still use a lambda though. If you have use for a utility function then great, if it's an occasional thing then the range / forEach style isn't much longer than calling the custom function. I probably wouldn't do it, but it's not that bad Counterpoint: both are bad and if you want to do that you should use Python. Honestly that goes for C#'s LINQ too, it's a nice idea in theory but in practice it's a great way to kill your performance.
|
# ? Nov 24, 2016 03:46 |
|
smackfu posted:If I want to do an action 5 times, is there a fancier way in modern Java than an old school for loop? Use the for loop. If the JVM determines that this code is a performance "hotspot," it'll "unroll" the loop, optimizing the code for you on the fly. (The JIT doesn't understand streams well enough to optimize around them, which is a damned shame.)
|
# ? Nov 24, 2016 08:53 |
|
Ornithology posted:Does anyone know of a good resource for learning how to create a Spring Web MVC project using Maven? The documentation for this seems to be all over the place and either doesn't correspond to the newest versions or is vague and has unexplained differences in file structure. Look at Spring Boot guides should get you rolling
|
# ? Nov 24, 2016 09:33 |
|
Paul MaudDib posted:Honestly that goes for C#'s LINQ too, it's a nice idea in theory but in practice it's a great way to kill your performance. If it does, you can optimize it away. Using functional programming in Java has not been the cause of performance problems in our project so far.
|
# ? Nov 24, 2016 13:59 |
Can anyone recommend a good resource to set up a BASIC spring MVC database connection layer to connect to MySQL database? I've spent the last week attempting various hibernate tutorials and now just basic jdbc access and nothing seems to work. All the guides inevitably list code without saying what file it belongs in or where, and it's getting extremely frustrating.
|
|
# ? Dec 1, 2016 23:27 |
|
Ornithology posted:Can anyone recommend a good resource to set up a BASIC spring MVC database connection layer to connect to MySQL database? I've spent the last week attempting various hibernate tutorials and now just basic jdbc access and nothing seems to work. All the guides inevitably list code without saying what file it belongs in or where, and it's getting extremely frustrating. A quick google gave me this: https://github.com/shagstrom/spring-mvc-hibernate-skeleton I didn't look at it in great detail but it seems to do the job. For a more detailed example checkout https://github.com/spring-projects/spring-mvc-showcase (and other repositories of spring-projects, they have examples for everything)
|
# ? Dec 2, 2016 01:06 |
|
I'm trying to implement some concurrency locking functionality. I need to lock on objects, but I want the locks in a map, and remove them from the map when they're unused, but I can't figure out how to do it without errors or deadlocks. Here's what I have right now:code:
code:
EDIT: If I use a ConcurrentHashMap and remove synchronized from the get and remove mutex functions, it doesn't deadlock, and doesn't seem to have errors. Is that a good idea? EDIT: I just learned what WeakHashMap is. Could I use that instead? That would simplify the code a great deal. Boz0r fucked around with this message at 10:32 on Dec 2, 2016 |
# ? Dec 2, 2016 09:33 |
|
Well, a couple of thingsJava code:
Java code:
pre:Thread 1 read (5) add (5+1) write(6) Thread 2 read (5) add (5+1) write (6) I can't really see why a ConcurrentHashMap would deadlock there, unless you're doing something like putIfAbsent in a compute operation - you'd have to post the code. I could be missing something though! I think a WeakHashMap is fine there, so long as you're ensuring all access to it is synchronised like you're doing there Also you probably shouldn't have the mutex holders doing that increment call themselves. Stick it in the Locker's getMutex function, so it's all encapsulated and the Locker handles all the counts internally. (It's not actually doing anything at the moment, the increment only happens once a thread enters the synchronised block, and it decrements it with the remove call before it leaves the block, so each mutex only ever has a count of 1 or 0) baka kaba fucked around with this message at 13:02 on Dec 2, 2016 |
# ? Dec 2, 2016 12:57 |
|
The race condition is what I'm trying to provoke in TestFunction(). I need to be sure that it locks the element. I've changed the code a bit, and it seems to be working now:code:
code:
code:
Boz0r fucked around with this message at 10:32 on Dec 5, 2016 |
# ? Dec 5, 2016 08:43 |
|
What are you actually trying to accomplish?
|
# ? Dec 5, 2016 09:20 |
|
I have some data that needs to be processed. Because it depends on some other data, I need to be sure that multiple threads don't process the same data. The data is fetched from a database, so I don't think I can just lock on the object, as they may not be identical. I'm trying to create a lock based on the ID, and save it in the map. If the next thread needs the same data, it can get the same lock, but if not, I want it removed from the map. EDIT: I forgot to add the Main class to the post. The errorCounters map represents the data that needs to be processed. Boz0r fucked around with this message at 10:33 on Dec 5, 2016 |
# ? Dec 5, 2016 10:31 |
|
You seem to be way overthinking this. Even with your description, it's entirely not clear why it needs to be so complex. What do you mean by "don't process the same data"? Is this a problem about distributing work to individual threads, or does this processing involve mutating the data? What aspect of the processing means that multiple threads might need to access the same data? What lead you to thinking Semaphores would be the appropriate solution to your problem?
|
# ? Dec 5, 2016 10:43 |
|
Jabor posted:You seem to be way overthinking this. Even with your description, it's entirely not clear why it needs to be so complex. The processing involves mutating the same data. The semaphore idea is from the lead dev, so I'm just falling in line . I'm very open to better solutions. Boz0r fucked around with this message at 11:05 on Dec 5, 2016 |
# ? Dec 5, 2016 11:03 |
|
It sounds like if you distribute the work appropriately (so that two threads aren't going to process the same data), then everything just works? So really it's just a work-distribution problem. The easiest way is to have a single thread determine which items need to be processed and use a thread pool to handle the actual execution: code:
|
# ? Dec 5, 2016 11:26 |
|
From your example everything looks to be more complicated than it needs to be. If you would have ConcurrentHashMap<Integer, AtomicInteger> you can get rid off synchronized, Sempahore and ... pretty much everything else. Can simply put into the map and increment the key when needed without worries. But, the first question is : do you have to mutate the same object from multiple threads? Can you do without?
|
# ? Dec 5, 2016 14:28 |
|
I have a process to pull data about an Object out of a database and then check a value of that data, only performing work on the Object if it a) exists and b) isn't in read-only mode due to editing. If the Object doesn't exist the pull request returns a null value, which in turn throws a NullPointerException if the second condition is checked. What is the best method for writing a condition check for these two? I could just write them separately, but I figure this is a good learning moment.
|
# ? Dec 5, 2016 16:51 |
|
Janitor Prime posted:Look at Spring Boot guides should get you rolling Seconded. Spring Boot and latest Spring framework in general means no more web.xml and no more WEB-INF folders. gently caress XML configuration forever. Also Gradle is a great replacement for Maven.
|
# ? Dec 5, 2016 22:58 |
|
The nice thing about Maven is it is declarative though - which makes it trivial to use as an IDE-agnostic project file. Literally any IDE can look at a POM file and spit out a working project, so you can just go ahead and git-ignore all IDE-specific project files from the repo forever and never worry about project files getting out of date or that one idiot who just can't help but check them in every single time clobbering your project config updates. If you still want to have your "libs" dir that can build the whole app without any dependency downloads, you can still do that by installing everything into a local repository and naming it in your POM file. Ideally you will give the path relative to ${project.basedir} for portability. You pretty much need to do this for some stuff anyway, like the Oracle OJDBC drivers or some of the Java EE JARs that aren't in Maven-Central due to licensing concerns. This applies to Gradle too of course. Also, as much as Maven has its bugs, at least it's not so slow that it needs to live as a resident daemon Paul MaudDib fucked around with this message at 01:24 on Dec 6, 2016 |
# ? Dec 6, 2016 01:18 |
|
PierreTheMime posted:I have a process to pull data about an Object out of a database and then check a value of that data, only performing work on the Object if it a) exists and b) isn't in read-only mode due to editing. If the Object doesn't exist the pull request returns a null value, which in turn throws a NullPointerException if the second condition is checked. What is the best method for writing a condition check for these two? I could just write them separately, but I figure this is a good learning moment. Writing them separately is pretty much fine and you shouldn't try to overthink it. That said, logical short-circuiting works pretty drat well here. code:
The same thing works on OR, but in reverse. With OR as soon as it sees a true value it short-circuits since TRUE OR X always yields True. But that's just fancy syntactic sugar really to make the code simple, you don't need it. There is *nothing* wrong with this: code:
|
# ? Dec 6, 2016 01:34 |
|
|
# ? Jun 13, 2024 06:22 |
|
Paul MaudDib posted:The nice thing about Maven is it is declarative though - which makes it trivial to use as an IDE-agnostic project file. Literally any IDE can look at a POM file and spit out a working project, so you can just go ahead and git-ignore all IDE-specific project files from the repo forever and never worry about project files getting out of date or that one idiot who just can't help but check them in every single time clobbering your project config updates. I find doing that is a huge pain in the butt compared to just dropping libs in a "libs" dir though. What I'd really prefer to Maven would be a system where you have a libs dir and then Maven just automatically downloads and places libs into the libs dir based on dependencies. Any files that were not part of the Maven POM file would be ignored in the libs dir, so you could have your own libraries that Maven doesn't know about, but Maven would also pull things for you automatically and replace the old versions, more like a package manager. Then you'd have a totally portable folder that can compile without Maven. Make it more of a setup tool. My problem is that Maven itself ends up becoming a dependency that anybody who wants to deal with your project has to deal with. I really love being able to just rip a lib from an FTP site and compile right away without having to edit POM files. That's great for the big heavyweight stuff with lots of dependencies and versions, but for some small lightweight libs its super overkill and takes too long to get going. I don't know why they don't just build that as a feature into Maven, even. "Maven copy dependencies" would build a libs dir or something to that effect. But I guess its kinda anathema to Maven's mentality, so it'd have to be some other software to offer that kinda approach.
|
# ? Dec 6, 2016 01:39 |