jsanc623
  • Member for 9 years, 6 months
  • Last seen more than a month ago
in_array() in Go
Accepted answer
18 votes

Rather than tie yourself to only one type (string), you could use the reflect package as well as interfaces to make it somewhat type indifferent. The following is my reworking of your code: package ...

View answer
Python caching generator
Accepted answer
8 votes

Memoizing a generator is a good idea to save having to rerun it. There are a few ways to do it - one is to handle it the way you're handling it, by returning the cached result if it exists, otherwise ...

View answer
Forum Sign up and Sign in software
7 votes

There's a few things wrong with this. Primarily that there is no separation of concerns - you have PHP mixed with HTML, etc. However, barring that, there are some low hanging fruit that we can nip: ...

View answer
Checking if an array is sorted
Accepted answer
7 votes

The better way would be your first, as you're just comparing the element A with element B and not creating a copy of the element as you are in the second version. Additionally, to be a bit of a ...

View answer
Converting string to dict using Python ast library
Accepted answer
5 votes

To answer your question, yes ast is ok to be used for this. Per the documentation for literal_eval: Safely evaluate an expression node or a Unicode or Latin-1 encoded string containing a Python ...

View answer
Redirecting to another page
Accepted answer
5 votes

Well, for starters, your redirect will never work if you pass in both $key and $page, as you're asking for the following: 1. is not isset($page) 2. is not empty($page) Which means that the redirect ...

View answer
Geocode an address
Accepted answer
5 votes

There are a number of concerns - from code styling to error handling (echoing out an error is not good. Use Exceptions or a custom Error handler). <? /** * Access API from http://api.mapserv....

View answer
Extremely basic authentication class
Accepted answer
5 votes

I took the liberty of going through your code - see the comments preceded with MOD (modification), STAT (statement), QUES (question), SOL (solution) and REMOVED (removed) # MOD: Moved all include() ...

View answer
User register script PHP
5 votes

Ok, there are a couple of things wrong with your code and your method of organizing the code. I've pointed them out to you in the code below within comments. The comments preceded with # are my ...

View answer
Setting .conf files for Nginx
Accepted answer
4 votes

The config looks great. However, a few points: If your server responds to both the www and non-www version of the site, just leave a wildcard. So server name should read: server_name *.my-site....

View answer
Guess My Number, Classic Edition
4 votes

You can kill off the if/else print in the while loop by using a one liner: print "Lower..." if guess > the_number else "Higher" Can also get rid of the last raw_input, as it is just annoying: ...

View answer
PHP LDAP connection
Accepted answer
4 votes

I've commented through your existing code below and noted what was removed/replaced/added (my comments are denoted by double #'s): ## Let's modify the class name 'adUser' to something more ## ...

View answer
Electricity Calculator
4 votes

You should think more about how you arrange and style your code. Additionally, you should be aware of DRY methodology and keep from repeating code wherever possible. I made a couple of modifications ...

View answer
Scanning a directory for plugins and loading them
Accepted answer
4 votes

I started a project with a structure similar to the one you detailed: My first order of business was to move the import plugins out of the discover_dictionaries() function and to the top of the file. ...

View answer
Select input that is generated and populated with options
Accepted answer
4 votes

I made a few changes - namely normalizing the styling (PascalCase classname, lower_case properties, camelCase methods) and spacing. I then made the buildOptions() function shorter and renamed it to ...

View answer
Removing everything after a certain character
4 votes

Doing a quick test, as follows, shows that using explode() is about 4 times as fast as calling strstr() twice. $url = "http://google.com/test.php?somevar=laksd"; $url2 = "http://google.com/test.php?...

View answer
Retrieving all directorie names (relative path)
4 votes

Have you considered using SPLs RecursiveIteratorIterator and RecursiveDirectoryIterator? Like this: $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($this->directory), ...

View answer
Different way of writing multiple click functions
4 votes

I think you're looking for something along the lines of: $('.tab, .edit, .cancel').click(function(){ switch(this.className){ case 'tab' : { $('.tab').removeClass('activeTab'); ...

View answer
Cleaner method of doing IF
Accepted answer
3 votes

I seem to get the feeling you are either not comfortable using maps as others have suggested here...so why not throw in a switch instead? $volume = $array[1]; if($volume > 10000 && $...

View answer
Form validation evaluation to avoid exploits
3 votes

I think it looks ok - though someone might want to add on if they notice any issues I may have missed. However, a bit of cleanup goes a long way: # Set a 500 (internal server error) response code. ...

View answer
Singleton Database wrapper
3 votes

It's fine to have a wrapper to abstract PDO functionality, especially to avoid code duplication. The class itself looks ok to me as well - aside from some minor issues, as delineated in the comments ...

View answer
Throwing an exception within a try/catch block in PHP
Accepted answer
3 votes

The way you did it is pretty much the standard way to bubble exceptions up the stack. Basically something like: function a(){ throw new Exception("Exceptional!"); } function b(){ try{ ...

View answer
Cleaner or better readability in URL composition
Accepted answer
3 votes

If you're going after readability and speed, then doing a preg_replace is actually a lot slower than doing a str_replace. I'm using this test code: $original_url = 'http://www.example.com/api/v1/...

View answer
How to re-factor dynamic menu code to OOP in a MVC framework?
Accepted answer
3 votes

Ok, firstly, you never want HTML in your controller. Always keep your output in your view, your application flow in the controller and your business logic in the model. Otherwise, this breaks ...

View answer
To send the email on some given date and time
3 votes

Use PDO, not mysql_* as they are deprecated and dangerous. Break up your code into functions, this makes it easier to call and test. Throw and catch exceptions. You can omit the ending ?> NOTE: This ...

View answer
Is my routing good or bad? in PHP
Accepted answer
3 votes

Separate your logic and site specific functionality like this: function Router{ $url = array_values(array_filter(explode('/', strtolower($_SERVER["REQUEST_URI"])))); switch($url[0]){ ...

View answer
PHP - avoid many on-demand imports
Accepted answer
3 votes

As N.B. mentioned in their comment - why are you implementing a module method if we already have lazy loading via autoloading? I've even written a class that you can simply attach and not have to ...

View answer
Calculating Chinese zodiac signs based on birthdays
3 votes

To start, lets look for something that we can loop through. From your link to relevant dates, we can see that the associated animals loop through 12 animals, in order: Rat Ox Tiger Rabbit Dragon ...

View answer
Convert Improper Fraction to Mixed Number with JavaScript
Accepted answer
3 votes

What you're doing with i = (n / d) >> 0 is a sign-propagating right shift. The interesting this is, you're doing a right shift by 0 (I'd imagine because you'd otherwise get NaN. Your method of ...

View answer
Improve Speed of RPI Calculation
3 votes

Not quite a lot that can be done performance-wise...you have a lot of data to pour over (16,000 * 81,000 = 1,296,000,000 records and with the new data 17,000 * 160,000 = 2,720,000,000 records). You ...

View answer