|
ulmont posted:Conceptually, both a DFS and a BFS will search every cell until they find their target; the only difference is the order. Makes much more sense now, thank you so much for the clarification!
|
# ? Jan 3, 2021 18:58 |
|
|
# ? May 9, 2024 09:29 |
|
Sorry if this is a bit late. The way I look at DFS versus BFS is the type of data structure that you use to store intermediate nodes to be processed. DFS tends to get written as an explicitly recursive algorithm but you can remove the call stack and replace it with an explicit stack in code iterated over with a loop. BFS doesn't have a nice recursive way to write it out (probably something involving coroutines though) but can be written as an iterative loop with a queue. At this point it's worth mentioning a fun little data structure called the deque that can be a stack or a queue at the same time. A search written with a deque therefore can do either BFS or DFS with remarkably little modification. code:
code:
|
# ? Jan 5, 2021 03:19 |
|
1337JiveTurkey posted:Dijkstra and A* These are terms I haven't heard in many a year...
|
# ? Jan 5, 2021 03:25 |
|
https://xkcd.com/2407/ is topical.
|
# ? Jan 5, 2021 08:21 |
|
Is there any good "best practices" for java? I recently started a job as a java programmer and I'm trying to find some... source of sanity basically. I know basic stuff like SOLID/DRY/"code is read 10x more often that written", and in general I try to copy the style of whatever I'm working on but, well, when a 1k line for-statement makes it through code review I'm assuming I shouldn't use the code base as inspiration for clean code. I'm new to java code and just want some way to sanity check my ideas, if possible. Code still goes through code review but I'd like to have some place to make sure I'm not reinventing the wheel or doing something stupidly elaborate before creating my pull request. I found some oracle thing (that hadn't been updated in 20 years) but I can't manage to find it now so welp :|
|
# ? Jan 5, 2021 22:03 |
|
I think "Clean Code" is the most-recommended book, at least in my experience. Although I haven't gotten around to buying it, myself...
|
# ? Jan 6, 2021 00:30 |
|
I think that Effective Java by Joshua Bloch is probably one of the best books in terms of understanding specific Java issues. The same author also wrote Java Puzzlers which cover subtle gotchas. Otherwise for general "Is this good Java?" advice, idiomatic Java doesn't really value terseness and if that thousand line for statement is mostly Spring bullshit, hopefully some day it'll just blend into the background.
|
# ? Jan 6, 2021 01:14 |
|
Others have already covered my recommendations (Clean Code and Effective Java). Once you have those under your belt one other thing I highly recommend is get a free copy of SonarQube, turn all the rules on, and scan every piece of code you write. There are plugins for every major IDE and standalone apps if you can't use an IDE for some reason. ~70% of the results will be opinionated bullshit but you will catch poo poo you never thought of and looking at the opinions and being forced to think about why you don't agree with that and are turning off the rule is very valuable imo. It won't teach you concepts to get good clean code the way the books will and it won't catch more elaborate logical errors but it absolutely will help you practice and can be useful to find spots where you have a knowledge gap. Its some of the best code analysis tools around packaged into a super convenient frontend and you should absolutely take advantage of it.
|
# ? Jan 6, 2021 02:48 |
|
As someone transitioning into the Java world right now, these things were super insightful and helpful for me:
|
# ? Jan 6, 2021 03:48 |
|
1337JiveTurkey posted:Sorry if this is a bit late. No not at all, it really helped a lot! Hippie Hedgehog posted:https://xkcd.com/2407/ is topical. EDIT: Solved the problem with my BFS where I couldn't only show the shortest path, solved it by backtracking the visitation list from the end to start. Thanks for the help everyone! Doc Neutral fucked around with this message at 17:07 on Jan 7, 2021 |
# ? Jan 6, 2021 16:42 |
|
I'm trying to relearn Java after not touching it for years, and that's taking the form of jumping into the deep end of the pool and just writing code. My current project is writing a program to simulate a deck of cards, but I'm feeling dumb because I can't seem to figure out how to use the enum I declared for my Card class. In my Card class I declared my enum as code:
code:
|
# ? Feb 10, 2021 18:17 |
|
F_Shit_Fitzgerald posted:I'm trying to relearn Java after not touching it for years, and that's taking the form of jumping into the deep end of the pool and just writing code. My current project is writing a program to simulate a deck of cards, but I'm feeling dumb because I can't seem to figure out how to use the enum I declared for my Card class. Are you importing Card.Suit.HEART or just Card? If your Suit enum is a member of the Card class, you'll either need to reference it as Card.Suit.HEART, or import the member you want to reference directly.
|
# ? Feb 10, 2021 18:33 |
|
Twerk from Home posted:Are you importing Card.Suit.HEART or just Card? If your Suit enum is a member of the Card class, you'll either need to reference it as Card.Suit.HEART, or import the member you want to reference directly. Ohhh...just Card. I forgot that in Java you had to do Card.Suit.HEART. e: Yep; that fixed it. Thank you! This is exactly why I'm trying to knock the dust off my my Java skills.
|
# ? Feb 10, 2021 18:36 |
|
Also you can use import static Card.Suit.* to just import the card suits directly like you want.
|
# ? Feb 10, 2021 19:41 |
|
And use IntelliJ so it'll give you suggestions on what to import.
|
# ? Feb 10, 2021 21:06 |
|
Sagacity posted:And use IntelliJ so it'll give you suggestions on what to import. Is it better than Eclipse? I've been using that because it was the Java IDE my university recommended. I've heard good things about IntelliJ, though. 1337JiveTurkey posted:Also you can use import static Card.Suit.* to just import the card suits directly like you want. Anything that saves typing out Card.Suit.HEART is great for me. Thanks!
|
# ? Feb 10, 2021 21:34 |
|
F_Shit_Fitzgerald posted:Is it better than Eclipse? I've been using that because it was the Java IDE my university recommended. I've heard good things about IntelliJ, though. You can't control a Mars rover with it like Eclipse has been used for but professional developers tend to like IDEA more for its smart autocomplete, whole-project indexing and other features.
|
# ? Feb 10, 2021 21:39 |
|
F_Shit_Fitzgerald posted:Is it better than Eclipse? I've been using that because it was the Java IDE my university recommended. I've heard good things about IntelliJ, though. Yes. Infinitely yes. (Protip: Don't assume Java advice from your time at uni is still valid. The language and tool chain have both improved immensely from even 5 years ago. Me, I learned in Emacs but once I was working, we use first Netbeans, then Eclipse, then finally IntelliJ came along and I didn't have to wait for the freaking IDE to catch up with my typing any longer.)
|
# ? Feb 10, 2021 21:40 |
|
Hippie Hedgehog posted:Yes. Infinitely yes. Look at this guys granytyping, not double VPN jumping like he should. seriously pray you don't have to do any coding in a java ide over remote desktop, it's the worst
|
# ? Feb 12, 2021 03:21 |
|
I do most of my coding over remote desktop and it's fine, lol (but the machine I'm remoting into is literally in the same city so latency is minimal)
|
# ? Feb 12, 2021 03:29 |
|
Java questions thread: Stream.of("poop")
|
# ? Feb 13, 2021 05:26 |
|
Jabor posted:I do most of my coding over remote desktop and it's fine, lol My flatmate has his remote desktop on Azure and it works surprisingly well. The low refresh rate obviously sucks, but there is very little perceivable latency when typing.
|
# ? Feb 13, 2021 10:38 |
|
I ran into some real library code that had an interface that looked like this:quote:<T> T entryAt(int index); So you could do: int i = entryAt(0); Or String s = entryAt(0); No cast required, no compile time type checking, it just failed at runtime with a class cast exception if you picked wrong. Is this as terrible as I think it is?
|
# ? Mar 4, 2021 13:52 |
|
smackfu posted:I ran into some real library code that had an interface that looked like this: So what is this, some kind of collection that can contain elements of different types and it's up to the caller to know what is the type at a particular index (since the generic type T is only on the method and not on the whole class as far as your example shows)? If so, then it's not great, no.
|
# ? Mar 4, 2021 15:29 |
|
If it would return Object at least you could ask it what kind of object it is before casting. Though I see it's returning an int so I'm totally confused what it holds in there. This looks wrong anyway you slice it.
|
# ? Mar 4, 2021 15:37 |
|
Volguus posted:If it would return Object at least you could ask it what kind of object it is before casting. Though I see it's returning an int so I'm totally confused what it holds in there. This looks wrong anyway you slice it. The int example either shouldn't work or the compiler is unboxing Integer to int automatically. This interface makes next to no sense without context as the standard case <T> is on the class and this example have a compile error unless the object type matched the type of the variable your assigning it to. In general without a very specific use case this is terrible design as it lets you cast to whatever you think is correct without compile time checks and will generate runtime errors unless your code is perfect.
|
# ? Mar 4, 2021 16:14 |
|
It looks a lot like Generics By Someone Who Doesn't Understand The Point Of GenericsTM
|
# ? Mar 4, 2021 16:30 |
|
smackfu posted:I ran into some real library code that had an interface that looked like this: I use this for a bit of testing code, where some encoded data is received and is normally sent to a backend. In tests I can know what type it should be when decoded so it works great. I'd never* use it in production code.
|
# ? Mar 4, 2021 21:54 |
|
Here’s the link: https://github.com/eclipse-vertx/ve...eImpl.java#L146
|
# ? Mar 4, 2021 23:55 |
|
1337JiveTurkey posted:You can't control a Mars rover with it like Eclipse has been used for but professional developers tend to like IDEA more for its smart autocomplete, whole-project indexing and other features. This is a good answer, eclipse is more flexible especially for things like embedded work or having multiple languages and codebases open at once, but jetbrains is much snappier and has overall better coding experience. Also Eclipse tends to be a giant memory hog on large projects and can occasionally crash. e: I use both and each have some things they're better at, but I concede that jetbrains IDEs are definitely more polished and stable. Visual Studio (the full one) is somewhere between the two. Private Speech fucked around with this message at 00:34 on Mar 5, 2021 |
# ? Mar 5, 2021 00:31 |
|
smackfu posted:Here’s the link: I like that future() function that it calls, which does a bounds check, then performs an array operation that does the same bounds check, except correctly (it should be >=, not >).
|
# ? Mar 5, 2021 00:44 |
|
smackfu posted:I ran into some real library code that had an interface that looked like this: This trick can be useful in very limited cases as a last resort way of reducing excessive casting boilerplate, but I would never write a method that used it that did not have “unsafe” in its name. Definitely a code smell.
|
# ? Mar 12, 2021 11:28 |
I'm having a weird problem with a simple survey/polling function in my Spring app. This is the fourth time that the app's been used by real people, and the problem's never occurred before (including in tests) with no changes to the code since it's started. Somehow, duplicate records are sometimes being saved to the database. I thought I was already checking for this; each Poll object has a 'usersVotedIn' array that keeps track of who's answered it, and there's an if statement that will cause the function to return if the requester is already in it. I've also checked the request logs for my server and it's not being caused by a person sending multiple requests with the same parameters.code:
There are a few things going on here that are making my head spin: 1) Both the poll's 'usersVotedIn' and the choice's 'keys' array are having duplicate rows added. 2) The same UUID is being added to a choice's 'keys' array multiple times. This makes me think that the problem is not the entire function being called multiple times, or else it would be generating a new UUID. 3) The problem does not happen for every poll, but when it does happen it affects almost everyone who voted. I have yet to see the problem occur on just a few people for any particular poll. 4) The problem happens an inconsistent amount of times per person. One user might have their vote duplicated once, while another might have 4 duplicate records. 5) I tried to add in a few extra print statements to track what exactly was going on. In order to do this I killed the system service that normally runs the process and started it manually. The entire time I was watching the server's output, the problem NEVER occurred despite it happening in 3 of the 4 polls that took place before I started observing. e: The classes for Poll/PollChoice: code:
code:
i vomit kittens fucked around with this message at 02:15 on Mar 23, 2021 |
|
# ? Mar 23, 2021 01:47 |
|
To me this sounds like a relationship problem with PollChoice and/or Poll. Can the voteKey be made unique in the database wherever it's being saved? And/or the foreign key to it? At least that way duplicate records cannot be entered and you can add a try/catch and log the stacktrace to see maybe who's the culprit. Without knowing more about the data model in here it's quite hard to make heads or tails about it.
|
# ? Mar 23, 2021 02:10 |
drat, I knew I was missing something. I just added in the class definitions for Poll and PollChoice. Putting a unique constraint on the keys was my first thought, and probably something I should have done in the first place. I guess I'd have to be pretty careful about how I handle the error it would generate though because I don't want: a) Duplicate records to still be added to the usersVotedIn array anyways (while this shouldn't actually affect anything in practice it will still annoy the gently caress out of me to know that it's happening). b) If it tries to add a duplicate and the request "fails" (despite their vote actually being recorded) the user will think that their vote did not count and the front-end would also not allow them to attempt to vote again.
|
|
# ? Mar 23, 2021 02:22 |
|
Oh wait, that method receives the objects from the browser itself (well, after it goes through the HttpRequest and friends), right? Those objects are not, yet, managed by the EntityManager. When you call save() on the object it may think that's a new thing and makes a new object with all the relations and all. That's just my assumption though. I would just retrieve managed objects within my transaction from the repository. That is, unless they're already managed and everything is within a transaction and everything is cool and all, at which point I don't know.
|
# ? Mar 23, 2021 02:31 |
The browser just sends the IDs of those objects, I have another function that gets them from the database and passes them into this one.
|
|
# ? Mar 23, 2021 02:57 |
|
i vomit kittens posted:Putting a unique constraint on the keys was my first thought, and probably something I should have done in the first place. I guess I'd have to be pretty careful about how I handle the error it would generate though because I don't want: Unfortunately, trapping constraint exceptions in Spring is a massive pain in the rear end - if you're using @Transactional, you can't catch them inside your service class, they have to be caught and turned into the appropriate HTTP response at the controller level.
|
# ? Mar 23, 2021 04:42 |
|
So my post-secondary group and I are about to graduate the program in about a week but we're having massive difficulty figuring out what should be an incredibly easy problem to solve for our final project. We have a JSP-based website and we've wasted days trying to connect it properly, via servlet, to our individually locally-hosted Oracle databases. Ultimately, where we're at is trying to figure out why we're getting a blank screen when pointing to our servlet and we are basically stuck so I gotta reach out to Java COBOL goons to ask: anybody mind giving me personal help resolving this problem? Our school expected us to be able to implement this with essentially zero instruction and it's coming down to the wire. I cant find a solution on Google and Im just lost at this point with what the minor thing is that we're missing that's causing this.
|
# ? Apr 8, 2021 00:17 |
|
|
# ? May 9, 2024 09:29 |
|
Argyle Gargoyle posted:We have a JSP-based website and we've wasted days trying to connect it properly, via servlet, to our individually locally-hosted Oracle databases. Are you using raw JDBC or an ORM? Are there any logs coming out of the driver?
|
# ? Apr 8, 2021 02:28 |