Programming thread

Yes, unless for some reason to you hate yourself and want to reinvent the wheel. This is a good opportunity to see if you can come up with a fast solution to the problem though. Always worth seeing if you can improve you initial solution.


You're learning don't feel too bad. If you're making progress and gaining new knowledge you're on the right track. It would be worth reading about software development patterns when you get a chance, because there's more than a few.

Understanding that we're working with some very basic code here, this might be of interest to you, you can loop over your users map a lot easier using `entrySet`. Don't treat this as good code, it's just something that immediately stood out to me.

Code:
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.lang.Integer;

class User {
    private String name;
 
    public User(String name){
        this.name = name;
    }
 
    public String getName(){
        return this.name;
    }
}

class UserDemo {
    private static Map<Integer, User> users = new HashMap<>();
 
    public static void main(String[] args) {
        users.put(1, new User("Mike"));
        users.put(2, new User("Bob"));
        users.put(3, new User("Billy"));
    
        boolean found = false;
    
        for (Entry<Integer, User> entry : users.entrySet()) {
            if (entry.getValue().getName().equals("Bob")) {
                found = true;
                break;
            }
        }
    
        System.out.println(found);
    }
}
>no zero-index array

Grab your torches, programmchads
 
  • Like
Reactions: Geranium and Catler
>no zero-index array

Grab your torches, programmchads
An autoinc integer key in a DB typically starts at 1, hence why I did in the example. Not a good excuse I know :)

Lets be real, you shouldn't be manually assigning incremental user IDs to begin with.
 
Last edited:
  • Like
Reactions: Geranium
Understanding that we're working with some very basic code here, this might be of interest to you, you can loop over your users map a lot easier using `entrySet`. Don't treat this as good code, it's just something that immediately stood out to me.

Code:
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.lang.Integer;

class User {
    private String name;
 
    public User(String name){
        this.name = name;
    }
 
    public String getName(){
        return this.name;
    }
}

class UserDemo {
    private static Map<Integer, User> users = new HashMap<>();
 
    public static void main(String[] args) {
        users.put(1, new User("Mike"));
        users.put(2, new User("Bob"));
        users.put(3, new User("Billy"));
  
        boolean found = false;
  
        for (Entry<Integer, User> entry : users.entrySet()) {
            if (entry.getValue().getName().equals("Bob")) {
                found = true;
                break;
            }
        }
  
        System.out.println(found);
    }
}
You're almost certainly right. Now that I understand how to make the hashmap's methods available to the service layer, it's just a matter of adding stuff like entrySet() and getValue(). Everything is just temporary and kludgy since there's clearly a lot of very basic concepts I don't understand very well yet.

>no zero-index array

Grab your torches, programmchads
To be fair.. I'm also using 1. Because then 0 can become a 'not found' flag. So something like this in the service layer:
Java:
public int findIdByName(String name) {
        int i = 0;
        for (Integer key : repository.keySet()) {
            i = Integer.parseInt(key.toString());
            if (getUserName(i).equals(name)) {
                return i;
            }
        }
        return i;
    }

and something like this in main:
Java:
int i = findIdByName("Bob");
if (i == 0) {
    System.out.println("User not found.");
} else {
    System.out.println(userService.retrieveUser(i).toString());
}

It's all broken and stupid now, partly because this really is the sort of thing that should be running a database. If I ever get that far, this will all have to be rewritten, but that was always the plan.

edit: Oh, I'm still a retard. My method can return any number when an ID isn't found, so it can be -1 and work with either index 1 or index 0. A liberal arts student would call this a journey of self discovery, but it's more like "It's not imposter syndrome when you really DON'T know what you're doing."
 
Last edited:
  • Feels
Reactions: Geranium
Java:
public class MyRepository {
    private Map<Integer, User> myRepo = new HashMap<>();
    //...
    protected boolean doesUserExist(String name) {
        for(User user: myRepo.values()) {
            if(user.getName().equals(name))
                return true;
        }
        return false;
    }
}
or in Java 8+:
Java:
public class MyRepository {
    private Map<Integer, User> myRepo = new HashMap<>();
    // ...
    public boolean doesUserExist(String name) {
        return myRepo.values().stream()
                              .filter(e -> e.getName().equals(name))
                              .count() > 0;
    }
}
 
Last edited:
  • Like
Reactions: namelesstacotruck
Java:
public class MyRepository {
    private Map<Integer, User> myRepo = new HashMap<>();
    //...
    protected boolean doesUserExist(String name) {
        for(User user: myRepo.values()) {
            if(user.getName().equals(name))
                return true;
        }
        return false;
    }
}
or in Java 8+:
Java:
public class MyRepository {
    private Map<Integer, User> myRepo = new HashMap<>();
    // ...
    public boolean doesUserExist(String name) {
        return myRepo.values().stream()
                              .filter(e -> e.getName().equals(name))
                              .count() > 0;
    }
}
Why loop through a hash instead of using containsKey()/containsValue()?
 
