body
stringlengths
25
86.7k
comments
list
answers
list
meta_data
dict
question_id
stringlengths
1
6
<p>I have made a carousel with sections and animated background images. How can I make the code better and more reusable? It's now working with a width of only 3 sections.</p> <pre><code>$(function () { var state = 0; var background = $(".selection-carousel .carousel-image"), buttonLeft = $(".selection...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-10-31T08:51:33.730", "Id": "8628", "Score": "1", "body": "In terms of readability, you can move the repeating part of your code to a separate function, and call it." }, { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011...
[ { "body": "<p>Some small tips could be:</p>\n\n<p>Use the jquery plugin strcture for extending it with options, create/destroy/next/prev functions. See: <a href=\"http://www.queness.com/post/112/a-really-simple-jquery-plugin-tutorial\" rel=\"nofollow\">http://www.queness.com/post/112/a-really-simple-jquer...
{ "AcceptedAnswerId": null, "CommentCount": "2", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-10-31T08:48:19.870", "Id": "5712", "Score": "2", "Tags": [ "javascript", "jquery", "performance" ], "Title": "Carousel with sections and animated background images" }
5712
<p>This is my code. I am having a file, whose first line's second word contains a number. Let this number is <code>$u</code>. 2nd line is empty, and from third line, I am given with two numbers separated by a space in each line where let first number is <code>$c</code> and second is <code>$w</code>. Now I need to figur...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-01T03:05:11.063", "Id": "8651", "Score": "9", "body": "Computers are quick, people are slow. Maintenance time is huge when you have to work out what each variable is. Name your variables: $f = $file, $u = $firstLineNumber (or whateve...
[ { "body": "<p>I guess my comment really was an answer in a philosophical way. So here is the answer:</p>\n\n<p>Use names for your variables that have meaning.</p>\n\n<p>There are lots of advantages to naming your variables well. Many people have looked at this question only with a very abstract idea of what y...
{ "AcceptedAnswerId": null, "CommentCount": "5", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-10-31T18:41:33.390", "Id": "5718", "Score": "2", "Tags": [ "php" ], "Title": "Finding the minimum ratio out of many lines of input" }
5718
<p>I am not so much used to OOP designing, So I took one of the very commonly used interview question , "Designing the Elevator System". Below is the prototype.</p> <p>I would like to get some feedback on the design like </p> <ul> <li>Best practices which will have a particular aspect</li> <li>Flexibility in the code...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-01T11:57:35.820", "Id": "8662", "Score": "1", "body": "Before I comment/look at the code. I would like to point out that the `elevator design` interview question is more about design than actual coding. It is designed to see if you und...
[ { "body": "<ul>\n<li><p>Dial pads usually also reflect back the set of requests which are already registered (which makes those buttons light-up). This means there could be way to telling all button pads - once activity happens on outside dial pads as well. </p></li>\n<li><p>Number of requests are always more t...
{ "AcceptedAnswerId": "5733", "CommentCount": "1", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-01T02:59:21.557", "Id": "5723", "Score": "4", "Tags": [ "c++", "object-oriented" ], "Title": "OOP elevator design evaluation" }
5723
<p>TO DO</p> <ul> <li>exception handling</li> <li>merge with dynamic arrary</li> </ul> <p>*SO Bug here--remove this line and Control K will not work</p> <pre><code>/* Added in comments from Jerry, Ed and Loki This code borrows from from http://cslibrary.stanford.edu/ C++ Version - lnode, lnode_reverse, lnode_p...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-03T07:48:00.807", "Id": "8744", "Score": "0", "body": "what's the `break_heap` method for? It sounds malicious and it's public too!" } ]
[ { "body": "<pre><code>/* C++ Version - lnode, lnode_reverse, lnode_push, lnode_print, lnode_count\n I need node_type turned into a templated class..How does one do this?\n*/\ntypedef struct node_type \n { \n int data; \n struct node_type* next; \n } lnode_type;\n</code></pre>\n\n<p>Turning it into a templ...
{ "AcceptedAnswerId": "5727", "CommentCount": "1", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-01T03:11:41.960", "Id": "5724", "Score": "4", "Tags": [ "c++", "c" ], "Title": "Single Linked List - Improvements - 1 - not complete" }
5724
<p>Feel free to critique this database wrapper which is written as a code example for employers or clients.</p> <pre><code>&lt;?php class Database { static function getInstance() { if (self::$instance == NULL) { self::$instance = new Database(); } ...
[]
[ { "body": "<p>I'm going to be harsh, maybe you will disagree with my assessment, or maybe it will help you get a job.</p>\n\n<p>I don't understand why everyone wants to make a singleton database class. Singleton is evil. Now you are limited to a single database object (should we all rejoice?). Interestingly ...
{ "AcceptedAnswerId": "5907", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-01T03:24:41.370", "Id": "5725", "Score": "3", "Tags": [ "php", "singleton" ], "Title": "Singleton wrapper" }
5725
<p>Here are the 3 files that make up my MVC. The two view files containing the HTML are not included.</p> <p>Any advice on how to improve this so it is usable by others.</p> <p>It is meant to be a light-weight alternative to heavier libraries.</p> <p>Thanks!</p> <pre><code>&lt;?php // index.php /* When the use...
[]
[ { "body": "<p>Some random thoughts:</p>\n\n<p>1, You should put your database connection informations (and other settings) to a <code>config.php</code>.</p>\n\n<p>2, Using inheritance to share code generally isn't a good idea: <a href=\"https://stackoverflow.com/questions/3294348/what-are-the-disadvantages-of-u...
{ "AcceptedAnswerId": "5854", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-01T03:59:13.587", "Id": "5729", "Score": "1", "Tags": [ "php" ], "Title": "mvc improvment - 0 - to_do" }
5729
<p>I'm experimenting with moving shapes in JavaScript with a <code>canvas</code> tag.</p> <p>I have one half circle (only stroked) which rotates on set intervals.</p> <p>I've written the code so many more can be added. I can give starting rotations and a lot of other handy parameters.</p> <p>I've tried this with twe...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-01-01T05:40:55.713", "Id": "32072", "Score": "0", "body": "You may be interested to know that your code performs differently between Firefox and Chrome. The statement `context.fillStyle = \"none\";` should instead be `context.fillStyle = ...
[ { "body": "<p>There is no need to recalculate the <code>linearDistEachFrame</code> during each update. You may want to precompute <code>2 * Math.PI</code> as well.</p>\n", "comments": [ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-02T08:01:26.847", "Id": "8701",...
{ "AcceptedAnswerId": null, "CommentCount": "1", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-01T09:53:33.403", "Id": "5731", "Score": "3", "Tags": [ "javascript", "optimization", "performance", "html5" ], "Title": "JavaScript canvas animation slowing down and hogging mem...
5731
<p>I'm an absolute beginner in PHP OOP in search of the "Holy Grail" of connecting to MySQL database once and reusing this connection for the whole site.</p> <p><strong>classes/db.php</strong></p> <pre><code>&lt;?php define('SERVER', 'localhost'); define('USERNAME', 'root'); define('PASSWORD', 'password'); define('DA...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-01T12:57:38.040", "Id": "8664", "Score": "1", "body": "If possible, you should be using the [PDO](http://php.net/manual/en/book.pdo.php) or [mysqli](http://php.net/manual/en/book.mysqli.php) extensions to avoid possible [SQL injections...
[ { "body": "<p>A better practice is passing a created <code>DB</code> instance to the <code>Users</code> class. Imagine that you have a <code>Products</code> class, an <code>Orders</code> class etc. Will all of their constructors create a separate MySQL connection? It doesn't look a good idea since all of them c...
{ "AcceptedAnswerId": "5742", "CommentCount": "1", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-01T11:25:01.753", "Id": "5732", "Score": "5", "Tags": [ "php", "beginner", "object-oriented", "mysql", "constructor" ], "Title": "MySQL database connection in the construct...
5732
<p>For the last few day I tried to figure out how to work with MySQL and PDO. Although I've tried to read a lot about the subject, there are still a lot of things I don't understand.</p> <p>Because of this lack of knowledge, I can't really judge this code (or other example code on-line) on its safety and logic, and th...
[]
[ { "body": "<p>Two issues:</p>\n\n<p>1) I would place the code to open the connection in the constructor. </p>\n\n<p>2) You want to look into parametrized prepared statements.</p>\n\n<p>You want to send a query like:</p>\n\n<pre><code>SELECT * from stock WHERE id = ?\n</code></pre>\n\n<p>Then send an array of pa...
{ "AcceptedAnswerId": "5737", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-10-31T18:00:19.093", "Id": "5735", "Score": "4", "Tags": [ "php", "mysql", "pdo" ], "Title": "PDO (MySQL) connection and query class, safety and logic" }
5735
<p>In building any new application I end up using something similar to this data access type code. I know it well, and it works well. But in the act of wanting to try something new on a new project I thought I could review it again and maybe simplify it more, or move it into an ORM. I've been looking at <a href="http:/...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-04T08:41:04.953", "Id": "8788", "Score": "0", "body": "In this small solution, speed is not going to be an issue. It's easy to get picky when it comes to performance, but in this case it doesnt matter. And Dapper should be one of the f...
[ { "body": "<p>Your headline question is in two parts. </p>\n\n<p>Taking the first part, \"How to simplify\", I'd say that you shouldn't bother trying. All of that effort is already in other frameworks, whether they are small helpers like Dapper or fully-featured ORMs like NHibernate. The world, as you've realis...
{ "AcceptedAnswerId": "5762", "CommentCount": "4", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-01T20:54:40.223", "Id": "5744", "Score": "4", "Tags": [ "c#", "design-patterns" ], "Title": "How to simplify my data access or shift it into an ORM" }
5744
<p>This is primarily a container for quicksort and mergesort:</p> <pre><code>#include "c_arclib.cpp" template &lt;class T&gt; class dynamic_array { private: T* array; T* scratch; public: int size; dynamic_array(int sizein) { size=sizein; array = new T[size](); } void p...
[]
[ { "body": "<p>My first comment is its named badly.</p>\n\n<p>dynamic_array implies that I can use [] operator on it and get a value out.</p>\n\n<p>You have owned RAW pointers in your structure.</p>\n\n<pre><code>private:\n T* array;\n T* scratch;\n</code></pre>\n\n<p>First this means you need to look up RAII ...
{ "AcceptedAnswerId": "5746", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-01T23:20:40.290", "Id": "5745", "Score": "7", "Tags": [ "c++", "array" ], "Title": "Dynamic array container" }
5745
<p><pre> class BencodeObject:</p> <code>def __init__(self,filename): with open(filename,'rb') as diskbytes: self.loads(diskbytes) def _isdictionary(self,bits): return(bits == b"d") def _isnumber(self,bits): return(bits &gt; b"/" and bits &lt; b":") def _iscolon(self,bits): return(bits == b":") def _isint(...
[]
[ { "body": "<pre><code>class BencodeObject:\n</code></pre>\n\n<p>Putting object inside Class names is generally not a good idea, because it doesn't tell me anything. </p>\n\n<pre><code>def __init__(self,filename):\n with open(filename,'rb') as diskbytes:\n self.loads(diskbytes)\n\ndef _isdictionary(sel...
{ "AcceptedAnswerId": null, "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-02T06:55:18.020", "Id": "5751", "Score": "3", "Tags": [ "python" ], "Title": "Bencode “Parser” in Python 3" }
5751
<p>I know that there are many similar questions, but I don't understand most of those questions because I'm not sure if I know what a factory method pattern is.</p> <p>So, after reading many examples over the web, I came up with the following simple classes.</p> <p>Am I doing it correctly? If so...any improvements I ...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-09-09T04:45:10.353", "Id": "49289", "Score": "0", "body": "Now your `create` function returns a string instead of an object. Was this for the sake of simplication? You got the Factory Method right though." } ]
[ { "body": "<p>I think is good implementation but you can reduce the multiple if else conditions\nI don't know the PHP I can write you as I implement in C# code\nin C# there is specific type Dictionary that represent key value pair collection and if in PHP exist some similar you can use it look at the foolwing c...
{ "AcceptedAnswerId": "5756", "CommentCount": "1", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-02T07:36:27.887", "Id": "5752", "Score": "10", "Tags": [ "php", "design-patterns", "factory-method" ], "Title": "Is this correct factory method pattern?" }
5752
<p>In a WPF application I have the following event handler for <code>PreviewTextInput</code> event in <code>ComboBox</code> control. The main purpose of this code is to select an item in <code>ComboBox</code> via pressing a letter key. There is duplicated logic in this code and I want it removed.</p> <pre><code>privat...
[]
[ { "body": "<ol>\n<li><p>I prefer a simple cast over <code>as</code> when I don't expect an object to be of any other type. If you do expect this, use <code>as</code> and be sure to check for <code>null</code>.</p>\n\n<p><code>var comboBox = (ComboBox)sender;</code></p></li>\n<li><p>When it makes sense to group ...
{ "AcceptedAnswerId": "5763", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-02T10:04:53.290", "Id": "5754", "Score": "7", "Tags": [ "c#", "event-handling" ], "Title": "Even handler for text input preview" }
5754
<p>I needed to write a function which would 'error diffuse' floating values to integers:</p> <pre><code>errorDiffuse :: Double -&gt; Int -&gt; Double -&gt; [Int] errorDiffuse _ 0 _ = [] errorDiffuse v num err = tv : errorDiffuse v (num - 1) (err + v - fromIntegral tv) where tv = truncate (v + err) </code></pre...
[]
[ { "body": "<p>First I'd get rid of the dependency on <code>num</code>, since that's just a counter,\nand you can use <code>take num</code> on an infinite list of <code>errorDiffuses</code>:</p>\n\n<pre><code>errorDiffuses :: Double -&gt; Double -&gt; [Int]\nerrorDiffuses v err = tv : errorDiffuses v (err + v - ...
{ "AcceptedAnswerId": "5761", "CommentCount": "0", "ContentLicense": "CC BY-SA 4.0", "CreationDate": "2011-11-02T14:12:46.817", "Id": "5757", "Score": "3", "Tags": [ "haskell" ], "Title": "Haskell function to error-diffuse a floating value to a list of integers" }
5757
<p>I'm learning to Jinja2 and what's important in this case isn't speed but i18n translations and functionality. With Python 2.7, Jinja2 and not Django seems to preferred way to go, so I'm rewriting much of my project to use Jinja2 templates. We already had plenty of translations in .mo and .po files and wanted to keep...
[]
[ { "body": "<p>I don't know jinja but I can give a few generic python pointers:</p>\n\n<ol>\n<li>Generally, its considered best to put all imports at the top of the file not scattered throughout</li>\n<li>Rather then importing a bunch of stuff to use once, use <code>self.gettext = translation.gettext</code> etc<...
{ "AcceptedAnswerId": "5769", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-02T19:48:00.347", "Id": "5767", "Score": "3", "Tags": [ "python", "python-2.x", "django", "google-app-engine", "i18n" ], "Title": "i18n translator using Jinja2" }
5767
<p>I'm creating a design database for an autoparts store (this is a project school). For the moment, I'm not sure if the relationship model especially in <code>MODEL</code>, <code>MAKE</code>, <code>BRAND</code> is correct (I really have no experience about this area).</p> <p>I'm not an expert in this area, so I need ...
[]
[ { "body": "<p>You have all the tables you need and you have the many-to-many relationships correct, I think. </p>\n\n<p>I'd suggest that you add a Parent field to Category and then create a hierarchy of categories instead of having a Category table with a single Subcategory table.</p>\n\n<pre><code>create table...
{ "AcceptedAnswerId": null, "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-02T21:05:27.143", "Id": "5770", "Score": "3", "Tags": [ "sql", "sql-server" ], "Title": "Relationship design for auto parts store database" }
5770
<p><a href="https://i.stack.imgur.com/Y5AZJ.png" rel="nofollow noreferrer">This is the result of the code it has extra option tags no idea why.</a></p> <p>I have no idea why It produces the extra option box. </p> <p>Below is the result of the query. Correct number of rows correct response...</p> <pre><code> Pare...
[]
[ { "body": "<p>You should close the <code>option</code> tag. Change</p>\n\n<pre><code>$opt7.=\"&lt;option value='$var'&gt;$m $name&lt;option&gt;\";\n</code></pre>\n\n<p>to</p>\n\n<pre><code>$opt7.=\"&lt;option value='$var'&gt;$m $name&lt;/option&gt;\";\n</code></pre>\n", "comments": [ { "Conten...
{ "AcceptedAnswerId": "5772", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-02T21:35:19.077", "Id": "5771", "Score": "1", "Tags": [ "php", "javascript", "mysql", "html" ], "Title": "<select> has extra blank <option>'s and I have no idea why. please hel...
5771
<p><code>CustomDialog</code> is the parent abstract class for <code>OkExclamationDialog</code> and <code>ExclamationDialog</code> with abstract method <code>displayDialog()</code> being overriden.</p> <p>Does this fall into a design pattern? Can this design be improved upon?</p> <p><code>JudgeButton</code> controls ...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-02T23:57:04.150", "Id": "8738", "Score": "0", "body": "What happened to your code? Did you not grab all of it when you cut and pasted it?" }, { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-03T00:03:50.403", ...
[ { "body": "<p>The <code>initialise</code> reminds me a factory but it's not exactly a factory. It's fine if you don't use the same if-elseif structure in your code elsewhere. If you do pull out it to a factory class.</p>\n\n<p>Just some idea:</p>\n\n<p>1, Consider using an <code>enum</code> instead of the integ...
{ "AcceptedAnswerId": "5789", "CommentCount": "4", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-02T23:45:35.183", "Id": "5775", "Score": "6", "Tags": [ "java", "design-patterns" ], "Title": "Does this code fall into a design pattern?" }
5775
<p>I wrote this without much thought:</p> <pre><code>(defn- tail-cmd "Construct a tail cmd" [file n &amp; ignore-patterns] (clojure.string/join " | " (flatten [(format "tail -n %s -f %s" (or n 200) file) (map #(format "grep --line-buffered -v \"%s\"" %) ignore-patterns)]))) <...
[]
[ { "body": "<p>You may or may not consider this simpler:</p>\n\n<pre><code>(defn tail-cmd-2\n \"Construct a tail cmd\"\n [file n &amp; ignore-patterns]\n (str \"cat \"\n file\n \" | \"\n (apply str (map #(format \"grep -v \\\"%s\\\" | \" %) ignore-patterns))\n \"tail -f \"\n (if ...
{ "AcceptedAnswerId": null, "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-03T03:58:35.517", "Id": "5777", "Score": "5", "Tags": [ "strings", "clojure" ], "Title": "Constructing a tail cmd" }
5777
<p>It's my first stab at TCP/IP and honestly I'm just starting out on C# also, so any comments on the design, method, etc are more than welcome.</p> <p>Basically I wanted to create a listening thread and someday a sending thread, that can handle listen/send heartbeat message and also other messages.</p> <p>Once it re...
[]
[ { "body": "<p>A few things come to mind when reading this...</p>\n\n<ul>\n<li>Set your thread priority. Remember that spawned threads do not follow the priority of the host process. So you need to decide what level this should be relative to other priorities</li>\n<li>This is going to be a foreground thread, is...
{ "AcceptedAnswerId": null, "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-03T07:04:11.357", "Id": "5778", "Score": "2", "Tags": [ "c#", "beginner", "multithreading", "networking", "tcp" ], "Title": "TCP/IP sending and receiving threads" }
5778
<p>I have fair concept in a programming (learner) but not an expert to refactor code at the highest level. I am trying to read a huge (100MB-2GB) XML file and parse necessary element (attributes) from a file into MySQLdb. The code is working perfectly as I have tested in small file sizes. But, unfortunately when I got ...
[]
[ { "body": "<pre><code>import os, sys\nimport stat\nimport getpass\nimport MySQLdb\nfrom lxml import etree\nimport datetime, time\nimport dbconfig as config\n</code></pre>\n\n<p>They python style guide recommends one import per line. I'd also probably group things together i.e. standard library imports then 3rd ...
{ "AcceptedAnswerId": null, "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-03T14:39:05.263", "Id": "5782", "Score": "3", "Tags": [ "python", "parsing", "xml", "python-2.x", "io" ], "Title": "Reading a large XML file and parsing necessary elements in...
5782
<p>How does this look for basic image rotation? What is there to be improved here?</p> <pre><code>&lt;p&gt; &lt;img src='images/rotation/dsc_0052.jpg' id='rotateImg0' alt='image rotation' /&gt; &lt;img src='images/rotation/bilde1.jpg' id='rotateImg1' style='display:none;' alt='image rotation' /&gt; &lt;img...
[]
[ { "body": "<p>Three things I noticed:</p>\n\n<p><strong>1. Looking through the document with DOM methods is a slow process.</strong> There's often no way around it, and it won't crash anything here, but try to minimize it when you can. From page 183 of Stoyan Stefanov's <em>JavaScript Patterns</em>:</p>\n\n<blo...
{ "AcceptedAnswerId": "5827", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-03T15:58:20.580", "Id": "5784", "Score": "4", "Tags": [ "javascript" ], "Title": "JavaScript Code for image rotation on a web page" }
5784
<p>I am starting to learn Ruby. </p> <p>This is pretty much the first thing I have coded. It's very simple; it's a prime number calculator. Since this is my first Ruby code, I would like some review on the following:</p> <ol> <li>Adherence to Ruby standards</li> <li>Is it done in the Ruby way (the way a Rubyist would...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-27T19:35:03.323", "Id": "9838", "Score": "0", "body": "first advice: 2-space indentation." } ]
[ { "body": "<p>Minor things. I will leave your actual questions to proper &quot;Rubyists&quot;. :)</p>\n<h2>defensive programming</h2>\n<p>I would change the first condition to</p>\n<pre><code>return false if number &lt;= 1\n</code></pre>\n<p>0 is not a prime number, and negative numbers aren't either.<br />\nMa...
{ "AcceptedAnswerId": "5798", "CommentCount": "1", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-04T01:40:07.870", "Id": "5792", "Score": "4", "Tags": [ "ruby", "primes" ], "Title": "Prime number calculator" }
5792
<p>I'm looking for the most concise regex that matches one or two 4-digit years in any the following setups:</p> <ul> <li>year </li> <li>year-</li> <li>-year</li> <li>year-year</li> </ul> <p>I can't think of anything slicker than this:</p> <pre><code>[\\-]?\d{4}|\d{4}\[\\-](\d{4})? </code></pre>
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-04T01:49:48.250", "Id": "8780", "Score": "0", "body": "Do you really need the \"\\\" before the \"-\"?" }, { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-04T02:08:48.330", "Id": "8781", "Score": "0", ...
[ { "body": "<p>If you first removed all \"-\" characters you could make it</p>\n\n<pre><code>(\\d{4}){1,2}\n</code></pre>\n\n<p>or</p>\n\n<pre><code>(\\d{4}|\\d{8})\n</code></pre>\n", "comments": [ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-04T01:33:44.033", "I...
{ "AcceptedAnswerId": "5875", "CommentCount": "4", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-04T00:18:48.470", "Id": "5794", "Score": "2", "Tags": [ "regex" ], "Title": "Best Regular Expression for a one or two values optionally divided by a certain character" }
5794
<p>What can I do better?</p> <pre><code>$(function () { "use strict"; /* * Header text. Vergroot de tekst door op lees meer te klikken */ var heading = $(".header .text h2").height(), firstP = $(".header p:first").height(), areaSmall = heading + firstP, areaBig = $(".hea...
[]
[ { "body": "<p>I am not sure of what you want to be better but, for better code legibility and maintainability I would say that you should replace all your string constants with variables:</p>\n\n<pre><code>var headerClassId = \".header\";\nvar textClassId = \".text\";\nvar aTextValueThatMeansSomething = \"easeI...
{ "AcceptedAnswerId": null, "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-04T12:22:33.603", "Id": "5802", "Score": "3", "Tags": [ "javascript", "jquery" ], "Title": "Increasing visible text with on-click \"read more\"" }
5802
<p>I have been developing Javascript for few months, and, as a former Java developer, needed a simple way to perform class inheritance.</p> <p>My needs are:</p> <ul> <li>having private members not accessible (encapsulation),</li> <li>to make inheritance simple,</li> <li>to avoid 'new' keywords bugs,</li> <li>to let t...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-07T11:15:35.457", "Id": "8856", "Score": "0", "body": "I do not agree: I do not try to do like Java, I try to do OOP. This means encapsulation and encapsulation is not possible without private attributes." }, { "ContentLicense"...
[ { "body": "<p>It's generally a bad idea to try writing code in one language as if it were a different language. </p>\n\n<p>Don't fight your tool; use it as it was designed. JavaScript has prototype-based OO rather than class-based OO, so use it like that.</p>\n\n<p>Read \"Javascript - the Good Parts\" and learn...
{ "AcceptedAnswerId": "5850", "CommentCount": "16", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-04T13:43:40.187", "Id": "5806", "Score": "7", "Tags": [ "javascript", "extension-methods" ], "Title": "Javascript inheritance: is my solution correct?" }
5806
<p>I want to review the URL routing for my appengine webapp:</p> <pre><code>routes = [ (r'/', CyberFazeHandler), (r'/vi/(eyes|mouth|nose)', CyberFazeHandler), (r'/realtime', RealtimeHandler), (r'/task/refresh-user/(.*)', RefreshUserHandler), ('/ai', FileUploadFormHandler), ...
[]
[ { "body": "<p>Have you considered using <a href=\"http://webapp-improved.appspot.com/api/webapp2.html#webapp2.Route\">named Route templates</a> instead of capturing regular expressions? It could make the code more readable. Consider</p>\n\n<pre><code>Route(\"/task/refresh-user/&lt;username&gt;\", RefreshUserHan...
{ "AcceptedAnswerId": "5914", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-05T04:36:22.323", "Id": "5817", "Score": "6", "Tags": [ "python", "regex", "google-app-engine" ], "Title": "Regexes for Google App Engine" }
5817
<p>I have the following code which works but seems like it could be improved. I'm not sure if I'm using <code>event.stopPropagation()</code> correctly or if it is the best solution.</p> <p>The elements are loaded via AJAX and so need to have <code>.on()</code> (previously <code>.live()</code>, which is now deprecated ...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-05T19:17:43.010", "Id": "8813", "Score": "0", "body": "Actually, you are not using `on` instead of `live`, but instead of `delegate`." }, { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-06T14:34:45.630", "I...
[ { "body": "<p>It did work in my Chrome. However, I felt your code was confusing with all those handlers. Let me suggest the following:</p>\n\n<pre><code>$('html').on('click', '.poster', function(e) {\n\n var target = $(e.target);\n var poster = $(this); \n\n if (!poster.hasClass('active')) {\n closeQ...
{ "AcceptedAnswerId": "5878", "CommentCount": "2", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-05T17:13:55.067", "Id": "5821", "Score": "3", "Tags": [ "javascript", "jquery", "html", "html5" ], "Title": "Using jQuery 1.7 .on() and event.stopPropagation to close boxes whe...
5821
<p>I have multiple form validation checks around my site so I decided to write them as functions to simplify the process of creating the validation pages, here is a snippet for an understanding of what I'm doing.</p> <pre><code>// Declare regex variables $regex_email = "/^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z.]{2...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-13T18:32:56.790", "Id": "9267", "Score": "0", "body": "Javascript in your backlinks. Just give them a class and hook the javascript into that class, so that if you ever want to change the behavior, you can change it in the js instead ...
[ { "body": "<p>This might not be what you are looking for but I recently wrote this code to validate my forms.</p>\n\n<p>As you might notice I define the data to be validated in the $validate array. Then some loops run through them and show the proper errors. If no errors are found, it sends the form.</p>\n\n<p>...
{ "AcceptedAnswerId": "5841", "CommentCount": "1", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-06T11:34:31.247", "Id": "5830", "Score": "3", "Tags": [ "php", "array", "form" ], "Title": "PHP Validation with functions and list()" }
5830
<p>This is a switch statement in JavaScript. I have a feeling that it can be done in a shorter way.</p> <pre><code>switch(n.length){ case 1: n = '00000' + n; break; case 2: n = '0000' + n; break; case 3: n = '000' + n; break; case 4: n = '00' + n; break; case 5: n = '0' + n; bre...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-06T20:00:28.267", "Id": "8838", "Score": "0", "body": "I'm curious what this is being used for. It's pretty non-general." }, { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-06T23:57:59.993", "Id": "8845", ...
[ { "body": "<pre><code>function foobar(n)\n{\n var zeroes = \"000000\";\n return zeroes.substr(n.length) + n.toString();\n}\n</code></pre>\n", "comments": [ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-06T12:42:38.127", "Id": "8820", "Score": "0", ...
{ "AcceptedAnswerId": "5833", "CommentCount": "2", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-06T11:58:14.513", "Id": "5831", "Score": "8", "Tags": [ "javascript", "formatting" ], "Title": "Zero-padding a number to have six digits" }
5831
<p>How can I write this shorter?</p> <pre><code>$('#pietra_svg #p1_4 polygon#p1').click(function() { $('#apla_p1').fadeIn("slow"); return false; }); $('#pietra_svg #p1_4 polygon#p2').click(function() { $('#apla_p2').fadeIn("slow"); return false; }); $('#pietra_svg #p1_4 polygon#p3').click(function() { $('#ap...
[]
[ { "body": "<p>You can use an array in a similar way here. Just leave the last one, as it doesn't fit the same pattern.</p>\n\n<pre><code>$.each(\n [\n { poly: 'p1', id: '#apla_p1' },\n { poly: 'p2', id: '#apla_p2' },\n { poly: 'p3', id: '#apla_p3' },\n { poly: 'p4', id: '#apla_p4' },\n { poly: '...
{ "AcceptedAnswerId": "5852", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-07T13:58:38.320", "Id": "5848", "Score": "4", "Tags": [ "javascript", "jquery", "event-handling" ], "Title": "Click handlers for SVG polygons" }
5848
<p>This is a sample exercise tutorial that would be used to test understanding of how simple classes communicate/message and are designed.</p> <p>Tester class</p> <pre><code>public class GuessingGameTester { public static void main(String[] args){ //create a new game GuessingGame g1 = new Guessin...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-07T18:04:45.767", "Id": "8895", "Score": "1", "body": "So is this for a tutorial you're writing and you want to make sure new programmers should understand it?" }, { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-1...
[ { "body": "<p>A couple observations:</p>\n\n<ol>\n<li>If none of them guess the number before the number of rounds runs out, you will print that p3 guessed in round 101.</li>\n<li>If more than one guessed correctly in the last round, you'll only credit one of them.</li>\n<li>I see no reason to keep instance var...
{ "AcceptedAnswerId": "5859", "CommentCount": "2", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-07T17:23:01.230", "Id": "5853", "Score": "3", "Tags": [ "java", "number-guessing-game" ], "Title": "Three-player number-guessing game" }
5853
<p>This is my first attempt ever to build a <code>Vector2</code> class. I scoured the net for anything that might make this class more efficient but know I'm to the point where I'm ready to share. This way I can get advice on how I can improve it further. I wanted to do this before I make my <code>Vector3</code> and ...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-07T22:08:23.630", "Id": "8903", "Score": "0", "body": "Cross product is meaningless in the context that you have created it in for 2d vectors. If you are going for the orthogonal vector, it is simply \"(x, y) -> (y, -x)\"" }, { ...
[ { "body": "<p>Cross product is meaningless in the context that you have created it in for 2d vectors. If you are going for the orthogonal vector, it is simply \"(x, y) -> (y, -x)\".</p>\n\n<p>Your \"Normal\" function is actually returning the orthogonal vector. \"Normal\" is vectorland means \"Vector of unit le...
{ "AcceptedAnswerId": null, "CommentCount": "4", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-07T21:42:09.950", "Id": "5856", "Score": "6", "Tags": [ "c++", "computational-geometry" ], "Title": "Mathematical Vector2 class implementation" }
5856
<p>Should take an array such as <code>['dog', 'cat', 'bird', 'monkey']</code> and return <code>'dog, cat, bird and monkey'</code>.</p> <p>Looking for a more elegant solution.</p> <pre><code>def self.english_join(array) return nil if array.nil? return array[0] if array.length == 1 return array[0..-2].join(', ')...
[]
[ { "body": "<pre><code>def self.english_join(array = nil)\n return array.to_s if array.nil? or array.length &lt;= 1\n array[-1] = \"and #{array[-1]}\"\n array.join(', ')\nend\n</code></pre>\n\n<p>A different way but not much better. Returns an empty string instead of <code>nil</code> if the array is empty. So...
{ "AcceptedAnswerId": "5890", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-07T22:49:42.143", "Id": "5863", "Score": "15", "Tags": [ "ruby" ], "Title": "Ruby function to join array with commas and a conjunction" }
5863
<p>After taking into account answers for my questions <a href="https://stackoverflow.com/questions/4522931/is-this-xor-based-encryption-function-safe">here</a> and <a href="https://stackoverflow.com/questions/4587863/is-this-encryption-function-safe">here</a> I created (well may-be) improved version of my wrapper. The ...
[ { "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-01-04T15:10:36.660", "Id": "8908", "Score": "0", "body": "http://stackoverflow.com/questions/401656/secure-hash-and-salt-for-php-passwords Please read this" }, { "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-01-04T15...
[ { "body": "<p>Your XOR-step is nonsense, it doesn't add any security.</p>\n\n<p>The reason is the way the CTR mode works: It encrypts a counter (thus the name) using the key (and the IV as starting point), and XORs the plaintext with the result.\nXORing the plaintext with some other constant <em>before encrypti...
{ "AcceptedAnswerId": null, "CommentCount": "5", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-01-04T14:13:25.683", "Id": "5864", "Score": "6", "Tags": [ "php", "security", "cryptography", "aes" ], "Title": "AES encryption wrapper" }
5864
<p>I learned here that it is unsafe to design encryption algorithms from scratch. Given that advice, I made a pair of encryption functions based on mcrypt:</p> <pre><code>function aes128ctr_en($data,$key,$hmac = false) { $key = ($hmac===false) ? hash('sha256',$key,true) : hash_hmac('sha256',$key,$hmac,true); $...
[ { "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-01-03T20:05:45.773", "Id": "8913", "Score": "0", "body": "are you encrypting something to be placed into a database?" }, { "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-01-03T20:08:26.320", "Id": "8914", "Sco...
[ { "body": "<p>If by safe you mean that you are not deviating from standardized encryption procedures which are thought to be secure, then yes you are safe.</p>\n\n<p>If by safe you mean that no one can ever crack this, then the answer is no. Given enough time and advances in computing power or cryptanalysis wil...
{ "AcceptedAnswerId": "5869", "CommentCount": "6", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-01-03T20:01:40.360", "Id": "5865", "Score": "21", "Tags": [ "php", "security", "cryptography", "aes" ], "Title": "Encryption functions based on mcrypt" }
5865
<p>I am having trouble with the random number generator. For week 0 only, I want to set the starting number of roaches in House A (<code>totalRoachesInHouseA</code>) to 97 and the number of roaches in House B (<code>totalRoachesInHouseB</code>) to 79. Then, for weeks 1 to 10, I want to generate random numbers for both ...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-08T04:39:28.210", "Id": "8949", "Score": "0", "body": "You want random numbers that equal a specific set of numbers? That does not sound random but rather like an [oxymoron](http://en.wikipedia.org/wiki/Oxymoron)." }, { "Conten...
[ { "body": "<p>Don't use global variables.</p>\n<pre><code>int roachesInHouseA, roachesInHouseB; //my two houses.\nint temporaryHouseA, temporaryHouseB;\ndouble totalRoachesInHouseA, totalRoachesInHouseB; //the total count of houses. \nint week;\n</code></pre>\n<p>It is better to use local variables and pass the...
{ "AcceptedAnswerId": null, "CommentCount": "2", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-08T04:16:13.403", "Id": "5879", "Score": "4", "Tags": [ "c++", "homework", "random" ], "Title": "Program with random number generation" }
5879
<p>Windows console windows do unfortunately not support stream I/O of international characters. For instance, in Windows 7, you can still do "chcp 65001" (sets the active code page to UTF-8), type "more", and get a crash.</p> <p>This means that it's practically impossible for a novice to write a "Hello, world!" progra...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-08T14:19:25.037", "Id": "8965", "Score": "0", "body": "I don't agree that the natural internal encoding for string in *nix is UTF-8. I would argue that internally the strings are UTF-32 (normally). **BUT** when you stream them (to eith...
[ { "body": "<p>Since the code is supposed to work for Unix the pragma is a bad idea.</p>\n\n<pre><code>#pragma once\n</code></pre>\n\n<p>Prefer to use normal include guards.</p>\n\n<p>You are imbuing streams here:</p>\n\n<pre><code> std::locale const utf8Locale( stream.getloc(), new CodecUtf8() );\n ...
{ "AcceptedAnswerId": null, "CommentCount": "8", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-08T13:52:18.690", "Id": "5889", "Score": "8", "Tags": [ "c++", "macros", "unicode" ], "Title": "Source code level portable C++ Unicode literals" }
5889
<pre><code>//parses the text path vector into the engine void Level::PopulatePathVectors(string pathTable) { // Read the file line by line. ifstream myFile(pathTable); for (unsigned int i = 0; i &lt; nodes.size(); i++) { pathLookupVectors.push_back(vector&lt;vector&lt;int&gt;&gt;())...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-08T14:11:39.553", "Id": "8962", "Score": "0", "body": "what is the type of `nodes`, it seems like, if nothing else, you could at least evaluate that once outside the double loop. I don't suspect that is the culprit, just mentioning it....
[ { "body": "<p>I setup nodes.size() to return 744.<br>\nThis then forces all 553536 to be read from the file.</p>\n\n<p>When I run your code it completes in 3.8 seconds (with -O3 it takes 2.5 seconds).</p>\n\n<p>If I update the code to reserve the appropriate amount of space in each vector we can reduce the time...
{ "AcceptedAnswerId": "5895", "CommentCount": "3", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-08T14:02:50.533", "Id": "5892", "Score": "7", "Tags": [ "c++", "c++11" ], "Title": "I/O function takes far too long" }
5892
<p>I was wondering if you could review the following code I've written as a polyfill for the placeholder attribute in HTML5?</p> <p>The object of the polyfill is to replicate the functionality of the placeholder attribute. In effect, the polyfill is supposed to do the following:</p> <ol> <li>Default text to be shown....
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-08T16:57:37.443", "Id": "8983", "Score": "0", "body": "[fixed](http://jsfiddle.net/HWKKa/)" }, { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-08T17:02:47.163", "Id": "8985", "Score": "0", "body": "...
[ { "body": "<p><strong>Note:</strong> Regarding the OP's comment on the \"nature of IE7 mobile\", I suspect it has more to do with jQuery's implementation of <code>focusin</code> and <code>focusout</code>. This may not be the case, but I have written my review with that assumption in mind.</p>\n\n<p>There are a ...
{ "AcceptedAnswerId": null, "CommentCount": "8", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-08T16:37:36.147", "Id": "5899", "Score": "3", "Tags": [ "javascript", "jquery", "html5" ], "Title": "Javascript Placeholder Polyfill" }
5899
<p>There are two keys: the main is aes128 and the second is XTEA, used just for randomization of data inside the AES. Randomization is done with secret random key that is not know even if someone knows exactly what is encrypted. Also the key is produced by doing hash many number of times and this hash is salted with th...
[ { "ContentLicense": "CC BY-SA 2.5", "CreationDate": "2011-01-05T21:55:18.883", "Id": "9061", "Score": "3", "body": "I hope this is the last of your \"I wrote some algorithm and don't know exactly how it works, but tell me if it is secure\" questions. At the very least you could have used test da...
[ { "body": "<p>The basic answer is: No</p>\n\n<p>Where is your random seed being generated from? (Oh wait, you are not using a random see - seriously big issue #1) Where it is being stored? What cryptographic symmetric ciphers are being used to generate your seed.</p>\n\n<p>You expect the method you are using to...
{ "AcceptedAnswerId": null, "CommentCount": "10", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-01-05T21:26:20.237", "Id": "5915", "Score": "13", "Tags": [ "php", "security", "cryptography", "aes" ], "Title": "PHP mcrypt AES encryption wrapper" }
5915
<p>I am developing a multilingual site, so i needed some sort of a function to detect user locale, as well as not to scare away search engines, so I wrote this kind of a method to detect locale. The priority is as following:</p> <ol> <li><p>We look if user has active language in $_SESSION;</p></li> <li><p>If not, we ...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-09T16:53:44.940", "Id": "9082", "Score": "1", "body": "Can you check if the locale is available/valid? If yes then I'd check `$lang` and only return if it's accepted and otherwise fall through to the subsequent tests. I'd also move the...
[ { "body": "<p>Your basic hierarchy seems fairly solid. There is just one thing - HTTP_ACCEPT_LANGUAGE is not so simple. I wrote an answer on it <a href=\"https://stackoverflow.com/questions/7466208/php-or-htaccess-rewrite-url-by-accept-language/7494297#7494297\">here</a>. What follows is the relevant code fr...
{ "AcceptedAnswerId": "5941", "CommentCount": "1", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-09T13:34:00.463", "Id": "5918", "Score": "4", "Tags": [ "php" ], "Title": "Detect locale with PHP" }
5918
<p>In this case I have an array that is parsed from a JSON file. Normally I use all the elements in the array, but for <a href="http://code.google.com/web/ajaxcrawling/">AJAX crawling</a> I only show the parts the search engine asked for:</p> <pre><code>&lt;?php $fitstyles = json_decode(file_get_contents(DATA_FILE), t...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-09T17:05:53.253", "Id": "9083", "Score": "1", "body": "IMO it would be more clear if you inverted the logic and built the resulting array instead of modifying an existing one (you can just assign `$fitstyles` with the resulting array i...
[ { "body": "<p>Your code is just fine, exactly how I would do it. </p>\n\n<p>There isn't much to discuss really, it's kind of simple. It's as effective / efficient as it could be, and fairly readable. </p>\n", "comments": [], "meta_data": { "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0...
{ "AcceptedAnswerId": "7368", "CommentCount": "3", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-09T15:28:42.350", "Id": "5919", "Score": "5", "Tags": [ "php", "array" ], "Title": "How should I efficiently delete elements from a PHP array?" }
5919
<p>I have the following code as part of an <code>sqlCommand</code> which is calling a stored procedure:</p> <pre><code>Dim groupObj If groupId = 0 Then groupObj = DBNull.Value Else groupObj = groupId End If Dim siteObj If siteId = 0 Then siteObj = DBNull.Value Else siteObj = siteId End If sqlComman...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-09T23:04:41.213", "Id": "9099", "Score": "2", "body": "You should probably use nullable types for the inputs." }, { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2012-01-03T20:00:07.673", "Id": "11641", "Score"...
[ { "body": "<p>You could define an extension method like this (I'm not a VB programmer so please excuse the syntax errors):</p>\n\n<pre><code>&lt;Extension()&gt; \nPublic Sub OrDBNull(ByVal val As Decimal)\n If val = 0\n return DBNull.Value\n End If\n\n return val ;\nEnd Sub\n</code></pre>\n\n<p>...
{ "AcceptedAnswerId": null, "CommentCount": "5", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-09T15:51:15.287", "Id": "5920", "Score": "2", "Tags": [ "sql", ".net", "asp.net", "vb.net" ], "Title": "Calling stored procedure with a sqlCommand" }
5920
<p>Does anyone know of a better / cleaner way to write the following:</p> <pre><code>GetSafeTagName(txtUserInput.text); public static string GetSafeTagName(string tag) { tag = tag.ToUpper() .Replace("'","`") .Replace('"','`') .Replace("&amp;", "and") .Replace(",",":") .Replace(@"\","/"); //Do ...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-09T18:54:04.213", "Id": "9086", "Score": "0", "body": "[Here](http://stackoverflow.com/questions/1321331/replace-multiple-string-elements-in-c-sharp) is a SO question where the answer recommends using a `StringBuilder`." }, { "...
[ { "body": "<p>When I had to do something similar, I used a <code>Dictionary&lt;string, string&gt;</code> to define the replacements.</p>\n\n<p>And then something like this to replace:</p>\n\n<pre><code>foreach( KeyValuePair&lt;string, string&gt; pair in replacements)\n{\n str = str.Replace(pair.Key, pair.Val...
{ "AcceptedAnswerId": null, "CommentCount": "3", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-09T17:51:03.453", "Id": "5924", "Score": "4", "Tags": [ "c#", "regex" ], "Title": "Regular Expression replace specific characters" }
5924
<p>This is a simple form validation script.</p> <p>I'd like to:</p> <ul> <li>improve the jQuery validation</li> <li>simplify the jQuery code</li> </ul> <p>Questions:</p> <ol> <li>Should I be exporting pure JS validation to avoid potential conflicts with other libraries that users might have installed?</li> <li>Is i...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-09T19:39:23.130", "Id": "9088", "Score": "1", "body": "You need a cross browser solution. jQuery is one of them." }, { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-09T19:42:01.300", "Id": "9089", "Scor...
[ { "body": "<p>To avoid conflict with any other libraries, wrap your code using the jQuery function, and additionally call jQuery.noConflict();</p>\n\n<p>The only conflict you may have is if somebody else imported an object named jQuery into the global namespace.</p>\n\n<p>Reference: <a href=\"http://docs.jquery...
{ "AcceptedAnswerId": "5929", "CommentCount": "9", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-09T19:24:21.480", "Id": "5927", "Score": "5", "Tags": [ "javascript", "jquery", "validation" ], "Title": "Simple form validation script" }
5927
<p>I've created a small method to test the connection to a database:</p> <pre><code>public bool TestConnection(string provider, string serverName, string initialCatalog, string userId, string password, bool integratedSecurity) { var canConnect = false; var connectionString = integratedSecurity ? string.Format...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-09T22:57:55.433", "Id": "9094", "Score": "1", "body": "You should use `OleDbConnectionStringBuilder`" }, { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-09T22:58:18.087", "Id": "9095", "Score": "0", ...
[ { "body": "<p>It's a little verbose.</p>\n\n<p>I would write</p>\n\n<pre><code>try {\n using(var connection = new OleDbConnection(...)) {\n connection.Open();\n return true;\n }\n} catch {\n return false;\n}\n</code></pre>\n\n<p>You should also use <code>OleDbConnectionStringBuilder</code...
{ "AcceptedAnswerId": null, "CommentCount": "6", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-09T22:54:38.487", "Id": "5930", "Score": "13", "Tags": [ "c#", ".net" ], "Title": "Test connection to database C#" }
5930
<p>I'm working on a small project with dBase files from a 3rd party. Realizing that most of the code to fetch tables and form objects is the same, I made this function:</p> <pre><code>public static IEnumerable&lt;T&gt; OleDbFetch&lt;T&gt;(Func&lt;OleDbDataReader, T&gt; formator, string connectionString, string query) ...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-10T02:48:55.637", "Id": "9106", "Score": "0", "body": "I guess I should secure the connection... Make sure it's open." } ]
[ { "body": "<p><code>Dispose()</code> of types that implement <code>IDisposable</code> with <code>using</code> blocks:</p>\n\n<pre><code>using (var conn = new OleDbConnection(connectionString))\n{\n conn.Open();\n using (var cmd = new OleDbCommand(query, conn))\n using (var reader = cmd.ExecuteReader())...
{ "AcceptedAnswerId": "5940", "CommentCount": "1", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-10T01:58:01.503", "Id": "5938", "Score": "5", "Tags": [ "c#", "linq", "database" ], "Title": "OLEDB table fetching function in C#" }
5938
<p>EDIT - 0 - Still need to fix markup language to make more unique so escaping is not needed:</p> <p>This will be a JSON / JQuery Alternative.</p> <pre><code>/******************** group:ajax ********************/ /* - ajax_object() - creates a browser dependent ajax_object for the ajax method call - good place t...
[]
[ { "body": "<ul>\n<li><p>If you're just passing the IDs of elements, rather than the elements themselves, your variable names should reflect that. (Yes, it's possible to pass DOM elements around -- they're just objects, as far as JS cares. And if you passed the elements instead of their IDs, the code to do stu...
{ "AcceptedAnswerId": "5947", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-10T03:00:10.023", "Id": "5939", "Score": "3", "Tags": [ "javascript" ], "Title": "group - ajax - 0 - to_do - consistent access, styling" }
5939
<p>I am working on a fifteen puzzle, and just got it working finally. My next step is to implement an Iterative Deepening search on the puzzle to solve it.</p> <p>I assume you go column by column, row by row with a loop until the checkwin function is complete? I would really love any pointers or guidance that can be g...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2012-01-09T14:24:51.220", "Id": "11962", "Score": "0", "body": "Can you factorize a bit the code duplicated for Test Mode and Normal Mode ?" } ]
[ { "body": "<p>I see that your using <code>system(\"PAUSE\")</code> it works all fine and dandy but behind the scenes it creates some extra overhead that could be avoided using standard c++ calls</p>\n\n<p>read this article for more info on it...</p>\n\n<p><a href=\"http://www.gidnetwork.com/b-61.html\" rel=\"no...
{ "AcceptedAnswerId": null, "CommentCount": "1", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-09T02:18:41.940", "Id": "5942", "Score": "4", "Tags": [ "c++" ], "Title": "Iterative Deepening and A*" }
5942
<p>I just started working in a project that wasn't developed by me, I'm worried about some of the functionalities and I would like some suggestions.</p> <p>After the login validations, the login procedure is made like this:</p> <pre><code>var _ticket = new FormsAuthenticationTicket(1, user.ID, DateTime.Now, DateTime....
[]
[ { "body": "<p>I think you can simply create a method (or even a property) in your master page, and use it in your pages to follow and adhere to the principal of DRY (Do not repeat yourself). Also in master page function (or property), you can use caching by a hidden class field, to make the performance even bet...
{ "AcceptedAnswerId": null, "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-10T13:50:38.797", "Id": "5944", "Score": "3", "Tags": [ "c#", ".net", "asp.net" ], "Title": "C# asp.net project functionalities" }
5944
<p>First of all: I am a total javascript beginner and therefore I am asking you to rate my script and tell me whether its okay or just a big mess. Note: it does work, but I guess it could be improved.</p> <p>My main goal was to create a script, than can be used multiple times and does not depend on any class or id nam...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-10T16:21:42.600", "Id": "9125", "Score": "1", "body": "Before I get started reviewing, have you [linted](http://www.jshint.com/) your code?" }, { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-10T16:39:41.627", ...
[ { "body": "<p>OK, this code looks fine to me. The only thing I can suggest is that you might prefer to use <code>event.preventDefault</code> instead of <code>return false</code> in your event handlers, because in jQuery, <code>return false</code> in an event handler causes both event.preventDefault() <em>and</e...
{ "AcceptedAnswerId": "5951", "CommentCount": "13", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-10T15:58:04.500", "Id": "5946", "Score": "2", "Tags": [ "javascript", "performance", "beginner", "jquery" ], "Title": "jQuery script to toggle an element and handle a Close bu...
5946
<p>I was wondering if there was a more efficient way of taking strings that represent dates with the pattern being <code>mmddyy</code> or <code>mmd yy</code> or <code>m d yy</code> and converting them into a DateTime object?</p> <p><code>111110</code> would be <code>11 11 10</code></p> <p><code>111 10</code> would be...
[]
[ { "body": "<p>Here's a one-liner for you:</p>\n\n<pre><code> private DateTime convertToDateFromString(string dateString)\n {\n DateTime bufferDate;\n\n return DateTime.TryParseExact(\n dateString,\n new[] { \"MMddyy\", \"MMd yy\", \"M d yy\" },\n CultureInfo....
{ "AcceptedAnswerId": "5953", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-10T21:24:20.707", "Id": "5952", "Score": "3", "Tags": [ "c#" ], "Title": "optimizing conversion between string and date" }
5952
<p>How can I improve this?</p> <p><a href="https://github.com/harbhag/Python-Scripts/blob/master/backup_script.py" rel="nofollow">GitHub</a> </p> <pre><code>import os, platform, logging, logging.handlers, glob from time import * import tarfile import zipfile, zlib class backup: source_s = '' destination_d = ...
[]
[ { "body": "<pre><code>import os, platform, logging, logging.handlers, glob\nfrom time import *\nimport tarfile\nimport zipfile, zlib\n\nclass backup:\n</code></pre>\n\n<p>The python style guide recommends CamelCase for class names</p>\n\n<pre><code> source_s = ''\n destination_d = ''\n</code></pre>\n\n<p>...
{ "AcceptedAnswerId": "5961", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-11T06:43:14.700", "Id": "5956", "Score": "2", "Tags": [ "python", "file-system" ], "Title": "Simple interactive backup script" }
5956
<p><strong>Topic 1: Switching to another form from the project startup form</strong></p> <p>I use the following code that calls an instance of my form for payment (<code>frmPayment</code>) from my startup form (<code>frmMainMenu</code>) using a click event:</p> <pre><code>Private Sub btnPayment_Click(ByVal sender As ...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-10T16:42:37.783", "Id": "9170", "Score": "0", "body": "The best practice is the one that works. What doesn't work?" } ]
[ { "body": "<p>Firstly, I think you need to clarify why you are wanting to show the payment form and hide the main form. In most applications, when you want to show a dialog of some description, you would show it modally (using <code>dialog.ShowDialog()</code>) so that it appears over the top of your current for...
{ "AcceptedAnswerId": "5960", "CommentCount": "1", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-10T15:17:20.480", "Id": "5957", "Score": "3", "Tags": [ "vb.net", "winforms" ], "Title": "Switching back and forth between forms" }
5957
<p>The following is the producer-consumer algorithm for a piece of software that I'm upgrading to take advantage of multi-core processing. The intended platform is some flavor of Linux running on an <a href="http://aws.amazon.com/ec2/instance-types/" rel="nofollow">EC2 HPC cc4x.large cluster</a>, which feature 2 x Inte...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-14T20:49:31.180", "Id": "9300", "Score": "1", "body": "http://stackoverflow.com/questions/4682889/is-floating-point-ever-ok/4682941#4682941" }, { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-15T12:20:27.227", ...
[ { "body": "<h1>Style</h1>\n\n<p>I see something that looks like a <em>thread safe queue</em> abstraction, which is mysteriously implemented as a collection of globals and one argument. These are a fairly well-known subspecies of container, so write one (or find an existing one that suits your purposes)</p>\n\n<...
{ "AcceptedAnswerId": "6036", "CommentCount": "2", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-11T14:56:26.803", "Id": "5963", "Score": "8", "Tags": [ "c++", "performance", "multithreading", "pthreads" ], "Title": "Multithreading algorithm for high performance parallel c...
5963
<p>I'm in the middle of writing my own little bullet shooter game much in the style of Touhou. Everything works fine so far but I'm disliking certain aspects of my code. Take this function for showing and unshowing the title screen:</p> <pre><code> private function runIntro() : void { if (introFadeInTit...
[]
[ { "body": "<p>Start with simple re factoring. </p>\n\n<p>1) Create a class called stateclass with variables introFadeInTitle_ , aliceIntroTxt , introFadeInBtns_ , startBtn etc. Set the variables of this class with the values </p>\n\n<p>2) Instead of checking and setting properties in runIntro , hide the comple...
{ "AcceptedAnswerId": "5971", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-11T15:10:27.740", "Id": "5964", "Score": "4", "Tags": [ "actionscript-3", "state", "actionscript" ], "Title": "Using Booleans to Manage State" }
5964
<p>Examples of where I've started migrating to short-circuit evaluation:</p> <p><strong>PHP</strong></p> <pre><code>$id=0;//initialized in case of no result $r=mysql_query(SQL); if($r &amp;&amp; mysql_num_rows($r)&gt;0){ list($id)=mysql_fetch_row($r); } </code></pre> <p>becomes</p> <pre><code>$id=0; $r=mysql_quer...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2015-05-10T20:08:50.337", "Id": "163802", "Score": "0", "body": "This question may have been acceptable at the time it was asked, but it does not meet Code Review's question standards in 2015. Since you've asked about two isolated excerpts in ...
[ { "body": "<p>This is an abuse of short-circuit evaluation. It's not immediately obvious what the intent is. Sure, i can <em>eventually</em> see that you're getting an ID, calling <code>Req.abort()</code>, whatever. But i shouldn't have to decipher a line of code first to figure out what it does.\nConcisenes...
{ "AcceptedAnswerId": "5982", "CommentCount": "1", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-11T23:59:29.447", "Id": "5976", "Score": "6", "Tags": [ "javascript", "php" ], "Title": "Relying on short-circuit evaluation instead of using the IF control structure" }
5976
<p>This is a function which produces the sum of primes beneath (not including) a certain number (using a variant on the <a href="http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes" rel="nofollow">sieve of Eratosthenes</a>). </p> <pre><code>erastosum = lambda x: sum([i for i in xrange(2, x) if i == 2 or i == 3 or reduc...
[]
[ { "body": "<p>As much as I love anonymous functions, they can be a nightmare to debug. Splitting this code up piece wise (into an actual function or otherwise) shouldn't and wouldn't decrease it's performance while improving maintenance and portability for you later on.</p>\n\n<p><a href=\"http://www.rfc1149.ne...
{ "AcceptedAnswerId": "5981", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-12T00:09:50.663", "Id": "5977", "Score": "1", "Tags": [ "python", "optimization", "lambda" ], "Title": "Excessive use of lambda with variant of Sieve of Eratosthenes?" }
5977
<p>I'm using this C# function to generate random coupons for a system. How can I improve it?</p> <pre><code>public static string GenerateCoupon(int length) { string result = string.Empty; Random random = new Random((int)DateTime.Now.Ticks); List&lt;string&gt; characters = new List&lt;string&gt;() { }; ...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-16T23:47:15.430", "Id": "9450", "Score": "0", "body": "I think in general you probably ought to avoid using _both_ upper and lower case letters. Especially if you're including numbers... What font are you outputting these in - how do ...
[ { "body": "<p>Some general idea, I hope all work in C# too. Feel free to edit the answer if it is not a proper C# syntax.</p>\n\n<p>1, Change the type of the <code>characters</code> list to <code>char</code> and change the loop variable to <code>char</code> too. This way you don't have to cast and the <code>for...
{ "AcceptedAnswerId": null, "CommentCount": "9", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-12T09:37:18.580", "Id": "5983", "Score": "32", "Tags": [ "c#", "strings", "random" ], "Title": "Random string generation" }
5983
<p>The index file:</p> <pre><code>&lt;?php require_once 'app/run.php'; $_action = (isset($_GET['cmd']) &amp;&amp; ctype_alnum($_GET['cmd'])) ? $_GET['cmd'] : 'index'; switch ($_action) { case 'index': case 'default': case 'home': include $con_dir . '/index.php'; break; default: i...
[]
[ { "body": "<p>You code looks fine. One minor issue: in the <code>catch(PDOException $e)</code> branch maybe you want to</p>\n\n<ul>\n<li>log the error (<a href=\"http://php.net/manual/en/function.error-log.php\" rel=\"nofollow\"><code>error_log</code></a>),</li>\n<li>show a fancy static error page to the user (...
{ "AcceptedAnswerId": "5992", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-12T17:09:39.597", "Id": "5987", "Score": "2", "Tags": [ "php", "security" ], "Title": "Is my approach structure good?" }
5987
<p>I am maintaining a site with significant security concerns and I wrote a helper class for validating potential XSS attacks. The <code>ValidateRequest</code> method is meant to evaluate the <code>HttpRequest</code> for any potential issues, and if issues are found, redirect the user to the same page, but in a away t...
[]
[ { "body": "<p>Some general idea:</p>\n\n<ol>\n<li>Use <a href=\"http://www.codinghorror.com/blog/2006/01/flattening-arrow-code.html\">guard statements</a> instead of big <code>if</code> blocks.</li>\n<li>Extract the <code>!string.IsNullOrEmpty(query) &amp;&amp; PotentialXssAttackRegex.IsMatch(query)</code> cond...
{ "AcceptedAnswerId": "5993", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-12T18:21:38.490", "Id": "5988", "Score": "3", "Tags": [ "c#", "asp.net", "security", "regex" ], "Title": "Evaluate/Refactor my ASP.NET XSS Security Helper Class" }
5988
<p>I'm just playing with the concept of <code>GroupBy</code> inside Rx. So, I wondered how hard it would be to write a console application that continuously reads lines, groups them by similar words and just prints out the word among the current count of how often the word was written before. It really was a breeze to ...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-12T17:58:10.313", "Id": "9217", "Score": "0", "body": "Can anyone who donvoted this question please leave a comment what's wrong with it? I'm willing to fix it!" }, { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-...
[ { "body": "<p>Ok, this looks a lot better! Can anyone even do better than that?</p>\n\n<pre><code> static void Main(string[] args)\n {\n var subject = new Subject&lt;string&gt;();\n\n subject\n .GroupBy(x =&gt; x)\n .SelectMany(x =&gt; x.Scan(new { Chars = string.Empty,...
{ "AcceptedAnswerId": null, "CommentCount": "6", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-12T17:32:59.680", "Id": "5989", "Score": "6", "Tags": [ "c#", "system.reactive" ], "Title": "Is there an easier way to use Rx's GroupBy operator?" }
5989
<p>I was given a problem to find out all the prime numbers within a range. Just after I wrote the program I found numerous other codes to solve the problem. I am interested to know is my algorithm efficient? Is it like sieve filtration? (before writing this code I had no clear idea on sieve filtration method) or the al...
[]
[ { "body": "<p>Your code is not correct, hence its efficiency is irrelevant. You only test for divisibility by 2, 3, 5 and 7, so you would classify e.g. 121 = 11*11 as prime.</p>\n", "comments": [ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-13T10:08:40.720", "Id...
{ "AcceptedAnswerId": "6017", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-12T23:14:40.863", "Id": "5996", "Score": "6", "Tags": [ "c++", "algorithm", "primes" ], "Title": "Generating prime numbers within a range in C++" }
5996
<p>I am a newbie at writing Android apps and using Java in general. I have went through most of the Android hello view tutorials but still seem to be lacking some understanding of the basics. Here is a snippet of code I wrote for an app. My goal was to have three edittext boxes for the user to input information. I w...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-11T15:32:21.150", "Id": "9236", "Score": "1", "body": "One suggestion... use the camel-case to name your functions/variables as you did in you class, though I only put in capital the first character on classes." }, { "ContentLi...
[ { "body": "<ul>\n<li>Replace \"43560*144\" by a constant.</li>\n<li>Store <code>new Double(sS), new Double(srS), new Double(pop)</code> in variables\nbefore <code>if (checkifempty(boxes) &lt;2)</code>. It'll improve speed and clarify\nthe code.</li>\n<li>Correct indentation in the <code>onCreate</code> method</...
{ "AcceptedAnswerId": "6002", "CommentCount": "2", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-11T15:24:03.917", "Id": "5998", "Score": "4", "Tags": [ "java", "android" ], "Title": "Three edittext boxes: suggestions to clean up code" }
5998
<p>I am currently writing a disassembler for Don Knuth's <a href="http://en.wikipedia.org/wiki/MMIX" rel="nofollow">MMIX</a>. The module in this <a href="https://gist.github.com/1346119" rel="nofollow">gist</a> is a pretty-printer for single instructions that reads in instructions from an <code>char*</code> buffer and ...
[]
[ { "body": "<pre><code>/* OP_X_Y_SZ, */\n/* OP_X_Y_Z, */\n</code></pre>\n\n<p>Commented out code is suspicious, it should probably just be removed. </p>\n\n<pre><code>#define IS_SIMPLE_ARGF(x) ((x) &lt;= OP_XYZ)\n</code></pre>\n\n<p>This is odd in the middle of an enum</p>\n\n<pre><code>case OP_SZ: printf(opf...
{ "AcceptedAnswerId": "6020", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-08T16:19:50.583", "Id": "6006", "Score": "10", "Tags": [ "c", "beginner" ], "Title": "Pretty printer for MMIX-assembly" }
6006
<p>Here is a recursive function that I wrote to search a file-system to a child depth defined by $depth.</p> <p>Could it be optimized more?</p> <pre><code>function stringSearch($working_dir,&amp;$results_array,$depth) { global $search_string; global $dir_count; global $file_count; global $working_url;...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-12T19:17:10.443", "Id": "9257", "Score": "2", "body": "Should look into DirectoryIterator (http://us2.php.net/directoryiterator), may help clean up your code a bit." }, { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2...
[ { "body": "<p>I answered this moments ago, but same applies</p>\n\n<p><a href=\"https://stackoverflow.com/questions/8108175/php-usleep-to-prevent-script-timeout/8108229#8108229\">https://stackoverflow.com/questions/8108175/php-usleep-to-prevent-script-timeout/8108229#8108229</a></p>\n\n<p>If you simply use : <c...
{ "AcceptedAnswerId": null, "CommentCount": "4", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-12T19:11:02.103", "Id": "6007", "Score": "5", "Tags": [ "php", "recursion", "file-system" ], "Title": "PHP - Optimizing Recursive Method" }
6007
<p>I would love to hear some comments on this small script, which (I hope) reduces repetitive tasks such as page refreshing, compilation and so on to minimum.</p> <pre><code># -*- coding: utf-8 -*- # Author: Jacek 'Sabr' Karolak (j.karolak@sabr.pl) # Watch [files] for change, and after every change run given [command]...
[]
[ { "body": "<pre><code>class Loop(object):\n</code></pre>\n\n<p>Loop isn't a very descriptive name for this object. Something like WatchDog or FileWatcher would make more sense</p>\n\n<pre><code> def __init__(self, cmd):\n self._command = cmd\n self._files_to_watch = {}\n</code></pre>\n\n<p>This...
{ "AcceptedAnswerId": "6014", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-13T16:10:49.490", "Id": "6010", "Score": "2", "Tags": [ "python", "file" ], "Title": "Running a given command after each change in a file" }
6010
<p>I'm a systems programmer teaching myself javascript. Prototypal inheritance is completely new to me, so I could use some best practices advice here.</p> <p>I made a simple game (breakout) for my first practice project. Here is a simple example of a game object. </p> <pre><code>function GameObject(sprite) { ...
[]
[ { "body": "<p>There hasn't been any standard for how to define getters and setters in Javascript until recently, so the implementations have various custom syntaxes, and not much <a href=\"http://robertnyman.com/javascript/javascript-getters-setters.html\" rel=\"nofollow\">seems to work in Internet Explorer</a>...
{ "AcceptedAnswerId": "6026", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-13T19:31:21.540", "Id": "6015", "Score": "3", "Tags": [ "javascript", "classes", "prototypal-class-design" ], "Title": "\"Class\" design in javascript" }
6015
<p>Just started to clean up an old code base. I'm interested in your perspective and ways that you would change the following function.</p> <pre><code> private void MarkSelectedRow() { Object obj = Bankkonto1.Value; if ((obj != null) &amp;&amp; (myBankkonten.Contains(obj.ToString()))) { ...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-14T13:15:21.600", "Id": "9282", "Score": "5", "body": "To be honest, I don't find this function that messy. I am completely unfamiliar with the code-base (obviously) yet with 30 seconds reading I can figure out what the function is doi...
[ { "body": "<p>I would consider inverting the first if to make it simpler and quicker to be aware of the else.</p>\n\n<hr>\n\n<p>I strongly suggest <em>always</em> leaving something inside a catch - if you have nothing else to put there at least leave a comment.</p>\n\n<hr>\n\n<p>I would change</p>\n\n<pre><code...
{ "AcceptedAnswerId": "6028", "CommentCount": "1", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-14T10:21:24.420", "Id": "6027", "Score": "4", "Tags": [ "c#" ], "Title": "C# Messy Function - Refactoring" }
6027
<p>If the current page URL has an argument 'myid1' or 'myid2' in the querystring, for each link in my webpage with class 'rewrite', I want the link href's querystring to be replaced by the current page URL's querystring. I'm using the code given below. Since I'm new to javascript, I'm not sure if its optimized. I want ...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-14T14:18:50.010", "Id": "9285", "Score": "0", "body": "Optimization is easy, don't use jQuery." }, { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-14T14:53:01.107", "Id": "9288", "Score": "0", "body...
[ { "body": "<h2>The code you provide is inefficient in two ways:</h2>\n\n<ol>\n<li><p><strong>Unnecessary loops</strong>. It loops through <code>a.rewrite</code> anchor each time for one querystring match. It could be optimized into one loop;</p></li>\n<li><p><strong>Repeated calculation</strong> on regular expr...
{ "AcceptedAnswerId": "6052", "CommentCount": "3", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-14T13:18:39.497", "Id": "6029", "Score": "2", "Tags": [ "javascript", "performance", "jquery", "url" ], "Title": "Query string substitutions for links on a page" }
6029
<p>For each of my various websites, I back up the web site files and the associated database for into a single dated compressed archive file (one zip file per website) and download that to my local Windows Vista machine. My websites are hosted on Unix machines accessible via ssh. I've decided to use Python <a href="h...
[]
[ { "body": "<p>The names are chosen well. The code is clear. Obviously it is for a specialized case so there are many constants in the code - that is OK. It is strictly procedural, which is a good thing - OOP has a tendency to just become boilerplate for small things like these (it would be useful if you intende...
{ "AcceptedAnswerId": null, "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-14T16:12:30.000", "Id": "6032", "Score": "4", "Tags": [ "python", "network-file-transfer" ], "Title": "Automatically backup website files and database" }
6032
<p>I have been working with AJAX and JSON for quite some years now, but to be honest I still do not know exactly what all the functionality are, and how to create optimized code for jQuery.</p> <p>So my first question: <strong>Does anyone know a good tutorial, or better even a cheatsheet where are the functionalities ...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-14T20:57:13.700", "Id": "9301", "Score": "0", "body": "I assume `$async` is `jQuery`" }, { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-14T21:46:31.820", "Id": "9304", "Score": "0", "body": "Oh oep...
[ { "body": "<p>I don't think that I can give reasons why this is better (i.e. faster). Only I feel that it is more readable and scalable.</p>\n\n<p>I like to use the data attributes for information like ids. For a long time, classes were the way to go but we had to deal with cluttered classes :(. Data attribu...
{ "AcceptedAnswerId": "6051", "CommentCount": "2", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-14T16:14:51.023", "Id": "6033", "Score": "3", "Tags": [ "javascript", "jquery", "html", "ajax", "json" ], "Title": "How to exactly work with the JSON Object and jQuery, add...
6033
<p>I've written some code to read a column of data from a dataset called <code>AltIds</code>. The values are nullable. My code is written to find all the values in the column that are not null and write them to an array.</p> <pre><code>Guid[] altIds = myDataSet.Tables[0].AsEnumerable() .Select(row =&gt; row.Field&...
[]
[ { "body": "<p>What you have is fine. You could restructure it to eliminate one of the selects, but then you would repeat yourself with the row. </p>\n\n<pre><code>var altIds = myDataSet.Tables[0].AsEnumerable()\n .Where(row =&gt; row.Field&lt;Guid?&gt;(\"AltId\").HasValue)\n .Sel...
{ "AcceptedAnswerId": null, "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-14T17:10:17.640", "Id": "6034", "Score": "1", "Tags": [ "c#", ".net", "database" ], "Title": "Querying .NET DataSet and ignore rows that contain NULL" }
6034
<p>I have a function that is being called in a tight loop. I've profiled my code and this is where my largest bottleneck is. The function is fairly simple: it checks if a point in (x,y) form is above a line in (slope, intercept) form. </p> <p>The problem is, it also has to deal with the case where the slope is positiv...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-14T22:36:30.263", "Id": "9312", "Score": "0", "body": "If you really want speedups, you should look at using numpy." } ]
[ { "body": "<p>It would be faster to use the <em><a href=\"http://docs.python.org/library/math.html#math.isinf\">math.isinf()</a></em> function:</p>\n\n<pre><code>&gt;&gt;&gt; from math import e, pi, isinf\n&gt;&gt;&gt; s = [0, float('inf'), e, pi]\n&gt;&gt;&gt; map(isinf, s)\n[False, True, False, False]\n</code...
{ "AcceptedAnswerId": "6041", "CommentCount": "1", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-14T21:41:21.713", "Id": "6040", "Score": "14", "Tags": [ "python", "performance", "coordinate-system" ], "Title": "Comparison to infinity when checking if a point is above a line" ...
6040
<h3>What I'm doing</h3> <p>Using the v3 of the <a href="http://www.wufoo.com/docs/api/v3/entries/get/" rel="nofollow">Wufoo API</a> to retrieve all entries and store in a single array ($all_results). This is necessary because of the limit 100 entries limit that Wufoo has when retrieving results from their api. Also st...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-15T07:45:21.000", "Id": "9334", "Score": "1", "body": "Would you consider turning your code into a class? It's ok if you don't..." }, { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-15T20:54:54.490", "Id": ...
[ { "body": "<p>Just some quick note:</p>\n\n<p>1, I would separate the configuration settings to a distinct php file (and use a <code>WufooConfig</code> class/struct) and include it at the beginning of the code. It makes the configuration easier to change. If you use a <code>WufooConfig</code> class/struct and p...
{ "AcceptedAnswerId": "6065", "CommentCount": "2", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-15T00:45:34.820", "Id": "6049", "Score": "4", "Tags": [ "php", "api" ], "Title": "Store all entries in a single array from Wufoo api" }
6049
<p>There are 28 check boxes that are given a common name and have different values:</p> <pre class="lang-html prettyprint-override"><code>&lt;input type="checkbox" name="Schedules" id="checkbox1" value="1"&gt; </code></pre> <p>In controller in post action:</p> <pre class="lang-cs prettyprint-override"><code>publ...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-15T05:46:38.393", "Id": "9337", "Score": "0", "body": "Are all the `if` conditions trying to set the `Timeschedule` property? Or are they all setting different properties on your view model? Also, do you have any control over how the v...
[ { "body": "<p>It seems to me that what you really need to do is change the view code. For example, if you do this:</p>\n\n<pre><code>&lt;input type=\"checkbox\" name=\"Timeschedule\" id=\"checkbox1\" value=\"OneTime\"&gt;\n&lt;input type=\"checkbox\" name=\"Timeschedule\" id=\"checkbox2\" value=\"Weekly\"&gt;\n...
{ "AcceptedAnswerId": "6056", "CommentCount": "5", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-15T05:41:13.233", "Id": "6054", "Score": "2", "Tags": [ "c#", "asp.net-mvc-3" ], "Title": "Creating 28 checkboxes with a common name and different values" }
6054
<p>Any suggestions on how to make this code more efficient?</p> <pre><code>import java.util.Scanner; public class RecursionLargestInArray { public static void main (String[] args) { int max = -999; Scanner scan = new Scanner (System.in); System.out.print("Enter the size of the array: "); int arr...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-15T15:25:22.133", "Id": "9342", "Score": "0", "body": "Does it need to use recursion? In case this is homework, please tag it as such." }, { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-15T16:15:55.113", "...
[ { "body": "<p>This is a situation where pointers in C would be useful (or tail recursion in a functional language), but we'll work with what we have. Since this looks like homework, I'll try to guide you to a solution instead of just giving it. </p>\n\n<p>Creating a new array for each recursion is terribly ine...
{ "AcceptedAnswerId": null, "CommentCount": "5", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-15T15:02:24.620", "Id": "6057", "Score": "6", "Tags": [ "java", "array", "recursion", "homework" ], "Title": "Getting the largest element in an array using recursion" }
6057
<p>The below code converts the window.location.search querystring into a JavaScript object. In particular, if a key occurs more than once the resulting value in the object will be an array. Please feel free to comment on both readability and speed of execution. Thanks!</p> <pre><code>window.location.search.split(/[?&a...
[]
[ { "body": "<p>The line <code>if (typeof prev[key] === typeof []) {</code> is... weird. If you're checking for a type specifically, check for it: <code>if (typeof prev[key] === 'object') {</code> - the way you're doing it there causes a new array literal to be instantiated needlessly, as well as a superfluous <c...
{ "AcceptedAnswerId": "6063", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-15T17:06:33.840", "Id": "6062", "Score": "2", "Tags": [ "javascript", "optimization", "array" ], "Title": "Javascript querystring to object conversion" }
6062
<p>I have some troubles to find the right loop to check if some values are contained in mysql DB. I'm making a software and I want to add license ID. Each user has x keys to use. </p> <p>Now when the user start the client, it invokes a PHP page that check if the Key sent in the POST method is stored in DB or not. </p>...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-15T17:24:26.350", "Id": "9371", "Score": "1", "body": "What does \"improve it\" mean? Does your code actually work? Or is it broken and you need help fixing it? This site is not for specific problems with code, but we can migrate it to...
[ { "body": "<p>First of all: both code and table design can (have?) to be improved:</p>\n\n<ol>\n<li>Tables must be normalized and can be improved by removing data and duplicates\n<ul>\n<li>Users table can (?) have <code>username</code> only as natural PK (pure id eliminated)</li>\n<li>Optional: licence table wi...
{ "AcceptedAnswerId": null, "CommentCount": "5", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-15T17:10:53.493", "Id": "6067", "Score": "1", "Tags": [ "php", "mysql" ], "Title": "PHP MYSQL loop to check if LicenseID Values are contained in mysql DB" }
6067
<p>I'm trying to write function, that replace digits at some positions in number.</p> <p>For example <code>f 91521 [3,4] -&gt; [91001,91111,91221,91331,91441,91551,91661,91771,91881,91991]</code></p> <p>It's behavior like <code>91**1 -&gt; ...</code>, so I named it as a 'whitemask'.</p> <p><strong>Question 1</strong...
[]
[ { "body": "<p>I didn't thought about the algorithm itself, but shortened the code a little bit:</p>\n\n<pre><code>decreaseList :: Num a =&gt; [a] -&gt; a -&gt; [a]\ndecreaseList b c = map (subtract c) b \n\nlistToNum :: [Int] -&gt; Integer\nlistToNum = toInteger . foldl1 ((+).(*10)) \n\nnumToList :: Integer -&...
{ "AcceptedAnswerId": "6073", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-15T21:33:52.043", "Id": "6069", "Score": "1", "Tags": [ "haskell" ], "Title": "replace digit at some positions in number" }
6069
<p>This program is supposed to calculate the determinants of the data entered. I believe it does. Within the guidelines of this exercise we were not to break this down to functions but all out of main. We will be breaking it down into functions in part a and b of this overall exercise.</p> <p>However, I don't know eno...
[]
[ { "body": "<p>General comments:</p>\n\n<p>Use one line per variable.</p>\n\n<pre><code>int x,y,z,;\n</code></pre>\n\n<p>Prefer to use smaller than than smaller than or equal. This is a bit nit-picky but when working with array bounds you can then use the same number in the test as in the size of the array, whic...
{ "AcceptedAnswerId": null, "CommentCount": "0", "ContentLicense": "CC BY-SA 4.0", "CreationDate": "2011-11-15T22:24:45.070", "Id": "6070", "Score": "1", "Tags": [ "c++" ], "Title": "Calculating the determinants of data entered" }
6070
<p>I wrote this PHP Login system and I would like to see if I have any errors/made any mistakes or if you think I can do something to improve it, like including tokenizing and improving security even more.</p> <p>This script allows "Remember Me" option which uses a cookie and a session table to authorize a user. It ha...
[]
[ { "body": "<p>Initial thoughts:</p>\n\n<p>Lots of redundant comments, for example:</p>\n\n<pre><code>// get the data, trim the blank spaces\n$username = trim($_POST['username']);\n</code></pre>\n\n<p>Some obvious refactoring not implemented, for example:</p>\n\n<pre><code>if($_POST['create']){\n // create an...
{ "AcceptedAnswerId": "6099", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-16T00:41:17.693", "Id": "6076", "Score": "4", "Tags": [ "php", "security" ], "Title": "PHP Login class" }
6076
<p>I understand not to rely on user agent information for anything detrimental towards the site since it can be faked or hidden etc, it's more of just an extra feature for something.</p> <p>Is there anyway this can be made shorter perhaps? Also it will be running on a few pages, so I was wanting to know if it's perfor...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-02-13T18:21:36.487", "Id": "34819", "Score": "0", "body": "When the if matches I would not `$browser = $value` but `return $value` instead." }, { "ContentLicense": "CC BY-SA 4.0", "CreationDate": "2021-03-25T03:38:29.813", ...
[ { "body": "<p>First of all, I guess somebody has already written a library for that. I would do some research and check existing libraries. </p>\n\n<p>1, Split the code to two smaller functions: <code>getOperatingSystem()</code> and <code>getBrowser()</code>. </p>\n\n<p>2, </p>\n\n<pre><code>} else if ...
{ "AcceptedAnswerId": "6090", "CommentCount": "3", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-16T00:43:22.147", "Id": "6077", "Score": "9", "Tags": [ "php", "performance" ], "Title": "Browser and OS detection script" }
6077
<p>The comment at the top was not followed because I printed the contents. I just want an opinion on coding style.</p> <pre><code> /* * Write a function setbits(x,p,n,y) that returns x with the n bits * that begin at position p set to the rightmost n bits of y, leaving * the other bits unchanged in t...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-16T02:59:52.337", "Id": "9401", "Score": "2", "body": "Not sure this does anything: `(0 << n)`" } ]
[ { "body": "<p>I think your code does not exactly what the comment wants. In line:</p>\n\n<pre><code>x |= (y &amp; ~(0 &lt;&lt; n)) &lt;&lt; p;\n</code></pre>\n\n<p><code>0 &lt;&lt; n</code> is 0. If you want <code>n</code> rightmost bits of <code>y</code> you may use a mask like <code>(1&lt;&lt;n)-1</code> (whi...
{ "AcceptedAnswerId": "6089", "CommentCount": "1", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-16T01:09:16.440", "Id": "6078", "Score": "5", "Tags": [ "c" ], "Title": "Bit Flipping Programming Exercise in C" }
6078
<p>I have a JavaScript/jQuery function that handles the drop-down menus for several different sites. I am a new to JavaScript and jQuery and would love any input on how to clean up my code. Essentially, I am asking how to make this script more efficient and less clunky, so I can improve performance and write better cod...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-16T01:53:16.400", "Id": "9389", "Score": "0", "body": "Please show us the corresponding HTML to we can see how to use it's structure to help make the code smarter." }, { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "20...
[ { "body": "<p>You've made a few mistakes.</p>\n\n<p>Firstly you used <code>$(parentItem)</code> and <code>$(nav)</code> when you don't need to, because they are already jQuery objects.</p>\n\n<p>Secondly you copy pasted 4 blocks of similar codes without abstracting it into a function.</p>\n\n<p>Then the functio...
{ "AcceptedAnswerId": null, "CommentCount": "2", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-16T01:46:51.000", "Id": "6079", "Score": "3", "Tags": [ "javascript", "jquery", "performance" ], "Title": "Handling drop-down menus for different sites" }
6079
<p>EDIT:</p> <p>Function is returnted to JClass Text. Prototypes hold non instance members (statics). No privacy. Privacy requires self executing methods.</p> <pre><code>var Text = function( form_name ) { this.text_array = document.forms[form_name].elements; }; Text.prototype.patterns = { prefix_url: /^...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-16T19:01:45.400", "Id": "9447", "Score": "0", "body": "generic function (should be renamed to checkpattern) should not be setting the response." } ]
[ { "body": "<p>Few additions </p>\n\n<p>1) First get some unit tests in </p>\n\n<p>2) I would do something like this <a href=\"http://jsfiddle.net/Brw4W/1/\" rel=\"nofollow\">http://jsfiddle.net/Brw4W/1/</a></p>\n", "comments": [ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011...
{ "AcceptedAnswerId": "6113", "CommentCount": "1", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-16T05:13:33.710", "Id": "6087", "Score": "0", "Tags": [ "javascript" ], "Title": "Object - Text Validator" }
6087
<p>I started playing with Raphaël and I'm wondering if somebody could do the code review of what I have so far and maybe give me some tips how to improve it. Basically what I'm trying to accomplish is the zoom-in/zoom-out effect on the element when mouseover event is being triggered on the element. The effect should p...
[]
[ { "body": "<p>If I understood your intention correctly, the code already works fine. However, as you are learning Raphaël, there are two minor things you could consider.\nFirstly, you can chain your methods when setting attributes via <code>.attr()</code>:</p>\n\n<pre><code>var dot = paper.circle(100, 100, 15)....
{ "AcceptedAnswerId": "12955", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-16T06:44:11.750", "Id": "6088", "Score": "4", "Tags": [ "javascript", "raphael.js" ], "Title": "Raphaël zoom effect" }
6088
<p>I have something akin to</p> <pre><code>object[] values = getValues(); string renderedValues = string.Join("-", Array.ConvertAll&lt;object,string&gt;(values, new Converter&lt;object,string&gt;(o2s) )); </code></pre> <p>where <code>o2s</co...
[]
[ { "body": "<p>Why not <del>Zoidberg</del> simply this?</p>\n\n<pre><code>string renderedValues = string.Join(\"-\", getValues());\n</code></pre>\n\n<p>Simply using the <code>Join(string separator, params Object[] values)</code> overload: <a href=\"http://msdn.microsoft.com/en-us/library/dd988350.aspx\" rel=\"no...
{ "AcceptedAnswerId": "6096", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-16T10:26:07.240", "Id": "6092", "Score": "3", "Tags": [ "c#", ".net", "converting" ], "Title": "Best C# idiom to convert the items on an object array to a string?" }
6092
<p>I always thought it would be handy to be able to write:</p> <pre><code>const std::string s = (std::ostringstream() &lt;&lt; "hi" &lt;&lt; 0).str(); </code></pre> <p>This doesn't work in C++ by default because <code>operator&lt;&lt;</code> returns the <code>ostringstream</code> as a <code>ostream</code>, which obvi...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-23T20:44:32.470", "Id": "9675", "Score": "0", "body": "You might be interested in this gizmo I had reviewed when the site first came live: http://codereview.stackexchange.com/questions/226/does-my-class-introduce-undefined-behavior" ...
[ { "body": "<p>Its a nice trick. But I don't see what it buys you.</p>\n\n<p>So you have a std::string object that you now use.</p>\n\n<pre><code>int main() {\n const std::string s = (std::ostringstream() &lt;&lt; \"hi\" &lt;&lt; 0).str();\n std::cout &lt;&lt; s &lt;&lt; std::endl;\n}\n</code></pre>\n\n<p>In C...
{ "AcceptedAnswerId": "6104", "CommentCount": "1", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-16T11:05:48.570", "Id": "6094", "Score": "10", "Tags": [ "c++", "c++11" ], "Title": "A version of operator<< that returns ostringstream instead of ostream" }
6094
<p>After having spent a month or two trying to learn JavaScript, especially functional programming, async, and closures, I finally get it. But I don't know if I'm writing elegant code... Specifically, I'm not sure if I'm creating too many closures, because I don't entirely grasp when contexts remain and when they're ma...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-16T16:22:43.060", "Id": "9434", "Score": "0", "body": "@Raynos yes. Oops. Just a test server running on 127.0.0.1, but still..." }, { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-16T16:34:41.213", "Id": "9...
[ { "body": "<p><code>var makeQuery = function makeQuery (id) {</code></p>\n\n<p>There is no need to make <code>makeQuery</code> a local variable, just use a function declaration.</p>\n\n<pre><code>results = results[0];\nresults.key = radix64.fromNumber(results.id);\n</code></pre>\n\n<p>Your just augmenting the o...
{ "AcceptedAnswerId": "6102", "CommentCount": "4", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-16T16:13:04.813", "Id": "6101", "Score": "4", "Tags": [ "javascript", "asynchronous", "node.js" ], "Title": "Async, callbacks, and closures" }
6101
<p>For a given Entity Framework data set, I have written several adapters to import data into my entity models. Rather than manually write mapping code for each entity model, I tried for a generalized approach using reflection.</p> <p>My requirements are: </p> <ul> <li>The updater must create the entity if it does no...
[]
[ { "body": "<p>I personally don't like exposing <code>Id</code>s as their base data type (in your case, <code>int</code>) for a couple of reasons:</p>\n\n<ol>\n<li><p>Somewhere, in client code, someone can assign an <code>int</code> of one type of\nentity to another by mistake and nothing will stop you (i.e. no\...
{ "AcceptedAnswerId": null, "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-16T23:06:54.483", "Id": "6107", "Score": "5", "Tags": [ "c#", "entity-framework" ], "Title": "Entity Framework Code First Data Updater" }
6107
<p>I am periodically getting tweets, formatting them, and storing them into the database. Now the line of code I use for this is an awful lot. I was wondering if this could be improved.</p> <pre><code>// This is the wordpress way of getting a json object // Wordpress will determine how the data is going to // be fetc...
[]
[ { "body": "<pre><code>wp_remote_get(\n 'http://twitter.com/statuses/user_timeline/' \n . $options['username'].'.json?count='.$options['count'] ) \n</code></pre>\n\n<p>I'd use at least a local variable which stores the url:</p>\n\n<pre><code>$url = 'http://twitter.com/statuses/user_timeline/' . \n ...
{ "AcceptedAnswerId": "6119", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-17T01:37:05.187", "Id": "6109", "Score": "2", "Tags": [ "php", "datetime", "json", "twitter" ], "Title": "Fetching / formatting / storing tweets to my database" }
6109
<p>I need to get some scripts from the different social networking site and cache them. I want the function to be asynchronous, and non-blocking.</p> <p>I have settled with a script, but I am not really sure if it is not overly complicated to do the job. </p> <p>I did replace a normal <code>for</code> loop, and this...
[]
[ { "body": "<p>I believe you don't need to use jquery deferred objects for this task. Just use script tags with sources - they will be downloaded in parallel from CDNs (try to use CDN, its always a good idea). The amount of parallel requests will be limited by browser restrictions.</p>\n\n<p>Since your code just...
{ "AcceptedAnswerId": "6131", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-17T03:31:49.497", "Id": "6110", "Score": "6", "Tags": [ "javascript", "jquery", "asynchronous", "dynamic-loading" ], "Title": "Loading JavaScript files asynchronously" }
6110
<p>I've been away from PHP for at least 5 years and I'm just starting to look into it again. I have a set of functions I created and used for database access and I'm wondering if they are still good to use.</p> <pre><code>&lt;?php // // A group of database function to hide any errors that may occur and // allow for so...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-18T12:05:44.003", "Id": "9496", "Score": "0", "body": "on a side note, in dbConnect you nest checks, ifs, and prints. It's more readable to do it this way `if(!check) {print \"stuff\"; exit();}`, and put your \"all ok\" code under the ...
[ { "body": "<p>A general OOP advice: use the <a href=\"http://www.oodesign.com/factory-pattern.html\" rel=\"nofollow\">factory pattern</a> and move MySQL related codes to a <code>MySqlDatabase</code>class and MSSQL related codes to an <code>MsSqlDatabase</code> class. In this way you'll have two separate classes...
{ "AcceptedAnswerId": "6114", "CommentCount": "1", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-17T05:58:14.557", "Id": "6111", "Score": "1", "Tags": [ "php", "mysql" ], "Title": "Database accessor functions" }
6111
<p>Can someone look over this <a href="http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes" rel="nofollow">Sieve of Eratosthenes</a> implementation? It seems almost too easy.</p> <p>Rather than maintaining a seperate <code>bit[]</code> to track prime/not prime, I'm just removing the noncandidates from the collection co...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-17T17:28:27.637", "Id": "9472", "Score": "2", "body": "Nobody mentioned this in their answers, but `Math.Sqrt()` isn't the quickest of functions, and the return value used here is effectively constant. Remove it from the comparison, a...
[ { "body": "<p><strong>update:</strong> as tested and explained by @Guffa and @EoinCampbell, this is actually <strong>much slower</strong>.</p>\n\n<p>The benefits of a <code>HashSet</code> are mainly in access speed by index.<br>\nSince the algorithm never even accesses the list by index, the <code>Hashset</code...
{ "AcceptedAnswerId": "6120", "CommentCount": "2", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-17T11:53:15.357", "Id": "6115", "Score": "8", "Tags": [ "c#", "linq", "sieve-of-eratosthenes" ], "Title": "Sieve of Eratosthenes in C# with LINQ" }
6115
<p>I need to write a class to batch <code>Order</code> objects from 3 calling components running on 3 separate threads. The 3 calling components will call the <code>Batcher</code> class at the same time (within a second of each other), but to handle clock issues and thread interleaving, the method should wait for a max...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-17T16:16:06.337", "Id": "9468", "Score": "0", "body": "Take a look at the tasks parallel Library" } ]
[ { "body": "<p>Not sure what is behind the requirement of 5 seconds, but from the top level (please comment to answer the items marked as warnings):</p>\n\n<pre><code>public sealed class Batcher\n {\n private readonly object _syncLock = new object();\n private readonly Timer _timer;\n pri...
{ "AcceptedAnswerId": null, "CommentCount": "1", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-17T16:14:47.413", "Id": "6123", "Score": "1", "Tags": [ "c#", "multithreading" ], "Title": "Multithreaded batching class" }
6123
<h2>The Problem</h2> <p>I found myself needing an instance of an <code>IObjectContext</code> interface that should never be null, but wanting to delay instantiation until after some required resources have been loaded.</p> <p>I decided to approach this with <a href="http://msdn.microsoft.com/en-us/library/dd642331.as...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-18T04:36:42.197", "Id": "9491", "Score": "0", "body": "A better question might be: is using reflection the only solution?" } ]
[ { "body": "<p>In conjunction with the rest of the code, the following line exhibits a bug:</p>\n\n<pre><code>Contract.Requires(typeof(TResult).Equals(lazyFactory.ReturnType));\n</code></pre>\n\n<p>The problem is that my <code>IObjectContext.ObjectSet()</code> method has the generic return type <code>IObjectSet&...
{ "AcceptedAnswerId": "6574", "CommentCount": "1", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-17T16:52:27.627", "Id": "6124", "Score": "4", "Tags": [ "c#", ".net", "design-patterns" ], "Title": "Providing factory method to Lazy<T> when factory exists on another lazy instanc...
6124
<p>Could you suggest ways of making this more simple?</p> <pre><code>/* * Next write a function invert(x,p,n) that returns x with the n bits * that begin at position p inverted, leaving the others unchange */ void printbits(unsigned x) { size_t size_of_int = sizeof(int) &lt;&lt; 3; unsigned mask = 1; i...
[]
[ { "body": "<p>Maybe:</p>\n\n<pre><code>void invert2(unsigned x, unsigned p, unsigned n) {\n printbits((~(~0 &lt;&lt; n) &lt;&lt; p) ^ x);\n}\n</code></pre>\n", "comments": [], "meta_data": { "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-17T21:48:59....
{ "AcceptedAnswerId": "6128", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-17T21:11:56.253", "Id": "6125", "Score": "7", "Tags": [ "c", "bitwise" ], "Title": "Print Bits Part II" }
6125