body
stringlengths
25
86.7k
comments
list
answers
list
meta_data
dict
question_id
stringlengths
1
6
<p><a href="http://jsfiddle.net/xkuZF/6/" rel="nofollow">http://jsfiddle.net/xkuZF/6/</a></p> <pre><code>function func() { document.body.scrollTop++; } window.onmouseover = function() { clearInterval(interval); }; window.onmouseout = function() { interval = setInterval(func); }; var interval = setInter...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-15T18:12:04.020", "Id": "5192", "Score": "1", "body": "In the future, please avoid using pastebins. As stated in the [faq], it's much easier to review code when it's here, and your code is very short." }, { "ContentLicense": "C...
[ { "body": "<p>To your questions:</p>\n\n<ul>\n<li><p>More options; sure you could provide a wait time to setInterval to control the speed of the scrolling down, you could also increase the scrollTop increment to make it scroll down faster.</p></li>\n<li><p>Less code; I think this is pretty much the bare minimum...
{ "AcceptedAnswerId": null, "CommentCount": "5", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-15T18:03:18.463", "Id": "3464", "Score": "3", "Tags": [ "javascript" ], "Title": "Better ways of making a scroller" }
3464
<p>I'm new to sockets and servers in general and I would like to know if I'm doing something really wrong in the following server loop for spawning threads to process requests.</p> <pre><code>private static ServerSocket server; private static final Object request_counter_lock = new Object(); private static int request...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2012-11-14T22:27:00.197", "Id": "29667", "Score": "0", "body": "Could you please clarify who is responsible for changing `exit` flag ? Currently looks like you have an infinite loop." }, { "ContentLicense": "CC BY-SA 3.0", "Creatio...
[ { "body": "<p>I wouldn't recommend creating new Threads on your own or using Threads without holding onto their references. It makes it too hard to shut down properly. In your example, I think that if <code>SocketCloser.close(socket)</code> throws, then the <code>request_counter</code> will be off by one.</p>\n...
{ "AcceptedAnswerId": null, "CommentCount": "2", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-15T23:03:05.640", "Id": "3472", "Score": "1", "Tags": [ "java", "multithreading", "thread-safety" ], "Title": "Simple multi-threaded Java server loop" }
3472
<p>The following code solves this problem:</p> <blockquote> <p>The 3072 characters below contain a sequence of 5 characters which is repeated. However, there is a twist: one of the sequences has a typo. To be specific, you're looking for two groups of 5 characters which share 4 of those characters. Enter the 4...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-19T01:51:12.413", "Id": "5281", "Score": "0", "body": "Actual URL of input data appears to be: https://www.readyforzero.com/challenge-data/2315ff7b4e1d47deb54e5457bd3334a9/1" }, { "ContentLicense": "CC BY-SA 3.0", "Creation...
[ { "body": "<p>I'd start bottom-up on this one if you want to do it in a slightly more idiomatic / functional style. Also it's good to get the data in Clojure sequences rather than strings as it makes it a bit easier to manipulate.</p>\n\n<p>You're trying to work on groups of five adjacent characters so I'd set ...
{ "AcceptedAnswerId": null, "CommentCount": "3", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-15T23:06:05.817", "Id": "3473", "Score": "3", "Tags": [ "programming-challenge", "lisp", "clojure" ], "Title": "Searching for repeated characters in a sequence of characters" }
3473
<blockquote> <p>The following text is an in-order traversal of a binary tree with 1023 nodes. What is the sum of the digits appearing in the leaves?</p> </blockquote> <p>How can I improve this Clojure code? It looks strange to me to have two recurs in the <code>if</code>.</p> <pre><code>(ns fun2) (defn parse [in...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-16T02:11:12.443", "Id": "5206", "Score": "1", "body": "Are we assuming this is a complete binary tree? Otherwise I don't think a single inorder traversal is enough to reliably figure out what the leaves are." }, { "ContentLice...
[ { "body": "<p>This code is doing several distinct things and one of the really great things about clojure is that it lets you make things simple by decomposing things that do more than one task into a series of individual tasks.</p>\n\n<p>if is:</p>\n\n<ul>\n<li>taking every other letter in a string (perhaps yo...
{ "AcceptedAnswerId": null, "CommentCount": "2", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-15T23:36:22.530", "Id": "3475", "Score": "6", "Tags": [ "programming-challenge", "tree", "clojure" ], "Title": "Finding the sum of the digits appearing in the leaves via in-order tra...
3475
<pre><code>$('#step-holder a').click(function (e) { e.preventDefault(); $('#step-holder div').each(function () { if ($(this).attr('class') == 'step-dark-left') { $(this).removeClass('step-dark-left'); $(this).addClass('step-light-left'); } if ($(this).attr('class'...
[]
[ { "body": "<p>One quick refactoring jumps out at me. Start out your each with:</p>\n\n<pre><code>$('#step-holder div').each(function () {\n var $this = $(this);\n</code></pre>\n\n<p>Every time you call it (possibly 12 times?) its performing a lookup ... you should look up that object once, store the result, ...
{ "AcceptedAnswerId": "3484", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-16T12:12:53.540", "Id": "3481", "Score": "-1", "Tags": [ "javascript", "jquery" ], "Title": "How can i shorten/refactor this jquery?" }
3481
<p>This code removes repeated words from a word array (2D char array). I want to optimize this as much possible for speed.</p> <pre><code>#include "stdio.h" #include "string.h" main() { char words[2000][20]; /*This array is populated by some other module. possible example of this array is given below, but in...
[]
[ { "body": "<ol>\n<li><p>Your algorithm is \\$O(n^2)\\$. You can reduce this to \\$O(n)\\$ by first inserting them into a closed hash table (i.e., one with bucket chains).</p></li>\n<li><p>I'd optimise is rather than copying the whole items, just copy pointers to the original strings (unless you intend to alter...
{ "AcceptedAnswerId": "3489", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-17T07:07:56.460", "Id": "3488", "Score": "4", "Tags": [ "optimization", "c", "strings", "array" ], "Title": "Remove repeated words from a 2D character array" }
3488
<p>I love lambdas, and functional programming etc. But sometimes I wonder if I take it too far..</p> <pre><code>public static T With&lt;T&gt;(this T source, Action&lt;T&gt; action) { action(source); return source; } public static IEnumerable&lt;T&gt; ForEach&lt;T&gt;(this IEnumerable&lt;T&gt; source, Action&l...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-15T15:21:32.773", "Id": "5227", "Score": "12", "body": "Causing side-effects is not about functional programming at all. Yep, this is too dirty." }, { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-15T15:26:53.0...
[ { "body": "<p>I don't think this really provides a lot of value beyond the existing .Select() projection. Anything you write into your action you could also write into a .Select() lambda.</p>\n\n<p>For example:</p>\n\n<pre><code>myItems.Select(item =&gt; \n{\n item.DoSomethingWeird();\n return item;\n}).Sel...
{ "AcceptedAnswerId": "3495", "CommentCount": "4", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-15T15:19:11.620", "Id": "3491", "Score": "7", "Tags": [ "c#", "extension-methods" ], "Title": "Extension methods, is this too dirty?" }
3491
<p>I'm using Codeigniter but this goes for any project in PHP. Let's say I have the following code in my view. I've been struggling, trying to figure out how best to indent and display PHP code within standard HTML code.</p> <pre><code>... ... &lt;div id="subcontent"&gt; &lt;?php if ($this-&gt;session-&gt;...
[]
[ { "body": "<p>This is really an individual opinion..</p>\n\n<p>Personally I'm all for putting the <code>&lt;?php ?&gt;</code> markers at the far left because they indicate different code 'segments'.. There is just no 'best way to do it.. Just choose a way and stick to it..</p>\n", "comments": [ { ...
{ "AcceptedAnswerId": null, "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-16T16:12:13.023", "Id": "3498", "Score": "5", "Tags": [ "php" ], "Title": "Best practice for lining up PHP syntax in HTML page" }
3498
<p>I wrote a simple function that calculates and returns the maximum drawdown of a set of returns. I am trying to squeeze as much efficiency for speed out of the code as possible. I've got it down to about as fast as I can go. Does anyone have suggestions on how to write this function more efficiently, perhaps through ...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-15T21:54:19.583", "Id": "5238", "Score": "0", "body": "What type of object is `returns`? `returns = returns + 1` suggests it might be an integer, or a numpy array, but neither of those has a `count` method." }, { "ContentLicens...
[ { "body": "<p>Don't just optimize this or optimize that by educated guessing.</p>\n\n<p>Find out which lines of code are responsible for a large fraction of time,\nas shown in <a href=\"https://stackoverflow.com/questions/4295799/how-to-improve-performance-of-this-code/4299378#4299378\">this answer</a>,\nand fo...
{ "AcceptedAnswerId": null, "CommentCount": "4", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-15T21:48:58.460", "Id": "3503", "Score": "6", "Tags": [ "python", "performance", "numpy" ], "Title": "Calculating the maximum drawdown of a set of returns" }
3503
<p>An animation is composed of frames. I have the following class:</p> <pre><code>class Frame { private: sf::IntRect rect; unsigned int duration; public: Frame(sf::IntRect rect, unsigned int duration); unsigned int getDuration(); sf::IntRect&amp; getRect(); }; </code></pre> <p>At the animation c...
[]
[ { "body": "<p>This is actually a matter of taste. for my preference, i would choose </p>\n\n<pre><code>void Animation::addFrame(sf::IntRect rect, unsigned int duration) // public\n{\n frames.push_back(Frame(rect, duration)); // ps: private std::vector&lt;Frame&gt; frames;\n}\n</code></pre>\n\n<p>because it h...
{ "AcceptedAnswerId": "3546", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-17T14:11:28.507", "Id": "3509", "Score": "2", "Tags": [ "c++" ], "Title": "Should I hide or show this implementation?" }
3509
<p>I am trying to express the classical TDD kata of mastermind in the most idiomatic scala I can. Here is a scalatest, test suite : </p> <pre><code>package eu.byjean.mastermind import org.junit.runner.RunWith import org.scalatest.junit.JUnitRunner import org.scalatest.FlatSpec import org.scalatest.matchers.ShouldMatc...
[]
[ { "body": "<p>This has <em>worse</em> performance, but I hope it's a little bit more readable:</p>\n\n<pre><code>def check(guess: Symbol*)(secret: Symbol*): (Int, Int) = {\n def goodBad(f: Seq[Symbol] =&gt; Seq[Symbol], s1: Seq[Symbol], s2:Seq[Symbol]) = \n f(s1) zip f(s2) partition {case (x, y) =&gt; x == ...
{ "AcceptedAnswerId": "3628", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-17T15:19:09.350", "Id": "3510", "Score": "2", "Tags": [ "functional-programming", "scala" ], "Title": "Scala mastermind solver, which is the most scala-ish" }
3510
<p>I am writing a piece of code which models the evolution of a social network. The idea is that each person is assigned to a node and relationships between people (edges on the network) are given a weight of +1 or -1 depending on whether the relationship is friendly or unfriendly. </p> <p>Using this simple model you ...
[]
[ { "body": "<p>The following lines seem particularly costly:</p>\n\n<pre><code>G.clear()\nG.add_weighted_edges_from(li)\n</code></pre>\n\n<p>Why are you clearing the entire graph and recreating it? Isn't there a more direct way to modify the graph without recreating it from scratch? When I looked at the referenc...
{ "AcceptedAnswerId": null, "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-17T19:09:54.193", "Id": "3511", "Score": "4", "Tags": [ "python", "performance", "random", "graph", "matplotlib" ], "Title": "Social network evolution" }
3511
<p>Context: I wrote a simple as an <a href="https://unix.stackexchange.com/questions/16849/importing-csv-contacts-into-evolution-3-0-2/16858#16858">answer</a> to a problem in <a href="http://unix.stackexchange.com">Unix.SE</a> and it was suggested to me to post it here for review.</p> <p>A comma separated file is gene...
[]
[ { "body": "<p>Use sys.exit to indicate success or failure of the program.</p>\n\n<p>file is a builtin python function, its best to avoid naming your variables the same thing.</p>\n\n<pre><code>for row in reader:\n print 'BEGIN:VCARD'\n print 'VERSION:2.1'\n print 'N:' + row[0] + ';' + row[1]\n print...
{ "AcceptedAnswerId": null, "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-18T01:47:28.223", "Id": "3517", "Score": "6", "Tags": [ "python", "converting", "csv" ], "Title": "Converting a csv to vcards in Python" }
3517
<p>I'm implementing argument check in a method where one parameter is a <code>List</code> of <code>LocalDate</code> objects (from <a href="http://joda-time.sourceforge.net/" rel="nofollow">Joda-Time</a>). I want to make sure that <code>List</code> contains <strong>at most one <code>LocalDate</code> per month</strong>. ...
[]
[ { "body": "<p>I understood that you only need to know if a month has been found before or not. I wouldn't then go through the trouble of using a Multimap if there is no other reason for it. </p>\n\n<pre><code>boolean[] monthFound = new boolean[12];\nfor(LocalDate date : firstYearDates) {\n if(monthFound[date.g...
{ "AcceptedAnswerId": "12901", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-18T12:18:11.710", "Id": "3523", "Score": "2", "Tags": [ "java", "guava", "jodatime" ], "Title": "At most one LocalDate per month in a list" }
3523
<p>I am looking for ideas to optimize the below code for speed. I'm also looking for some good improvement in execution time. </p> <p>This code reads a text file. The text file has many lines of ASCII text. Then it finds words out of them (space/tab delimiter). It has to neglect any punctuation symbol in the file. Th...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-18T16:19:28.267", "Id": "5272", "Score": "0", "body": "The somewhat random indentation of your code makes the structure hard to see. You may get more reviews quicker if you use consistent indentation (either 2 or 4 characters per inde...
[ { "body": "<p>First, a note for those who might wonder why I'd help somebody who's apparently trying to build a tool to do dictionary attacks on passwords. The reason is pretty simple: first, there are tools that do the job pretty well already, so this is hardly unleashing anything new on the world. Second, thi...
{ "AcceptedAnswerId": "3526", "CommentCount": "3", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-18T13:43:06.943", "Id": "3525", "Score": "6", "Tags": [ "optimization", "c", "cryptography" ], "Title": "Forming word combinations from file" }
3525
<p>I've written code to parse dollars and cents entered by the user. The value returned is the total number of cents.</p> <p>For example:</p> <pre><code>f('$1.50') = 150 f('1.5') = 150 f('0') = 0 f('1000') = 1000 f('$12,500.00') = 12500000 functions.GetTotalCents = function (dollarsAndCentsString) { // Cast...
[]
[ { "body": "<p>Wouldn't it be simpler to:</p>\n\n<ul>\n<li>strip out any '$' and ',' characters;</li>\n<li>convert to a real number;</li>\n<li>multiply by 100;</li>\n<li>take the integer part.</li>\n</ul>\n\n<p>You can probably do that in one line (my Javascript is a bit rusty):</p>\n\n<pre><code>return Math.rou...
{ "AcceptedAnswerId": null, "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-18T20:51:25.340", "Id": "3527", "Score": "5", "Tags": [ "javascript", "parsing" ], "Title": "Code to Parse dollars and cents?" }
3527
<p>I have a view function that allows a user to Add / Edit / Delete an object. How does the following function look, and in what ways could I improve it? I was thinking about separating aspects of the function into different parts, but since no 'component' is more than four-five lines of code, I thought that would be a...
[]
[ { "body": "<p>The if conditions can be simplified.</p>\n\n<pre><code>fpv_list = [ item.partition(\"_\") for item in request.POST.iteritems() ]\ndeletes = [ v for f,p,v in fpv_list if f == 'delete' ]\nfor education_id in deletes:\n whatever\n\nedits = [ v for f,p,v in fpv_list if f == 'edit' ] \nfor educatio...
{ "AcceptedAnswerId": "3545", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-19T02:19:13.110", "Id": "3532", "Score": "2", "Tags": [ "python", "django" ], "Title": "Django view function" }
3532
<p>I am working on a PHP/MySQL application that uses a different time zone from the server it runs on, and neither of the timezones are UTC. I'm converting displayed times to be in the user's timezone, and am wondering whether my approach is sensible. I would love any feedback you might have.</p> <p>Firstly, the app...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-19T06:02:04.310", "Id": "5285", "Score": "0", "body": "What is going wrong? I made fiddle out of this http://jsfiddle.net/xXVPC/" } ]
[ { "body": "<p>I would think it would be easier and more reliable to use timezone offsets rather than exact server time vs. client time. You can get the client-side offset using <code>new Date().getTimezoneOffset()</code> which returns the offset in minutes from UTC. When the timezone is +2 then the offset is -1...
{ "AcceptedAnswerId": null, "CommentCount": "1", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-19T04:12:23.347", "Id": "3534", "Score": "3", "Tags": [ "javascript", "datetime", "converting" ], "Title": "Conversion of datetime strings in PHP pages to user's timezone" }
3534
<p>I'm just refactoring some code I've come across whilst fixing a bug, but just wanted a quick sanity check to make sure I'm not missing any reason for keeping what I'm going to remove.</p> <p>The current code:</p> <pre><code>uxButton.Enabled = ((from a in actions select a).Distinct().Count() == 1); </code></pre> <...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-19T13:24:01.727", "Id": "5297", "Score": "0", "body": "I'm not sure what the title of this question has to do with the content. You might consider revising it." }, { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-0...
[ { "body": "<p>They both do the same thing. The first one just looks bizarre. Looks like the original author just didn't understand Linq.</p>\n\n<p>I'm not sure why you are checking to see if the count is 1. Looks like, in this situation</p>\n\n<pre><code>uxButton.Enabled = actions.Distinct().Any()\n</code></...
{ "AcceptedAnswerId": "3542", "CommentCount": "2", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-19T08:33:18.583", "Id": "3536", "Score": "2", "Tags": [ "c#", "linq" ], "Title": "LINQ Query returning all original objects" }
3536
<p>Since I'm venturing more and more into the multithreading, I now need to think about how to protect my precious <code>OdbcConnection</code> from breaking at random times.</p> <p>The project specifications:</p> <ul> <li>.NET-2.0</li> <li>ODBC-Connection via MySQL-Connector 3.51 to MySQL 5.0</li> </ul> <p>The main ...
[]
[ { "body": "<p>The <a href=\"http://msdn.microsoft.com/en-us/library/system.data.odbc.odbcconnection%28v=VS.80%29.aspx\" rel=\"nofollow\">OdbcConnection</a> is not thread-safe in any way, therefor it needs a locking mechanism.</p>\n\n<p>My observation is that the <code>OdbcConnection</code> does send it's receiv...
{ "AcceptedAnswerId": "6091", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-19T10:07:37.727", "Id": "3538", "Score": "5", "Tags": [ "c#", "database", "synchronization", "odbc" ], "Title": "Synchronization of an ODBC connection" }
3538
<p>This is working as expected, except in the speed area + I need to make it more readable and shorter if possible, I probably have lot's of things I don't need :).</p> <p><strong>Edit</strong> it is working now </p> <pre><code>charset = dict(opening='{[(&lt;',\ closing='}])&gt;',\ string = ('"', "'"),\ ...
[]
[ { "body": "<p>You can drop the <code>del</code> statements. They don't help much.</p>\n\n<p>It's much easier to break this into separate functions and simply allow Python's ordinary namespace rules handle the deletes.</p>\n\n<p>The <code>if type(mn) == type(str()):# if there only is one element</code> is a nee...
{ "AcceptedAnswerId": "3558", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-19T18:39:35.823", "Id": "3543", "Score": "3", "Tags": [ "python", "regex" ], "Title": "Brace pairing ({}[]()<>) cleanup/speedup" }
3543
<p>I have two quite mutually exclusive desires - detailed logs and keeping my code clean. It appears that you can only get either of these. By "logging" I mean logging and not tracing, so AOP adepts wouldn't appreciate it.</p> <p>What I want to have is real description of action my program does, why it does these acti...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-19T21:42:55.453", "Id": "5313", "Score": "2", "body": "I'm a bit unclear on what the ExecutionContext object is. It appears that the ExecutionContext decides if the function is critical or not and handles writing log messages. The Exec...
[ { "body": "<p>I think you should use a logging library instead of reinventing the wheel. See the very good <a href=\"http://logging.apache.org/log4net/\">log4net</a>.</p>\n\n<p>You can simply do</p>\n\n<pre><code>log.DebugFormat(\"creating file reader for file {0}\", fileName);\nlog.Error(\"Unable to find the s...
{ "AcceptedAnswerId": "40520", "CommentCount": "3", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-19T18:47:21.573", "Id": "3544", "Score": "7", "Tags": [ "c#" ], "Title": "Detailed logs and keeping code clean (not AOP)" }
3544
<p>What i'm doing</p> <p>I have a string with html information like this:</p> <pre><code>&lt;p&gt; &lt;span class="fieldText" fieldId="field-4"&gt;Some text&lt;/span&gt; this is a test&lt;/p&gt; </code></pre> <p>My goal in the method is to create a dictionary with this value:</p> <pre><code>**key** **value** fi...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-19T20:38:00.713", "Id": "5308", "Score": "4", "body": "What about your gold?" }, { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-19T21:50:15.203", "Id": "5314", "Score": "0", "body": "@Steven Jeuris...
[ { "body": "<p>I know this is <em>Code Review</em> not <em>Rewrite My Code</em>, however I would suggest using a third-party Html parser (like the <a href=\"http://htmlagilitypack.codeplex.com/\">Html Agility Pack</a> for example) over regular expressions if that's an option. </p>\n\n<p>I realize you're doing ve...
{ "AcceptedAnswerId": "3552", "CommentCount": "6", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-19T20:29:01.187", "Id": "3547", "Score": "4", "Tags": [ "c#", "html", "parsing" ], "Title": "Extracting text fields from <span> tags in an HTML message" }
3547
<p>I have never done anything functional before. These two functions are hideous to look at. II think the first step to do is extract the inner bits out and then map over my arrays. What else can I do to make this code a bit more functional?</p> <p><a href="https://github.com/dbousamra/scalacloth/blob/master/src/cloth...
[]
[ { "body": "<p>There is an idea for scala for-loops: You can include the assignments into the production part:</p>\n\n<pre><code>for (constraint &lt;- neighbors) {\n val c2 = grid (constraint.getX) (constraint.getY).getCurrentPos\n val c1 = p.getCurrentPos\n val delta = Tuple2 (c2.getX - c1....
{ "AcceptedAnswerId": "3556", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-20T04:42:25.380", "Id": "3553", "Score": "6", "Tags": [ "scala" ], "Title": "Decided to learn Scala. Here's an attempt at a Cloth simulation" }
3553
<p>I've created this code to send a response to Flash which can be handled with AS2/AS3:</p> <pre><code>&lt;?php /** * Basic server-side API controls for Flash-PHP communication * @author Marty Wallace * @version 1.0.2 */ class API { // MySQL private $con; privat...
[]
[ { "body": "<p>I have used PHP for a few years now, and here are my suggestions:</p>\n\n<p><code>array_push()</code> can be used to add multiple values into an array at once, but in your case, your pushing only one value in at a time which means you can use:</p>\n\n<p><code>$ar[] = $key . \"=\" . $value</code></...
{ "AcceptedAnswerId": null, "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-20T05:17:15.983", "Id": "3555", "Score": "3", "Tags": [ "php" ], "Title": "PHP Class for sending a response to Flash" }
3555
<p>I want to keep things DRY, so I'm trying to figure out: Is it possible to remove repetitive parameters and code from ASP.NET MVC Controller actions? eg given these Actions:</p> <pre><code>public ActionResult List(string key, string signature) { GetSignature(key); // etc } public ActionResult Add(string key, strin...
[]
[ { "body": "<p>You could create a factory to create ActionResults, and make the key and signature members of the factory. Unless you are reusing the same key and signature a lot of the time, it's likely to look like:</p>\n\n<pre><code>myList = new ActionResultsFactory(key, signature).List();\n</code></pre>\n\n<...
{ "AcceptedAnswerId": null, "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-20T11:08:20.530", "Id": "3557", "Score": "2", "Tags": [ "c#", "asp.net-mvc-2" ], "Title": "Removing repetitive code in ASP.NET MVC Controller actions?" }
3557
<p>I'm developing an application using ASP.NET MVC 3 and Entity Framework 4.1. In that application I have a lot of paged lists. Users can filter and sort these lists.</p> <p>This results in code like the one below. I'm not really happy with this code. Is there a better way to do the filtering and sorting with Entity F...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-21T14:16:41.307", "Id": "5393", "Score": "0", "body": "I am not sure about what your actual concern is. The code is readable and at least a first pass looks like it should work as expected. It is easy to follow. Sometimes the logic req...
[ { "body": "<p>I've written a TON of code like this as well... </p>\n\n<p>I've read about \"dynamic LINQ\" but that's not going to address the issue when you need to orderby().thenby(). The only things I've done differently is using enums for field names and sort directions which works great in my WebForms apps ...
{ "AcceptedAnswerId": "4910", "CommentCount": "5", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-20T12:40:05.767", "Id": "3560", "Score": "37", "Tags": [ "c#", "asp.net-mvc-3", "entity-framework" ], "Title": "Dynamic filtering and sorting with Entity Framework" }
3560
<p>this is a basic PHP auth class I've put together for use with simple sites I do from time to time which don't warrant using a framework. I'm wondering if i'm separating out responsibilities all that well, even at this early stage! For example, I'm not sure where I should be filtering user input with mysql_real_escap...
[]
[ { "body": "<p>Not sure what version of php they implemented this in, but you don't need to use the <code>__construct</code>, you can use <code>Auth</code> instead like many other OOP langs. </p>\n\n<p>You should check if <code>$this-&gt;db</code> is <code>null</code> before you do any <code>sql</code> work, oth...
{ "AcceptedAnswerId": null, "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-20T15:03:33.583", "Id": "3563", "Score": "5", "Tags": [ "php", "design-patterns", "object-oriented" ], "Title": "Simple PHP Auth Class feedback" }
3563
<p>Also, I've commented out a function I was trying to do to count the elements of an array; I realized that is not possible due to the fact that I won't be be able to return a NULL on an array.</p> <p>Although this part of a beginner to C exercise, I'd like to know if there is anything I can do to improve my coding.<...
[]
[ { "body": "<p>see where you have done</p>\n\n<pre><code> //generate elements\n for (i = 0; i &lt; num; i++) {\n x[i] = rand(time(NULL));\n }\n</code></pre>\n\n<p>Thats a good indication you need a function. Same deal for the other place you put a comment</p>\n\n<p>x shouldn't be a global. Should just be ...
{ "AcceptedAnswerId": "3570", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-20T17:00:35.367", "Id": "3566", "Score": "8", "Tags": [ "c", "beginner", "statistics" ], "Title": "Calculate sum, mean, sum of squares, and standard deviation of array elements" }
3566
<p>I need to write a function that "packs" an array of bytes (integers between 0 and 255) into a string. I also need to be able to perform the reverse operation, to get my byte array from the string that it was packed into. This needs to be done as fast as possible. Seeing as JavaScript has 16-bit strings, I packed two...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-21T05:17:43.627", "Id": "5373", "Score": "1", "body": "Regarding 3., just add a sentinel to the end of your packed array indicating whether the final byte should be included or not." }, { "ContentLicense": "CC BY-SA 3.0", "...
[ { "body": "<pre><code>function pack(bytes) {\n var str = \"\";\n// You could make it faster by reading bytes.length once.\n for(var i = 0; i &lt; bytes.length; i += 2) {\n// If you're using signed bytes, you probably need to mask here.\n var char = bytes[i] &lt;&lt; 8;\n// (undefined | 0) === 0 so ...
{ "AcceptedAnswerId": "3589", "CommentCount": "8", "ContentLicense": "CC BY-SA 4.0", "CreationDate": "2011-07-20T21:14:25.230", "Id": "3569", "Score": "31", "Tags": [ "javascript", "performance", "strings", "bitwise" ], "Title": "Pack and unpack bytes to strings" }
3569
<p>I've got a lexer written in C++ (Visual Studio 2010, so including lambdas and a few other C++0x tricks). This is my first lexing experience, the only other source code interpretation I ever did was trivial, didn't separate parsing and lexing, that sort of thing. The grammar closely resembles C++ without templates an...
[]
[ { "body": "<p>well....duplicated code is the first order</p>\n\n<pre><code> current &gt;= L'0' &amp;&amp; current &lt;= L'9'\n</code></pre>\n\n<p>might want to extract it out to isDigit or something. same with isWhitespace.....etc even if used one spot, it will help it be a lot more readable. </p>\n\n<p>In ...
{ "AcceptedAnswerId": null, "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-20T23:54:44.220", "Id": "3573", "Score": "7", "Tags": [ "c++", "c++11" ], "Title": "Lexer code in C++" }
3573
<p>I have written this method for converting a date time zone. How do i reduce the execution time of this method further.</p> <pre><code>public static Timestamp convertTimeZone(final Timestamp fromDate, final TimeZone fromTZ, final TimeZone toTZ ){ Long timeInDate = fromDate.getTime() ; ...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-21T08:14:13.563", "Id": "5375", "Score": "3", "body": "What is the ultimate purpose of this method? Dates a represented as milliseconds from the beginning of the epoch. For printing you can choose the timezone of the output using Simpl...
[ { "body": "<p>The method can be shortened a bit. It'll probably have some effect on perfomance as well.</p>\n\n<pre><code>public static Timestamp convertTimeZone(final Timestamp fromDate, final TimeZone fromTZ, final TimeZone toTZ ){\n\n // primitive long should be enough for his task\n final long timeInDate ...
{ "AcceptedAnswerId": null, "CommentCount": "4", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-21T06:04:45.303", "Id": "3575", "Score": "2", "Tags": [ "java" ], "Title": "Converting timezone for date using java" }
3575
<p>I am new to Python and had to create a schema parser to pull information on attributes and complex types, etc. and then convert data from flat files into the proper XML format. We are processing a lot of data and I've run into some issues once we pass around 1 millions processed records.</p> <p>I'm looking for any ...
[]
[ { "body": "<p>You might want to rethink your approach in a fundamental way if you want to create millions of rows of XML.</p>\n\n<ul>\n<li><p>Define Python classes to contain your actual data. These must be absolutely correct, based on ordinary Python processing. No XSD-based lookup or validation or range che...
{ "AcceptedAnswerId": "3586", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-21T16:50:21.340", "Id": "3580", "Score": "3", "Tags": [ "python", "beginner", "parsing", "xml" ], "Title": "XML schema parsing and XML creation from flat files" }
3580
<p>So I have this code in my program, and there are multiple variations of it. Just looking at it, I feel like there must be a way to make it more efficient, but I can't think of anything. Just looking at it, does anyone have any ideas?</p> <p>Essentially, what the code does is it determines which level of the text th...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-21T17:15:22.223", "Id": "5399", "Score": "0", "body": "I do not have time right now but what you need is a recursive function to call to dig into its child and call itself o dig into that child until there are no children. If I have a...
[ { "body": "<p>That much repetition does have a <a href=\"http://en.wikipedia.org/wiki/Code_smell\" rel=\"nofollow\">smell</a> to it. Can you simplify it by saving and reusing intermediate values similar to what I've shown below?</p>\n\n<pre><code> else if (currentLevel == 3)\n {\n Section level0Se...
{ "AcceptedAnswerId": "3582", "CommentCount": "2", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-21T16:53:24.570", "Id": "3581", "Score": "6", "Tags": [ "java", "optimization" ], "Title": "Determine which level of the text that it is currently parsing" }
3581
<p>I am using XMLReader and avoided SimpleXML because I must handle a huge file and for memory issues, SimpleXML is not the ideal solution. However, even I coded the below script in SimpleXML, it gives me the result really much faster.</p> <p>Because speed and memory is a must for this project, is there any way to spe...
[]
[ { "body": "<p>Your code seems quite tidy and easy to understand, congratulations. But maybe you can check for built in functionality. XPath can search items for you:</p>\n\n<p><a href=\"http://www.php.net/manual/en/simplexmlelement.xpath.php\" rel=\"nofollow\">http://www.php.net/manual/en/simplexmlelement.xpath...
{ "AcceptedAnswerId": null, "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-21T19:09:41.640", "Id": "3585", "Score": "3", "Tags": [ "php", "xml" ], "Title": "In need to speed up this code that filters the data of XML using XMLReader" }
3585
<p>this code finds if there is the string 2004 in <code>&lt;date_iso&gt;&lt;/date_iso&gt;</code> and if it is so, I echo some data from that specific element that the search string was found.</p> <p>I was wondering if this is the best/fastest approach because my main concern is speed and the XML file is huge. Thank yo...
[]
[ { "body": "<p>First of all, put up a timer so you know if things get better.</p>\n\n<p>You're repeating <code>'/' . preg_quote($search) . '/i'</code>for each book. You should create the search string only once or else you are wasting time:</p>\n\n<pre><code>&lt;?php\n$books = simplexml_load_file('planet.xml');...
{ "AcceptedAnswerId": "3591", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-22T03:17:31.633", "Id": "3590", "Score": "2", "Tags": [ "php", "xml" ], "Title": "Is there any faster way instead of using preg_match in the following code?" }
3590
<pre><code> IList&lt;WebSite&gt; wsl = new List&lt;WebSite&gt;(); login1 l = new login1(); string q = "select Id, DomainUrl, IsApproved, Date from website where UserId = @UserId"; using (MySqlConnection con = new MySqlConnection(WebConfigurationManager.ConnectionStrings["MySqlConnectionString"].ToString())) { ...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-22T06:36:41.493", "Id": "5407", "Score": "1", "body": "Two things: First, stop giving variables one-letter-names. Second, `using` the reader." }, { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-22T08:08:46.300"...
[ { "body": "<p>I would </p>\n\n<ul>\n<li>reduce the visibility of the variable <code>wsl</code> as it's not required outside the seconds <code>using</code> scope (unless you listen to peer)</li>\n<li>reduce the visibility of the variable <code>q</code> as it's not required outside the first <code>using</code> sc...
{ "AcceptedAnswerId": "3595", "CommentCount": "3", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-22T06:21:25.703", "Id": "3592", "Score": "6", "Tags": [ "c#", "mysql", "sql" ], "Title": "Read from SQL database table, store in list of objects" }
3592
<p>For some time now, I've been using a pattern which I think could be done more elegantly. I have a lot of XML files that need to be represented in a Django views. My current approach is like this:</p> <p>I get a XML response from an API which gives me several documents.</p> <p>First I define a mock class like this...
[]
[ { "body": "<p>Using a generator function gives you some flexibility. You don't have to create a list.</p>\n\n<pre><code>def generate_documents( document_nodes ):\n\n for document in document_nodes:\n # create a MockDocument for each item returned by the API\n document_object = MockDocument() ...
{ "AcceptedAnswerId": "3596", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-22T10:48:16.397", "Id": "3593", "Score": "2", "Tags": [ "python", "xml", "django", "xpath" ], "Title": "Parsing and data presentation in Django" }
3593
<p>I am writing a simpel program that can basicly do the same as piping two linux commands can do. For example, <code>ls -l | grep vars.sh</code>, this can be done on the Linux console. I need to write a programm in C that can do the same. I think I got it working but I want to know if I am doing it rite and if there a...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-06-16T20:48:24.177", "Id": "5422", "Score": "0", "body": "Homework problem?" }, { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-06-16T20:49:33.000", "Id": "5423", "Score": "0", "body": "Code looks fine, n...
[ { "body": "<p>The only other way that I could see this can be done in POSIX would be to use <code>popen()</code> rather than using raw pipes. It also returns a stream pointer that can be used with the C standard I/O functions like <code>fprintf()</code>, <code>fgets()</code>, etc. to read and/or write to the p...
{ "AcceptedAnswerId": null, "CommentCount": "6", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-06-16T20:45:49.070", "Id": "3598", "Score": "5", "Tags": [ "c", "homework" ], "Title": "pipe 2 linux commands in C" }
3598
<p>I'm trying to learn scala. It's hard.</p> <p>I currently have a tree in the form</p> <pre><code>class Node(children:List[Node], value:Int){ } </code></pre> <p>I want to calculate a total cost defined by value + the sum of the totalcost of the children. My java background made me do this:</p> <pre><code>def tota...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-23T19:27:02.113", "Id": "5436", "Score": "0", "body": "I don't quite understand how this question is not within scope. @Michael K: Could you review your closure, and if it is in fact out of scope, explain how, so I won't be asking out ...
[ { "body": "<p>There is already a <code>sum</code> function:</p>\n\n<pre><code>def totalCost = value + children.map(_.totalCost).sum\n</code></pre>\n", "comments": [], "meta_data": { "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-23T09:59:00.953", ...
{ "AcceptedAnswerId": "3605", "CommentCount": "4", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-22T19:37:12.890", "Id": "3602", "Score": "2", "Tags": [ "scala" ], "Title": "Scala list folding" }
3602
<p>I implemented the algorithm by Pr. Chrystal described <a href="http://www.personal.kent.edu/~rmuhamma/Compgeometry/MyCG/CG-Applets/Center/centercli.htm" rel="nofollow">here</a> in Haskell so could someone please tell me: Do i have implemented this algorithm correctly?<br> Initial calling of findPoints takes the firs...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-29T16:05:41.600", "Id": "5589", "Score": "0", "body": "I have solved this problem. Accepted code of problem QCJ4 [spoj](http://www.spoj.pl/problems/QCJ4) on [ideone](http://ideone.com/m9T82). I hope Mihai don't need to give away his re...
[ { "body": "<p>Not an answer, just a small improvement (not tested):</p>\n\n<pre><code>import Data.Function(on)\n\nfindAngle :: (Num a , Ord a , Floating a ) =&gt; Point a -&gt; Point a -&gt; Point a -&gt; ( Point a , Point a , Point a , a ) \nfindAngle u v t \n | u == t || v == t = (u , v , t , 10 * pi) -...
{ "AcceptedAnswerId": null, "CommentCount": "5", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-23T08:36:12.677", "Id": "3603", "Score": "5", "Tags": [ "haskell", "functional-programming" ], "Title": "Enclosing Circle Problem implementation in Haskell" }
3603
<p>So, I wrote this small library for fast DOM building with API tailored for CoffeeScript</p> <p>I feel like the code overall can be made better/smaller/faster.</p> <p>I am also not particularly happy about a few functions, namely:</p> <pre><code> # Parses out css classes and id from string like: # p#warning.b...
[]
[ { "body": "<p>A bunch of random observations. I'd rethink the way you approach \"classes\". I'd start with something like</p>\n\n<pre><code>classes = attr['class'] || []\nclasses = [classes] if typeof classes == 'string'\n</code></pre>\n\n<p>And then write or delete it back at the end.</p>\n\n<p>Creating hash...
{ "AcceptedAnswerId": "3863", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-23T20:40:13.133", "Id": "3608", "Score": "3", "Tags": [ "library", "coffeescript" ], "Title": "A library written in CoffeeScript" }
3608
<p>I want to make figures of a rectangle and a circle. How can I make the code more elegant?</p> <pre><code>import java.awt.*; import java.awt.event.*; import javax.swing.*; class r extends JPanel { public int x1,x2,y1,y2; public static double SWITCH; public r() { setBackground(Colo...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-24T11:53:54.680", "Id": "5470", "Score": "1", "body": "Why do you make `SWITCH` a `double`? If it were e.g. an `int` - or even better an `enum` with some meaningful states - you could use a `switch` statement in `paintComponent`. Just ...
[ { "body": "<p>Just a few thoughts...</p>\n\n<p>You are using x and y coordinates. Why not take advantage of the <a href=\"http://download.oracle.com/javase/6/docs/api/java/awt/Point.html\"><code>Point</code> class</a>? The <a href=\"http://download.oracle.com/javase/6/docs/api/java/awt/event/MouseEvent.html\"><...
{ "AcceptedAnswerId": "3618", "CommentCount": "3", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-23T21:15:46.383", "Id": "3609", "Score": "3", "Tags": [ "java", "swing" ], "Title": "Drawing simple shapes by mouse dragging" }
3609
<p>Provided <code>Object.prototype</code> hasn't been modified, will the following snippet work in major browsers?</p> <pre><code>var obj = {length:0}; var push = Array.prototype.push; push.call(obj,'1st value') push.call(obj,'2nd value'); </code></pre> <p>Many have responded that the above code is bad practice, but ...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-21T18:48:56.393", "Id": "5441", "Score": "4", "body": "Huh? What are you talking about?" }, { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-21T18:49:44.127", "Id": "5442", "Score": "1", "body": "Int...
[ { "body": "<p>I'm going to say no, because this is an array. It works, but you are hammering a screw at this point. Simply use an array.</p>\n", "comments": [ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-21T18:50:36.133", "Id": "5453", "Score": "0", ...
{ "AcceptedAnswerId": null, "CommentCount": "12", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-21T18:47:25.320", "Id": "3611", "Score": "6", "Tags": [ "javascript" ], "Title": "Does this work cross browser?" }
3611
<p>I have a large namespace, but I don't want enum constants to be a part of it. In cases where an enum is related with a class, I coded enums inside the class so I can reach it using the class name. However, if an enum is related with namespace functions, what is the best way to do it?</p> <ol> <li><p>Use another nam...
[]
[ { "body": "<p>I like class. Not because its better or anything, but mainly because class is a nice unit for small amounts of information that's easy to work with. namespaces less so. Some tooling often helps you work with classes better to manipulate things. </p>\n\n<p>But in practice, it's not going to ma...
{ "AcceptedAnswerId": "3707", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-24T10:32:26.763", "Id": "3617", "Score": "6", "Tags": [ "c++", "object-oriented", "enum" ], "Title": "C++ enums - which one is better?" }
3617
<p>I am progressing through the problems listed at projecteuler.com as I learn Scala (<a href="http://fromjavatoscala.blogspot.com" rel="nofollow">my blog where I am covering this experience</a>). I have written a function to produce all the Fibonacci numbers possible in an <code>Int</code> (it's only 47). However, the...
[]
[ { "body": "<pre><code>object Euler002 extends App{\n // Infinite List (Stream) of Fibonacci numbers \n def fib(a: Int = 0, b: Int = 1): Stream[Int] = Stream.cons(a, fib(b, a+b)) \n\n // Take how many numbers you want into a List \n val fibsAll = fib() takeWhile {_&gt;=0} toList\n fibsAll reverse \n}\n</cod...
{ "AcceptedAnswerId": "3622", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-24T17:56:29.523", "Id": "3619", "Score": "5", "Tags": [ "scala", "fibonacci-sequence" ], "Title": "Producing full list of Fibonacci numbers which fit in an Int" }
3619
<p>The basic requirement of my code is to accept a file input, parse it, convert it to a java type (A DTO essentially) for use in another component of the system.</p> <p>I should also note that I am not actually parsing a file from a filesystem, the FileParser will be used by a Message Driven Bean that has the content...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-25T04:38:12.283", "Id": "5488", "Score": "0", "body": "Not a criticism, just curious - any special reason for using a `LinkedHashMap`?" }, { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-25T04:55:05.287", "...
[ { "body": "<p>If you can change your file format slightly (to <code>type=value</code> or <code>type:value</code>) I'd suggest using the Java <a href=\"http://download.oracle.com/javase/6/docs/api/java/util/Properties.html\" rel=\"nofollow\">Properties</a> class. It can parse the file from a stream for you (Unte...
{ "AcceptedAnswerId": null, "CommentCount": "2", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-25T02:28:16.177", "Id": "3623", "Score": "2", "Tags": [ "java" ], "Title": "File parser to Java type design" }
3623
<p>I'm trying to make an MVC3 application with an exchangeable data layer. I currently have this in a single file (Global.asax.cs) for brievity.</p> <p>It seems to be working as I want but as I'm new to DI I really would appreciate comments on this code.</p> <pre><code>public interface IDataLayer { dynamic GetIte...
[]
[ { "body": "<p>One tip I would give is to use the <a href=\"http://nuget.org/List/Packages/Ninject.MVC3\" rel=\"nofollow\">\"Ninject for ASP NET MVC 3\"</a> NuGet package. Install that package into your project. After doing this, you should find a file named \"NinjectMVC3.cs\" located in the \"App_Start\" folder...
{ "AcceptedAnswerId": "3948", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-25T15:12:12.947", "Id": "3627", "Score": "2", "Tags": [ "c#", ".net", "asp.net", "asp.net-mvc-3" ], "Title": "Use of Ninject and an injectable DataLayer in MVC3" }
3627
<p>Whenever a button is clicked, the following need to be performed:</p> <ol> <li>All siblings of the clicked button get toggled</li> <li>All <code>edit_button</code> get toggled, except for the <code>edit_button</code> within the same span as the clicked button (note: the excluded <code>edit_button</code> can be the ...
[]
[ { "body": "<p>The selector for all <code>edit_buttons</code> can be expressed as a single selector <code>$('div#personal_info button.edit_button')</code>. </p>\n\n<p>I am tempted to rewrite the code to use closure scope to cache the selector values e.g. </p>\n\n<pre><code>// Iterate using each to define cached ...
{ "AcceptedAnswerId": "3637", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-25T23:59:41.740", "Id": "3634", "Score": "3", "Tags": [ "javascript", "jquery" ], "Title": "Toggling buttons on click" }
3634
<p>Just wondering if I could solicit some feedback on a stored procedure I'm running and whether there's a more efficient way of handling the scenario (I'm pretty sure there will be!).</p> <p>Basically I have a single SP that I call to return a list of records (Jobs) that may have one or more statuses and a sort order...
[]
[ { "body": "<p>You can use <code>CASE</code> statements in the <code>ORDER BY</code> clauses to decide which sort order to use:</p>\n\n<pre><code> SELECT (...)\nORDER BY CASE @OrderBy\n WHEN 'JobNumberDESC' THEN sk.jobnumber\n WHEN (etc.)\n END DESC,\n CASE @OrderBy\n ...
{ "AcceptedAnswerId": "3704", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-26T01:16:14.367", "Id": "3636", "Score": "2", "Tags": [ "sql", "sql-server" ], "Title": "Recommendations for stored procedure" }
3636
<p>I have some code which looks like this:</p> <pre><code>do { hr = pEnumMgmt-&gt;Next(1, &amp;mgmtObjProp, &amp;ulFetched); if (hr == S_FALSE) { break; } llCbStorage += mgmtObjProp[0].Obj.DiffArea.m_llAllocatedDiffSpace; } while (hr != S_FALSE); </code></pre> <p>It is bad because <code>hr == S_FALSE</cod...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-27T23:13:42.850", "Id": "5533", "Score": "2", "body": "I would go with the `while (true)`: all alternatives have some duplicated code somewhere." } ]
[ { "body": "<p>You could combine your loop condition and the hr assignment in one line, like this:</p>\n\n<pre><code>while ((hr = pEnumMgmt-&gt;Next(1, &amp;mgmtObjProp, &amp;ulFetched)) != S_FALSE) {\n llCbStorage += mgmtObjProp[0].Obj.DiffArea.m_llAllocatedDiffSpace;\n}\n</code></pre>\n", "comments": []...
{ "AcceptedAnswerId": "3642", "CommentCount": "1", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-26T11:38:51.817", "Id": "3639", "Score": "4", "Tags": [ "c++" ], "Title": "Most elegant way to write this loop" }
3639
<p>Is there any different in performance for the following 2 queries. I'm just wondering what is the better of the two:</p> <pre><code>var res1 = (from a in _ctx.DataContext.Actions join e in _ctx.DataContext.Events on a.EventId equals e.EventId select a).Single(a =&gt; a....
[]
[ { "body": "<p>There should not be any performance difference that is a result of the syntax used. The query syntax is just eye candy that gets converted to the same underlying code. The difference between the two is really just</p>\n\n<pre><code>_ctx.DataContext.Actions\n.Join(_ctx.DataContext.Events, blah, bl...
{ "AcceptedAnswerId": "3646", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-26T15:40:42.047", "Id": "3645", "Score": "4", "Tags": [ "c#", ".net", "linq" ], "Title": "Is there any loss in performance converting a 'where' clause to a lambda?" }
3645
<p>Before I start working on a quite <strong>JavaScript</strong> heavy application I would like you to review my JS MVC approach.</p> <ul> <li>How can it be improved?</li> <li>Are there any mistakes/issues I should be aware of?</li> </ul> <p>Since the entire app will have multiple modules I would like to split the co...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-26T19:55:08.880", "Id": "5514", "Score": "1", "body": "I dont know where to start. Why is everything global :S" } ]
[ { "body": "<blockquote>\n <p>Since the entire app will have multiple modules I would like to split\n the code into standalone modules that can be loaded as required (any\n suggestions how to do this?)</p>\n</blockquote>\n\n<p>Use a dynamic module loader such as <a href=\"http://requirejs.org/\" rel=\"nofollo...
{ "AcceptedAnswerId": null, "CommentCount": "1", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-26T18:55:46.853", "Id": "3648", "Score": "3", "Tags": [ "javascript", "design-patterns" ], "Title": "JavaScript MVC - review and suggestions" }
3648
<p>I was wondering if this code can be coded better in terms of semantics or design. Should I return the LOS as numbers? should I have the constants be read from the db in case the user or I want to change them in the future?</p> <pre><code>def get_LOS(headway = None, frequency = None, veh_per_hr = None): if frequen...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-26T21:20:47.117", "Id": "5515", "Score": "1", "body": "Edit so that the code is readable and correct. Also you might want to explain a little more about what get_LOS is actually trying to accomplish and what the values 'A' - 'F' mean....
[ { "body": "<pre><code>def get_LOS(headway = None, frequency = None, veh_per_hr = None):\n</code></pre>\n\n<p>You have no docstring. That would helpful in explaining what the parameters are doing.</p>\n\n<pre><code> if frequency != None and headway = veh_per_hr == None:\n</code></pre>\n\n<p>When checking wheter...
{ "AcceptedAnswerId": "3654", "CommentCount": "3", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-26T19:46:56.183", "Id": "3650", "Score": "3", "Tags": [ "python" ], "Title": "Simple Function in python" }
3650
<p>I'm learning a little Scala by writing a little card game. What I want to do here is check that the <code>Traversable[Team]</code> supplied has the same number of team members for each team.</p> <p>How can I clean this up?</p> <pre><code>val teamSizes = teams.map(_.members.size) require(teamSizes.foldLeft((true, t...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-27T14:04:30.110", "Id": "5522", "Score": "2", "body": "The `forall` solution seems like the optimal solution to me." } ]
[ { "body": "<p>An alternative approach is:</p>\n\n<pre><code>require (teamSizes.min == teamSizes.max)\n</code></pre>\n\n<p>but ỳour <code>forall</code>-solution expresses better the idea, that all members share the same size.</p>\n\n<p>And without measuring it, or trying to investigate my assumption, I guess tha...
{ "AcceptedAnswerId": "3684", "CommentCount": "1", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-26T20:37:05.350", "Id": "3651", "Score": "5", "Tags": [ "scala" ], "Title": "Checking a collection of Ints" }
3651
<p>How can I improve up this code?<br> Is it ok to write code using <code>Invoke</code> and <code>Action</code> so liberally or is this bad? </p> <p><em>Performance is not an issue as I'm not using <code>Invoke</code> 50,000x in a a row (a lot of other things will be done in-between, making the performance hit null).<...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-27T18:37:06.647", "Id": "5529", "Score": "0", "body": "Let me check if I understand the idea. You have 2 concepts: tasks and items. You say \"This task should be done against this item\". The relations between tasks and items are many-...
[ { "body": "<p>Instead of calling your delegates by using <code>Invoke</code> you can call them using the following easier formatting:</p>\n\n<pre><code>Action someAction = () =&gt; {};\nsomeAction();\n</code></pre>\n\n<p>Delegate invocations aren't that much slower than ordinary method calls. No need to worry a...
{ "AcceptedAnswerId": "3695", "CommentCount": "4", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-27T00:15:03.510", "Id": "3652", "Score": "4", "Tags": [ "c#", "delegates" ], "Title": "Improve my Task Loops" }
3652
<p>jQuery is a fast and concise JavaScript Library that simplifies HTML document traversing, event handling, animating, and Ajax interactions for rapid web development.</p> <blockquote> <p>jQuery is designed to change the way that you write JavaScript.</p> </blockquote> <p><strong>Useful links:</strong></p> <ul> <...
[]
[]
{ "AcceptedAnswerId": null, "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-27T06:19:25.427", "Id": "3655", "Score": "1", "Tags": null, "Title": null }
3655
A JavaScript Library that simplifies HTML document traversing, event handling, animating, and Ajax interactions for rapid web development.
[]
[]
{ "AcceptedAnswerId": null, "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-27T06:19:25.427", "Id": "3656", "Score": "1", "Tags": null, "Title": null }
3656
<p>I'm working on set of methods that locate DVR surveillance footage, based on timestaps.</p> <p>The files are in the following format: </p> <p><code>&lt;drive&gt;\&lt;year&gt;\&lt;month&gt;\&lt;day&gt;\&lt;camera&gt;\&lt;hour&gt;-&lt;minute&gt;-&lt;second&gt;.&lt;extension&gt;</code></p> <p>Example: Z:\2011\07\02\...
[]
[ { "body": "<p>String.IsnullOrEmpty instead of blank string comparisons. Also return immediately in your if and remove the else, put the else contents in a seperate explicitely named method which you return directly.</p>\n", "comments": [ { "ContentLicense": "CC BY-SA 3.0", "CreationDat...
{ "AcceptedAnswerId": "3675", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-27T14:54:16.697", "Id": "3659", "Score": "2", "Tags": [ "c#" ], "Title": "Any suggestions on how to clean up this convoluted file look-up method?" }
3659
<p>I am a C# developer, dont know much about vc++ or c++, never used it, for some reasons i have decided to use a c++ dll in my app for downloading content from the web.</p> <p>I dont want to use WebClient.</p> <p>I want it to download html content by providing Url of the resource, the dll will return the string resp...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-27T04:36:47.350", "Id": "5523", "Score": "7", "body": "Your implementation is completely broken with respect to HTTP compliance. You might be lucky if it works in more than one case. Implementing HTTP is not trivial. Use WebClient or a...
[ { "body": "<p>The above implementation has little to no chance of working. The HTTP protocol is quite complex, and none of that complexity is contemplated.</p>\n\n<p>My advice is, don't waste time and energy in this. Use a high-level library.</p>\n\n<p>Also, using low-level code will <strong>NOT</strong> improv...
{ "AcceptedAnswerId": null, "CommentCount": "1", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-27T04:05:21.340", "Id": "3660", "Score": "1", "Tags": [ "c#", "c++" ], "Title": "VC++ Fast Http Download" }
3660
<p>Rather than doing what is essentially a large switch statement for every possible type, is there a better more generic way of converting to a specific type with reflection? I've looked up TypeConverter but don't understand the documentation.</p> <pre><code>if (header.Property.PropertyType == typeof(Int32)) { hea...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-27T17:11:17.780", "Id": "5525", "Score": "0", "body": "Maybe you mean \"with reflection\"? Is reflection mandatory approach? Could you post how does your code's consumer look like?" } ]
[ { "body": "<p>You could use an extension method (if this is common), or a regular generic method with a &quot;<a href=\"http://msdn.microsoft.com/en-us/library/system.iconvertible.aspx\" rel=\"noreferrer\">IConvertible</a>&quot; constraint on the desired value then call &quot;<a href=\"http://msdn.microsoft.com...
{ "AcceptedAnswerId": "3664", "CommentCount": "1", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-27T15:14:48.000", "Id": "3663", "Score": "6", "Tags": [ "c#", "reflection" ], "Title": "Is there a better way to convert to a specific type with reflection?" }
3663
<p>I've been researching various patterns for structuring my business logic &amp; data access, particular in the context of C# and the Entity Framework. I've come up with a basic idea of how I <em>think</em> I'd like to do it, based on several SO answers:</p> <ul> <li><p><a href="https://stackoverflow.com/questions/11...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-29T23:37:56.957", "Id": "5628", "Score": "0", "body": "This looks a little too generic to really comment on, you need a bit of a problem spec before anyone can speak to implementation details at this level. Given this is architectural,...
[ { "body": "<p>By reviewing this I have two general comments to your code:</p>\n\n<h2>Transaction</h2>\n\n<p>Is there any reason to use explicit transaction scope? When you call <code>SaveChanges</code> on the context it already uses transaction. It either uses ambient transaction or create a new one database tr...
{ "AcceptedAnswerId": "3760", "CommentCount": "2", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-27T18:21:21.353", "Id": "3667", "Score": "2", "Tags": [ "c#", "design-patterns", "entity-framework" ], "Title": "Advice on approach to organizing my business logic & data access?" ...
3667
<p>I have a requirement where I need to build a grid with dynamic columns. I am dealing with a large dataset so I would like to use server-side paging, sorting, filtering logic so that I render only a page-size. I think I got the basic functionality working but just wanted to get my approach reviewed.</p> <p>An action...
[]
[ { "body": "<p>Small recommendations:</p>\n\n<ol>\n<li>It seems to me that you can remove <code>async: false</code> parameter for the <code>$.ajax</code> call.</li>\n<li>You can remove <code>result.data</code> from the data returned by the ajax call. (After that you should and the line with <code>var colD = resu...
{ "AcceptedAnswerId": "3669", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-27T21:53:44.453", "Id": "3668", "Score": "3", "Tags": [ "javascript", "jquery", "ajax", "asp.net-mvc", "url-routing" ], "Title": "JQGrid with dynamic columns and server-sid...
3668
<p>I've built a <a href="http://en.wikipedia.org/wiki/Discrete_event_simulation" rel="nofollow">discrete event simulation system</a>, similar to the bank problem presented on the wikipedia page but with a key difference.</p> <p>Let's say, that a <code>TELLER</code> can service two <code>CUSTOMERS</code> at the same ti...
[]
[ { "body": "<p>Event is some kind of stateful object being modeled as a list. Often a bad idea.</p>\n\n<pre><code>class Event( object ):\n def __init__( self, time, callback, *args, **kwargs ):\n self.time= time\n self.live= True\n self.callback= callback\n self.args= args\n ...
{ "AcceptedAnswerId": null, "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-27T21:15:15.647", "Id": "3670", "Score": "5", "Tags": [ "python", "simulation" ], "Title": "Discrete event simulation with variable intervals" }
3670
<p>The below code works fine with no errors and no problems.</p> <p>However, I think that it is too long, and I want to refactor it by separating the three parts (as commented on the code) by creating a new method for each of them, so that I can reuse them later.</p> <p>I tried to separate Part 2 and created a method...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-25T21:08:40.623", "Id": "5540", "Score": "0", "body": "@M. Tibbits I didn't know that site Mr.Tibbits thank you." }, { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2014-07-28T14:26:31.397", "Id": "104556", "Sc...
[ { "body": "<p>Well this jumped out at me as a relatively easy thing to break out. EDIT: not sure what the data type is on the makbuzQuery type -- hence the data type of the parameter of the new function</p>\n\n<pre><code>//Part 2 -- change to\nList&lt;Makbuz&gt; makbuzListesi = getMakbuzList(makbuzQuery);\n\n...
{ "AcceptedAnswerId": "3683", "CommentCount": "2", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-07T06:59:02.100", "Id": "3677", "Score": "2", "Tags": [ "c#" ], "Title": "Refactoring and creating separate methods" }
3677
<p>I have a method to parse <code>strings</code>. It can be used to parse a <code>float</code> using a specific <code>culture</code> and then get a new <code>string</code> with a specific out-<code>culture</code>. The same applies to <code>ints</code>, <code>doubles</code> and <code>decimals</code>. </p> <p>The code t...
[]
[ { "body": "<p>I have a method that does something similar in that it takes a string and converts it to a given primitive type. It uses generics so there's less code.</p>\n\n<pre><code> public static T ReadConfigItem&lt;T&gt;(string strValue, IFormatProvider formatProvider) where T : struct\n {\n T ...
{ "AcceptedAnswerId": "3691", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-28T09:35:07.750", "Id": "3686", "Score": "9", "Tags": [ "c#", "parsing" ], "Title": "Same code to parse int, float, decimal?" }
3686
<p>So, consider the following code:</p> <pre><code>public class OneConcreteTypeOfOutputFormat { private Dictionary&lt;Type, IEntityWriter&gt; writers; public Dispatcher() { this.writers = new Dictionary&lt;string, IEntityWriter(); CreateWriters(writers); } private void CreateWrite...
[]
[ { "body": "<p>I don't know if it will be useful for you, but here is one variant:<br>\nAdd to your Entity base class a method called GetWriter. This method should return IEntityWriter.\nThis IEntityWriter should have method Write() without parameters.</p>\n\n<p>Then an implementation of an Entity would be somet...
{ "AcceptedAnswerId": null, "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-28T10:22:55.930", "Id": "3689", "Score": "3", "Tags": [ "c#" ], "Title": "Type checking to perform a variation of the strategy pattern" }
3689
<p>I often use jQuery plugins for front-end functionality in web applications. This requires me to write code like this, where I am assigning a function to an object property:</p> <pre><code>var trainingDiary = $("#training_diary").fullCalendar({ dayClick: function(date, allDay, jsEvent, view) { var title = pro...
[]
[ { "body": "<p>You should use the <a href=\"http://www.adequatelygood.com/2010/3/JavaScript-Module-Pattern-In-Depth\" rel=\"nofollow\">JavaScript Module Pattern</a> to create a private scope for your function declarations:</p>\n\n<pre><code>// Directives for JSLint\n/*global console, prompt, $ */ // declaration ...
{ "AcceptedAnswerId": null, "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-28T10:32:27.653", "Id": "3690", "Score": "3", "Tags": [ "javascript", "datetime" ], "Title": "Training diary calendar" }
3690
<p>I have roughly 40 or 50 functions that are similar to the one provided by the code below.</p> <p>I kept all the common calculations between multiple functions separate such as <code>service_coverage_area</code> . I then kept all the specific calculations that are not repeated or needed by any other functions in the...
[]
[ { "body": "<blockquote>\n <p>Am I doing this right?</p>\n</blockquote>\n\n<p>Almost.</p>\n\n<blockquote>\n <p>as in is this the best practice to code utility functions? </p>\n</blockquote>\n\n<p>These aren't \"utility\" functions. They appear to be central to your app. There are rarely \"utility\" functions...
{ "AcceptedAnswerId": "3700", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-28T14:51:05.573", "Id": "3696", "Score": "3", "Tags": [ "python" ], "Title": "A large number of math type equations and functions in a python module" }
3696
<p>Should the following be considered wtf code?</p> <pre><code>abstract class BaseCat { public virtual void SayMiau() { Console.WriteLine("MIAU!"); } } class StrangeCat : BaseCat { private readonly bool _canMiau; public StrangeCat(bool canMiau) { _canMiau = canMiau; } ...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-29T13:11:41.360", "Id": "5586", "Score": "0", "body": "Relevant discussion: http://stackoverflow.com/questions/2340487/is-the-non-virtual-interface-nvi-idiom-as-useful-in-c-as-in-c" } ]
[ { "body": "<p>Yes, it should.<br>\nIt breaks the Liskov substitution principle.</p>\n\n<p>I don't know what is the NVI, but I think that the right approach in this particular case would be\nto create a sub class NormalCat: BaseCat and move the method SayMiau() to this class.</p>\n", "comments": [], "met...
{ "AcceptedAnswerId": "3711", "CommentCount": "1", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-29T07:09:15.673", "Id": "3705", "Score": "3", "Tags": [ "c#", ".net" ], "Title": "Subclassing, NVI" }
3705
<p>For a little background, I'm a contributor to SquishIt and decided that I should start cleaning up the mess of unit tests (<a href="https://github.com/Akkuma/SquishIt/blob/master/SquishIt.Tests/JavaScriptBundleTests.cs" rel="nofollow">my updated version of one set of tests</a> vs. <a href="https://github.com/jethere...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-08-08T18:49:55.267", "Id": "5947", "Score": "1", "body": "I usually shy away from inheritance until I have significant nesting inside of my method contexts. Your tests communicate intent and test behavior. That's really all that matters...
[ { "body": "<p>I've just started out using NSpec myself last night. I really like how expressive it can be, though I see how easy it is to get into a bit of a class inheritance mess (I certainly did last night! :).</p>\n\n<p>Here's my suggested way of coding your test:</p>\n\n<pre><code>public class describe_Jav...
{ "AcceptedAnswerId": "3970", "CommentCount": "1", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-29T15:19:43.323", "Id": "3712", "Score": "4", "Tags": [ "c#", "unit-testing" ], "Title": "Unit tests for the SquishIt framework" }
3712
<p>I'm trying to optimize performance for recursive directory listing with some exceptions:</p> <pre><code>function listdir($basedir) { global $root, $ignore_dirs, $ignore_files, $filetypes, $ignore_starts_with; if ($handle = @opendir($basedir)) { while (false !== ($fn = readdir($handle))) { ...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-29T17:51:06.583", "Id": "5594", "Score": "3", "body": "Optimize how? Where is the bottleneck? What have your profiling results shown?" }, { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-29T18:27:12.860", ...
[ { "body": "<p>First of all, I'd use <a href=\"http://www.php.net/~helly/php/ext/spl/classRecursiveDirectoryIterator.html\" rel=\"nofollow\">RecursiveDirectoryIterator</a>, removed globals and corrected formatting.</p>\n\n<p>Then used Xdebug + CacheGrind to try different versions.</p>\n", "comments": [], ...
{ "AcceptedAnswerId": null, "CommentCount": "3", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-29T16:18:32.857", "Id": "3713", "Score": "2", "Tags": [ "php", "recursion", "file-system" ], "Title": "Recursive directory listing" }
3713
<p>A short while ago, I have submitted a coding exercise to a potential employer. The response came back the next morning and you can guess what it was from the subject of this post. </p> <p>I am not totally at loss, but I need another programmer's perspective. Is there anything that jumps out?</p> <p>The idea of...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-29T16:51:50.873", "Id": "5591", "Score": "9", "body": "I think the question is simple and you have put much more coding efforts. I believe in 3 kind of optimizations; `\"time, space, text\"`; \"text\" optimization is called *readabilit...
[ { "body": "<p>You're doing manual memory management. That's not a good idea. In fact, that's something that you don't need to do at all in modern C++. You either use automatic objects, or use use smart pointers to dynamically allocated objects.</p>\n\n<p>In your case, there's no need to do dynamic allocation at...
{ "AcceptedAnswerId": null, "CommentCount": "4", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-29T16:45:50.490", "Id": "3714", "Score": "94", "Tags": [ "c++", "interview-questions" ], "Title": "Outputting the names of cars, without repetitions, with the number of occurrences in or...
3714
<p>I have a method with too many parameters. </p> <p>How would you refactor it?</p> <p>Things I considered so far:</p> <ul> <li>pass an array to the object and check for required keys (no code hint, phpdoc, programmer doesn't know which are required)</li> <li>pass an object (silly method with getters and setters, pr...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-29T21:52:16.140", "Id": "5641", "Score": "3", "body": "biggest argument list I've ever seen :)" } ]
[ { "body": "<p>Passing an object (class name 'Payment' perhaps?) is not that silly actually. You can add public method <code>validate()</code> to it, that could be called from <code>doDirectPayment()</code> in order to check if all required fields have been filled. </p>\n\n<p>You will probably want to make it as...
{ "AcceptedAnswerId": null, "CommentCount": "1", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-29T20:04:34.570", "Id": "3733", "Score": "7", "Tags": [ "php" ], "Title": "Payment function with too many parameters" }
3733
<p>I'm hoping some folks can take a few minutes to review some code I was working on today.</p> <p><a href="https://gist.github.com/1115202" rel="nofollow">https://gist.github.com/1115202</a></p> <p>Essentially, I'm doing a lot of work in Node.js at the moment and I'm a big fan of Promises.</p> <p>Since 99% of the w...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-30T13:01:51.340", "Id": "5654", "Score": "0", "body": "I use [After](https://gist.github.com/1115502) instead of when/then. I don't think you can get much lighter then that" }, { "ContentLicense": "CC BY-SA 3.0", "CreationD...
[ { "body": "<p>I made The following edits:</p>\n\n<ol>\n<li>Prevent the same <code>when</code> function from calling <code>pass</code> twice(therefore invalidating results)</li>\n<li>Sends <code>pass</code> function as 1st argument to avoid <code>var that = this</code></li>\n<li>It stores the first pass arg only...
{ "AcceptedAnswerId": "3744", "CommentCount": "4", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-30T05:14:28.303", "Id": "3739", "Score": "2", "Tags": [ "javascript", "node.js" ], "Title": "Please review my implementation of a simple When/Then (pseudo Promise) in javascript" }
3739
<p>I am new to erlang (2 days to be honest) and I would highly appreciate some peer review. I am doing <a href="http://www.erlang.org/course/exercises.html">these exercises</a> and was a bit stuck at this point:</p> <blockquote> <p>2) Write a function which starts N processes in a ring, and sends a message M times...
[]
[ { "body": "<pre><code>lists:map (fun ( {Process, Recipient} ) -&gt; Process ! {setRecipient, Recipient} end, ProcessPairs)\n</code></pre>\n\n<p>Whenever you use <code>lists:map</code> and don't use its return values, you almost certainly want to be using <code>lists:foreach</code> instead. <code>map</code> will...
{ "AcceptedAnswerId": null, "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-30T14:10:37.717", "Id": "3745", "Score": "10", "Tags": [ "erlang" ], "Title": "Erlang concurrency in circles" }
3745
<p>I was told this is the place to come for input/feedback on your programming. I am always looking to improve either by personal review or outside opinion/help. Would the community here be willing to provide positive criticism of my script below?</p> <pre><code>/*******************************************************...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-11-20T19:03:46.217", "Id": "58178", "Score": "0", "body": "I learned [something new](http://stackoverflow.com/a/6321226/1157100) today: `pow(x, 2)` performs the same as `x * x` with optimization on." } ]
[ { "body": "<p>A couple of comments.</p>\n\n<p>1) Why is this a free standing function and not a member?</p>\n\n<pre><code>float calc_distance( const point *a, const point *b )\n</code></pre>\n\n<p>It access members that should potentially be private. Best to make it a member.<br>\nAlso why are you passing by po...
{ "AcceptedAnswerId": "3755", "CommentCount": "1", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-31T04:33:30.377", "Id": "3754", "Score": "2", "Tags": [ "c++", "stl" ], "Title": "Reading a sequence of points and finding the closest one to the first" }
3754
<p>I am trying to get in a lot of practice for interviews so I am trying to write as much code as possible, and getting feed back from excellent programmers like "you". Can I get some general feed back on this small bit of code? Any recommendations on style, good practice, etc. is greatly appreciated.</p> <p>Normally ...
[]
[ { "body": "<p>I am not an expert on modern c++ but this is a hint I would include:</p>\n\n<p>Instead of having the method print that always prints to cout, I would also include a method where it receives the stream to print to.\nSomething like this for point (not tested):</p>\n\n<pre><code>void print(std::ostre...
{ "AcceptedAnswerId": "3764", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-31T08:03:52.740", "Id": "3757", "Score": "6", "Tags": [ "c++", "mathematics", "graph" ], "Title": "Compute random triplets of float pairs and average area of generated triangles" }
3757
<p>I'm writing a BitTorrent client. Part of the protocol is an exchange of length-prefixed messages between peers. The forms of these messages are described in the <a href="http://www.bittorrent.org/beps/bep_0003.html#all-non-keepalive-messages-start-with-a-single-byte-which-gives-their-type">official specification</a>...
[]
[ { "body": "<p>Can't really complain about most of it.</p>\n\n<p>The only thing I dislike is the abuse of auto:</p>\n\n<pre><code> auto m = static_cast&lt;const Message*&gt;(this);\n const auto payload = m-&gt;payload(); // get the payload\n const auto data_type_size = sizeof(header_type)+pa...
{ "AcceptedAnswerId": "3786", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-08-01T01:25:47.383", "Id": "3770", "Score": "8", "Tags": [ "c++", "c++11", "memory-management", "networking" ], "Title": "BitTorrent peer protocol messages" }
3770
<p>I found the following two types of exception-handling in Business Logic Layer. </p> <p><a href="http://rads.stackoverflow.com/amzn/click/0470396865" rel="nofollow noreferrer">ASP.NET 3.5 Enterprise Application Development</a> uses a similar method like the first one (I read it few years ago).</p> <p>I also found <...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-29T20:32:57.117", "Id": "5703", "Score": "1", "body": "As an aside, you don't have to pass a `List<string>` as a `ref` argument. `List<T>`, like all classes, is a reference type, so changes made to `errors` by `InsertUser()` will alway...
[ { "body": "<p>My approach is to catch and log/report errors at the highest point in the call stack possible, unless I have a way of actually <em>handling</em> them (ie I can take steps to allow the app to continue processing)in which case I'll add a try/catch at the appropriate point. </p>\n\n<p>Since it looks ...
{ "AcceptedAnswerId": "3775", "CommentCount": "1", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-29T19:14:57.720", "Id": "3773", "Score": "8", "Tags": [ "c#", ".net", "comparative-review", "error-handling", "asp.net" ], "Title": "Place try/catch in business logic or us...
3773
<p>I guess I might use delegates. But I'm not certain if I can apply for it. </p> <p>Sorry if the code is a mess. I'm a beginner and am still learning. For that reason, I need a little of help to improve this code. It's not finished.</p> <p>I'm creating a Math quiz system. And there are many questions as you will b...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-29T21:13:37.660", "Id": "5710", "Score": "3", "body": "I think you mean generics, not delegates?" }, { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-29T21:14:07.037", "Id": "5711", "Score": "1", "bo...
[ { "body": "<p>You can remove duplication from this code. There are a couple of ways to do this but as a result you'll get really complicated code. I think you should leave it as it is now. There are situations when it's better to have some duplication than to have complicated code.</p>\n", "comments": [], ...
{ "AcceptedAnswerId": null, "CommentCount": "8", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-07-29T21:09:37.860", "Id": "3780", "Score": "-2", "Tags": [ "c#", "beginner", "wpf" ], "Title": "Can I improve this code using delegates?" }
3780
<p>I'm just looking for more feedback on style/approach regarding this small problem. Also, if you have tips to make it more readable or C++-like, please let me know.</p> <p>Also, why is the problem asking for the sum and mean of each name and all names, while the sum of a single element is itself? Am I missing anythi...
[]
[ { "body": "<p>Comments:</p>\n\n<p>At the file scope all words beginning with underscore are reserved.<br>\nSee: <a href=\"https://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier/228797#228797\">https://stackoverflow.com/questions/228783/what-are-the-rules-about-...
{ "AcceptedAnswerId": "3793", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-08-01T09:45:24.167", "Id": "3783", "Score": "3", "Tags": [ "c++", "stream" ], "Title": "Compute and print the sum and mean of name and value pairs" }
3783
<p>What I am creating is essentially a binary clock face with touch input for setting values as well as displaying - so I need to convert both ways between an int and a binary sequence in a BitArray.</p> <p>I cobbled this together (borrow and modifying a bit from a couple different examples I found):</p> <pre><code> ...
[]
[ { "body": "<p>You've made it much more complicated than necessary.</p>\n\n<p>The conversion to a <code>BitArray</code> needlessly copies the values to the bool array <code>bits</code>. You could instead use that on the conversion back to <code>int</code>.</p>\n\n<pre><code>public static class BinaryConverter\n...
{ "AcceptedAnswerId": "3797", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-08-02T03:09:14.860", "Id": "3796", "Score": "12", "Tags": [ "c#", ".net", "converting" ], "Title": "Converting binary value from BitArray to an int and back in C#" }
3796
<p>I wrote below code to crypt and decrypt some bytes in three algorithm with Java but I do not know if I wrote them in correct mode or not. Please tell me about truth of code.</p> <p>First class:</p> <pre><code>public class Cryptography { Cryptography() {} public byte[] Encryption_AES128(byte[] plain , by...
[]
[ { "body": "<p>You may want to use \"DESede/CBC/PKCS5Padding\" and \"AES/CBC/PKCS5Padding\" for added security.</p>\n\n<ul>\n<li><p>\"CBC\" is chained cipher block. Both AES and DES are symmetric block ciphers, they encrypt only a fixed block of at a time. Basically, CBC makes the cipher retain state from prev...
{ "AcceptedAnswerId": null, "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-08-02T06:10:22.670", "Id": "3798", "Score": "5", "Tags": [ "java", "cryptography" ], "Title": "DES, Triple DES and AES-128 code in Java" }
3798
<p>I will make it more reader friendly after I get the algorithm completed. Does anything with the quick sort algorithm stand out as for why it is not working 100%? This is not homework; I am just practicing problems out of books and I am caught on this one.</p> <p>Hints, more so than answers, would be more appreciate...
[]
[ { "body": "<p>I have a couple of suggestions.</p>\n\n<ol>\n<li><p>Don't wait until you are finished to make your code reader friendly. That's a bad habit to develop because of situations like the one you are in now. You'd like someone else to look at your code but it's not as easy to read as it should be, so ...
{ "AcceptedAnswerId": "3817", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-08-02T09:36:17.663", "Id": "3799", "Score": "4", "Tags": [ "c++", "sorting", "quick-sort" ], "Title": "My own quicksort algorithm" }
3799
<p>This is a short piece of code but I feel like it could be done more elegantly. What am I missing?</p> <p>The goal: if there are any items in a list, and none of them have a given property, set one of them to have that property.</p> <pre><code>static void GuaranteeAtLeastOne&lt;T&gt;(IEnumerable&lt;T&gt; list, Func...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-08-03T19:12:28.997", "Id": "5790", "Score": "2", "body": "I think you've done the best you can." } ]
[ { "body": "<p>I wouldn't assume to go for the First, rather I'd have a <code>Func&lt;T, bool&gt; predicate</code> parameter which you pass to a <code>.FirstOrDefault(predicate)</code> and <code>??</code> it with <code>.First()</code></p>\n\n<p>Also, I'd verify <code>IEnumerable&lt;T&gt; != null</code> before an...
{ "AcceptedAnswerId": "3804", "CommentCount": "1", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-08-02T13:53:44.013", "Id": "3802", "Score": "5", "Tags": [ "c#", ".net", "linq" ], "Title": "Guarantee at least one in a list" }
3802
<pre><code>&lt;?php class Database { private $_dbh; private $_stmt; private $_queryCounter = 0; public function __construct($user, $pass, $dbname) { $dsn = 'mysql:host=localhost;dbname=' . $dbname; //$dsn = 'sqlite:myDatabase.sq3'; //$dsn = 'sqlite::memory:'; $optio...
[]
[ { "body": "<p>I think adding a new method to your existing class and calling existing query,bind and execute methods should be enough: </p>\n\n<pre><code>public function query2($query,$params) { \n $this-&gt;query($query); \n foreach($params as $k=&gt;$v){\n $this-&gt;bind($k, $v);\n ...
{ "AcceptedAnswerId": "11131", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-08-02T17:05:54.080", "Id": "3806", "Score": "6", "Tags": [ "php", "mysql", "pdo" ], "Title": "PDO wrapper class" }
3806
<p>I have a NumPy array of about 2500 data points. This function is called on a rolling basis where 363 data points is passed at a time.</p> <pre><code>def fcn(data): a = [data[i]/np.mean(data[i-2:i+1])-1 for i in range(len(data)-1, len(data)-362, -1)] return a </code></pre> <p>This takes about 5 seconds to r...
[]
[ { "body": "<p><code>range</code> returns a list. You could use <code>xrange</code> instead.</p>\n\n<blockquote>\n<pre><code>range([start,] stop[, step]) -&gt; list of integers\n</code></pre>\n \n <p>Return a list containing an arithmetic progression of integers.</p>\n</blockquote>\n\n<p>vs</p>\n\n<blockquote...
{ "AcceptedAnswerId": "3827", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-08-02T18:29:16.250", "Id": "3808", "Score": "2", "Tags": [ "python", "array", "numpy" ], "Title": "Rolling loop with an array of data points" }
3808
<p>I've got a case where I need to call a couple of methods, checking if they return null, and return some property from the result of the second when neither returns null. </p> <p>I've thought of two ways to do this - one "verbose" and one "concise". For the purposes of this question, I'll just call the return types ...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-08-02T20:47:50.597", "Id": "5768", "Score": "1", "body": "I fail to see how your second example is more concise. As measured by lines of code it is actually less concise... and harder to read as well." }, { "ContentLicense": "CC ...
[ { "body": "<p>I would modify CallMethodB to return null when it is passed null and then use the following:</p>\n\n<pre><code>private dynamic VerboseVersion()\n{\n var b = CallMethodB( CallMethodA() );\n if ( b == null )\n {\n return null;\n }\n else\n {\n return b.SomeProperty;\n ...
{ "AcceptedAnswerId": "3821", "CommentCount": "3", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-08-02T19:21:42.917", "Id": "3810", "Score": "5", "Tags": [ "c#" ], "Title": "Concise null checking vs readability" }
3810
<p>I'm writing a method that takes (1) an array of <code>int</code>s and (2) an <code>int</code>. The purpose of the method is to find the indices of the two numbers in the array that add up to the value passed in as the second parameter to the method (sum) and return those values in an <code>int[]</code>. Exactly tw...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-08-02T19:49:58.077", "Id": "5757", "Score": "4", "body": "Who told you they're bad? I believe it's a matter of opinion." }, { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-08-02T19:53:53.083", "Id": "5758", "...
[ { "body": "<p>There's nothing wrong with a break statement used outside a switch statement. (2nd example)</p>\n\n<p>If a professor or some other authority figure is enforcing that rule, that's a pain, but the code is perfectly fine. There's even a similar example in the <a href=\"http://download.oracle.com/java...
{ "AcceptedAnswerId": "3814", "CommentCount": "9", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-08-02T19:47:03.513", "Id": "3813", "Score": "3", "Tags": [ "java", "comparative-review" ], "Title": "Finding array indices that add up to another number" }
3813
<p>This code gets me where I need to be, but my word it is ugly. </p> <p>I am checking for the existence of two $_GET variables with if() statements. I am setting the variables to empty strings first so I do not get an error when I echo them back.</p> <pre><code>$getvar1 =''; $getvar2 =''; $np =''; if(isset($_GET['g...
[]
[ { "body": "<p>How about writing a function like this:</p>\n\n<pre><code>function getOrDefault($index, $default)\n{\n return (!isset($_GET[$index]) || empty($_GET[$index]) ? $default : $_GET[$index]);\n}\n\n$getVar1 = getOrDefault('getvar1', '');\n$getVar2 = getOrDefault('getvar2', '');\n</code></pre>\n\n<p>T...
{ "AcceptedAnswerId": "3832", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-08-02T20:20:39.010", "Id": "3819", "Score": "4", "Tags": [ "php" ], "Title": "PHP: checking for $_GET variables with if statements" }
3819
<p>I'm just curious if this is clear to the average person.</p> <pre><code>template&lt;typename IteratorType&gt; inline IteratorType skip_over( IteratorType begin, IteratorType end, typename std::iterator_traits&lt;IteratorType&gt;::value_type skippedCharacter) { typedef typename std::iterator_traits&l...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-08-03T03:56:06.687", "Id": "5777", "Score": "0", "body": "It took me a while to work out what it did (comments would not go amiss)." } ]
[ { "body": "<p>A few comments:</p>\n\n<ul>\n<li><p>Algorithms typically use the names <code>first</code> and <code>last</code> for the iterators they take, not <code>begin</code> and <code>end</code> (in common usage, <code>begin</code> and <code>end</code> refer specifically to the iterators that delimit a rang...
{ "AcceptedAnswerId": null, "CommentCount": "1", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-08-03T01:37:17.850", "Id": "3830", "Score": "5", "Tags": [ "c++", "algorithm", "iterator" ], "Title": "Skip_over() algorithm" }
3830
<p>I've been seeking to optimize this algorithm, perhaps by eliminating one of the loops, or with a better test to check for prime numbers.</p> <p>I'm trying to calculate and display 100000 prime numbers has the script pausing for about 6 seconds as it populates the list with primes before the primes list is returned ...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-08-03T18:10:55.187", "Id": "5787", "Score": "3", "body": "Take a look at http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes for ideas." } ]
[ { "body": "<p>Welcome to programming and code review!</p>\n\n<p>Your code is pretty good especially for a beginner, but I'm going to be picky:</p>\n\n<pre><code>from time import time\n</code></pre>\n\n<p>I recommend putting at least one blank line between the imports and the code proper</p>\n\n<pre><code>odd = ...
{ "AcceptedAnswerId": null, "CommentCount": "1", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-08-03T03:48:57.560", "Id": "3833", "Score": "4", "Tags": [ "python", "optimization", "algorithm", "beginner", "primes" ], "Title": "Optimizing this 'print up to the nth prime nu...
3833
<p>I have an mail object that returns its headers as an Enumerator of Header objects, which have two methods, getName and getValue. I need to convert that to a nice Clojure map so I wrote this short function:</p> <pre><code>(defn extract-headers [message] (reduce (fn [hs h] (conj hs {(.getName h) (.getValue h)})) ...
[]
[ { "body": "<p>You can use <code>into</code>, for example: </p>\n\n<pre><code>(defn extract-headers [message]\n (let [headers (enumeration-seq (.getAllHeaders message))]\n (into {} (map #(vector (.getName %1) (.getValue %1)) headers))))\n</code></pre>\n\n<p>first, with <code>map</code> you build sequence ...
{ "AcceptedAnswerId": "3840", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-08-03T06:11:43.780", "Id": "3834", "Score": "2", "Tags": [ "clojure" ], "Title": "From enumerator to map" }
3834
<p>I am using the following template to program my 2D games in. Is there any way I can improve it?</p> <p><strong>Splash screen:</strong></p> <pre><code>package com.dingle.template2d; import android.app.Activity; import android.content.Intent; import android.os.Bundle; public class template2d extends Activity { ...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2015-03-18T19:28:07.987", "Id": "151854", "Score": "0", "body": "When you say you put all the game code in the `GameView` class, do you mean only rendering and UI code? Or are you putting all of your game model in this class also?" } ]
[ { "body": "<p>Notions in no particular order, not probably what you're expecting though:</p>\n\n<p><strong>On the unpredictability of sleep</strong> </p>\n\n<p>In the loop </p>\n\n<pre><code>int waited = 0;\nwhile(_active &amp;&amp; (waited &lt; _splashTime)) {\n sleep(100);\n if(_active) {\n waited +=...
{ "AcceptedAnswerId": "3839", "CommentCount": "1", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-08-03T08:35:11.160", "Id": "3838", "Score": "8", "Tags": [ "java", "game", "android", "template" ], "Title": "Template for creating 2D games" }
3838
<p>How would I accomplish the same thing in a functional paradigm?</p> <pre><code>Player.prototype.d2 = function(ratingList, rdList) { var tempSum = 0; for (var i = 0; i &lt; ratingList.length; i++) { var tempE = this.e(ratingList[i], rdList[i]); tempSum += Math.pow(this.g(rdList[i]), 2) * temp...
[]
[ { "body": "<p>I think something like this will work for you.</p>\n\n<pre><code>Array.prototype.zip = function(other) {\n if (!Array.prototype.isPrototypeOf(other)) {\n throw new TypeError('Expecting an array dummy!');\n }\n if (this.length !== other.length) {\n throw new Error('Mu...
{ "AcceptedAnswerId": "3845", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-08-03T14:25:18.877", "Id": "3842", "Score": "1", "Tags": [ "javascript", "functional-programming" ], "Title": "Replacing a for loop that iterates over an array with the functional program...
3842
<p>This is my personal project. This class is responsible for running jobs asynchronously that are registered using dependency injection.</p> <p>All improvement suggestions are welcome. It can be as small as using a better name for a variable, re-architect the whole class, whatever. I'm just trying to improve myself....
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-08-03T17:56:09.113", "Id": "5785", "Score": "1", "body": "You should try to focus your review question to a particular area. See FAQ: http://codereview.stackexchange.com/faq#make-sure-you-include-your-code-in-your-question" } ]
[ { "body": "<p>Taking the processqueue part.... I'd do something like :- </p>\n\n<pre><code> private void ExecuteJob(Tuple&lt;Type,int&gt; job)\n {\n try\n { \n Execute(job.Item1, job.Item2); \n } \n ...
{ "AcceptedAnswerId": null, "CommentCount": "1", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-08-03T16:33:50.957", "Id": "3848", "Score": "4", "Tags": [ "c#", "asynchronous", "scheduled-tasks" ], "Title": "Asynchronous task runner" }
3848
<p>I am learning Scala while solving some exercises and I am currently solving an exercise where I need to initialize a <a href="http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes" rel="nofollow">Sieve of Eratosthenes</a>.</p> <p>I am using the following code:</p> <pre><code> val sieve = Array.fill[Boolean](100)(tru...
[]
[ { "body": "<p>There are many other ways of writing a Sieve of Eratosthenes in Scala. Regarding this particular code, the for-comprehension is better written as:</p>\n\n<pre><code>val sieve = Array.fill[Boolean](100)(true)\n\nfor {\n p &lt;- 2 until sieve.length \n if sieve(p) &amp;&amp; math.pow(p, 2) &lt;= s...
{ "AcceptedAnswerId": "3853", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-08-03T22:31:20.920", "Id": "3850", "Score": "3", "Tags": [ "scala", "sieve-of-eratosthenes" ], "Title": "Initialize a Sieve of Eratosthenes in Scala" }
3850
<p>I am working on using STL and in general safe C++ practices. Instead of posting large samples of code I am working with a small sample to catch problems before I move onto larger ones.</p> <p>How do you find out if an <code>istringstream</code> found an input that is not of the desired type? That is, you have a st...
[]
[ { "body": "<p>Here are two small points regarding the formatting:</p>\n\n<ol>\n<li><p>In <code>all_numeric_arguments</code> after introducing <code>v_tmp</code>, you should call <code>v_tmp.reserve(argc);</code> so that you won't do to many reallocations. (This is purely an optimization which is probably not im...
{ "AcceptedAnswerId": "3874", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-08-04T05:16:58.077", "Id": "3857", "Score": "6", "Tags": [ "c++", "strings", "console" ], "Title": "Determining which command line arguments are numeric" }
3857
<p>Looking at <code>$wpdb</code> class, I found <code>get_row</code>, and I am unsure if the function automatically prepares the query. I believe it does not and I may have to reexamine my code to prevent a future disaster.</p> <p>Here is what my code looks, and I feel that it is vulnerable:</p> <pre><code>$wpdb-&gt;...
[]
[ { "body": "<p>Yes, you should prepare the statement or at least SQL escape the value of <code>$_GET['fromUrl']</code>. It's good practice to be paranoid about any input potentially provided by the user as it will be wrong at some point. </p>\n\n<p>The <a href=\"http://codex.wordpress.org/Class_Reference/wpdb#Pr...
{ "AcceptedAnswerId": "3869", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-08-04T08:17:54.297", "Id": "3862", "Score": "2", "Tags": [ "php", "mysql", "security", "wordpress" ], "Title": "Wordpress $wpdb->row Query Check" }
3862