Why loop through a hash instead of using containsKey()/containsValue()?
Because the value is a User object, not just a name, so you can't guarantee that containsValue() will work with new User("blahname") unless you explicitly know they've overriden the hashCode and equals methods to work just on usernames.

tbh I'd probably do something like this now that I've had time to chew on it a bit:
Java:
public class MyRepository {
    private Map<Integer, User> myRepo = new HashMap<>();
    // ...
    public Optional<User> findUserByName(String name) {
        return myRepo.values().stream()
                              .filter(e -> e.getName().equals(name))
                              .findFirst();
    }
    public boolean doesUserExist(String name) {
        return findUserByName(name).ifPresent();
    }
}
 
Last edited:
  • Like
Reactions: namelesstacotruck
Hey guys im new to Coding and was wondering if you guys know any good youtube tutorials for C#?
Microsoft has extensive learning C# resources, including several video courses. I came to C# from Java so I only dipped into them, but there’s a lot there.

Specifically on YouTube, I do see that Derek Banas has a course for beginners:

(Banas I’m so-so on, he mostly does these one hour overview videos that a prof I respect was keen on, but personally I don’t feel are any better than Learn X in Y minutes)

Edit, forgot to include this:
I am the biggest fucking retard the world has ever known because every 12-14 year old in their computer class figures this out right away.
Soon you’ll enter the endless cycle of frustration-to-elation of programming, but do try to give yourself a break, and keep chipping away at it. You’ll get there.
 
Last edited:
Microsoft has extensive learning C# resources, including several video courses. I came to C# from Java so I only dipped into them, but there’s a lot there.

Specifically on YouTube, I do see that Derek Banas has a course for beginners:

(Banas I’m so-so on, he mostly does these one hour overview videos that a prof I respect was keen on, but personally I don’t feel are any better than Learn X in Y minutes)

Edit, forgot to include this:

Soon you’ll enter the endless cycle of frustration-to-elation of programming, but do try to give yourself a break, and keep chipping away at it. You’ll get there.
Thanks for the resources man I appreciate it!
 
  • Like
Reactions: Geranium
@Fcret Thank you for all the suggestions! Even when they don't work for my use case, it's been great to troubleshoot and try to understand why I can't make them work. Streaming the data will probably be the best option if I ever work with a database of any real size since streams can (I think) be chopped up for threading and parallel streaming. Not so much for for : each iteration.

@Geranium If troons can do this, surely a dried up old terfy hag can too? I should be moving on to Spring Boot, but I want a better grip on the basics first.
 
*sigh* Why does the compiler have to wait until the linking stage to fuck up?

A project I'm building (not mine) uses WolfSSL as a dependency. I have the library and dev files installed. When it tries to link the executable I get
Code:
rpcn_client.cpp:(.text+0x3db8): undefined reference to `wolfSSL_write_dup'
Using grep on $SOURCEDIR/rpcs3/Emu/NP/rpcn_client.cpp shows the relevant line
Code:
if ((write_wssl = wolfSSL_write_dup(read_wssl)) == NULL)
Grepping "wolfSSL_write_dup" on /usr/include/wolfssl/ssl.h gives
Code:
WOLFSSL_API WOLFSSL* wolfSSL_write_dup(WOLFSSL* ssl);

So I have the header installed and the header contains the relevant function, so I don't see what I'm doing wrong here? Does anyone have experience with ld? Looking on the GitHub issue page doens't bring up anything useful.
 
  • Like
Reactions: Agent Sandoval
*sigh* Why does the compiler have to wait until the linking stage to fuck up?

A project I'm building (not mine) uses WolfSSL as a dependency. I have the library and dev files installed. When it tries to link the executable I get
Code:
rpcn_client.cpp:(.text+0x3db8): undefined reference to `wolfSSL_write_dup'
Using grep on $SOURCEDIR/rpcs3/Emu/NP/rpcn_client.cpp shows the relevant line
Code:
if ((write_wssl = wolfSSL_write_dup(read_wssl)) == NULL)
Grepping "wolfSSL_write_dup" on /usr/include/wolfssl/ssl.h gives
Code:
WOLFSSL_API WOLFSSL* wolfSSL_write_dup(WOLFSSL* ssl);

So I have the header installed and the header contains the relevant function, so I don't see what I'm doing wrong here? Does anyone have experience with ld? Looking on the GitHub issue page doens't bring up anything useful.
Are you sure you're passing -lwolfssl to the compiler/linker when trying to create the executable so that it actually knows to look for the library? Don't forget the library path either if it's nonstandard.
 
  • Like
Reactions: Agent Sandoval
*sigh* Why does the compiler have to wait until the linking stage to fuck up?

