body stringlengths 25 86.7k | comments list | answers list | meta_data dict | question_id stringlengths 1 6 |
|---|---|---|---|---|
<p>I have written this program after a lot of effort, and I was wondering about the efficiency of that solution and if it's neat.</p>
<p>How plain is this program and is there any better way to rewrite it?</p>
<pre><code>import string
# Strength of operations:
# -> [] (brackets)
# 6 -> ~ (negative)
# 5 ->... | [] | [
{
"body": "<p>Every <code>levelX</code> functions looks roughly the same:</p>\n\n<pre><code>idx = 0\nwhile idx < len(exp):\n if exp[idx] in (...):\n //...\n\n value = str(value)\n\n exp = exp.replace(''.join(sub_exp), value)\n idx = 0 #The len has been changed, scan again.\n\n ... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-04T14:49:39.327",
"Id": "19291",
"Score": "3",
"Tags": [
"python",
"parsing"
],
"Title": "Calculator for parsing user input"
} | 19291 |
<p>I created a function to calculate the font size to fit the width of the website I'm doing. I believe the code is a bit ugly and could use some improvement by creating a more logical function, faster and less heavy.</p>
<p>Can someone look at the code and comment on it for my own improvement?</p>
<pre><code>//Scrip... | [] | [
{
"body": "<h1>Use strict-mode</h1>\n\n<p>Start your code with <code>\"use strict\";</code>:<br>\n<a href=\"https://stackoverflow.com/questions/1335851/what-does-use-strict-do-in-javascript-and-what-is-the-reasoning-behind-it\">https://stackoverflow.com/questions/1335851/what-does-use-strict-do-in-javascript-an... | {
"AcceptedAnswerId": "19319",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-04T15:49:38.503",
"Id": "19292",
"Score": "4",
"Tags": [
"javascript",
"jquery"
],
"Title": "Calculating the font size to fit the width of a website"
} | 19292 |
<p>Below is my current code for sorting an array, "$jobs_list".</p>
<p>It's not very DRY and it's fugly. And there are several more similar case statements still to go in. How could I write it so my brain doesn't twitch to look at it?</p>
<pre><code>switch ($sort) {
case 'number':
usort($jobs_list, functi... | [] | [
{
"body": "<p>I'd start by moving the <code>switch</code> statement into the callback function:</p>\n\n<pre><code>usort($jobs_list, function($a, $b){\n switch ($sort):\n case 'number':return ( (int) $a->number > (int) $b->number ); break;\n case 'client':return strcmp($a->client_contact_la... | {
"AcceptedAnswerId": "19296",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-04T15:00:23.627",
"Id": "19294",
"Score": "1",
"Tags": [
"php",
"sorting"
],
"Title": "Sorting an array of jobs by various criteria"
} | 19294 |
<p>I've built a Connection class that uses a PDO connection, but it looks messy and im sure there are ways of improving it. Currently I only want 1 instance of the connection (singleton). </p>
<pre><code><?php
namespace AQEConnect {
use \PDO as PDO;
/** Includes */
require_once dirname(__DIR__) . '/con... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-05T23:12:50.883",
"Id": "30996",
"Score": "1",
"body": "I plan on writing a proper response at some point (probably Friday), but for the time being, you might should read http://stackoverflow.com/questions/4595964/who-needs-singletons/... | [
{
"body": "<p>This is a little bit of a reworked class based on your code example. Some things to keep in mind while writing your version:</p>\n\n<ul>\n<li><p>Is this testable? Currently it is not; the db config is assumed, the toString is assuming a db credential ( DBNAME ), etc.</p></li>\n<li><p>Your db conne... | {
"AcceptedAnswerId": null,
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-04T16:17:11.317",
"Id": "19297",
"Score": "1",
"Tags": [
"php"
],
"Title": "Connection class"
} | 19297 |
<p>I have been presented with a simple but slow performing LINQ to Entities query which I've pasted below.</p>
<p>Could anybody give me some pointers on how to improve performance? The query below has been significantly improved since I inherited it but I don't know what else I can do without moving to the DB (which ... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-04T18:06:21.673",
"Id": "30901",
"Score": "6",
"body": "I hope there is a *strong* reason why `db.Oportunities` has a `StatusReason_ID` and not a `StatusReason`, a `Lead_ID` and not a `Lead`, etc. Because all these joins? \"POOF GONE!\... | [
{
"body": "<p>This can't be called \"simple\". And it's not possible to tell where bottleneck is because of so many joins and subqueries.</p>\n\n<p>Subqueries are certainly suspect, you can optimize them by using Dictionary<> (as you would add indexes in real DB).</p>\n\n<p>I would also try to split this hug... | {
"AcceptedAnswerId": null,
"CommentCount": "4",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-04T16:43:43.730",
"Id": "19299",
"Score": "6",
"Tags": [
"c#",
"linq"
],
"Title": "LINQ to Entities query with lots of grouping"
} | 19299 |
<p>Mask.html</p>
<pre><code><html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
<script type="text/javascript" src="mask.js"></script>
<link rel="stylesheet" type="text/css" href="mask.css">
&l... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-04T19:04:35.400",
"Id": "30904",
"Score": "4",
"body": "Quick comment: PascalCase is only for constructors which are meant to be called with `new`. All other methods should use **camelCase**."
}
] | [
{
"body": "<p>The code looks fine to me. Just a couple minor points:</p>\n\n<ol>\n<li><p>Why do you change the <code>div</code>'s <code>position</code> to <code>relative</code>? If the function is called on a <code>div</code> with absolute positioning, it would mess up the layout. And you should revert the CSS ... | {
"AcceptedAnswerId": "19739",
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-04T18:18:14.190",
"Id": "19304",
"Score": "6",
"Tags": [
"javascript",
"jquery",
"html",
"css"
],
"Title": "DIV mask implemented in JavaScript/CSS"
} | 19304 |
<p>I've written a die roller which can be run from the command line. It currently exists as three files, but I'll say later why I'd like to make it four. The files are as follows:</p>
<ul>
<li><strong>die.py</strong>: Defines the <code>Side</code> and <code>Die</code> classes and extends <code>Die</code> with the <c... | [] | [
{
"body": "<pre><code>from random import choice\n\nclass Side(dict):\n</code></pre>\n\n<p>You should almost never inherit from the builtin python collections. This is certainly not a case where you should. A side is not a kind of dictionary, it shouldn't try and inherit from <code>dict</code></p>\n\n<pre><code>... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-04T18:26:46.473",
"Id": "19305",
"Score": "4",
"Tags": [
"python",
"parsing",
"random",
"dice"
],
"Title": "Die Roller for command line"
} | 19305 |
<pre><code>@implementation User
- (void)refreshProperties
{
assert(!self.isSyncing);
NetworkOperation *op = [[NetworkOperation alloc] initWithEndPoint:GetUserDetails parameters:nil];
[self setupCompletionForRefresh:op];
[self setNetworkOperation:op];
[self setSyncing:YES];
[[NetworkManager s... | [] | [
{
"body": "<p>In <a href=\"http://mac.github.com\">GitHub for Mac</a>, we use a combination of <a href=\"https://github.com/AFNetworking/AFNetworking\">AFNetworking</a> and our own <a href=\"https://github.com/github/Mantle\">Mantle</a> and <a href=\"https://github.com/github/ReactiveCocoa\">ReactiveCocoa</a> f... | {
"AcceptedAnswerId": "19373",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-04T19:16:19.607",
"Id": "19307",
"Score": "6",
"Tags": [
"objective-c",
"ios"
],
"Title": "Dependency Inversion / Injection? - Networking code in model classes"
} | 19307 |
<p>I've created a class that's used for cleaning variables for use. You set a name, value and options and it will set the value if it matches the criteria/options.</p>
<p>It is used like this:</p>
<pre><code>$var = new Variable();
$var->clean('username', $_POST['username'], '3,13,alphanumeric,True');
echo 'Usernam... | [] | [
{
"body": "<p>Yes, your class does too much.</p>\n\n<p>First of all, it actually lying: It does not handle one variable as the name of the class suggest, but it does handle multiple variables at once.</p>\n\n<p>Second, I do not like the overloading of the <code>clean</code> method. It is used both for getting a... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-04T19:48:23.957",
"Id": "19308",
"Score": "3",
"Tags": [
"php"
],
"Title": "Variable cleaning class"
} | 19308 |
<p>I tried to write a <code>nullptr_t</code> class based on <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2431.pdf">the official proposal</a> to be used in C++03 only. The only differences with the proposal are that we can compare two <code>nullptr_t</code> instances and that it is convertible to <c... | [] | [
{
"body": "<p>At the moment, your implementation allows</p>\n\n<pre><code>auto& p = nullptr;\n</code></pre>\n\n<p>This is forbidden in C++11 as <code>nullptr</code> is an rvalue. You also do not allow the following:</p>\n\n<pre><code>auto p = nullptr;\nauto pp = &p;\n</code></pre>\n\n<p>While C++11 doe... | {
"AcceptedAnswerId": "19337",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-04T23:18:08.020",
"Id": "19316",
"Score": "11",
"Tags": [
"c++",
"c++11",
"reinventing-the-wheel",
"c++03"
],
"Title": "Custom nullptr_t class"
} | 19316 |
<p>I have finished my router and would like your thoughts on anything that may be inefficient of could be done better!:</p>
<p><code>
class Router {</p>
<pre><code>public $start_page = 'Dashboard' ; // Change to your home/default page.
public $staticRoutes = array(
'alias' => 'ActualController',
) ;
function ... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-05T22:27:01.430",
"Id": "30991",
"Score": "0",
"body": "It has access to global state (in $_SERVER), which is a bad idea. Wrap all data from the request into a request object and pass it to the router. There you can examine it without ... | [
{
"body": "<p>Your constructor is doing too much. Just because the only thing you are going to do with your router is initialize it, that doesn't mean you can't have more methods. And as Sven pointed out, it probably shouldn't have access to the global state. Inject whatever you need.</p>\n\n<p>You have two pub... | {
"AcceptedAnswerId": null,
"CommentCount": "3",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-05T09:19:20.803",
"Id": "19323",
"Score": "0",
"Tags": [
"php",
"object-oriented",
"mvc",
"controller"
],
"Title": "Review: Finished router class for custom mini framework"
} | 19323 |
<p>This is one of a classes (that sends email based on object type to AM (role)) that is executed by an engine. I also have commands like <code>SendEmailToAOCommand</code> that send email to a role AO based on object type and so one.</p>
<pre><code>namespace SFE.Workflow.Commands
{
public class SendEmailToAMComma... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-05T10:49:48.087",
"Id": "30928",
"Score": "1",
"body": "You want to modify your in a specific way, you're not asking us to review it and look for possible improvements. Because of that, I think your question doesn't fit on codereview w... | [
{
"body": "<p>You don't provide any example of how <code>SendEmailToAOCommand</code> would differ from <code>SendEmailToAMCommand</code>, so I can't be more specific, but here's the gist of what you should do:</p>\n\n<pre><code>public abstract class SendEmailBase : ActionCommand\n{\n private readonly Func<... | {
"AcceptedAnswerId": "19359",
"CommentCount": "3",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-05T09:22:26.393",
"Id": "19324",
"Score": "1",
"Tags": [
"c#",
"generics"
],
"Title": "Sending email based on object type"
} | 19324 |
<p>I am having trouble designing a module, can anybody help me?</p>
<p>Because it will be hard to maintain this kind of module, I also think that this can test my skill of design pattern usage.</p>
<h1>Requirement</h1>
<p>This is basically an agricultural project (web application). I need to design a module where some ... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-05T11:40:01.603",
"Id": "30930",
"Score": "2",
"body": "Next time please **make an effort** when asking. You can as a bare minimum avoid spelling mistakes by using your browser's [spelling tools](https://addons.mozilla.org/en-US/firefo... | [
{
"body": "<p>I'm not entirely sure of what you're asking, but try this:</p>\n\n<ul>\n<li>Create a Crop class\n<ul>\n<li>Crop has a list of Traits</li>\n</ul></li>\n<li>Create a Trait class\n<ul>\n<li>Trait has an Enum to define which calculation it uses (for example: <code>Calculations.Average</code>, <code>Ca... | {
"AcceptedAnswerId": null,
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-05T10:14:40.683",
"Id": "19326",
"Score": "0",
"Tags": [
"java",
"algorithm",
"design-patterns"
],
"Title": "Redesign a module to be generic"
} | 19326 |
<p>I would like to know if there is a more efficiency way to speed up below code. It uses a procedure where subsampling is required in the nested loop (which a <a href="https://stackoverflow.com/a/13629611/1176697">previous answer</a> helped to make more efficient). </p>
<p>R has a tendency to hang when <code>B=500</c... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-05T12:52:48.230",
"Id": "30931",
"Score": "0",
"body": "Session infommation:R version 2.15.2 (2012-10-26) Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit) locale: [1] C/en_US.UTF-8/C/C/C/C attached base packages: [1] stats graphics g... | [] | {
"AcceptedAnswerId": null,
"CommentCount": "3",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-05T12:52:15.907",
"Id": "19328",
"Score": "4",
"Tags": [
"r",
"simulation"
],
"Title": "Speed up a monte carlo simulation with nested loop"
} | 19328 |
<p>I have been reading some NumPy guides but can't seem to figure it out. My TA told me I should be able to speed up my code by using a NumPy array instead of a <code>for</code> loop in the following segment of code.</p>
<pre><code>for neighbor in get_neighbors(estimates,i,j):
pXgivenX_ *= edge_model(True,... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-04T03:22:04.067",
"Id": "30932",
"Score": "1",
"body": "What is `neighbor`? What does `observation_model(obs,True)` return?"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-04T03:29:42.743",
"Id": "30933... | [
{
"body": "<p>Since this is homework, I won't give you the exact answer, but here are some hints. </p>\n\n<ol>\n<li><p><code>==</code> is overloaded in numpy to return an array when you pass in an array. So you can do things like this: </p>\n\n<pre><code>>>> numpy.arange(5) == 3\narray([False, False, F... | {
"AcceptedAnswerId": "19330",
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-04T02:31:53.703",
"Id": "19329",
"Score": "3",
"Tags": [
"python",
"image",
"numpy",
"vectorization"
],
"Title": "Using Gibbs sampling to segment an image"
} | 19329 |
<p>I'd like some tips on optimizing the following code.</p>
<p>I'm a bit concerned that I'm 'polluting the global namespace' by not wrapping it in a function. The commented out <code>var rotator { init {</code> came from a JavaScript tutorial but I'm not sure it's the jQuery way? Should I wrap it in <code>$(function()... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-05T10:54:44.483",
"Id": "30936",
"Score": "1",
"body": "_\"I'm not sure it's the JQuery way?\"_ jQuery way? `$(function(){}) === $(document).ready(function(){})`"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012... | [
{
"body": "<p>To your question ( confirmed by the comments ), you are not polluting the namespace because you are</p>\n\n<ol>\n<li>Wrapping already inside <code>$(document).ready(function() {</code> </li>\n<li>Using <code>var</code> for every variable.</li>\n</ol>\n\n<p>Furthermore, from what I can see that is... | {
"AcceptedAnswerId": null,
"CommentCount": "5",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-05T10:52:38.380",
"Id": "19331",
"Score": "3",
"Tags": [
"javascript",
"jquery",
"namespaces"
],
"Title": "jQuery accordion list"
} | 19331 |
<p>This nests Google Maps Event Listener for click or drag. How should I refactor the last part where it reuses the function <code>getAddressComponents()</code>? If there are other parts to be refactored, please do offer suggestions.</p>
<pre><code> function getAddressComponents() {
geocoder.geocode({'latLng': ma... | [] | [
{
"body": "<pre><code>google.maps.event.addListener(marker, 'drag', fgetAddressComponents);\n</code></pre>\n\n<p>And you don't need to add it twice. No need to add it in the <code>click</code> handler again.</p>\n\n<p>Otherwise it's mostly ok, except for the <code>==</code> (instead of <code>===</code>), I just... | {
"AcceptedAnswerId": "19336",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-05T14:23:34.650",
"Id": "19335",
"Score": "1",
"Tags": [
"javascript",
"google-maps"
],
"Title": "Nested Google Maps Listener"
} | 19335 |
<p>I've just made this class, which supports caching. I'd really appreciate any comments on how to make it better etc.</p>
<p><a href="https://github.com/Prashles/PHP-Currency-Convert-Class" rel="nofollow">Code</a></p>
<pre><code><?php
/*
*
* PHP Validation Class
*
* The currency rates are fetched and cached for... | [] | [
{
"body": "<p>Your class does too much. Split it up into multiple pieces:</p>\n\n<ol>\n<li>It converts currencies.</li>\n<li>It validates currency identifier.</li>\n<li>It fetches HTTP resources</li>\n<li>It caches fetched HTTP resources.</li>\n</ol>\n\n<p>Although it does plenty of things, it has no answer to ... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-05T15:45:11.443",
"Id": "19338",
"Score": "3",
"Tags": [
"php",
"object-oriented",
"converting",
"cache",
"finance"
],
"Title": "Currency conversion class with caching"
} | 19338 |
<p>I have a class that uses the SqlBulkCopy to bulk insert data into a SQL Server database. My original implementation just passed the <code>m_buffer</code> (which is a reference to a class that implements IDataReader) directly into the <code>WriteToServer</code>. Now a new requirement has been added that, upon failur... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-05T17:57:32.177",
"Id": "30966",
"Score": "0",
"body": "What if you could tell how much RAM is available? If `taskmgr.exe` knows that info, then there must be a way. http://veskokolev.blogspot.com/2008/03/how-to-get-free-available-memo... | [
{
"body": "<p>Based on your comments it looks like your code is actually some sort of library used by different clients in different environments. If that's the case I would rather let clients define the batch size they want rather than writing tricks around memory management on your own. </p>\n\n<p>If you do n... | {
"AcceptedAnswerId": "19356",
"CommentCount": "5",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-05T17:28:46.790",
"Id": "19339",
"Score": "1",
"Tags": [
"c#",
"sql",
"sql-server"
],
"Title": "Is this a good way to limit the occurrence of OutOfMemoryException?"
} | 19339 |
<p>I have made a small project and I am just wondering if I could add more functionality to the project e.g more classed etc. Is a better way of displaying my results and what error handling I could do?</p>
<p>I was also wondering what would be the best way to add Inheritance to the this project.I was thinking a <cod... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-10-02T10:04:02.607",
"Id": "60461",
"Score": "0",
"body": "You could have used a single function for the `name`, `age`, `year` and `studentnum`."
}
] | [
{
"body": "<p>Yes, you can add more functionality to your project. You can take this in many different directirons such as adding courses that students and register for. </p>\n\n<p>As for a error handling, you could add a check in your set functions of the <code>Student</code> class to check for formatting or c... | {
"AcceptedAnswerId": "19342",
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-05T19:47:23.860",
"Id": "19341",
"Score": "8",
"Tags": [
"java"
],
"Title": "Student information system"
} | 19341 |
<p>I'm trying to come up with a way to make a drop down box that is displayed through a jquery mouse hover event and with nested dropdown boxes displayed through hovering over elements of the original drop down box. I wrote some terribly inefficient code and I'm struggling to find ways of simplifying it. If anyone has ... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-06T10:08:45.090",
"Id": "31008",
"Score": "0",
"body": "Try using JavaScript for state and CSS for style. It would make this code **so** much simpler!"
}
] | [
{
"body": "<p>Your code is very big and messy so this a bit tricky to really see what you are trying to do. A few obvious things to make the code more readable:</p>\n\n<ul>\n<li>Replace <code>.css(\"display\",\"block\")</code> with <code>.show()</code></li>\n<li>Replace <code>.css(\"display\",\"none\")</code> ... | {
"AcceptedAnswerId": "19346",
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-05T20:45:19.423",
"Id": "19343",
"Score": "5",
"Tags": [
"javascript",
"jquery",
"html",
"css"
],
"Title": "Simplifying Code for Drop-Down-Box in JQuery and HTML"
} | 19343 |
<p>How I can rewrite or refactor my controller code? I have the same SQL query (@plan_gp_users) in all <code>def</code>s.</p>
<pre><code> class PlanGpsController < ApplicationController
def index
@search = PlanGp.joins(:user).search(params[:q])
@plan_gps = @search.result.order("created_at DESC... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2014-01-08T23:53:17.243",
"Id": "64970",
"Score": "0",
"body": "It looks like much of the code is double-spaced (has extra blank lines). Is that an accident of cut-and-paste?"
}
] | [
{
"body": "<p>You should use a named Scope in your model</p>\n\n<pre><code>class User < ...\n scope :by_ab_without_user, lambda do |ab_id, id| \n where(:ab_id => ab_id, :is_admin => null).\n where(\"id != :user_id AND role != 'head'\", { :user_id => id })\n end\n ...\nend\n</code></pre>\n... | {
"AcceptedAnswerId": "34083",
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-06T07:11:24.647",
"Id": "19353",
"Score": "1",
"Tags": [
"ruby",
"sql",
"ruby-on-rails",
"controller",
"active-record"
],
"Title": "Duplicate SQL code in controller"
} | 19353 |
<p>One thing I realized while going through code yesterday at my internship is that I run sql queries quite often. So I decided to write a function to keep all of the calls in one place and reduce the number of lines of code. It doesn't cover all possible sql calls but it does cover the basic calls I believe.</p>
<p>... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-06T16:35:01.167",
"Id": "31023",
"Score": "0",
"body": "So what's your question?"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-06T17:17:01.720",
"Id": "31024",
"Score": "0",
"body": "Opps, For... | [
{
"body": "<h1>Argggh, my eyes burn!</h1>\n\n<p><strong>SQL Injections</strong></p>\n\n<p>You should understand SQL injections. This code is likely to promote SQL injections. Read up about prepared statements. It looks like you are probably passing in a <code>mysql_*</code> function as a string. This stops ... | {
"AcceptedAnswerId": null,
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-06T13:22:13.197",
"Id": "19355",
"Score": "0",
"Tags": [
"php"
],
"Title": "Generalized Sql Query Function"
} | 19355 |
<p>This code basically changes the color of the lines and move text for menu navigation. Is there a simpler way of writing it?</p>
<pre><code>$('.nav1 a').click(function () {
$('.nav1').addClass("current_nav1");
$('.sf_menu li').removeClass('current_nav2 current_nav3 current_nav4 current_nav5 current_nav6');
... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-06T17:36:21.370",
"Id": "31027",
"Score": "1",
"body": "Is there anyway you can show your relevant html also? It would probably be easier for testing purposes"
}
] | [
{
"body": "<p>Why do you need different <code>navXXX</code> and <code>current_navXXX</code> classes? Just have a single</p>\n\n<pre><code>var menus = {//you need some kind of mapping between elements clicked and menu items\n id1: '#home',\n id2: '#design',\n id3: '#seo',\n id4: '#galery',\n id5: '#about',\... | {
"AcceptedAnswerId": null,
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-06T17:14:24.977",
"Id": "19362",
"Score": "2",
"Tags": [
"javascript",
"jquery",
"event-handling"
],
"Title": "Changing line colors and move text for menu navigation"
} | 19362 |
<p>I created a simple wordfeud solver that checks what words can be created with the supplied letters.</p>
<pre><code>public void GetResults(string characters)
{
// place all the characters we have in a dictionary with key=letter, value=amount
var charCount = SplitCharacters(characters);
// get all the un... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-06T18:22:58.710",
"Id": "31031",
"Score": "0",
"body": "Is your list of words (`_words`) fixed, or it changes all the time? How many words you expect to be there?"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "201... | [
{
"body": "<p>Nice task for brains :). First of all you should prepare your available list of words for faster search: extract all letters, sort them and order by descending length (so that you don't need to sort results later. Make sure to cache it somewhere as that would be the most time-consuming operation.<... | {
"AcceptedAnswerId": null,
"CommentCount": "3",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-06T18:12:19.607",
"Id": "19364",
"Score": "4",
"Tags": [
"c#",
"algorithm",
"performance"
],
"Title": "How to improve wordfeud solver algorithm?"
} | 19364 |
<p>Is the following PHP laid out fine to go inside the mail function? </p>
<pre><code>$to = "My Name <myemail@mydomain.com>";
$subject = "Contact Form: $name";
$message = "Name: $name\r\nEmail: $email\r\nMessage:\r\n$message";
$headers = "From: Contact Form <contactform@mydomain.com>";
mail($to, $subject,... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-07T01:09:19.650",
"Id": "31046",
"Score": "0",
"body": "What do you mean by secure? The link you posted also does not define secure."
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-07T12:11:34.770",
"Id... | [
{
"body": "<p>As far as I think, your script is good and will achieve the required task. But before finalising on this script, you should read about PHPMAILER. It is easy to implement and will provide you many features like to add CC, BCC, and attachments. The <code>mail()</code> function is the easiest way, bu... | {
"AcceptedAnswerId": "19376",
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-06T18:14:05.837",
"Id": "19365",
"Score": "3",
"Tags": [
"php",
"security",
"email"
],
"Title": "Is this a secure and best-practice PHP mail() function?"
} | 19365 |
<p>The <a href="http://www.zlib.net" rel="nofollow">zlib library</a> is a venerable piece of C software. The copyright for the decompression routine goes back to 1995, with the last rewrite in 2002. It is used daily by millions.</p>
<p>To be perfect, the decompression routine should not invoke undefined behavior, even... | [] | [
{
"body": "<p>I am not an expert in the <code>zlib</code> source tree nor am I familiar with the coding style of its authors, but I found this to be an interesting question. I'd say <s>it looks safe and</s> my guess is the original authors are expecting <code>offset</code> to be initialized to zero. <b>Update:... | {
"AcceptedAnswerId": "19370",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-06T21:38:23.270",
"Id": "19368",
"Score": "3",
"Tags": [
"c"
],
"Title": "Potential uninitialized access in zlib"
} | 19368 |
<p>I have two different ideas on how to handle this Update method in my MVC Model class "DataAccess" And I'm wondering if in the first, I'm handling my class incorrectly or ambiguously, because I'm using members from the same object for two different purposes.</p>
<p>The man Class has 2 relevant members. ID, which is... | [] | [
{
"body": "<p>The second one seams to me a better approach but you say that the ManID is a primary key, so why are you handling it as a non primary key and using the query result as a sequance?</p>\n\n<pre><code>public DbResult Update(int idToLookFor, string newName)\n{\n return Update(_MyDBEntities.Men.Sing... | {
"AcceptedAnswerId": "19380",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-07T00:13:08.360",
"Id": "19371",
"Score": "1",
"Tags": [
"c#",
"entity-framework"
],
"Title": "Passing an entity to use it's fields to update method is ok to do?"
} | 19371 |
<p>I load in static setting via an ini file. Ini file have the benefits</p>
<ul>
<li>of NOT being server by most web servers by default. </li>
<li>easier to edit than XML</li>
<li>even simplier than JSON</li>
</ul>
<p>Setting get loaded in Application.cfc</p>
<pre><code>void function setupApplication() output="false... | [] | [
{
"body": "<p>What kind of things are you looking to improve? Your solution looks pretty straightforward. The only problem with an ini file is that ColdFusion can't instantly/natively parse it. Whereas XML/JSON can be parsed directly into a Coldfusion Struct.</p>\n\n<pre><code><cfscript>\npublic struct fu... | {
"AcceptedAnswerId": "25802",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-07T04:36:18.127",
"Id": "19375",
"Score": "2",
"Tags": [
"coldfusion",
"configuration",
"cfml"
],
"Title": "Loading site configuration from ini file"
} | 19375 |
<p>I am a new developer therefore I have many questions:</p>
<p>In this class I get file path on the disk (wireshark file that needs to be converted to pcap format extension) and convert it with the method <code>convertFileToPcap()</code>, in the constructor I am calling <code>convertFileToPcap()</code> and from the m... | [] | [
{
"body": "<p>Hello and welcome to Code Review!</p>\n\n<ul>\n<li>I'd like to first direct you to Microsoft's official <a href=\"http://msdn.microsoft.com/en-us/library/xzf533w0.aspx\">naming\nguidelines</a> for C# development. Your code goes against a few of\nthose, and if that's your company's or team's guidel... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-07T13:29:59.627",
"Id": "19385",
"Score": "3",
"Tags": [
"c#"
],
"Title": "Is this common way to build this class and method?"
} | 19385 |
<p>I was being troubled a lot by Robocopy (or maybe anti-virus or maybe Network Hardware). I copy files from the Dynamic View of Clearcase to the local machine for a fresh build. The copy would frequently fail due to:</p>
<blockquote>
<p>2012/12/06 15:35:07 ERROR 64 (0x00000040)... </p>
<p>The specified network... | [] | [
{
"body": "<p><strong>Naming</strong> </p>\n\n<p>The method <code>StartCopy()</code> implies that the copy process can also be stopped. So we should better rename it to <code>CopyFiles()</code>. </p>\n\n<p><strong>Refactoring</strong> </p>\n\n<p>First step would be to refactor the composition of the commandl... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-07T14:05:53.203",
"Id": "19386",
"Score": "5",
"Tags": [
"c#"
],
"Title": "Resilient Wrapper for Robocopy in C#"
} | 19386 |
<pre><code>def dec_to_bin(ip):
ip_array = ip.split(".")
ip_array = filter(None, ip_array)
if len(ip_array) != 4:
return "Invalid IP Address format"
else:
ip_bin = []
for x in range(len(ip_array)):
# Formatting example referenced from:
# http://stacko... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-08T19:16:57.090",
"Id": "31096",
"Score": "0",
"body": "Beware: This will not work for IPv6."
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-11-09T11:44:23.653",
"Id": "54808",
"Score": "0",
"body"... | [
{
"body": "<pre><code>def dec_to_bin(ip):\n\n ip_array = ip.split(\".\")\n ip_array = filter(None, ip_array)\n</code></pre>\n\n<p>Why do you need to filter it?</p>\n\n<pre><code> if len(ip_array) != 4:\n return \"Invalid IP Address format\"\n</code></pre>\n\n<p>Never return strings to indicate e... | {
"AcceptedAnswerId": "19392",
"CommentCount": "7",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-07T14:50:41.837",
"Id": "19388",
"Score": "5",
"Tags": [
"python",
"converting",
"ip-address"
],
"Title": "Decimal-to-binary converter for IP addresses"
} | 19388 |
<p>This class acts like a synchronization context except the work could be done on any one of the available threads.</p>
<p>I've done quite a bit testing on my own, but I'm generally concerned about potential deadlocks. (I'm also curious if there are better ways to accomplish my goal.)</p>
<p>I created it because I h... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-07T22:48:58.130",
"Id": "31085",
"Score": "0",
"body": "Which .NET version are you targeting?"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-07T23:01:44.777",
"Id": "31088",
"Score": "0",
"body... | [
{
"body": "<p>It looks to me like you've reinvented the thread pool. What exactly is this giving you that the thread pool doesn't have? I see you've got events for termination and whatnot, but if you use TPL you can easily emulate everything you've written.</p>\n\n<p>One thing I would like to point out is that ... | {
"AcceptedAnswerId": "19407",
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-07T20:12:06.337",
"Id": "19394",
"Score": "3",
"Tags": [
"c#",
"multithreading",
"thread-safety"
],
"Title": "Invoking a callback on a pool of threads"
} | 19394 |
<p>I have created an api in php and I was hoping some clever individuals would mind reviewing it to see what I could do better. I have commented it heavily to explain my reasoning. please feel free to pick holes wherever you can!</p>
<pre><code> //On my page 'localhost/api'
if (!$_POST) {
die('No direc... | [] | [
{
"body": "<p>What you are really trying to accomplish here is taking http requests and dispatching them to the correct class. This is much better handled by one of many mvc frameworks in existence. That said, I see several problems with what you have here.</p>\n\n<ol>\n<li>Improper check for POST requests. $_P... | {
"AcceptedAnswerId": "19410",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-07T22:27:45.267",
"Id": "19397",
"Score": "1",
"Tags": [
"php",
"api"
],
"Title": "PHP api design - anything done badly?"
} | 19397 |
<p>Please help me to make more readable and simple code like this. What if I had 20 components? Is it right to organize code like this?</p>
<pre><code>package newpackage.view;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordFiel... | [] | [
{
"body": "<p>Creating UIs programmatically in Swing is verbose and error prone. There are visual tools that let you create your layout in a WYSIWYG style, and autogenerate the code for you. Many years ago I used NetBeans for this, I don't know what's today's flavor, if you use Eclipse you may want to check out... | {
"AcceptedAnswerId": "19459",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-08T10:21:21.923",
"Id": "19401",
"Score": "5",
"Tags": [
"java",
"swing",
"gui"
],
"Title": "Readable code with many components (Swing)"
} | 19401 |
<p>How would you refactor this kind of code? I've unsuccessfully tried several approaches (NSArray, NSPointerArray, plain C array), but always ran into pointer type checking error.</p>
<pre><code>NSError *error1;
NSError *error2;
NSError *error3;
//imagine MANY of them
[object1 methodWithError: &error1];
[object2 ... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-08T14:21:01.680",
"Id": "31092",
"Score": "0",
"body": "With only three items, I wouldn't refactor it at all. With more items, I would put my errors in an array and iterate over them."
},
{
"ContentLicense": "CC BY-SA 3.0",
... | [
{
"body": "<p>I found a solution using plain C array and __autoreleasing</p>\n\n<pre><code>int number = 3;\nNSError __autoreleasing *errors[number]; //without __autoreleasing, compiler (with ARC) will complain\n//imagine MANY of them\n[object1 methodWithError: &errors[0]];\n[object2 methodWithError: &er... | {
"AcceptedAnswerId": "19449",
"CommentCount": "3",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-08T13:40:15.030",
"Id": "19403",
"Score": "3",
"Tags": [
"objective-c",
"error-handling",
"cocoa"
],
"Title": "Storing and iterating over multiple NSError pointers"
} | 19403 |
<p>Here is my new STL-like implementation of an unweighted graph. Could you please tell me what member functions I should include in my library?</p>
<p><strong>unweighted_graph.hpp</strong></p>
<pre><code>#include <algorithm>
#include <stdexcept>
#include <vector>
#include <map>
#include <s... | [] | [
{
"body": "<h3>The big pain points:</h3>\n\n<ol>\n<li>Put the include guards first.<br>\nNo point in re-including header files if you don't need to</li>\n<li>Never use <code>using namespace std;</code> in a header file.<br>\nAnybody that includes your code now has all the standard stuff pushed into global names... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-08T14:30:22.510",
"Id": "19404",
"Score": "1",
"Tags": [
"c++",
"template",
"graph",
"stl"
],
"Title": "unw_graph class (unweighted graph)"
} | 19404 |
<p>I have made a student details project. The user is asked to enter the info for two students, which is then displayed on the screen.</p>
<p>Questions:</p>
<ol>
<li>Have I used inheritance correctly?</li>
<li>What would be a better way of outputting the results?</li>
</ol>
<p></p>
<pre><code> public class Person e... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-09T08:43:54.177",
"Id": "31105",
"Score": "3",
"body": "`Course` definitely should not extend `Student`. Inheritance represents an is-a relationship, and a Course is not a Student."
},
{
"ContentLicense": "CC BY-SA 3.0",
"C... | [
{
"body": "<p>You must remember that inheritance is good to avoid code duplication, but also you must know that you will have messy code if you will extends for one field or method, they must be connected logically Student extends Person it's OK, Cat extends Dog isn't OK. I think you don't need make Person exte... | {
"AcceptedAnswerId": null,
"CommentCount": "3",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-08T20:34:08.620",
"Id": "19408",
"Score": "4",
"Tags": [
"java"
],
"Title": "Student details project"
} | 19408 |
<p>Here is Python code written to perform the following operations:</p>
<ol>
<li>Find each occurrence of a three-letter sequence in a character array (e.g. a sequence such as <code>('a','b','c')</code>), including overlapping sequences of up to 2 shared characters.</li>
<li>Count the characters between the start of ea... | [] | [
{
"body": "<p>The first way to improve the code that I can see is: make it more readable, more expressive. In one word: refactoring.</p>\n\n<p>Try to avoid so many one-letter variables and long lines as they obstruct the meaning of the code. Also, you're using two nested loops and two nested if statements with ... | {
"AcceptedAnswerId": "19457",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-08T21:12:55.967",
"Id": "19409",
"Score": "3",
"Tags": [
"python",
"array",
"search"
],
"Title": "Array-search algorithm for identical sequences and character distances"
} | 19409 |
<p>I'm writing a custom dictionary which is to be used as a helper for caching. The reason I use delegates is because it must interface with generated code, so I can't change this. However, you can assume that the delegates are thread-safe. I did some preliminary tests that it's thread-safe, but I can always use an ext... | [] | [
{
"body": "<p>Your code is not safe:</p>\n\n<ul>\n<li>simultaneous calls to getter of the indexer and setter for the same key may result in data missing in cache: \n<ul>\n<li>setter adds the key to <code>RealKeys</code></li>\n<li>getter reads it and looks for value in... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-08T21:28:06.403",
"Id": "19411",
"Score": "4",
"Tags": [
"c#",
"multithreading",
"thread-safety",
"hash-map"
],
"Title": "Custom dictionary for caching assistance"
} | 19411 |
<p>Imagine a website that publishes the number of daily commuters in only a graphical form each day using a bar chart. I want to determine the number by reading the bar chart after saving the graphic as an image (the image stuff is not important here). The way I want to read the bar chart is by going to a pixel numbe... | [] | [
{
"body": "<p>How about</p>\n\n<pre><code>if BlackOrWhite == 1:\n if oldtrial == high:\n newtrial = oldtrial+factorIncrease*(high-low)+1\n high = newtrial\n low = oldtrial\n elif high-oldtrial==1:\n finished = 1\n newtrial = oldtrial\n else:\n newtrial = oldtrial+(high-oldtrial)/2\n low ... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-08T23:26:39.413",
"Id": "19413",
"Score": "4",
"Tags": [
"python",
"algorithm",
"performance"
],
"Title": "Edge finding binary search"
} | 19413 |
<p>I wanted to open Database (Mongo) connection only once when I start application and share the same connection across the application. What am doing as part of app.js is</p>
<p>Creating connection </p>
<pre><code>var dbConnection = mongoose.createConnection(config.database.address, config.database.dbName, config.da... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-08T19:01:17.160",
"Id": "31101",
"Score": "0",
"body": "Does this code work for you, or are you having problems with it?"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-08T19:38:08.937",
"Id": "31102",
... | [
{
"body": "<p>You can use the <code>connect</code> function, which returns the default connection.</p>\n\n<p>Just add <code>mongoose.connect('mongodb://username:password@host:port/database');</code> whenever you need it.</p>\n\n<p>See <a href=\"http://mongoosejs.com/docs/connections.html\" rel=\"nofollow\">http... | {
"AcceptedAnswerId": null,
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-08T18:58:29.550",
"Id": "19415",
"Score": "1",
"Tags": [
"javascript",
"node.js"
],
"Title": "Passing Database connection"
} | 19415 |
<p>I'm new to jQuery and played a bit with what can I do with radio button. I want to create <strong>ONLY</strong> one input type when I click the <kbd>Notice</kbd> radio button. I already using many way to achieve this, but this is the only way I can do this, you can see the following code.</p>
<p>HTML</p>
<pre><cod... | [] | [
{
"body": "<ul>\n<li><p>There's no need to use a <code>while</code> loop, as you only want to add the element once, after which the condition won't hold anymore. That's what <code>if</code> is for.</p></li>\n<li><p>There's a shorter way to create a new element</p></li>\n</ul>\n\n<p>So I get to:</p>\n\n<pre><cod... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-09T10:49:58.417",
"Id": "19421",
"Score": "1",
"Tags": [
"javascript",
"jquery",
"dom"
],
"Title": "Creating input when checking/clicking radio button"
} | 19421 |
<p>I want to store image (details) in my database, and want for each image the possibility to have from 1 to N versions.</p>
<p>I want a single file (src) to be associated with a single version. There must not be more than one version for an single image with the same dimensions and format (no point having a version w... | [] | [
{
"body": "<p>The unique keys will enforce the requirements you specified, but unless your data entry program either traps error codes returned by MySQL and prompts the user for correction or checks for uniqueness before saving the record, you will probably experience a lot of record rejections where you won't ... | {
"AcceptedAnswerId": "19482",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-09T17:05:56.493",
"Id": "19431",
"Score": "1",
"Tags": [
"mysql",
"sql"
],
"Title": "Are these tables concepted correctly?"
} | 19431 |
<p>I will try to be as concise as I can:</p>
<p><strong>The goal:</strong> trying to universalize a specific section of a project, that is dealing with the SQL database transactions.</p>
<p>To assist you with your answer, I've pasted the following (just for reference):</p>
<ul>
<li><p>a usage code: <code>GetTestOfTa... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-09T18:14:43.407",
"Id": "31121",
"Score": "0",
"body": "Have you read about ORMs, e.g. Entity Framework? Is there a strong reason to use `DataTables` rather than typed entities? Is there a strong reason to use stored procedures? Your c... | [
{
"body": "<p>Just to give you idea how your code would look like if you use Entity Framework:</p>\n\n<pre><code>using (var db = new HpDatabaseContext())\n{\n return db.GetTimesWithCustomerNames(userId, month, year, reasonId).ToArray();\n}\n</code></pre>\n",
"comments": [
{
"ContentLicense"... | {
"AcceptedAnswerId": null,
"CommentCount": "8",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-09T17:50:29.673",
"Id": "19433",
"Score": "1",
"Tags": [
"c#",
"optimization",
"asp.net",
"sql-server"
],
"Title": "Using a Dedicated Class & Constructor to initialize Stored Pro... | 19433 |
<p>I want to use this function to store 1024 bit keys in my Mysql database. If you call generateKey(128), you'll get a 128 byte string, same as 1024 bit string. I want to know if there is something wrong with this code or if I should be worried about inter-operability issues with MySQL, Java, Android, File Read/Write, ... | [] | [
{
"body": "<ul>\n<li>Use <code>chr($randInt)</code> instead of <code>$charUniverse[$randInt]</code>, and abandon the <code>$charUniverse</code> variable altogether.</li>\n<li>Use the <code>$str .= \"str\"</code> dot-equals operator in PHP to append to your final string.</li>\n</ul>\n\n<p>Thus your code becomes:... | {
"AcceptedAnswerId": "19443",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-09T17:54:54.720",
"Id": "19434",
"Score": "2",
"Tags": [
"php",
"random",
"cryptography"
],
"Title": "1024 bit key generation in php"
} | 19434 |
<p>I've just written a validation routine for a UPC-A, which ensures the check digit matches the given UPC number (according to <a href="https://en.wikipedia.org/wiki/Universal_Product_Code#Check_digit_calculation" rel="nofollow noreferrer">rules I found on Wikipedia</a>).</p>
<p>Below is the code, what do you think?<... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2014-02-06T15:31:35.163",
"Id": "70412",
"Score": "0",
"body": "I took the liberty to roll back the changes. Please do not change the code after posting it except for errors which should not have been there in the first place (like typos when ... | [
{
"body": "<p>Two things I noticed.</p>\n\n<ol>\n<li>The return statement if the length is invalid should be on a separate line, and wrapped in braces</li>\n<li>What purpose does the int cast solve? I thought php was typeless.</li>\n</ol>\n\n<p>Other than that, the only other thing I noticed is to space out you... | {
"AcceptedAnswerId": "229397",
"CommentCount": "2",
"ContentLicense": "CC BY-SA 4.0",
"CreationDate": "2012-12-09T20:51:09.967",
"Id": "19438",
"Score": "4",
"Tags": [
"algorithm",
"php",
"validation",
"checksum"
],
"Title": "UPC-A validation"
} | 19438 |
<p>I am building a time tracking application and my requirement says that any hours past 40 total should be counted as overtime and not regular hours.</p>
<p>Which function is more clear? What can be done to clarify the function? Even the second one at ~ 10 lines seems wordy</p>
<h1>Option #1</h1>
<pre><code>functio... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-10T01:11:07.470",
"Id": "31140",
"Score": "0",
"body": "Does it need to be on a day-by-day basis? If overtime is defined here as > 40 hrs on a weekly basis, why not just sum the week's hours, and get \"40 regular, 24 overtime\" total. ... | [
{
"body": "<p>You can accomplish your task using only core Javascript, with no reference to JQuery code, in about that many lines. JQuery is overkill for this task. </p>\n\n<p>I applied the following changes to make your code more readable:</p>\n\n<ol>\n<li>Reduced knowledge required to understand it (removed J... | {
"AcceptedAnswerId": null,
"CommentCount": "3",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-09T21:28:00.343",
"Id": "19439",
"Score": "1",
"Tags": [
"javascript",
"functional-programming"
],
"Title": "mapping & loops - counting up or counting down"
} | 19439 |
<p>I wrote a function in Scala to find out and return a loopy path in a directed graph.</p>
<p>One of the arguments is a graph presented in an adjacent list, and the other is a start node. It returns a pair including a loopy path by a list of nodes.</p>
<p>I wonder if there are more elegant ways of doing this.</p>
<... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-10T14:33:20.830",
"Id": "31162",
"Score": "0",
"body": "I am not familiar with scala. If the question is about the algorithm, yes there are some interesting ones. Start here: http://ostermiller.org/find_loop_singly_linked_list.html go ... | [
{
"body": "<p>You should use an array to check if you have already visited a node and not <code>visits.contains(node)</code>, it would give you the answer in constant time instead of linear time.</p>\n\n<p>The overall complexity of your algorithm is exponential. For instance, if you run your algorithm on this g... | {
"AcceptedAnswerId": "19465",
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-10T05:31:01.217",
"Id": "19447",
"Score": "4",
"Tags": [
"algorithm",
"scala",
"graph"
],
"Title": "Finding and returning a loopy path in a directed graph"
} | 19447 |
<p>I'm trying to come up with the most basic example of making a JQuery slide show where you click on the current image you're viewing and you cycle through a gallery of photos. I know its probably not the most basic example, because if I want to add a new image I have to code more JQuery. Is there a more abstract appr... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-10T13:41:48.087",
"Id": "31160",
"Score": "0",
"body": "You should include the main code in the question."
}
] | [
{
"body": "<p>This should do the trick:</p>\n\n<p><a href=\"http://jsfiddle.net/XjdTX/11/\" rel=\"nofollow\">http://jsfiddle.net/XjdTX/11/</a></p>\n\n<p>I changed the <code><div id=\"slideframe\">...</div></code> to use a class instead. This will allow you to have multiple slide shows functioning of... | {
"AcceptedAnswerId": "19450",
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-10T06:00:59.017",
"Id": "19448",
"Score": "0",
"Tags": [
"javascript",
"jquery",
"html",
"html5"
],
"Title": "JQuery Slide Show Simplification"
} | 19448 |
<p>I've written an extension method to truncate strings:</p>
<pre><code>/// <summary>
/// Returns the string, or a substring of the string with a length of <paramref name="maxLength"/> if the string is longer than maxLength.
/// </summary>
/// <param name="maxLength">The maximum length of the ... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-10T11:43:06.523",
"Id": "31148",
"Score": "0",
"body": "I needed this for UI strings, so I made a version that adds ellipsis: https://gist.github.com/4250125"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-... | [
{
"body": "<ol>\n<li><p>About the function comment: </p>\n\n<p>You're over-complicating the summary. As of how I understand the function does <code>Get the first <paramref=\"maxLength\"> characters</code> as a summary. Special conditions are mentioned bellow. Those should be simplified as well. </p></li>\... | {
"AcceptedAnswerId": "19454",
"CommentCount": "3",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-10T10:10:49.600",
"Id": "19453",
"Score": "3",
"Tags": [
"c#",
"unit-testing"
],
"Title": "Do these unit tests cover my method under test?"
} | 19453 |
<p>I have to implement a tree programming here. What I mean by tree programming is I have data stored in tree format. I have a parent object which will have child objects of same type the level of depth can go any long:</p>
<p>I am able to store objects in tree format and also able to display properly. I am facing ... | [] | [
{
"body": "<p>It is not possible to test your classes, because of:</p>\n\n<pre><code>import com.chartis.gp.support.util.BrokerSupportUtil;\nimport com.chartis.gp.support.vo.Links;\nimport com.chartis.kernel.user.UserVO;\nimport com.chartis.kernel.utils.Utils;\n</code></pre>\n\n<p>But ok, the general idea is ( i... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-10T11:32:03.227",
"Id": "19455",
"Score": "4",
"Tags": [
"java",
"algorithm",
"recursion",
"tree"
],
"Title": "Recursive search to delete n'th child in tree"
} | 19455 |
<p>This is a try at implementing integers in <a href="http://www.idris-lang.org/" rel="nofollow">Idris</a>. Any advice or comments are most welcome.</p>
<pre><code>-- tries to implement integers using fold and a generalized notion of 'reduction' instead of
-- utilizing recursion explicitly in all operations./
-- maxim... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2014-12-20T23:40:25.020",
"Id": "135173",
"Score": "1",
"body": "You might want to use [Literate Haskell](https://www.haskell.org/haskellwiki/Literate_programming) if you write this many comments."
}
] | [] | {
"AcceptedAnswerId": null,
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-10T11:46:34.463",
"Id": "19456",
"Score": "1",
"Tags": [
"haskell",
"functional-programming"
],
"Title": "Integer implementation in Idris"
} | 19456 |
<p>This is a working example that I am trying to improve. </p>
<p>Throughout the application I am working on one of the requirements was to have tooltips everywhere. However the customer wanted the ability to see a "Summary" of these tooltips and be able to manage them from a single location.</p>
<p>What I ended up ... | [] | [
{
"body": "<pre><code>function FormatToolTip() {\n \"use strict\";\n\n amplify.request.define(\"listTooltips\", \"ajax\", {\n url: \"resources/prototype/tooltips.json\",\n dataType: \"json\",\n type: \"GET\"\n });\n\n amplify.request({\n resourceId: \"listTooltips\",\n ... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-10T13:59:29.707",
"Id": "19458",
"Score": "3",
"Tags": [
"javascript",
"jquery",
"css",
"twitter-bootstrap"
],
"Title": "Improving Tooltips with json data"
} | 19458 |
<p>As a learning exercise, I'm developing my own PHP framework. I'm looking for a way to "load views" (kinda like CodeIgniter does it), without polluting my general scope. </p>
<p>I came up with the following, basic, example:
<pre><code>$data['test'] = 'Hello world';
$str = load_view($data, true);
echo $str;
f... | [] | [
{
"body": "<p>No this is an ugly way to do it.</p>\n\n<h2>extract() (PHP4 stuff in 2012/2013?)</h2>\n\n<p>The problem with exract() is that it's creating variables ionto the global hyperspace (oh God, why) where can exist (in localy also!) any other variable and can have name collisions and can overwrite the o... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-10T14:12:33.480",
"Id": "19460",
"Score": "9",
"Tags": [
"php"
],
"Title": "Is this a proper way of \"loading\" views in PHP?"
} | 19460 |
<p>I found a code on my computer that i wrote a while ago. It is based on an exercise from O'Reilly book <em><a href="http://shop.oreilly.com/product/9780596153823.do" rel="noreferrer">Programming the Semantic Web</a></em>. There is a class, that stores <a href="http://en.wikipedia.org/wiki/Resource_Description_Framewo... | [] | [
{
"body": "<p>You could gain some simplicity by breaking the task into two parts.</p>\n\n<p>Firstly, inspect the <code>None</code>ness of the parameters to figure out which dictionary to look into. Store the results in local variables. So something like:</p>\n\n<pre><code>if (should use _spo):\n data = self.... | {
"AcceptedAnswerId": "19527",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-10T19:00:27.527",
"Id": "19470",
"Score": "6",
"Tags": [
"python",
"hash-map"
],
"Title": "Refactor deeply nested if-else"
} | 19470 |
<p>I have the following interface:</p>
<pre><code>public interface ILogger
{
void Log(string message, string title, StatusType type, DateTime timestamp);
IAsyncResult LogAsync(string message, string title, StatusType type, DateTime timestamp, AsyncCallback callback);
void EndLogAsync(IAsyncResult result);
... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-11T05:54:42.430",
"Id": "31181",
"Score": "3",
"body": "Is there any chance you're writing on .NET 4.5? And why not use existing logging solutions (NLog, log4net)?"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "20... | [
{
"body": "<p>To answer your question:</p>\n\n<ul>\n<li>you can access the delegate instance by reading <code>((AsyncResult)asyncResult).AsyncDelegate</code> property (see <code>Executing a Callback Method When an Asynchronous Call Completes</code> section of <a href=\"http://msdn.microsoft.com/en-us/library/2e... | {
"AcceptedAnswerId": null,
"CommentCount": "3",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-11T00:16:50.673",
"Id": "19474",
"Score": "2",
"Tags": [
"c#",
"asynchronous",
"delegates"
],
"Title": "Putting called delegate into AsyncState parameter - Pros/cons?"
} | 19474 |
<p>I am trying to remove files based on an array of prefixes, from files present in <code>sourceDirectory</code>. I am using <code>prefixFileFilter</code> to get the list of files with the prefixes. After that I am removing these from the original list.</p>
<p>Is there any better approach?</p>
<pre><code>package srir... | [] | [
{
"body": "<p>you can use java.io.FilenameFilter like:</p>\n\n<pre><code> final List<String> excludedFiles = Arrays.asList(filesToBeExcluded);\n File[] totalFiles = sourceDirectory.listFiles(new FilenameFilter() {\n @Override\n public boolean accept(File dir, String name) {\n ... | {
"AcceptedAnswerId": "19481",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-11T03:06:49.913",
"Id": "19476",
"Score": "2",
"Tags": [
"java"
],
"Title": "File-exclusion based on an array of prefixes"
} | 19476 |
<p>There is my SSCCE to generate a value of discrete random variable. </p>
<p><code>values</code> is set of value the RV can take and <code>procents</code> is equivalent to discrete pdf.</p>
<p>Can you anticipate any issue with this snippet? </p>
<pre><code>import java.util.Random;
public class RandomTest {
p... | [] | [
{
"body": "<p>In general, this looks ok, i.e. it works.<br>\nI will put some notes on the lines of your method and will provide my suggestion after this. </p>\n\n<p>Spelling: procent is not an English word. Well, the compiler does not care, but the next programmer probably will.</p>\n\n<pre><code>public int di... | {
"AcceptedAnswerId": "19491",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-11T06:00:09.297",
"Id": "19477",
"Score": "2",
"Tags": [
"java",
"random",
"generator"
],
"Title": "Discrete random variable generator"
} | 19477 |
<p>I form DOM nodes as strings and append them to DOM tree like below using jQuery.</p>
<pre><code>var dom = '<div><div style="display: inline-block">first name</div>'
'<div style="display: inline-block">last name</div></div>';
$("#contacts").append(dom);
</code></pre>... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-16T07:38:50.913",
"Id": "31469",
"Score": "0",
"body": "this used to be the case, but these days they are saying concatenation is faster than join now."
}
] | [
{
"body": "<p>Strings are <a href=\"https://stackoverflow.com/questions/51185/are-javascript-strings-immutable-do-i-need-a-string-builder-in-javascript\">immutable</a>. It means that when you allocate memory while creating a string, you are not able to reallocate it.\nSo, this code:</p>\n\n<pre><code>var a = 'a... | {
"AcceptedAnswerId": "19483",
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-11T06:57:02.563",
"Id": "19479",
"Score": "10",
"Tags": [
"javascript",
"jquery",
"performance",
"html",
"dom"
],
"Title": "Efficient string concatenations with DOM"
} | 19479 |
<p>Can someone have a look at my script and maybe find something which can be optimized, as there shall be later 100 of it running.</p>
<p>First of all this script shall be triggered once per day to receive data from a website into a sheet called "<em>getdata</em>". Some fields of "<em>getdata</em>" are transferred to... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-11T12:52:48.107",
"Id": "31194",
"Score": "0",
"body": "How long does it take to run now and does it take up lots of resouces?"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-11T22:16:27.103",
"Id": "31... | [
{
"body": "<p>From my research, the slowest part of your script is by far <code>SpreadsheetApp.openById(docid)</code>, there is nothing much you can do about that. I am not sure how this script will behave once you have a 100 instances of it running, but my prediction is that it will be bad..</p>\n\n<p>Furtherm... | {
"AcceptedAnswerId": null,
"CommentCount": "4",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-11T02:51:13.357",
"Id": "19486",
"Score": "3",
"Tags": [
"javascript",
"optimization",
"beginner"
],
"Title": "Complex spreadsheet script optimization"
} | 19486 |
<p>My aim is to avoid using threadpool threads for CPU bound work, thus avoiding a situation where IIS stops responding to new requests.</p>
<p><strong>Can you see any problems with the code below?</strong> Is this a safe/clean approach? Can you offer any improvements?</p>
<pre><code> private static ConcurrentQueu... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-11T14:40:53.183",
"Id": "31196",
"Score": "2",
"body": "Er, `ConcurrentQueue<T>` is already thread-safe, I don't think you need to do any locking to access it."
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-1... | [
{
"body": "<p>From my experience ASP.NET is using threadpool threads only for requests.\nWhen creating a new thread inside a request thread will usually* not cause your application pool to run out of resources.</p>\n\n<p>*I say usually as there are cases when a new thread created from inside a request is causin... | {
"AcceptedAnswerId": null,
"CommentCount": "4",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-11T13:27:18.730",
"Id": "19489",
"Score": "4",
"Tags": [
"c#",
".net",
"multithreading",
"asp.net",
"thread-safety"
],
"Title": "Strategy for avoiding threadpool starvation w... | 19489 |
<p>I am building an application that opens Wireshark services (Wireshark has several services) in order to to different things on Wireshark files like edit, change format, statistics, etc. Each option usually uses a different service, so I want to build my classes with inheritance.</p>
<p>I was wondering if what I wan... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-11T15:56:45.913",
"Id": "31199",
"Score": "0",
"body": "You mention that you want to use inheritance but I see no evidence of it. Also, these fields should be properties. You can detect the 32-bit vs the 64-bit once and then build the ... | [
{
"body": "<p>The only thing you are getting out of this inheritance is which directory should be used to access the executable. And you are only using that to define a number of (what should be constant) strings. The fact that you have an error in your value for <code>_capinfos</code> in the seconds case shoul... | {
"AcceptedAnswerId": "19499",
"CommentCount": "7",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-11T15:39:26.677",
"Id": "19493",
"Score": "1",
"Tags": [
"c#",
"error-handling"
],
"Title": "Performing various Wireshark services"
} | 19493 |
<p>I am learning PHP OOP. I am familiar with PHP, and limited use of OOP, but not writing OOP. I have a small successful website that uses large associative arrays but is now suffering performance problems because the arrays soak up memory (My teamarray contains 16k teams of the data below, plus more). I am rewriting t... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-12T19:38:29.457",
"Id": "31295",
"Score": "0",
"body": "Q: Are you just now moving your 16k records into a database? i.e. was it all in memory or a text file or something else previously? Q: Where does `$teamid` come from in the state... | [
{
"body": "<p>Okay, here's a preliminary tidying up of the code, with comments: </p>\n\n<pre><code><?php\n\nclass team {\n # You can stream line the private fields\n private $teamid, $name, $urlname, $mascot, $city, $stateid, $jv;\n $color1, $color2, $address, $zip, $lat, $lng;\n\n # Avoid `globa... | {
"AcceptedAnswerId": "19496",
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-11T15:49:25.457",
"Id": "19494",
"Score": "2",
"Tags": [
"php",
"object-oriented"
],
"Title": "Best Practice: Learning PHP OOP"
} | 19494 |
<p>I currently have a method in my repository that will run SQL and map the reader to objects:</p>
<pre><code>protected IEnumerable<T> Query<T, TFactory>(string sql, List<IDbDataParameter> parameters = null) where TFactory : IFactory<T>
{
List<T> list = new List<T>();
var f... | [] | [
{
"body": "<p>As of code itself it is quite good, the only thing to note is that you can add a <code>new()</code> constraint on <code>TFactory</code> and just use <code>var factory = new TFactory();</code>.</p>\n\n<p>But as with all self-written DB access frameworks I would suggest to switch to mature ORM frame... | {
"AcceptedAnswerId": "19508",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-11T19:57:13.713",
"Id": "19500",
"Score": "3",
"Tags": [
"c#",
"asp.net"
],
"Title": "Running SQL and mapping the reader to objects"
} | 19500 |
<p>I've written a Time class that records the time of day and performs simple timing operations (add 2 times, convert a time object to an integer and back again,etc.) following the prompts in How to Think Like a Computer Scientist: Learning with Python. </p>
<pre><code>class Time(object):
"""Attributes: hours, minutes... | [] | [
{
"body": "<p>It is perfectly fine to implement an <code>__init__</code> method in this case. I think the only thing you should note is, by the way it's defined, the <code>Time</code> class forces the programmer to give values for <code>hours</code>, <code>minutes</code> and <code>seconds</code> to define a <co... | {
"AcceptedAnswerId": "19520",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-11T21:02:52.100",
"Id": "19502",
"Score": "3",
"Tags": [
"python",
"object-oriented"
],
"Title": "Python Time Class definition and methods, use of __init__"
} | 19502 |
<p>I have written two programs to find out a loopy path in a directed graphs.</p>
<p>The first version is a pure functional recursive solution, but its complexity is exponential. The second version following it achieves a linear complexity, but it doesn't look perfect either.</p>
<pre><code> def GetACycle(start: Stri... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-12T14:03:29.360",
"Id": "31265",
"Score": "0",
"body": "`path = path.+:(node)` is very bad. Add values to the front of a list, and reverse it when you are finished."
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2... | [
{
"body": "<p>Here is a solution in OCaml (I don't know Scala).</p>\n\n<pre><code>module M = Map.Make(String)\n\nlet rec loopy (graph: string list M.t) (node: string) (path_from_start: string list) (visited: string list M.t) =\n if M.mem node visited then\n (true, visited, path_from_start, M.find node visit... | {
"AcceptedAnswerId": "19562",
"CommentCount": "3",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-11T21:49:59.810",
"Id": "19504",
"Score": "1",
"Tags": [
"algorithm",
"scala"
],
"Title": "How to break this curse by a pure functional recursive function?"
} | 19504 |
<p>I am new to JavaScript development, so I'm seeking help in guidelines on how I can better organize my code in a better way. This is what I have for a small app I am working on:</p>
<pre><code>var app = app || {};
app.p = function(){ ... };
app.q = function(){ ... };
app.r = function(){ ... };
app.init = function(... | [] | [
{
"body": "<h1>Namespacing</h1>\n\n<blockquote>\n <p>1) Is it good enough that I am splitting my functions and then adding them to a single global object? Any better way?</p>\n</blockquote>\n\n<p>Yes, it's a good and <a href=\"https://stackoverflow.com/a/881556/1888292\">popular</a> approach. It's simple and i... | {
"AcceptedAnswerId": "19579",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-11T22:26:56.433",
"Id": "19507",
"Score": "9",
"Tags": [
"javascript",
"beginner"
],
"Title": "Structure JavaScript code"
} | 19507 |
<p>I have this prime factor generator for some number <code>n</code>. It returns list of all prime factors. In other words, product of the list equals <code>n</code>.</p>
<pre><code>def prime_factors_generate(n):
a = []
prime_list = sorted(primes())
pindex = 0
p = prime_list[pindex]
num = n
whi... | [] | [
{
"body": "<p>The implementation of sundaram's sieve that you've mentioned returns primes in sorted order. So, <code>sorted()</code> is not needed.</p>\n\n<p>Also, it is best to define <code>prime_list = primes()</code> outside the definition of <code>prime_factors_generate</code> (This will reduce calls to pri... | {
"AcceptedAnswerId": "19523",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-11T22:46:03.297",
"Id": "19509",
"Score": "1",
"Tags": [
"python",
"algorithm",
"functional-programming",
"primes"
],
"Title": "Functional prime factor generator"
} | 19509 |
<p>I have a function for finding factors of a number in Python</p>
<pre><code>def factors(n, one=True, itself=False):
def factors_functional():
# factors below sqrt(n)
a = filter(lambda x: n % x == 0,
xrange(2, int(n**0.5)+1))
result = a + map(lambda x: n / x, reversed(a)... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-12T03:46:51.630",
"Id": "31249",
"Score": "0",
"body": "The straight forward way isn't that verbose. I don't see a different way to do it the doesn't sacrifice readability."
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationD... | [
{
"body": "<p>What about this?</p>\n\n<pre><code>[1] * one + result + [n] * itself\n</code></pre>\n",
"comments": [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-13T05:37:14.763",
"Id": "31316",
"Score": "1",
"body": "I was thinking `[1]*int(one) ... | {
"AcceptedAnswerId": "19528",
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-11T23:15:37.983",
"Id": "19510",
"Score": "3",
"Tags": [
"python",
"functional-programming"
],
"Title": "Function to find factors of a number, optionally including 1 and the number it... | 19510 |
<p>I am creating a commercial API for the first time for responsive webpages/web applications (mobile devices).</p>
<p>I am new and, sadly, working alone as well as new to Javascript (long complicated story).</p>
<p>I was just wondering if someone from the industry could offer their professional opinion on the follow... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-12T01:22:58.527",
"Id": "31238",
"Score": "0",
"body": "Is `getSample` supposed to be getting or setting? It seems to do both."
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-12T01:30:16.450",
"Id": "31... | [
{
"body": "<p>From what I understand, you are building an abstraction layer and making your own functions for your API. Here's what you need:</p>\n\n<ol>\n<li><p>A Namespace </p>\n\n<p>Sounds very C++-ish but yes, you do need namespaces in JS. This prevents you from polluting the global namespace and have a cen... | {
"AcceptedAnswerId": "19632",
"CommentCount": "6",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-12T00:50:51.903",
"Id": "19511",
"Score": "2",
"Tags": [
"javascript",
"jquery",
"api"
],
"Title": "Creating JavaScript API for first time; request review"
} | 19511 |
<p>I am looking for advice on making the following code shorter. Any help is appreciated.</p>
<pre><code>public String determineWinner(String winner) {
winner = "";
if (playChoice.equals(compChoice))
winner = "The result is a tie.";
else if (compChoice.equals("R"))
if (playChoice.equals("S") |... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-12T13:59:38.693",
"Id": "31264",
"Score": "1",
"body": "A very simple improvement would be using `String.equalsIgnoreCase()` in order to avoid checking twice for uppper and lower case."
}
] | [
{
"body": "<p>You code looks unclear from point of view of parameter usage and ignoring case difference is case of \"The result is a tie.\", so ...</p>\n\n<p>Try this :).</p>\n\n<pre><code>public class testik {\n // No throws clause here\n public static void main(String[] args) {\n // Paper < Ro... | {
"AcceptedAnswerId": null,
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-12T01:25:36.490",
"Id": "19513",
"Score": "3",
"Tags": [
"java",
"game",
"rock-paper-scissors"
],
"Title": "Making my Rock, Paper, Scissors game shorter"
} | 19513 |
<p>When I have a place with limited space, where I entry a text that can be few letters or huge amount of text because it's dynamic, I'm using this code to make the text fit the specific space.</p>
<pre><code>#region TextSizing
string textSizing = databaseTable.Description;
using (System.Drawing.Graphics g = System.Dr... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-12T12:02:18.610",
"Id": "31261",
"Score": "0",
"body": "Not sure how to put a good title for this question, so **please** feel free to change it so it can be fit more the Q&A format. thanks."
},
{
"ContentLicense": "CC BY-SA 3.... | [
{
"body": "<p>Cache the instance of <code>Graphics</code> and <code>Font</code> objects so that you don't need to create them each time you measure the length of string. I've created a small test to see which part takes most of the time:</p>\n\n<pre><code>private static void Main()\n{\n Stopwatch stopwatch ... | {
"AcceptedAnswerId": "19535",
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-12T12:01:37.283",
"Id": "19526",
"Score": "2",
"Tags": [
"c#",
"performance"
],
"Title": "How can I make this code (that is usually in a loop) faster and efficiently?"
} | 19526 |
<p>I have made a class for generating html form elements:</p>
<pre><code>class input
{
public $tag = 'input', $type = 'text', $customClass, $name;
public function __construct(array $cfg){
if ( isset($cfg['name']) ){
if ( isset($cfg['tag']){
$this->tag = strtolower($cfg['tag']);
}
... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-12T14:13:08.240",
"Id": "31266",
"Score": "0",
"body": "some further information on how the class is used / in which context / which other components you do have regarding configuration etc... would be helpful. AFAIK there are many ver... | [
{
"body": "<p>You could either a <a href=\"https://stackoverflow.com/questions/3394853/builder-pattern-vs-config-object\">configuration object or a builder</a> for this. As you probably can see yourself, using static gets complicated quite fast.</p>\n\n<p><strong>Using a builder</strong>:</p>\n\n<p>\n\n<pre><co... | {
"AcceptedAnswerId": "19534",
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-12T13:17:59.040",
"Id": "19529",
"Score": "1",
"Tags": [
"object-oriented",
"php5",
"static"
],
"Title": "Using static for class default properties"
} | 19529 |
<p>As an example, suppose I had a large amount of data about a set of Restaurants for a large set of Dates in a database that I need to analyze / output to the user.</p>
<p>I have a custom class that holds the data for each restaurant for each date:</p>
<pre><code>Public Class DateData
Public Property Var1 As Dou... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-12T14:38:28.177",
"Id": "31269",
"Score": "0",
"body": "Why do you need a `SortedDictionary`? Other options are: just to sort incoming data by dates and store in array or list; use `SortedList`"
},
{
"ContentLicense": "CC BY-SA... | [
{
"body": "<p>Inheriting from collections is rarely a good practice. In your case it's better to have a private collection (<code>SortedDictionary</code> in your case), and populate it as needed (in other words it's better to use delegation than inheritance here). </p>\n\n<p>The reason why inheriting is not so ... | {
"AcceptedAnswerId": null,
"CommentCount": "3",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-12T13:33:56.460",
"Id": "19530",
"Score": "2",
"Tags": [
".net",
"object-oriented",
"vb.net"
],
"Title": "Inherit SortedDictionary"
} | 19530 |
<p>I came across a situation where I'd like to improve my code, but don't really have a clue about what I could do... It's for a game I'm making, I want to get a perimeter surrounding a unit, but I don't want tiles inside the perimeter. I'm pretty sure there is a better way to do this. (@x and @y are the unit's coordin... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-12T14:55:23.023",
"Id": "31275",
"Score": "0",
"body": "Please a give some assert test (or a couple) in the form \"If I have this @x, @y and r, I want this value in radius\""
},
{
"ContentLicense": "CC BY-SA 3.0",
"Creation... | [
{
"body": "<blockquote>\n<p>Better late than never - Anon</p>\n<p>first make it work, then make it right, and, finally, make it fast. -\nKernighan & Johnson</p>\n<p>Rules of optimization:\n(1) Don't.\n(2) (for experts only) Don't yet.\n(3) Profile before optimizing</p>\n</blockquote>\n<h1>First make it work... | {
"AcceptedAnswerId": null,
"CommentCount": "5",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-12T14:13:09.980",
"Id": "19531",
"Score": "3",
"Tags": [
"performance",
"ruby"
],
"Title": "Irregular perimeter in Ruby code"
} | 19531 |
<p>I need help refactoring this method. It is way too long.</p>
<pre><code> def report_total(feed_event, advisor)
count = 0
advisor.activity_feed_events.each do |lead|
if lead == SignupFeedEvent
count += 1
else
if lead.is_a?(feed_event)
if lead.event_date > (Time.now ... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-12T17:14:16.907",
"Id": "31283",
"Score": "0",
"body": "Is this in a Rails app? Are the different types of `FeedEvent` in a single table (i.e. are you using STI)? A little context would be helpful."
},
{
"ContentLicense": "CC B... | [
{
"body": "<p>You could use scopes to do this at the database level, which would (potentially) be much faster than returning then iterating over all <code>ActivityFeedEvent</code>s.</p>\n\n<pre><code># ActivityFeedEvent\nscope :by_type, ->(type){ where(type: type.to_s) }\nscope :recent, ->{ where(\"event_... | {
"AcceptedAnswerId": null,
"CommentCount": "5",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-12T16:56:05.130",
"Id": "19538",
"Score": "2",
"Tags": [
"ruby",
"ruby-on-rails"
],
"Title": "Simple: Need help refactoring this awkward looking method"
} | 19538 |
<p>I have a mammoth SQL statement, however it's taking a long time to load (27 secs on the server). I think the issue lies with the IN statements towards the bottom (the IN statement is repeated too, can this be stored so it only does it once?) (taking it out shaves 23 seconds, anything under 10 will be brilliant) but ... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-12T18:08:32.740",
"Id": "31291",
"Score": "2",
"body": "Please include execution plan"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-12T18:36:18.293",
"Id": "31293",
"Score": "0",
"body": "Also... | [
{
"body": "<p>Subquery should not be used.Please prefer a join with Table\\view</p>\n\n<pre><code>Select * from ProductDescription pinner join Product PR on P.ProductCode=PR.ProductCode on AND pr.Department > 0 \n AND pr.ApplicationID = @site \n AND ... | {
"AcceptedAnswerId": null,
"CommentCount": "4",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-12T18:01:51.440",
"Id": "19542",
"Score": "5",
"Tags": [
"sql",
"sql-server"
],
"Title": "SQL Query Tuning"
} | 19542 |
<p>I just started learning how to write code in Java. I want to turn the code to OOP with fewer methods, using classes like Sudoku rather than <code>int[][]</code> puzzle.</p>
<pre><code>import java.util.Random;
//Generates a Sudoku puzzle through brute-force
public class SudokuPuzzle
{
public int[][] puzzle = ne... | [] | [
{
"body": "<p>Try to think of different components with different responsibilities, like: </p>\n\n<ul>\n<li>SudokuTable, with basic functionality to store/change the table values</li>\n<li>SudokuGenerator</li>\n<li>SudokuSolver</li>\n<li>SudokuVisualizer</li>\n<li>MatrixUtils</li>\n</ul>\n\n<p>as a later exerci... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-12T18:11:46.487",
"Id": "19544",
"Score": "6",
"Tags": [
"java",
"sudoku"
],
"Title": "Change Sudoku code to OOP"
} | 19544 |
<p>I've just started to practice coding in OOP and just wanted to ask if my code's pattern is correct. I need your comments or suggestions so I can improve it.</p>
<p><strong>sample_class.php:</strong></p>
<pre><code><?php
$host = 'localhost';
$db = 'sample_db';
$user = 'sample_user';
$pass = 'sample_password';
$... | [] | [
{
"body": "<p>Here's a recommendation to bind parameters correctly. This sort of thing is used to prevent against SQL injection attacks.</p>\n\n<pre><code>$query = \"SELECT * FROM table WHERE name = :keyword\"; \n$statement = $this->pdo->prepare($query);\n$statement->bindParam(':keyword', $keyword, PD... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-12T20:11:43.380",
"Id": "19551",
"Score": "3",
"Tags": [
"php",
"object-oriented",
"search",
"form"
],
"Title": "Search form sample pattern"
} | 19551 |
<p>I want to determine which separator is used in a csv file. CSV.foreach will return something like this:</p>
<pre><code>["something1;something2;something3"]
</code></pre>
<p>The code beneath does the trick, but something better must exist. I find it annoying to have the need for sep_count. Do you know of a method t... | [] | [
{
"body": "<p>maybe this?</p>\n\n<pre><code>SEPERATORS = [\";\", \",\"]\n\nCSV.foreach(@file, @config) do |header|\n sep_counts = Hash.new(0)\n header.each_char {|c| sep_counts[c] += 1 if SEPERATORS.include? c }\n @config[:col_sep] = sep_counts.sort { |a,b| a[1] <=> b[1] }.first.first\n break\nend\n</... | {
"AcceptedAnswerId": "19566",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-12T20:46:02.120",
"Id": "19555",
"Score": "3",
"Tags": [
"ruby"
],
"Title": "Look through a string and return the most frequent character (Ruby)"
} | 19555 |
<p>I have a controller. It functions as follows:</p>
<p>A user imports a file and decides whether the files contents contain new entries to be placed into a database, or existing entries entries to be updated.</p>
<p>The user clicks on the import button and is sent to the controller below. If there are any warnings o... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-13T00:19:41.283",
"Id": "31304",
"Score": "1",
"body": "If you're uncomfortable with the method size, just refactor as much of the logic as you can into another class, perhaps your repository class."
},
{
"ContentLicense": "CC ... | [
{
"body": "<p>Maybe something like this? (commentary included):</p>\n\n<pre><code>//Do file locations really need to be saved in the session? I would hope not.\nprotected string TempFileLocation {get; set;}\n//This assumes DatabaseAccessMode is an class of some kind.\nprotected DatabaseAccessMode DatabaseAccess... | {
"AcceptedAnswerId": "19563",
"CommentCount": "11",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-13T00:17:34.900",
"Id": "19559",
"Score": "0",
"Tags": [
"c#",
"mvc",
"controller",
"asp.net-mvc-4"
],
"Title": "How could I make my controller a little less hideous?"
} | 19559 |
<p>I have a process that runs as 32-bits regardless of the architecture. In it, I want to be able to spawn a process from the 64-bit program files menu (e.g. <code>c:\program files</code> instead of <code>c:\program files (x86)\</code>). </p>
<p>I tried using <code>System.Environment.GetFolderPath</code>, but for a ... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-13T03:48:03.297",
"Id": "31311",
"Score": "0",
"body": "Is there a reason why iy needs to be in `\\Program Files\\\\` instead of `\\Program Files (x86)\\\\`?"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-... | [
{
"body": "<p>In the end, I made a slight simplification and ended up with:</p>\n\n<pre><code>System.Environment.ExpandEnvironmentVariables(\"%systemdrive%\\Program Files\\...\")\n</code></pre>\n",
"comments": [],
"meta_data": {
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"C... | {
"AcceptedAnswerId": null,
"CommentCount": "3",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-13T00:45:31.547",
"Id": "19560",
"Score": "7",
"Tags": [
"c#",
".net"
],
"Title": "Create path to file under 64-bit %programfiles% from 32-bit process"
} | 19560 |
<p>I have two functions to split string to array of strings.
Input string is: (1Letter E|V|R) + 8 number, last component of string (1Letter + V) + 8 or less numbers.</p>
<p>I'v got two functions advised to use, both of them are working properly. But I can't decide which one is better to use.</p>
<p>1st:</p>
<pre><c... | [] | [
{
"body": "<p>I would favor the first category approach, as it takes benefit of a great Objective-C feature: extend existing classes with new methods. </p>\n\n<p>On the other hand probably both ways will fail if it comes to more advanced characters, like <a href=\"http://en.wikipedia.org/wiki/UTF-16#Code_points... | {
"AcceptedAnswerId": "19586",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-13T08:46:56.210",
"Id": "19567",
"Score": "3",
"Tags": [
"objective-c",
"cocoa"
],
"Title": "Spliting string to array by number"
} | 19567 |
<p>My purpouse is to select every character which is surrounded by <code>{</code> and <code>}</code>, this is easily achievable using this regexp <code>{\w*}</code>.</p>
<p>I've developed an extenstion method for strings:</p>
<pre><code> public static IEnumerable<string> ObtainTokens(this string originalStri... | [] | [
{
"body": "<p>Modify your regexp: <code>{(\\w*)}</code> and then replace:</p>\n\n<pre><code>yield return Regex.Replace(element.Value, @\"{*}*\", \"\");\n</code></pre>\n\n<p>with</p>\n\n<pre><code>yield return element.Groups[1].Value;\n</code></pre>\n\n<p>ps: full code is avaialbe <a href=\"https://gist.github.c... | {
"AcceptedAnswerId": "19570",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-13T09:34:45.633",
"Id": "19569",
"Score": "1",
"Tags": [
"c#",
"regex"
],
"Title": "RegExp selecting strings surrounded by brackets"
} | 19569 |
<p>I've got dictionary of lists of tuples. Each tuple has timestamp as first element and values in others. Timestamps could be different for other keys:</p>
<pre><code>dict = {
'p2': [(1355150022, 3.82), (1355150088, 4.24), (1355150154, 3.94), (1355150216, 3.87), (1355150287, 6.66)],
'p1': [(13... | [] | [
{
"body": "<p>Have a look at <a href=\"http://pandas.pydata.org/\"><code>pandas</code></a>:</p>\n\n<pre><code>import pandas as pd\n\nd = {\n 'p2': [(1355150022, 3.82), (1355150088, 4.24), (1355150154, 3.94), (1355150216, 3.87), (1355150287, 6.66)],\n 'p1': [(1355150040, 7.9), (1355150110, ... | {
"AcceptedAnswerId": "19574",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-13T07:50:28.937",
"Id": "19572",
"Score": "6",
"Tags": [
"python",
"hash-map"
],
"Title": "Convert dictionary of lists of tuples to list of lists/tuples in python"
} | 19572 |
<p>My company's web application projects are heavily based on user controls. One common case is like we want to reuse the UI visual elements but note the packaged original logic .</p>
<p>I have read some very smelly code in some user controls code behinde. </p>
<pre><code>if(WebConfig.CurrentProject == "xxx")
{
... | [] | [
{
"body": "<blockquote>\n <p>Is there any better elegent ways to solve my problem?</p>\n</blockquote>\n\n<p>You are going down a horrible path messing with all these user controls. ASP.NET MVC is much better for dealing with reusable visual parts, and everything happens on the request level so you don't have ... | {
"AcceptedAnswerId": "19613",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-13T10:17:45.357",
"Id": "19576",
"Score": "4",
"Tags": [
"c#",
"asp.net"
],
"Title": "Reusable user control (ascx) design for multiple web application"
} | 19576 |
<p>I have this code:</p>
<pre><code>Tabzilla.fillZGContacts = function(){
if (!Tabzilla.panel.id) return;
$.ajax({
url: 'zgcontacts.json',
success: function(d){ // "Type","Name","Link","Contact","Location","Icon"
Tabzilla.zgContacts = d;
var countries = [];
d.rows.forEach(f... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-13T13:54:41.517",
"Id": "31329",
"Score": "0",
"body": "Does not belong here, because the code does not work. `var $col; if ($cols.length>0) $col = $cols.pop(); cc.forEach(function(c){ $col.append(countryTemplate(c)); });` This part ca... | [
{
"body": "<p>Use braces <code>{</code> in all your if blocks. It makes it easier to read.</p>\n\n<pre><code>if (byletter[firstletter]){\n byletter[firstletter]++;\n}else{\n byletter[firstletter]=1;\n}\n</code></pre>\n\n<p>You have <code>for</code> loops like : <code>countries.forEach(function(c){</code> ... | {
"AcceptedAnswerId": null,
"CommentCount": "8",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-13T11:16:02.433",
"Id": "19577",
"Score": "1",
"Tags": [
"javascript",
"jquery"
],
"Title": "Optimise JavaScript loop"
} | 19577 |
<p>I am studying graphs and I am trying to make a library in C# for myself and anyone else interested, that is didactic and simple, so I can remember how to solve common problems.</p>
<p>I would like to improve it.<br>
How can I improve, for example, the <code>DFS</code>, complete the <code>Prim's algorithm</code> and... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-14T22:30:32.887",
"Id": "31410",
"Score": "0",
"body": "“I think that it is not quite correct.” What do you mean? Why do you think it's not correct?"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-16T16:34:... | [
{
"body": "<p>You have put too many responsibilities in a single interface and class. Your <code>IGraph</code> interface knows not only about graph itself, but also about all the possible methods of traversing this graph. It breaks <a href=\"http://en.wikipedia.org/wiki/Single_responsibility_principle\" rel=\"n... | {
"AcceptedAnswerId": "19751",
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-13T13:37:58.430",
"Id": "19580",
"Score": "7",
"Tags": [
"c#",
"algorithm",
"breadth-first-search"
],
"Title": "Improve Graph Adjacency List Implementation and Operations"
} | 19580 |
<pre><code>['content', 'answers', 'comments'].each do |t|
break if results.length >= MAX_RESULTS
Question.search_tank('', :conditions => conditions.merge(t => "#{search_term}"), :page => 1, :per_page => 5).each do |q|
next if matched_qs.include? q.id
matched_qs << q.id
r = {:title ... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-13T18:34:04.933",
"Id": "31338",
"Score": "0",
"body": "is there a `results = []` before the loop? are you missing an `end` at the end? is this a helper?"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-13T2... | [
{
"body": "<p>This needs some tweaking, but I hope it helps you identify the main issues to address in your code (IMHO):</p>\n\n<pre><code>objects = [:content, :answers, :comments].flat_map do |type|\n questions = Question.search_tank('', {\n :page => 1, \n :per_page => 5,\n :conditions => co... | {
"AcceptedAnswerId": "19585",
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-13T16:35:55.190",
"Id": "19582",
"Score": "2",
"Tags": [
"ruby",
"ruby-on-rails"
],
"Title": "How to refactor this complicated html generator in Ruby?"
} | 19582 |
<p>I'm wanting to throw an error when reference keys are missing, however instead of failing through a integrity check, I want to list out the missing keys. I've created the below which works. However I'm hoping there is a way to optimise it and reduce the number of lines of code.</p>
<pre><code>DECLARE @NonRefKeys IN... | [] | [
{
"body": "<p>I'm not sure I completely understand your logic, but since this is a stored procedure the temp table isn't visible outside it so you don't need to check if it exists (unless you have other code in the same procedure that might have created it already).</p>\n\n<p>And the most obvious way to reduce ... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-13T16:38:31.317",
"Id": "19583",
"Score": "2",
"Tags": [
"sql",
"sql-server",
"error-handling"
],
"Title": "Throwing a custom error in a stored procedure"
} | 19583 |
<p>This reads a list of points from a file with a certain format:</p>
<blockquote>
<pre><code><number of points>
x1 y1
x2 y2
</code></pre>
</blockquote>
<p>How I can make it better?</p>
<pre><code>from collections import namedtuple
Point = namedtuple('Point ', ['x', 'y'])
def read():
ins = open("PATH_TO_FI... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-14T03:25:21.697",
"Id": "31365",
"Score": "0",
"body": "If you're dealing with points you might want to use a library like Shapely, which will give you a nice Point object."
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationD... | [
{
"body": "<p>You don't need all that <code>expected_length</code> and <code>first=True</code> stuff. You can treat a file as an iterator, and it will return objects until it quits, so you can just use the <code>.next()</code> method and throw item away, or save it to a variable if you wish. In that sense, it... | {
"AcceptedAnswerId": "19602",
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-13T19:52:28.513",
"Id": "19588",
"Score": "3",
"Tags": [
"python",
"file"
],
"Title": "Reading data from file"
} | 19588 |
<p>After experimenting with different design approaches for the past two years, I've created a library for managing and processing dynamic content to be displayed on a single HTML view. This library is <a href="https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md" rel="nofollow">PSR-0</a>, PSR-1 and P... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2015-04-25T04:39:34.083",
"Id": "159065",
"Score": "0",
"body": "I have rolled back your Rev 4. If you would like a review of your new code, please ask a follow-up question. (See [this Meta post](http://meta.codereview.stackexchange.com/q/1763... | [
{
"body": "<p>I'm going to try hard to answer your questions. I'm a little confused by how you're doing this. It seems like you're trying to generate a complicated request from <code>$_GET</code> and instead of just loading up what you need, you've created a complicated structure that loads things up and then u... | {
"AcceptedAnswerId": "19599",
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-13T21:34:55.137",
"Id": "19591",
"Score": "2",
"Tags": [
"php",
"object-oriented",
"dependency-injection"
],
"Title": "Nesting multiple objects vs referencing them for a slot mach... | 19591 |
<blockquote>
<p>The following is a new question based on answers from here: <a href="https://codereview.stackexchange.com/q/7531/3163">Small PHP MVC Template</a> </p>
</blockquote>
<p>I have written a small MVC template library that I would like some critiques on.</p>
<p>The library is <a href="https://github.com/... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-13T23:59:53.853",
"Id": "31363",
"Score": "0",
"body": "Saw this on /r/php...you should read the fig standards wrt things like keyword case, prefixing underscores on properties, brace placement, using \"exit\", etc. At the least please... | [
{
"body": "<p>A few things: </p>\n\n<p>Firstly, It's not MVC. In MVC, controllers do not feed the view data. This is a common misconception but the architecture you're using here is closer to PAC. See <a href=\"http://www.garfieldtech.com/blog/mvc-vs-pac\">http://www.garfieldtech.com/blog/mvc-vs-pac</a> and <a... | {
"AcceptedAnswerId": null,
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-13T21:38:10.270",
"Id": "19592",
"Score": "3",
"Tags": [
"php",
"object-oriented",
"mvc",
"library"
],
"Title": "Small MVC for PHP"
} | 19592 |
<p>I was looking for a way to start elevated (i.e. administrator permissions) and/or invisible processes via batch in Windows. It turns out that this is kind of impossible with batch only, so I googled some vbs stuff and hacked it all together into a neat command line power utility.</p>
<p>The interface is as follows:... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-14T09:18:15.553",
"Id": "31374",
"Score": "1",
"body": "silvinci, please include the relevant code directly in your question as required by the [faq]."
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-15T01:4... | [
{
"body": "<p>The number one improvement you can make is to include <a href=\"http://msdn.microsoft.com/en-us/library/bw9t3484%28v=vs.84%29.aspx\" rel=\"nofollow\"><code>Option Explicit</code></a> as the first line of your script and explicitly declare your variables using <code>Dim</code>. This will save you a... | {
"AcceptedAnswerId": null,
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-14T02:01:09.067",
"Id": "19596",
"Score": "4",
"Tags": [
"optimization",
"vbscript"
],
"Title": "Power utility for starting processes"
} | 19596 |
<p>Developing some industrial WinForms application for some industrial setting, I wanted to provide users of our software with a convenient way to back up the database the software uses (to send it to developer for investigation of any issues that might arise, or just to keep it at hand in case something goes wrong).</... | [] | [
{
"body": "<p>Leaving aside your general design, here are some comments - in no particular order - purely about the code itself:</p>\n\n<ul>\n<li>There are no comments anywhere. This makes the code very difficult to read. You should have enough comments in the code so that someone can read through them quickly ... | {
"AcceptedAnswerId": "22895",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-14T04:00:59.057",
"Id": "19598",
"Score": "4",
"Tags": [
"sql",
"sql-server"
],
"Title": "Backup a database over an SQL connection"
} | 19598 |
<p>I have code that is accessed by multiple threads. If by any chance it has a problem, all accessing threads will wait for recovery. By default, the first thread that fails, will have to recover and all others will have to wait. When the process is complete, everyone leaves the method.</p>
<pre><code>[TestMethod]
... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-14T13:55:05.393",
"Id": "31393",
"Score": "0",
"body": "It's not quite clear what do you want us to review... AutoRecover() looks like just a stub, right? Are you asking to review the test method?"
},
{
"ContentLicense": "CC BY... | [
{
"body": "<p>There is no sense in creating a task and waiting on it straight away. The following code is almost identical to your test method (almost - because tasks are created on a thread pool here):</p>\n\n<pre><code>[TestMethod]\npublic void RecoveryConcurrentTest()\n{\n var tasks = Enumerable.Range(0, ... | {
"AcceptedAnswerId": "19639",
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-14T10:42:59.907",
"Id": "19607",
"Score": "0",
"Tags": [
"c#",
"multithreading",
"concurrency"
],
"Title": "Simplifying and optimize a concurrent access"
} | 19607 |
<p>I wrote this <a href="http://plus.maths.org/content/os/issue50/features/havil/index" rel="nofollow">Sundaram's sieve</a> in <a href="http://coffeescript.org/" rel="nofollow">Coffeescript</a> to quickly generate prime numbers up to <code>limit</code>:</p>
<pre><code>sieve_sundaram = (limit) ->
numbers = (n fo... | [] | [
{
"body": "<p><code>result.shift 2</code> probably doesn't do what you think it does. You meant <code>unshift</code>.</p>\n\n<ul>\n<li><code>shift</code> removes the first element of an array (and doesn't take any arguments).</li>\n<li><code>unshift</code> adds new items at the beginning of the array.</li>\n</u... | {
"AcceptedAnswerId": "22843",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-14T11:12:16.283",
"Id": "19608",
"Score": "3",
"Tags": [
"javascript",
"functional-programming",
"coffeescript",
"primes"
],
"Title": "Functional Sundaram's sieve"
} | 19608 |
<p>I have this code for a website I'm working on. It uses <code>window.scroll</code> events to parallax some items. It's running a bit slow on average machines. Is there any way I could improve it to make it run faster?</p>
<pre><code>$(function() {
// Tell the DOM that JS is enabled
$('html').removeClass('no... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-14T15:48:28.670",
"Id": "31399",
"Score": "2",
"body": "Except for the idiomatic `i` in `for` cycles, I suggest you avoid one-letter variables. `var a = Math.round((((o/2)+b)+m)/2)` is indeed compact, but not very nice to read."
},
... | [
{
"body": "<p>The <a href=\"http://api.jquery.com/on/#event-performance\">jQuery documentation</a> offers the best advice:</p>\n\n<blockquote>\n <p><strong>Event performance</strong></p>\n \n <p>In most cases, an event such as <code>click</code> occurs infrequently and performance is not a significant concer... | {
"AcceptedAnswerId": "19690",
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-14T14:21:29.170",
"Id": "19610",
"Score": "6",
"Tags": [
"javascript",
"performance",
"jquery",
"parallax"
],
"Title": "jQuery parallax site (on scroll)"
} | 19610 |
<p>I have a nested <code>for</code>-loop that populates a list with elements:</p>
<pre><code>a = []
for i in range(1, limit+1):
for j in range(1, limit+1):
p = i + j + (i**2 + j**2)**0.5
if p <= limit:
a.append(p)
</code></pre>
<p>I could refactor it into list comprehension:</p>
<p... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-19T18:56:55.870",
"Id": "31637",
"Score": "0",
"body": "Just pointing out that you can divide limit by 2 in the range function (since you square the numbers anyway) and you'll still get the full set of results. Also, I would write a `... | [
{
"body": "<p>You can rewrite it with two separate comprehensions. One to generate the values and one to filter them out. You can use <a href=\"http://docs.python.org/2/library/itertools.html#itertools.product\" rel=\"nofollow\"><code>itertools.product</code></a> to get the cartesian product.</p>\n\n<pre><cod... | {
"AcceptedAnswerId": "19948",
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-12-14T14:31:14.840",
"Id": "19611",
"Score": "5",
"Tags": [
"python",
"combinatorics"
],
"Title": "Finding perimeters of right triangles with integer-length legs, up to a limit"
} | 19611 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.