Jump to content
Crusader

Programming Question Megathread!

Recommended Posts

So I have no idea if there are enough nerds on this site but I'll start this thread up anyway. If you've got a programming specific question and need help, post it here! Hopefully, someone will help you.

 

Wrap your code in the CODE or CODEBOX tag for easy reading.

 

 

 

 

 

Anyway, for anyone who knows PHP5...

 

>		try
	{
		$rs = $this->db->CacheExecute($sql, $username);
		$this->user = $rs->FetchRow();
	}
	catch(Exception $e)
	{
		$this->user = array();
	}

	return $rs == true;

 

Will this evaluate the last line?

Link to comment
Share on other sites

Not a PHP expert, but if the try catch functions as it does in most languages with the construct then I can take a stab at it. Usually the catch part of the try-catch terminates the exceptions propagation. If you don't catch an exception it will bubble up, aborting everything in the call stack until it hits something with a handler. In any language I have encounter that supports the it, there is a highest level handler, which exits your code. So since the catch should stop the exception (and in theory puts the system back into an expected state) then the code will continue after the catch regardless of it there was an exception thrown in the try.

 

Anybody every worked on a mircocontroller writing an interrupt based UART driver?

Link to comment
Share on other sites

Does anyone here know Java? I'm having trouble with an assignment, I keep getting a Null Pointer Exception.

 

Here's a very simplified version, there are 3 classes.

 

>public class Three 
{	
public static void main(String[] args)
{
	Two arr = new Two("hi");
	
	System.out.println(arr.toString());
}
}


public class Two 
{
private One[] arr;
private final int NUM = 3;

public Two(String a)
{
	String nm = a;
	
	One[] arr = new One[NUM];
	
	arr[0] = new One(nm);
}

public String toString()
{
	return arr[0].toString();
}
}

public class One {
private String ya;

public One(String n)
{
	ya = n;
}

public String toString()
{
	String str = "String: " + ya;
	return str;
}
}

Link to comment
Share on other sites

You declared arr twice. If you set it up as a class variable you don't need to declare it again in your constructor, you can simply just refer to it as arr.

 

This:

 

>	   One[] arr = new One[NUM];

 

Should be this:

 

>		arr = new One[NUM];

 

Also that's not how a toString() should really work ;)

Link to comment
Share on other sites

That really depends on if the programs sat down and thought out the system, or if they just hacked it together in perl... In fact I imagine it is only one line of perl and the side effect of running it is that it writes the rest of the program...

Link to comment
Share on other sites

I've got about 5,000 integers that need to be checked if a user has seen before. These integers are grouped into different categories of varying size, so one category could contain 325 of the integers while another has 50 of them.

 

They're all unique integers and won't appear in any category more than once. Meaning that if it's in one category, it's only in that category.

 

Right now I have a table setup to contain the following:

 

user_id | category_1 | category_2 | category_3 | category_n... etc

 

In category_1 there's just a comma separated list of integers that the user has seen before.

 

eg. 1,2,3,5,6,7,111,9,10

 

This seems a bit clunky so I'm thinking about storing each individual integer seen in its own row. Kind of like this.

 

user_id | int_seen | category

 

The only problem I can think of is that there could be any number of user_ids... It'd probably be capped at 10,000 but that'd still be 50,000,000 entries in the table...

 

My solution to that would be clearing the table of a specific user_id's entries when they have seen all the integers and making another table that contains a list of users that have done this.

 

That and maybe having a group_id column in the original table so people in a group can share a result set instead.

 

What do you guys think? Any ideas or alternatives?

 

edit: hope that made sense

Link to comment
Share on other sites

Your structure choice is going to depend a lot on what you want to do with the data.

 

For example, if you only want to store the history, then a comma separated list is going to be just fine.

 

If you need to look through this data on a regular basis, then you are going to take increasing performance hits each time you have to process the data.

 

In the later case you will need to develop a better structure. It seems like you are using SQL so it isn't really actionable to build a substructure. If you aren't using sql, then a binary tree for each category will give you optimal information access. You could do that if you are the only reader and write of the table by jamming a binary object in there too.

 

You are right that creating your second structure might balloon out of control.

 

You might consider creating a table for each user containing the elements int_seen and category. That will give you only one table per user with at most the number of integers you have for entries. You would need a table of users (which can locate the individual user tables), but that is worst case the number of users you have in length.

Link to comment
Share on other sites

Sorry if you've already found a solution.

 

But from what I can tell you're trying to get a method for determining what a user has viewed correct?

 

The problem with a comma separated list, is it will become increasingly labour intensive to go through the list as the list continues to grow. The reality of the fact is, as a web-ap developer if you can do any sort of optimization inside of MySQL you should (sure there are some exceptions but we'll ignore them). LOL not to comment on your coding skills, but the DATABASES have a lot more optimization of code than you'll ever be able to put into a reasonable web-ap. Plus they've spent the time getting those optimizations inside the DB why not use them?

 

Ideally you'd have 3 tables.

Table: User

user_id|...|...|...

 

Table: Category

category_id|...|...|...

 

Table user_viewed

user_id|category_id (setting the pkey to be both user_id and category_id)

 

Then to determine if someone has viewed the category you simply query the DB to see if the user/category id combination is there.

Link to comment
Share on other sites

I actually figured out a nice way to do this with joins and a "viewed" table. Only problem is the result sets can be a little big and it can be a strain on MySQL. I'm working on a cache but I've been delayed lately.

 

We need more posts in this thread.

Link to comment
Share on other sites

Does anyone here know Java? I'm having trouble with an assignment, I keep getting a Null Pointer Exception.

 

Here's a very simplified version, there are 3 classes.

 

>public class Three
{	
public static void main(String[] args)
{
	Two arr = new Two("hi");
	
	System.out.println(arr.toString());
}
}


public class Two
{
private One[] arr;
private final int NUM = 3;

public Two(String a)
{
	String nm = a;
	
	One[] arr = new One[NUM];
	
	arr[0] = new One(nm);
}

public String toString()
{
	return arr[0].toString();
}
}

public class One {
private String ya;

public One(String n)
{
	ya = n;
}

public String toString()
{
	String str = "String: " + ya;
	return str;
}
}

 

Oh man, ignoring the strange code unicode character stuff going on in those code blocks, I have no idea what this 5 and a half year old garbled pile of shit was supposed be lol. And the error I was asking about is super obvious. Ah well I was in school still, I was allowed to to stupid things and make mistakes.

Link to comment
Share on other sites

I can't post any of my programming problems on account of the fact it's my job and I'm not allowed to talk about it.

 

Pshhh, I'm pretty sure 75% of StackOverflow posts fall into that category.

 

Heck, if my team had to cite our sources, I think most of our codebase is copy paste!  Haha

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.