A project I'm building (not mine) uses WolfSSL as a dependency. I have the library and dev files installed. When it tries to link the executable I get
Code:
rpcn_client.cpp:(.text+0x3db8): undefined reference to `wolfSSL_write_dup'
Using grep on $SOURCEDIR/rpcs3/Emu/NP/rpcn_client.cpp shows the relevant line
Code:
if ((write_wssl = wolfSSL_write_dup(read_wssl)) == NULL)
Grepping "wolfSSL_write_dup" on /usr/include/wolfssl/ssl.h gives
Code:
WOLFSSL_API WOLFSSL* wolfSSL_write_dup(WOLFSSL* ssl);

So I have the header installed and the header contains the relevant function, so I don't see what I'm doing wrong here? Does anyone have experience with ld? Looking on the GitHub issue page doens't bring up anything useful.
A couple of things to bring up.
The header's definition could be in an `#ifdef` block, so sadly grep won't always tell you the full story. That would cause the compiler to complain about the undefined reference earlier than linking though and it would have reported the error at the line of source rather than a .text+ reference. In this case it won't be the culprit.
The error isn't going to be in source, since this is the linking step the problem must be in the library you have installed. I can't look very deeply right now, but if I could my questions would be:
1. Does WolfSSL have a devel package?
2. When I look in my library path are there multiple versions of WolfSSL's `.so`?
3. If there are multiple `libwolfssl.so`s which one is the main one and what symbols does it include? You can check which symbols a shared library provides using `strings`.
 
Last edited:
  • Informative
Reactions: Friendly Primarina
A couple of things to bring up.
In this case it isn't relevant, the header's definition could be in an `#ifdef` block, so sadly grep won't always tell you the full story. That would cause the compiler to complain about the undefined reference earlier than linking though and it would have reported the error at the line of source rather than a .text+ reference.
The error isn't going to be in source, since this is the linking step the problem must be in the library you have installed. I can't look very deeply right now, but if I could my questions would be:
1. Does WolfSSL have a devel package?
2. When I look in my library path are there multiple versions of WolfSSL's `.so`?
3. If there are multiple `libwolfssl.so`s which one is the main one and what symbols does it include? You can check which symbols a shared library provides using `strings`.
So it turns out that, despite being defined in wolfssl's include files, wolfSSL_write_dup is not contained as a symbol in libwolfssl.so.35.3.0 . That would explain it, thanks. I guess this is what I get for using Debian Testing :story:
 
So it turns out that, despite being defined in wolfssl's include files, wolfSSL_write_dup is not contained as a symbol in libwolfssl.so.35.3.0 . That would explain it, thanks. I guess this is what I get for using Debian Testing :story:
In this case the function might be deprecated. I just poked at the WolfSSL manual and it doesn't mention wolfSSL_write_dup anywhere in over 1500 pages.
 
In this case the function might be deprecated. I just poked at the WolfSSL manual and it doesn't mention wolfSSL_write_dup anywhere in over 1500 pages.
Now you come to mention it, when looking around I did see the function in a blog entry from 5 years ago. I thought How old is the library in the repository that it doesn't have this? Turns out the lib might be too new rather than too old lol.
 
@Fcret Thank you for all the suggestions! Even when they don't work for my use case, it's been great to troubleshoot and try to understand why I can't make them work. Streaming the data will probably be the best option if I ever work with a database of any real size since streams can (I think) be chopped up for threading and parallel streaming. Not so much for for : each iteration.

@Geranium If troons can do this, surely a dried up old terfy hag can too? I should be moving on to Spring Boot, but I want a better grip on the basics first.
You should also look into the Optional<T> class for return types when defining your repository interface because it comes in handy as a safer way to handle cases where things don't exist (rather than returning null, forgetting to check for it up the call chain, and then crashing with an NPE).

EDIT: Also, yeah, on the subject of streams - replacing the stream() method with parallelStream() returns a potentially parallel stream. Sometimes streams can be parallelized and sometimes they can't but you generally always start with a sequential stream and then only try parallelStream if you see your code chugging during stream operations while profiling. Keep in mind though that parallel streams are not always more performant, because the extra overhead of synchronization and possible non-locality can potentially outweigh any performance gains you get from doing things in parallel. As always - only optimize after measuring.
 
Last edited:
You should also look into the Optional<T> class for return types when defining your repository interface because it comes in handy as a safer way to handle cases where things don't exist (rather than returning null, forgetting to check for it up the call chain, and then crashing with an NPE).

EDIT: Also, yeah, on the subject of streams - replacing the stream() method with parallelStream() returns a potentially parallel stream. Sometimes streams can be parallelized and sometimes they can't but you generally always start with a sequential stream and then only try parallelStream if you see your code chugging during stream operations while profiling. Keep in mind though that parallel streams are not always more performant, because the extra overhead of synchronization and possible non-locality can potentially outweigh any performance gains you get from doing things in parallel. As always - only optimize after measuring.
Once I get to where I'm ready to try importing test data from a CSV file, the hashmap should get big enough to where there would even be a difference to measure. Right now it's just 4 to 6 entries.

And I'll need to find a For Retards guide on generics and Optional<T>, because it's just better to not trust myself to not make stupid mistakes all the time. And unit testing (because not taking this opportunity to try to learn it is dumb).
 
  • Like
Reactions: std::string
Back