body
stringlengths
25
86.7k
comments
list
answers
list
meta_data
dict
question_id
stringlengths
1
6
<p>I have just started playing around with L4 and would like to get some feedback on how I implemented a multi-page form</p> <p>Here are my routes:</p> <pre><code>Route::model('appointment', 'Appointment'); Route::get('appointment/book_new/{appointment}', array('as' =&gt; 'booking-form', 'uses' =&gt; 'AppointmentBook...
[]
[ { "body": "<p>Looks pretty standard to me! The only minor tweak I would make just for readability is to eliminate the <code>else</code> in <code>AppointmentBookingController::verify()</code>; since you <code>return</code> in the <code>if</code>, the <code>else</code> block is not necessary, and using guard clau...
{ "AcceptedAnswerId": null, "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-05-31T03:02:36.043", "Id": "26918", "Score": "1", "Tags": [ "php", "mvc", "laravel" ], "Title": "Multi-page form attempt" }
26918
<p>I have a WCF service, which has a method for updating a EF entity. The update method works just fine but looks ugly. I think due to some EF design I have to manually set the change tracker state to <code>Modified</code> or <code>Deleted</code> or <code>Added</code>. The update method in the server side looks like th...
[]
[ { "body": "<p>You shouldn't expose entities in WCF service. Every change in entity will cause WCF webservice change and incompatibility for existing consumers. I would create decicated data transfer objects for WCF webservice and use library like Automapper to handle property mapping. </p>\n\n<p>EF entities oft...
{ "AcceptedAnswerId": null, "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-01T14:14:11.560", "Id": "26919", "Score": "2", "Tags": [ "c#", "entity-framework" ], "Title": "Updating entities with EF (Delete, Add, Update) with one method" }
26919
<p>is there a better why how can I refactor this code, making sure that values in a <code>Hash</code> are typecasted to <code>true/false</code> if their value is <code>'1'</code> or <code>'0'</code> while leaving unaltered the rest?</p> <p>I'm using Ruby 2.0.0 if that matters, and I'd like to improve this code.</p> <...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-03T14:27:58.090", "Id": "41739", "Score": "0", "body": "don't select an answer so soon! leave time for others to answer." }, { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-03T14:38:25.950", "Id": "41740", ...
[ { "body": "<p>Code should be as declarative as possible (usually by using <a href=\"https://code.google.com/p/tokland/wiki/RubyFunctionalProgramming\" rel=\"nofollow\">functional style</a>):</p>\n\n<pre><code>def transform\n special_values = {\"1\" =&gt; true, \"0\" =&gt; false}\n Hash[preferences.map { |k, v...
{ "AcceptedAnswerId": "26933", "CommentCount": "3", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-03T13:55:09.390", "Id": "26923", "Score": "1", "Tags": [ "ruby" ], "Title": "Manipulate Hash to typecast true/false for certain values" }
26923
<p>I have a small WPF application which I'm not exactly localizing, but not hard-coding resources either. I know WPF doesn't use .resx files for that purpose, but I exposed the resource strings as public ViewModel properties which the view's controls bind to:</p> <pre><code>using resx = MyApp.Properties.Resources; ......
[]
[ { "body": "<p>I have had the same thought when implementing localisation in WPF, and came to the conclusion that .resx files are fine. I came to that conclusion because alternatives quickly became more complicated than I could be bothered with, and a RESX-based solution works for me.</p>\n\n<p>So, in my various...
{ "AcceptedAnswerId": "26965", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-03T14:13:19.360", "Id": "26928", "Score": "5", "Tags": [ "c#", "wpf", "localization" ], "Title": "WPF Localisation - using resx" }
26928
<p>I have a deep equality comparison method that returns <code>true</code> when two objects are equal (<a href="http://www.boilerjs.com/#api_isEqual" rel="nofollow">see method Doc</a>):</p> <pre><code>_.isEqual = function (obj1, obj2) { // Quick compare objects that don't have nested objects if (_.type(obj1) === ...
[]
[ { "body": "<p>After going through the code and the library itself, here are some thoughts:</p>\n\n<ul>\n<li><p>Functions are still objects. You can even attach properties to it. You might want to look into this unless you only want to compare the function, excluding the things attached to it.</p>\n\n<pre><code>...
{ "AcceptedAnswerId": null, "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-03T14:16:20.057", "Id": "26929", "Score": "2", "Tags": [ "javascript", "performance", "library-design" ], "Title": "Boiler.js Deep Object Comparison Code/Performance Optimizations?" ...
26929
<p>The purpose is just to create a dictionary with a path name, along with the file's SHA-256 hash.</p> <p>I'm very new to Python, and have a feeling that there's a much better way to implement the following code. It works, I'd just like to clean it up a bit. fnamelst = [r'C:\file1.txt', r'C:\file2...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-03T14:20:10.763", "Id": "41735", "Score": "1", "body": "Why are you replacing slashes??" }, { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-03T14:20:58.690", "Id": "41736", "Score": "1", "body": "We...
[ { "body": "<p>In python, <code>for iter in range(0,len(container)):</code> is pretty much always a bad pattern.</p>\n\n<p>Here, you can rewrite :</p>\n\n<pre><code>for f in fnamelst:\n dic2 = {f: hashlib.sha256(open(f, 'rb').read()).digest()}\n</code></pre>\n\n<p>As @tobias_k pointed out, this doesn't do muc...
{ "AcceptedAnswerId": null, "CommentCount": "3", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-03T14:18:40.437", "Id": "26931", "Score": "1", "Tags": [ "python", "hash-map" ], "Title": "Creating a dictionary with a path name and SHA-256 hash" }
26931
<p>I was solving one of the problem in USACO 2007 Open Silver:</p> <blockquote> <p><strong>Description</strong></p> <p>Farmer John has taken his cows on a trip to the city! As the sun sets, the cows gaze at the city horizon and observe the beautiful silhouettes formed by the rectangular buildings.</p> <p...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-03T21:43:02.093", "Id": "41773", "Score": "0", "body": "Is this algorithm complexity O(n log n)? I'm thinking that using radix sort, it should be possible to get it down to O(n)." } ]
[ { "body": "<p>I don't know for sure why your program is running out of time (since I know nothing about the target platform), but my guess is that it's either item (1) or (2) in the list below. These errors lead to <em>undefined behaviour</em>, and then, well, anything could happen (that's what <em>undefined</e...
{ "AcceptedAnswerId": null, "CommentCount": "1", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-03T14:52:57.073", "Id": "26938", "Score": "3", "Tags": [ "optimization", "c", "programming-challenge" ], "Title": "Why is this divide and conquer algorithm inefficient?" }
26938
<p>I've tried to write an "OOP" approach to printing a website using the DOMDocument class. I'm kind of stuck here because I feel like I am blundering in how I am passing and using the DOMDocument. I found that I can just return the object from the class and use it that way. </p> <p>What are some safe practices here? ...
[]
[ { "body": "<h2>Some things I spot on the fly:</h2>\n\n<ul>\n<li>Lack of documentation. <strong><a href=\"http://www.phpdoc.org/\" rel=\"nofollow\">PHPDoc</a></strong> works well, every method, every class, every file should have a DocBlock.</li>\n<li>Naming conventions. It's considered good naming convention to...
{ "AcceptedAnswerId": "29233", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-03T18:43:21.733", "Id": "26947", "Score": "5", "Tags": [ "php", "beginner", "dom" ], "Title": "OOP approach to using DOMDocument" }
26947
<p>I always considered <code>switch</code> statement as somehow defective:</p> <ul> <li>works only on integral types and enumerations.</li> <li>isn't an readability improvement over traditional if/else chain.</li> <li>forget a <code>break</code> - you have a bug.</li> <li>variable declaration spills over neighbouring ...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-05T00:58:36.253", "Id": "41868", "Score": "0", "body": "Not sure I agree wiht: `isn't an readability improvement over traditional if/else chain.` or `forget a break - you have a bug` lot of functionality for fall through and when you d...
[ { "body": "<p>Lisp has an uniform syntax, so it’s possible to create user defined flow control constructs that look exactly like the built in constructs. In C++, that’s generally not possible.</p>\n\n<p>Your code is certainly clever, and it solves the problem of omitted breaks (I consider the other drawbacks yo...
{ "AcceptedAnswerId": "26968", "CommentCount": "3", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-03T20:54:33.977", "Id": "26952", "Score": "4", "Tags": [ "c++", "c++11" ], "Title": "Creating my own flow control statements in C++. Is that acceptable?" }
26952
<p>There is repetition here and would appreciate help in improving this code. Suggestions? Here's the framework for it:</p> <pre><code>Class Author { private Integer id; public static List&lt;Book&gt; sortBooksAlphabetically( Author a ) { Integer id = a.id; if (id == null) { return null; } List&lt;Bo...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-03T23:03:26.123", "Id": "41777", "Score": "0", "body": "http://qconlondon.com/london-2009/presentation/Null+References:+The+Billion+Dollar+Mistake ;-)" }, { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-04T00:0...
[ { "body": "<p>Tentative answer...</p>\n\n<p>First of all, it looks like you didn't include the source of the <em>full</em> <code>Author</code> class. Where is the list of <code>Book</code>s for that author, for instance?</p>\n\n<p>Second: you seem to want methods to sort this inner list of books for a given aut...
{ "AcceptedAnswerId": null, "CommentCount": "8", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-03T22:13:33.233", "Id": "26956", "Score": "0", "Tags": [ "java" ], "Title": "refactoring and optimization the class" }
26956
<p>I've tried to catch all exceptions and errors in PHP in a way so that I can deal with them consistently.</p> <p>This is the code with the exception of the very last bit where I instead pass <code>$e</code> on to a method that outputs an error page as HTML.</p> <pre><code>// Only let PHP report errors in dev error_...
[]
[ { "body": "<ol>\n<li><p>I don't think that printing raw errors/data to the users is a good idea:</p>\n\n<blockquote>\n<pre><code>var_dump($e);\n</code></pre>\n</blockquote>\n\n<p>They (usually) can't fix it, so it doesn't help them. (<a href=\"https://codereview.stackexchange.com/a/45075/7076\">See #3 here</a>)...
{ "AcceptedAnswerId": null, "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-03T23:47:58.903", "Id": "26957", "Score": "6", "Tags": [ "php", "error-handling" ], "Title": "Catching all exceptions and errors in PHP" }
26957
<p>I'm taking a text file that looks like this:</p> <blockquote> <pre><code>77, 66, 80, 81 40, 5, 35, -1 51, 58, 62, 34 0, -1, 21, 18 61, 69, 58, 49 81, 82, 90, 76 44, 51, 60, -1 64, 63, 60, 66 -1, 38, 41, 50 69, 80, 72, 75 </code></pre> </blockquote> <p>and I'm counting which numbers fall in a certain bracket:</p>...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-07T09:32:47.040", "Id": "42109", "Score": "2", "body": "Is 70 meant to be ignored or is that a mistake?" }, { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2016-11-24T06:18:46.120", "Id": "278206", "Score": "0"...
[ { "body": "<p>Looks good, but there are a few things I've noticed that could be better:</p>\n\n<ul>\n<li>You should use the <code>with</code> statement when working with files. This ensures that the file will be closed properly in any case.</li>\n<li><p>I would split the input string on <code>','</code> instead...
{ "AcceptedAnswerId": null, "CommentCount": "2", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-04T01:04:11.230", "Id": "26958", "Score": "6", "Tags": [ "python", "interval" ], "Title": "Counting which numbers fall in a certain bracket" }
26958
<p>I'm learning BackboneJS and I just made an attempt at converting a pre-existing module to a Backbone.View. I was hoping to get some feedback on my attempt and learn. I've been using the <a href="http://backbonejs.org/docs/todos.html" rel="nofollow noreferrer">annotated ToDo source</a> as a guide.</p> <p>Here's some...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-05T19:53:10.190", "Id": "41938", "Score": "0", "body": "RequireJS, correct?" }, { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-05T21:44:54.687", "Id": "41943", "Score": "0", "body": "Hmm? Yes, the ...
[ { "body": "<p>I think this is fine for something as simple as a volume control; however, there are some limitations to at least be aware of:</p>\n\n<ol>\n<li>Since RequireJS invokes the module, it would be problematic to\nconstruct <code>player</code> dynamically.</li>\n<li>There's no good way of creating more ...
{ "AcceptedAnswerId": "27106", "CommentCount": "4", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-04T04:10:45.810", "Id": "26963", "Score": "2", "Tags": [ "javascript", "jquery", "backbone.js", "require.js" ], "Title": "Control consisting of a mute button and an expanding ...
26963
<p>I have two arrays:</p> <ul> <li>first one is ip addresses from billing for blocking</li> <li>second is ip addresses from firewall - already blocked</li> </ul> <p>My task is, compare it, and if ip address new in billing list - block it (run a command) if ip address not in billing list unblock it.</p> <p>Current s...
[]
[ { "body": "<p>The completely procedural way to write the same is:</p>\n\n<pre><code>foreach my $fw_ip (@ipfw_ip) {\n foreach my $bill_ip (@bill_ip) {\n if($fw_ip eq $bill_ip) {\n ...\n last;\n }\n }\n}\n</code></pre>\n\n<p>The main advantage is that it is easy to understand...
{ "AcceptedAnswerId": "27104", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-04T05:21:02.627", "Id": "26966", "Score": "0", "Tags": [ "array", "perl" ], "Title": "Comparing two arrrays" }
26966
<p>I've tried to improve the speed of my query in MySQL, but when condition <code>IN</code> clause happens too often, the query runs very slowly. Can anyone help me optimize the query?</p> <pre><code>EXPLAIN SELECT bi.syouhin_sys_code AS syouhin_sys_code ,dwh_syo.name AS syouhin_sys_name ,dwh_syo.co...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-04T13:36:23.630", "Id": "41809", "Score": "1", "body": "Welcome to Code Review. This is a very specific question with little information about your own research into the problem, and as such is likely to be [closed](http://codereview.s...
[ { "body": "<p>Optimizing this query can only properly be done on a system where the data exists, and you can 'play' a little bit. There are four things I recommend that you improve:</p>\n\n<ol>\n<li>temporary table</li>\n<li>better case statements</li>\n<li>better joins</li>\n<li>LIKE</li>\n</ol>\n\n<h2>Temp ta...
{ "AcceptedAnswerId": null, "CommentCount": "4", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-04T09:55:59.680", "Id": "26974", "Score": "2", "Tags": [ "optimization", "mysql", "sql" ], "Title": "Speed up MySQL query with IN clause" }
26974
<p>I come from the world of Python, so I'm used to every file representing their own enclosed module, never polluting the global environment. Up until now (I've worked with Ruby for a week) I've created one file per class in a nested structure such as this:</p> <pre>lib `- project_name |- sub_module | `- foo_ba...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-04T22:45:21.390", "Id": "41858", "Score": "0", "body": "In ruby, it is standard practice to indent two spaces (not four). So a double indent is just four spaces, if that helps you cope with it." }, { "ContentLicense": "CC BY-SA...
[ { "body": "<p>I use the version with double indent and I think it is the common usage:</p>\n\n<pre><code>module ProjectName\n module SubModule\n # Double indent for all the code in the file!?\n end\nend\n</code></pre>\n\n<p>If you don't like the double indention, just don't do it. </p>\n\n<p>It is ...
{ "AcceptedAnswerId": "26998", "CommentCount": "7", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-04T10:33:44.357", "Id": "26977", "Score": "3", "Tags": [ "ruby" ], "Title": "What is the best practice for working with a project as nested modules?" }
26977
<p>I think many people for some reason use a singleton class to handle the PDO connection (?) but I was thinking it would be nice to have a parent abstract class initializing the connection and the child DAO classes would just have to extend that class and use a $db variable. Code below (I didn't test it so sorry if th...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-04T13:10:21.997", "Id": "41807", "Score": "0", "body": "First remove all static keyword you don't need them (static is evil untestable, unreadable and anyone can change a static instance state). If you wan't to use only one database br...
[ { "body": "<p>Continuing on Peter Kiss's answer, the <code>static</code> keyword is indeed hard to test, but you don't have to worry about it when it's in a dependency container. If you use <a href=\"http://en.wikipedia.org/wiki/Inversion_of_control\" rel=\"nofollow\">Inversion of Control</a> and have your obje...
{ "AcceptedAnswerId": "26989", "CommentCount": "6", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-04T10:55:51.763", "Id": "26978", "Score": "1", "Tags": [ "php", "pdo" ], "Title": "Creating a PDO abstract parent class" }
26978
<p>I have this code:</p> <pre><code>JPanel controlPanel = new JPanel(); controlPanel.setLayout(new BoxLayout(controlPanel, BoxLayout.PAGE_AXIS)); TitledBorder tb2 = BorderFactory.createTitledBorder(null, "Control Panel", TitledBorder.CENTER, TitledBorder.DEFAULT_POSITION, myFont, new Color(0, 153, 0)); contro...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-10T12:44:48.503", "Id": "42274", "Score": "1", "body": "BoxLayout accepting min, max and preferred size, override these coordinates, but GBC is easier" } ]
[ { "body": "<p>When you create a form, a <code>GridBagLayout</code> is usually needed.</p>\n\n<p>Here's one way to create the form in your question.</p>\n\n<pre><code>import java.awt.GridBagConstraints;\nimport java.awt.GridBagLayout;\nimport java.awt.Insets;\n\nimport javax.swing.JButton;\nimport javax.swing.JF...
{ "AcceptedAnswerId": "26982", "CommentCount": "1", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-04T12:22:26.113", "Id": "26980", "Score": "1", "Tags": [ "java", "swing", "gui", "layout" ], "Title": "Nested layouts - BoxLayout inside BorderLayout" }
26980
<p>I'm relatively new to JavaScript and jQuery so go easy on me. I'm creating a website where upon jQuery <code>document.ready</code> a set of basic animations are performed on different <code>div</code>s on the HTML markup. All <code>div</code>s have separate IDs and I am storing all <code>div</code>s with same CSS pr...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-04T14:41:33.253", "Id": "41816", "Score": "0", "body": "not the best example of coding best practises, but a nudge in the right direction would be grateful :)" }, { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06...
[ { "body": "<p>I would say two things could improve this considerably:</p>\n\n<ol>\n<li>Use CSS classes elements that share the same animations. This way you can just fetch <em>all</em> the elements that need to be animated with a single <code>$()</code>. e.g. <code>$('.animate')</code></li>\n<li>Instead of usin...
{ "AcceptedAnswerId": "26986", "CommentCount": "2", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-04T14:39:22.180", "Id": "26984", "Score": "3", "Tags": [ "javascript", "jquery", "css" ], "Title": "More efficient jQuery scripting when manipulating multiple elements with multip...
26984
<p>I'm a PHP newbie and I have a question. Is there a better way to do this? A nice solution?</p> <pre><code>$list = isset($_GET['list']) ? htmlspecialchars($_GET['list']) : "experience"; &lt;select name="type" id="type"&gt; &lt;option value="experience"&gt;Experience&lt;/option&gt; &lt;option value="magic"&lt;?=$lis...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-04T16:01:46.970", "Id": "41829", "Score": "0", "body": "See also: http://codereview.stackexchange.com/a/26810/20611" } ]
[ { "body": "<p>You can use a <a href=\"http://php.net/manual/en/language.types.array.php\" rel=\"nofollow\">lookup table</a> for your values like this:</p>\n\n<pre><code>$values = array(\"magic\" =&gt; \"Magic points\", ...);\n</code></pre>\n\n<p>and then <a href=\"http://ch2.php.net/manual/en/control-structures...
{ "AcceptedAnswerId": null, "CommentCount": "1", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-04T15:38:07.523", "Id": "26987", "Score": "1", "Tags": [ "beginner", "php", "form" ], "Title": "Making a preselected drop-down selection box in HTML" }
26987
<p>I am using a long array of over 300 var &amp; 7,000 lines of code written like this:</p> <pre><code>var a = []; var b = []; var c = []; var d = []; var e = []; a[0] = "a"; b[0] = "b"; c[0] = "c"; d[0] = "d"; e[0] = "e"; a[1] = "1"; b[1] = "2"; c[1] = "3"; d[1] = "4"; e[1] = "5"; a[2] = "one"; b[2] = "two"; c[2] ...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-04T16:48:12.680", "Id": "41832", "Score": "0", "body": "If you have a finite number of elements that you need to add to an array which follow a pattern, use a for loop. If you don't know how many you're going to need, a while loop will...
[ { "body": "<p>If you have a finite number of elements that you need to add to an array which follow a pattern, use a for loop. If you don't know how many you're going to need, a while loop will do the trick. Loop through them and use the index to assign the numbers accordingly.</p>\n\n<pre><code>for (var i = 0;...
{ "AcceptedAnswerId": "27028", "CommentCount": "1", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-04T15:48:25.190", "Id": "26988", "Score": "3", "Tags": [ "javascript", "array" ], "Title": "re-write javascript array" }
26988
<p>I wrote this code to solve a problem from a John Vuttag book:</p> <blockquote> <p>Ask the user to input 10 integers, and then print the largest odd number that was entered. If no odd number was entered, it should print a message to that effect.</p> </blockquote> <p>Can my code be optimized or made more concise?...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-04T19:04:50.700", "Id": "41836", "Score": "0", "body": "Here's my response to a suspiciously similar question: http://codereview.stackexchange.com/questions/26187/i-need-my-code-to-be-more-consise-and-i-dont-know-what-is-wrong-with-it-...
[ { "body": "<p>Your main loop can just be:</p>\n\n<pre><code>for i in num_list:\n if i &amp; 1:\n mylist.append(i)\n</code></pre>\n\n<p>There is no need for the else at all since you don't do anything if <code>i</code> is not odd.</p>\n\n<p>Also, there is no need at all for two lists. Just one is enoug...
{ "AcceptedAnswerId": "26993", "CommentCount": "3", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-04T18:35:39.707", "Id": "26992", "Score": "4", "Tags": [ "python", "beginner" ], "Title": "Print the largest odd number entered" }
26992
<p>I wrote an example program about UNIX semaphores, where a process and its child lock/unlock the same semaphore. I would appreciate your feedback about what I could improve in my C style. Generally I feel that the program flow is hard to read because of all those error checks, but I didn't find a better way to write ...
[]
[ { "body": "<ul>\n<li><code>main()</code> must always return an <code>int</code>. <code>return 0</code> at the end of your function.</li>\n<li>The <a href=\"https://stackoverflow.com/questions/461449/return-statement-vs-exit-in-main\">consensus</a> is that, when possible, <code>return</code> instead of <code>exi...
{ "AcceptedAnswerId": "27019", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-04T20:38:33.683", "Id": "27000", "Score": "3", "Tags": [ "c", "linux" ], "Title": "UNIX semaphores" }
27000
<p>I am starting to write some functions while studying how to program in C. In this small function I take a string as an argument and print it out in reverse order.</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;string.h&gt; int main(int argc, char *argv[]) { int aLen = argc; char *sWord = argv[aLen ...
[]
[ { "body": "<p>You need to allocate storage for the reversed string using <code>malloc</code> unless that's changed in the last twenty years and place a <code>'\\0'</code> string terminator at the end.</p>\n\n<pre><code>char *reversed = malloc(sLen + 1);\nreversed[sLen] = '\\0';\n</code></pre>\n\n<p>Also, you're...
{ "AcceptedAnswerId": "27033", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-04T20:51:33.837", "Id": "27003", "Score": "3", "Tags": [ "c" ], "Title": "Learning C, basic string reversal function" }
27003
<p>I wanted an excuse to do some OO JavaScript and I decided to do a JSON editor. It will basically allow you to input JSON and either collapse objects to see other objects better, delete objects (can currently only delete KV pairs), or edit objects. If you give it valid JSON it will output valid JSON. </p> <p>The edi...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-05T00:04:17.703", "Id": "41863", "Score": "0", "body": "See \"Make sure you include your code in your question\" http://codereview.stackexchange.com/helpcenter/on-topic" } ]
[ { "body": "<p>Not bad. Regarding your concerns that toPretty.json is visible from outside object-scope, people typically get around this by using the <a href=\"http://addyosmani.com/resources/essentialjsdesignpatterns/book/#modulepatternjavascript\" rel=\"nofollow\">Module Pattern</a>.</p>\n\n<blockquote>\n <p...
{ "AcceptedAnswerId": null, "CommentCount": "1", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-04T21:43:48.727", "Id": "27005", "Score": "5", "Tags": [ "javascript", "jquery", "object-oriented", "html" ], "Title": "Should I encapsulate my fields differently?" }
27005
<p>I've been thinking about implementing events and wrote some abstract code:</p> <pre><code>#include &lt;memory&gt; #include &lt;vector&gt; #include &lt;iostream&gt; template&lt;typename Event&gt; class Dispatcher { public: class Listener { public: virtual void OnEvent(Event&amp; event, Dispatche...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-04T21:54:01.277", "Id": "41855", "Score": "0", "body": "Could you make Listener::OnEvent(...) into a pure virtual destructor, turning Listener into an interface? That would clear up most of your inheritance issues." }, { "Conte...
[ { "body": "<p>Listner needs a virtual destructor<br>\nand the <code>OnEvent()</code> method should probably be pure virtual:</p>\n\n<pre><code>class Listener\n{\npublic:\n virtual void OnEvent(Event&amp; event, Dispatcher&amp; sender) {};\n};\n\n// Try\n\nclass Listener\n{\n public:\n virtual ~List...
{ "AcceptedAnswerId": null, "CommentCount": "2", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-04T21:52:08.327", "Id": "27007", "Score": "4", "Tags": [ "c++", "c++11", "inheritance", "event-handling", "polymorphism" ], "Title": "Very simple events implementation" }
27007
<p>How can I improve the access time to this unordered map? If I instead allocate a 8 x 10 x 30 x 30 x 24000:</p> <pre><code>std::vector&lt;std::vector&lt;std::vector&lt;std::vector&lt;std::vector&lt;float&gt; &gt; &gt; &gt; &gt; </code></pre> <p>each access is about 0.0001 ms. Using an <code>unordered_map</code> imp...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2016-08-01T14:41:45.663", "Id": "255921", "Score": "0", "body": "If your access pattern is the same as in provided example it's better to use single vector or even static array to store all data, and calculate index like this: `[z*8*10*30*30 +...
[ { "body": "<p>Firstly, it's somewhat hard to say, because you haven't given the code you are using with <code>vector</code>. Are you keeping it sorted and doing some kind of binary search to find the element, or is this just raw vector access times where you don't care about adding <code>this_vote</code> to a s...
{ "AcceptedAnswerId": null, "CommentCount": "1", "ContentLicense": "CC BY-SA 4.0", "CreationDate": "2013-06-04T23:47:00.310", "Id": "27013", "Score": "7", "Tags": [ "c++", "performance", "c++11" ], "Title": "Accessing an unordered_map" }
27013
<p>I have two classes <code>DigBackground</code> and <code>DigParallaxBackgroundLayer</code> (a parallax background is a image consisting of several images that move at different scroll speeds, for games etc.). My idea was to have the <code>DigBackground</code> class which is basically just a image with some added pro...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-05T12:54:53.897", "Id": "41902", "Score": "1", "body": "Looks pretty straightforward. `InitialOffsetOf:` is not idiomatic, I would have expected lowercase, and why not just `initialOffset:`? Also, are there technical reasons why the pr...
[ { "body": "<p>Just a couple nit-picky things...</p>\n\n<p><code>InitialOffsetOf:</code> should instead be <code>initialOffset:</code></p>\n\n<p><code>withScrollSpeed:</code> should just be <code>scrollSpeed:</code></p>\n\n<p>I'd probably also add factory methods. When I'm writing code, no class is complete unt...
{ "AcceptedAnswerId": null, "CommentCount": "1", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-04-25T20:08:40.927", "Id": "27018", "Score": "3", "Tags": [ "object-oriented", "objective-c", "ios", "graphics" ], "Title": "Classes for background layer and parallax effect" }
27018
<p>I need to do a long running task. I've done this by executing a Task while there's a loading box on the UI. When an exception is thrown, I want to stop the task and show a msgbox to the user. If everything goes right, I stop the loading box.</p> <p>The code below works as expected, but I was wondering if I'm doing ...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-05T05:56:44.503", "Id": "41882", "Score": "0", "body": "I had asked [this question on SO](http://stackoverflow.com/questions/16914891/tasks-exceptions-and-cancelation), but maybe that wasn't the right place." } ]
[ { "body": "<p>Since you're using Task-based async processing it's better to declare long-running method as returning <code>Task</code> or <code>Task&lt;T&gt;</code> object:</p>\n\n<pre><code>public async Task ProgramImageAsync()\n{\n await Task.Delay(TimeSpan.FromSeconds(5)); // Actual programming is done he...
{ "AcceptedAnswerId": "27027", "CommentCount": "1", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-05T05:50:41.753", "Id": "27021", "Score": "5", "Tags": [ "c#", "error-handling", "task-parallel-library" ], "Title": "Tasks: exceptions and cancelation" }
27021
<p>Is there a tighter (less characters) way to use perl on the command line to search and replace text from STDIN than I've got here? The code below works.</p> <pre><code>echo hi | perl -e '$a = &lt;STDIN&gt;; $a =~ s/i/o/g; print $a;' </code></pre>
[]
[ { "body": "<p>You can use <code>-p</code> option (see <code>perl --help</code>):</p>\n\n<blockquote>\n <p>assume loop like -n but print line also, like sed</p>\n</blockquote>\n\n<p>So the script becomes:</p>\n\n<pre><code>echo hi | perl -pe 's/i/o/g'\n</code></pre>\n\n<p>which is really compiled to:</p>\n\n<pr...
{ "AcceptedAnswerId": "27024", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-05T07:21:41.897", "Id": "27022", "Score": "4", "Tags": [ "perl" ], "Title": "Search and replace on command line" }
27022
<p>A Base10 Pandigital Number is a number which uses all the digits 0-9 once:</p> <ul> <li>1234567890</li> <li>2468013579</li> <li>etc...</li> </ul> <p>My naive solution was just to use a bunch of nested loops to do this but it's quite slow. And I'm blanking on a more efficient way to do it? The following takes ~6 se...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-05T16:54:29.590", "Id": "41922", "Score": "0", "body": "http://users.telenet.be/vdmoortel/dirk/Maths/permutations.html" } ]
[ { "body": "<p>The easiest way to do this is to note that each value that is generated will be a permutation of the values <code>0 - 9</code>. This will generate <code>10! = 3628800</code> possibilities. There are a number of algorithms to generate permutations, the easiest in this case would simply be to genera...
{ "AcceptedAnswerId": "27026", "CommentCount": "1", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-05T08:13:30.563", "Id": "27025", "Score": "2", "Tags": [ "c#", "performance", "mathematics" ], "Title": "Generating Pandigital Numbers" }
27025
<p>This reads in data from a .csv file and generates an .xml file with respect to the .csv data. Can you spot any parts where re-factoring can make this code more efficient?</p> <pre><code>import csv from xml.etree.ElementTree import Element, SubElement, Comment, tostring from xml.etree.ElementTree import ElementTree ...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-06T13:29:56.623", "Id": "42008", "Score": "1", "body": "one note : you have a `for elem in elem` that reassigns elem to the last element of elem, and then you access elem again. Not sure if that's what you want to do or not, but anyway...
[ { "body": "<p>Minor stuff:</p>\n\n<ul>\n<li>Remove the last <code>import</code> - <code>etree</code> is not used anywhere.</li>\n<li>Merge the two first <code>import</code>s</li>\n</ul>\n\n<p>Possibly speed-improving stuff:</p>\n\n<ul>\n<li>Avoid converting the <code>csv.reader</code> output before <code>return...
{ "AcceptedAnswerId": null, "CommentCount": "1", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-05T14:35:37.980", "Id": "27037", "Score": "3", "Tags": [ "python", "xml", "csv" ], "Title": "Generating .xml files based on .csv files" }
27037
<p>I have a very simple Pong game that I've built in Java. The code is quite long so I've decided to focus this question on the collision that occurs with the ball and the bat and also the effects in the game. I'm using ACM Graphics package to learn Java so most of the methods are from that package. I want to know how ...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-05T15:25:45.913", "Id": "41914", "Score": "0", "body": "Is it on purpose that none of what seems to be instance variables are `private`? Also, why aren't `WAIT` and `MV_AMT` `static`?" }, { "ContentLicense": "CC BY-SA 3.0", ...
[ { "body": "<p>In the original Pong&reg; brand video game, there were two bats which were confined to move vertically. A collision would be detected if ball circuit was triggered at the same time as one of the bat circuits, and if the ball was not already moving in the proper direction for that bat. The ball's...
{ "AcceptedAnswerId": null, "CommentCount": "4", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-05T14:36:23.093", "Id": "27038", "Score": "2", "Tags": [ "java", "game", "collision" ], "Title": "Java Pong Game Collision between bat and ball" }
27038
<p>Something that has bugged me for a while, is what way is best to perform a conditional where a string is compared to other strings. </p> <p>Here is some sample code. This code is just for illustration purposes.</p> <p>The 3 methods I use are</p> <ol> <li>Do a word for word comparison. This is probably the most...
[]
[ { "body": "<p>I'd use the third version, albeit with a small modification:</p>\n\n<pre><code>return badWords.Contains(word);\n</code></pre>\n\n<p>After all, the <code>Contains()</code> method returns a boolean!</p>\n\n<p>Also, I'd put <code>badWords</code> out of the method, so that it needn't be recreated on e...
{ "AcceptedAnswerId": null, "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-05T15:19:21.210", "Id": "27044", "Score": "2", "Tags": [ "c#", ".net" ], "Title": "What's the most optimal way to see if a string is included in a list of strings?" }
27044
<p>Is it? And maybe theres a better way to do this?</p> <pre><code>$allowed = array("name_desc", "name_asc", "level_desc", "level_asc", "vocation_desc", "vocation_asc"); $order = isset($_GET['order']) ? $_GET['order'] : "name"; $order = in_array($order, $allowed) ? str_replace("_", " ", $order) : "name"; $query = "S...
[]
[ { "body": "<p>It is safe. You have a white list of allowed values and ensure that the user provided input is on the list.</p>\n\n<p>But I'd still do the white list checking as the very first thing, and then do the other processing later, so it would be:</p>\n\n<pre><code>$field = 'name';\n$direction = 'asc';\ni...
{ "AcceptedAnswerId": "27049", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-05T17:19:13.290", "Id": "27048", "Score": "1", "Tags": [ "php", "sql", "security" ], "Title": "Is this a safe way of using HTTP query parameters to build a SQL query?" }
27048
<p>I've written a function for converting strings to upper case. It currently works by replacing each character using a Regex pattern with a hash:</p> <pre><code># Special upcase function that handles several Norwegian symbols. def norwegian_upcase(string) string.upcase.gsub(/[æøåäâãàáëêèéïîìíöôõòóüûùúý]/, { ...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-05T18:25:57.860", "Id": "41934", "Score": "1", "body": "I'd suggest looking over http://stackoverflow.com/questions/1020568/how-does-one-convert-a-string-to-lowercase-in-ruby which covers .downcase .upcase .capitalize and the gem unico...
[ { "body": "<p>After a cursory glance at the character codes for these, it looks like the lowercase is 32 (decimal) higher than uppercase. e.g. 'é'.ord - 32 == 'É'.ord</p>\n\n<p>You could try something like this:</p>\n\n<pre><code>string.upcase.gsub(/[æøåäâãàáëêèéïîìíöôõòóüûùúý]/){|s| (s.ord-32).chr(Encoding::UT...
{ "AcceptedAnswerId": null, "CommentCount": "1", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-05T17:51:39.340", "Id": "27050", "Score": "2", "Tags": [ "ruby", "regex" ], "Title": "Should I be using Regex to uppercase uncommon characters?" }
27050
<p>I'm reviewing my code and trying to figure out how I can do some simplifying to flatten my code. Whether it be making simpler function out of the segment of code or by removing parts of it and wanted some opinions.</p> <pre><code>if ($regenerated_post_password !== $user_data-&gt;password) { /* Password ...
[]
[ { "body": "<p>First thing to do is to notice the code repeted in the different branches. Then your code becomes :</p>\n\n<pre><code>if ($regenerated_post_password !== $user_data-&gt;password)\n{ \n /* User has atleast one failed login attempt for the current session */\n\n if ($failed_logins &lt...
{ "AcceptedAnswerId": null, "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-05T18:28:59.483", "Id": "27052", "Score": "-1", "Tags": [ "php", "authentication", "controller", "codeigniter" ], "Title": "Counting failed login attempts to enforce temporary lo...
27052
<p>You can check the problem here: <a href="http://projecteuler.net/problem=14" rel="nofollow">http://projecteuler.net/problem=14</a></p> <p>My first approach in Haskell was this:</p> <pre><code>import Data.Ord import Data.List computeCollatzSequenceLength n = let compute l n | n == 1 = l+1 | eve...
[]
[ { "body": "<p>The main reason why this is slow is that every update to your array is making a new copy of it. The version you linked to on the wiki avoids this problem by using a boxed array instead, which allows you to define the array in one go and let lazy evaluation take care of evaluating the elements in a...
{ "AcceptedAnswerId": "27130", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-05T20:27:53.717", "Id": "27059", "Score": "4", "Tags": [ "optimization", "haskell", "project-euler" ], "Title": "Project Euler #14 (Longest Collatz Sequence) in Haskell" }
27059
<p>I would like some feedback on the code below. And how would I Implement pattern match on find users in role so that exact match not required? </p> <pre><code>// MembershipProvider.FindUsersByName public override MembershipUserCollection FindUsersByName(string usernameToMatch, int pageIndex, int pageSize...
[]
[ { "body": "<pre><code>List&lt;Profile.Membership&gt; recs = (List&lt;Profile.Membership&gt;)memberMapper.GetMembershipsByUsername(_memberUtil.GetApplicationId(), usernameToMatch, pageIndex, pageSize, out totalRecords);\n</code></pre>\n\n<p>I don't understand why is the cast needed here, <code>GetMembershipsByUs...
{ "AcceptedAnswerId": null, "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-05T20:45:14.613", "Id": "27061", "Score": "2", "Tags": [ "c#" ], "Title": "Implement pattern match on FindUsersByName" }
27061
<p>I have this very simple problem : my code is too repetitive. It works, but it could be much cleaner. I don't know exactly where to start. </p> <p>I'd like to learn to work better, and I'd really like someone to help me with it.</p> <p>Here's the 2 bits of my code that are very repetitive (everything I've done myse...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2015-10-05T13:34:27.280", "Id": "195377", "Score": "0", "body": "As we all want to make our code more efficient or improve it in one way or another, try to write a title that summarizes what your code does, not what you want to get out of a re...
[ { "body": "<p>Before removing the code repetition, let's have a look at :</p>\n\n<pre><code>var eventsFiredDr1 = 0;\nvar drawing1 = function() {\n if (eventsFiredDr1 == 0) {\n $('#drawing1').lazylinepainter('paint');\n eventsFiredDr1++; // &lt;-- now equals 1, won't fire again until reload\n ...
{ "AcceptedAnswerId": null, "CommentCount": "1", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-05T21:01:33.017", "Id": "27062", "Score": "0", "Tags": [ "javascript" ], "Title": "Tips to refactor code : very repetitive code" }
27062
<p>I have a data-structure class in C++ with an accessor to some object (may be large) and I have const and non-const methods using this accessor so I need to overload it. I am looking for a <strong>critique</strong> of the code below - maybe there is a way to accomplish the same thing that is cleaner?</p> <p>The way ...
[]
[ { "body": "<blockquote>\n <p>const-version of the method get() returns a copy</p>\n</blockquote>\n\n<p>Why is this a good thing?</p>\n\n<blockquote>\n <p>the non-const method get() is const only by contract, (not checked by compiler)\n harder to get a const-reference, though not impossible</p>\n</blockqu...
{ "AcceptedAnswerId": "27161", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-05T21:39:59.003", "Id": "27064", "Score": "2", "Tags": [ "c++", "classes" ], "Title": "overloaded const and non-const class methods returning references in C++" }
27064
<p>I recently asked <a href="https://codereview.stackexchange.com/questions/27013/how-can-i-speed-up-access-to-an-unordered-map">this question about performance of an unordered_map</a>, but I realize my question is probably more about this piece of code here.</p> <p>I create a 8 x 10 x 30 x 30 x 24000 nested vectors o...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-05T23:41:28.040", "Id": "41951", "Score": "0", "body": "How sparse (i.e. what % of populated entries) do you expect your structure to be?" }, { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-05T23:53:27.790", ...
[ { "body": "<p>Since your overall number of indices just fits into 31 bits, you could try a <code>std::map</code>:</p>\n\n<pre><code>uint32_t keyFrom5DIndex(uint32_t a, uint32_t b, \n uint32_t x, uint32_t y, uint32_t z)\n{\n return z+24000*(x+30*(y+30*(b+10*a)));\n}\n\nstd::map&lt;uint3...
{ "AcceptedAnswerId": null, "CommentCount": "3", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-05T22:15:08.613", "Id": "27066", "Score": "4", "Tags": [ "c++", "performance", "c++11", "memory-management", "vectors" ], "Title": "Reducing memory usage of nested vectors" }
27066
<p>In my controller I had the idea to do something like the following:</p> <pre><code>// retrieve fields from form $firstname = $form-&gt;getInput('firstname'); $lastname = $form-&gt;getInput('lastname'); [...] // create Member object $member = new Member($firstname, $lastname, [...]); // save Member in DB $this-&gt;_...
[]
[ { "body": "<p>A lot of MVC frameworks do the datamapping in the same class (usually with static methods). I would also avoid creating hard links between controllers and models (like having each model property as a parameter in the class constructor). Something like this is a little more elegant, IMO:</p>\n\n<...
{ "AcceptedAnswerId": null, "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-05T22:33:18.887", "Id": "27067", "Score": "1", "Tags": [ "php", "mvc" ], "Title": "Is this a good way for Controller to interact with Model ? (MVC)" }
27067
<p>I have a block of C# code here that stores data to an <code>ArrayList</code> from the database and compares each with the values from the <code>GridView</code>.</p> <p>Is there a way to reduce the line of code and improve its readability?</p> <pre><code> while (DataReader.Read()) { arrHostName.Add(DataReader.G...
[]
[ { "body": "<p>for improvement, to reduce no of iterations</p>\n\n<pre><code> foreach (Infragistics.Win.UltraWinGrid.UltraGridRow row in Grid2.Rows)\n {\n string rowHostName = row.Cells[\"HostName\"].Value.ToString();\n string rowUsers = row.Cells[\"Users\"].Value.ToString();\n string ...
{ "AcceptedAnswerId": "27076", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-06T06:15:52.177", "Id": "27074", "Score": "6", "Tags": [ "c#" ], "Title": "Comparing database values with values from a GridView" }
27074
<p>I need to test performance of a function in PHP. This has to happen accurately, of course. In the end I need to know how long it takes for a function to perform. What I'm doing now:</p> <pre class="lang-php prettyprint-override"><code>&lt;?php $start = microtime(true); while (microtime(true) != $start + 1); // Wait...
[]
[ { "body": "<p>I don't see any immediate problems with it, but I would probably simplified it a bit. Not sure why you're doing it in two loops like that, but I might be missing something of course :)</p>\n\n<pre><code>&lt;?php\n$loops = 10000;\n\n$start = microtime(true);\n\nfor ($i = 0; $i &lt; $loops; $i++)\n ...
{ "AcceptedAnswerId": "27079", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-06T08:19:44.217", "Id": "27078", "Score": "1", "Tags": [ "php", "optimization", "performance", "php5" ], "Title": "Performance testing in PHP" }
27078
<p>I am an amateur programmer and I tried coding <a href="http://www.codechef.com/problems/RGPVR302" rel="nofollow">this problem</a> using the TurboC++ IDE (though my code is in C), but the site's compiler throws a "TIME LIMIT EXCEEDED" error. How can I optimize my code further?</p> <pre><code>#include&lt;stdio.h&gt;...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-06T13:54:37.617", "Id": "42012", "Score": "0", "body": "Have you tried your code on some sample inputs? Does it seem to work?" }, { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-06T14:17:39.117", "Id": "420...
[ { "body": "<ol>\n<li>You need to pre-calculate all square-free integers below the greatest in input (in test it's 100) and don't calculate that for every input value. </li>\n<li>You need a very fast precalculation algorithm like a modified Sieve of Eratosthenes. </li>\n<li><p>Don't forget about the time limit -...
{ "AcceptedAnswerId": "27092", "CommentCount": "2", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-06T12:53:17.990", "Id": "27085", "Score": "1", "Tags": [ "optimization", "c", "beginner", "programming-challenge" ], "Title": "Square free Integer" }
27085
<p>I am creating a C# service that will be doing some specialized processing of data coming into our ODS (Operational Data Store) DB. To support this, I’m building a DAL that will be used to access our ODS. In my DAL, I’ll have a number of GetItem (get a single item) and GetList (get a list of items) methods with a num...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-06T13:12:53.027", "Id": "42003", "Score": "0", "body": "This is just my opinion, but this kind of expression passing should be handled outside of the DAL in a separate layer. When you introduce this kind of thing into the DAL is can ha...
[ { "body": "<p>As you've discovered complex DALs are ugly to build because they are just an uninteresting hard to maintain bunch of plumbing :(</p>\n\n<p>Fortunately some smart guys have built <strong>generics DALs</strong> that you can leverage to do almost all the operations like <strong>projecting</strong> an...
{ "AcceptedAnswerId": null, "CommentCount": "4", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-06T13:09:50.977", "Id": "27088", "Score": "4", "Tags": [ "c#", "design-patterns" ], "Title": "Am I violating any sound coding principles with this approach?" }
27088
<p>Below you'll find a Symfony2 service I wrote to assist in handling the Library entity. Each user in my app has a personal library where one can upload items to and a purchased library, that contains items one has purchased. Both libraries fall under a parent library.</p> <p>At several points in my controller, I nee...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-06T13:52:10.067", "Id": "42011", "Score": "0", "body": "I don't know PHP much, but can't these persist methods throw exceptions? How are they dealt with?" }, { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-06T1...
[ { "body": "<p>Switch statements can often be a sign that you need an extended class. Doctrine supports <a href=\"http://docs.doctrine-project.org/en/latest/reference/inheritance-mapping.html\" rel=\"nofollow\">Inheritance Mapping</a>. With some liberal re-factoring I created a PersonalLibrary and PurchasedLibra...
{ "AcceptedAnswerId": null, "CommentCount": "3", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-06T13:48:18.223", "Id": "27091", "Score": "2", "Tags": [ "php", "symfony2" ], "Title": "Handling library entity" }
27091
<p>One of the servlet in my web app takes parameters in the form <code>www.service.com/servletname/parameter1/parameter2</code>.</p> <p>The first parameter is required and the second parameter is an integer. I've made some code to validate the parameters but it looks really really messy. Is there a nicer and cleaner ...
[]
[ { "body": "<p>In my opinion you could use the Regex to solve this</p>\n\n<pre><code>protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {\n\n resp.setContentType(CONTENT_TYPE);\n PrintWriter out = resp.getWriter();\n int maximumResults = defaultM...
{ "AcceptedAnswerId": "27129", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-06T14:08:33.410", "Id": "27094", "Score": "2", "Tags": [ "java", "servlets" ], "Title": "How can I tidy up this servlet parameter check code?" }
27094
<p>I found this code in our project and it just feels like the wrong way to do what it seems to be doing</p> <pre><code>id&lt;NSFastEnumeration&gt; results = [info objectForKey: ZBarReaderControllerResults]; ZBarSymbol *symbol = nil; for (symbol in results) { // this just selects the first symbol in the results } ...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-13T16:20:07.943", "Id": "42526", "Score": "0", "body": "I'd have to see the rest of the code (inside the for loop) to tell you what it is doing, but if you simply put a `break;` command in the loop, then it selects the first symbol." ...
[ { "body": "<p>ZBarSDK is the only place I've ever seen <code>NSFastEnumeration</code>. I don't know a whole lot about it, nor do I know exactly why ZBarSDK was designed to be used in this way.</p>\n\n<p><code>NSFastEnumeration</code> isn't any faster than using a <code>forin</code> loop. It is faster than a r...
{ "AcceptedAnswerId": null, "CommentCount": "3", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-06T15:48:54.967", "Id": "27096", "Score": "3", "Tags": [ "objective-c", "ios" ], "Title": "Select first or last object from id<NSFastEnumeration>" }
27096
<p>I have to create a program in which <code>t</code> sets of <code>m</code> and <code>n</code> will be given by the user. For each set, we have to generate all prime numbers between <code>m</code> and <code>n</code>. This code is successfully executed with the correct output, but I have to reduce the time limit for a...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-06T17:19:17.133", "Id": "42036", "Score": "2", "body": "First off, indent your program properly." }, { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-06T22:03:19.060", "Id": "42056", "Score": "2", "b...
[ { "body": "<p>The main problem is: the algorithm contains a lot of divisions and a lot of function calls. A division can be very time consuming.</p>\n\n<p>As Harish Ved said, the key is the <a href=\"http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes\" rel=\"nofollow\">sieve of Eratosthenes</a>. It is one of th...
{ "AcceptedAnswerId": null, "CommentCount": "3", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-06T15:55:16.140", "Id": "27097", "Score": "2", "Tags": [ "c", "primes", "time-limit-exceeded" ], "Title": "Reducing time limit for prime number program" }
27097
<p>This code parses a text file, replacing special tokens marked as $(token) with some replaced text. The $ symbol can be escaped by entering a double $$. In the example a user enters a text file containing tokens to replace and a separate mapping file with, for instance, colour:red to denote how to replace a token.<...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-08T00:39:55.520", "Id": "42163", "Score": "0", "body": "Why are you using vector<char> instead of string for the token etc?" }, { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-08T04:49:01.837", "Id": "42169...
[ { "body": "<p>This looks pretty good as it is. No project strictly \"needs\" OOP, especially one of this size. But here is one way to frame it in OOP -</p>\n\n<p>Create a <code>Mapped</code> class and a <code>Mapper</code> class, both accepting stream references in their constructors. Perhaps constructor overlo...
{ "AcceptedAnswerId": "27160", "CommentCount": "2", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-06T16:43:44.660", "Id": "27098", "Score": "2", "Tags": [ "c++", "parsing" ], "Title": "Replacing tokens in a text file with replaced text" }
27098
<p>I have recently started using Ruby and would like to hear your take on the following piece of code.</p> <pre><code>class Generator def initialize @context = nil end def start(params) @context = Context.new params image = create_image if cache_update_request? upload image end re...
[]
[ { "body": "<p>Looks OK, except for a few small notes on style.</p>\n\n<ul>\n<li><p>Don't <code>return</code>, just let things fall off the end of methods:</p>\n\n<p>This...</p>\n\n<pre><code>def start(params)\n @context = Context.new params\n image = create_image\n if cache_update_request?\n upload image\...
{ "AcceptedAnswerId": "27108", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-06T16:48:44.657", "Id": "27099", "Score": "1", "Tags": [ "beginner", "ruby", "image" ], "Title": "Image uploader and manager" }
27099
<p>I am writing web application using flask framework + mongodb. I have an issue, should I use classes for my mongo entities or just use dictionaries for them.</p> <pre><code>class School: def __init__(self, num, addr, s_type): self.num = num self.addr = addr self.s_type = s_type </code></p...
[]
[ { "body": "<p>I'm not familiar with that framework, but if there is more than one school and you are going to use Python to access those values (such as the school's address), I'd use a class. Think of it this way: Classes are usually meant to be instantiated, or at least to be a group of functions for a specif...
{ "AcceptedAnswerId": "27116", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-06T18:09:24.143", "Id": "27105", "Score": "2", "Tags": [ "python", "mongodb", "flask" ], "Title": "Python flask web app, simplification" }
27105
<p>We know when we call <code>HttpContext.Current.Response.Redirect("http://tvrowdy.in");</code> It throw an exception. Can we do it this way(working fine). Need suggestions and improvements.</p> <pre><code>public static void ReferToPage(string strPageName) { try { HttpContext.Current.Response.Redirect...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-07T03:09:37.703", "Id": "42086", "Score": "0", "body": "It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. ...
[ { "body": "<p>I'd recommend against using 'catch-all' blocks unless absolutely necessary. </p>\n\n<p><a href=\"http://msdn.microsoft.com/en-us/library/a8wa7sdt.aspx\" rel=\"nofollow\"><code>Response.Redirect</code></a> always throws a <code>ThreadAbortedException</code> by design (like it or not), unless you pa...
{ "AcceptedAnswerId": null, "CommentCount": "2", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-06T18:41:06.113", "Id": "27110", "Score": "-1", "Tags": [ "c#", ".net", "asp.net" ], "Title": "How to optimize response.redirect in ASP.net" }
27110
<p>Not so long ago I learned how to implement the minimax algorithm with alpha beta pruning, and even created a perfect Tic Tac Toe player. How can I improve this?</p> <pre><code>public Best chooseMove(boolean side,int[]board,int alpha,int beta,int depth,int maxDepth){ Best myBest = new Best(); Best reply; ...
[]
[ { "body": "<p>OK, here is a first cleanup...</p>\n\n<pre><code>public Best chooseMove(final boolean side, final int[] board, \n int alpha, int beta, final int depth, final int maxDepth)\n{\n final Best myBest = new Best();\n Best reply;\n final int num;\n\n if (Board.checkGameOver(board) || depth...
{ "AcceptedAnswerId": null, "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-06T19:15:18.030", "Id": "27113", "Score": "2", "Tags": [ "java", "ai" ], "Title": "Minimax Alpha-beta code for Java" }
27113
<p>I am building a 2d dictionary with an altered list at the end Afterwords I want to find the min of each list. Is there a better way to do it? Better being faster or at least less of an eyesore.</p> <pre><code>#dummy function, will be replaced later so don't worry about it. def alter(x, y, z,): a = list(x) f...
[]
[ { "body": "<p>Python's variable names are conventionally named lowercase_width_underscores.</p>\n\n<p>Loop using .items() not .keys() if you will need the values, so your final loop can be:</p>\n\n<pre><code>for numberServed, numberServedStats in masterStats.items():\n print(numberServed)\n for timeToServ...
{ "AcceptedAnswerId": "27124", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-06T23:04:09.643", "Id": "27120", "Score": "1", "Tags": [ "python", "hash-map" ], "Title": "Making a 2D dict readable" }
27120
<p>Okay, I'm under the impression that my current jQuery can be simplified (don't worry! It's short!) and I'm curious to know how this can be achieved. I think a solution to this will help produce a lot less code.</p> <p>I currently have a list menu set up, with class names referring to their titles (HTML below) and i...
[]
[ { "body": "<p>In the click handler you have a reference to the link that was clicked, so you can just select the relevant item from <code>this</code>. Here I've used the href of the link to point at an ID for each item, which makes it simple to do <code>jQuery(this.hef)</code> to select the item to fade in. It...
{ "AcceptedAnswerId": null, "CommentCount": "0", "ContentLicense": "CC BY-SA 4.0", "CreationDate": "2013-06-06T15:00:44.397", "Id": "27121", "Score": "1", "Tags": [ "javascript", "jquery" ], "Title": "JS arrays to simplify jQuery filtering" }
27121
<p>I have written a java code to transfer files from one server to another using the concept of socket programming. Now, I also want to check for the network connection availability before each file is transferred. I also want that the files transferred should be transferred according to their numerical sequence, and w...
[]
[ { "body": "<p>General advice here:</p>\n\n<ul>\n<li>All your created object instances look like they can be immutable: make them so (for instance by using builders instead of beans -- God do I <em>hate</em> beans).</li>\n<li>You should not embed thread starting logic in a method call -- how do you know anything...
{ "AcceptedAnswerId": null, "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-07T11:44:32.593", "Id": "27138", "Score": "4", "Tags": [ "java", "network-file-transfer" ], "Title": "Program to transfer files from one server to another in java and also display a prog...
27138
<p>I am currently working on a site where I have a bunch of tiles "fly in" to the screen. I need to add a class to each tile on a delay so they come in one after another. I have the following code that works, but if I want to add more tiles, I will have to edit the jQuery each time.</p> <p>Is there a way to trim thi...
[]
[ { "body": "<p>One simple solution would be to extract your common functionality into a method. We'll also cache the selector for speed (don't do this if the elements change).</p>\n\n<pre><code>var tabs = $(\"ul.homepageTabs li\");\n\nfunction animateAfter(index,timeout){\n\n setTimeout(function () {\n ...
{ "AcceptedAnswerId": null, "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-07T12:51:55.583", "Id": "27142", "Score": "1", "Tags": [ "javascript", "jquery" ], "Title": "Shortening Script - Add new class/css to an li in setIntervals" }
27142
<p>I'm having trouble structuring the logic of OAuth integration into my web app. In the app, a user creates a <code>Report</code> that consists of data from their Google Analytics account.</p> <p>User steps:</p> <ol> <li>User clicks 'New Report'</li> <li>Google presents the 'Allow Access' page for OAuth access</li> ...
[]
[ { "body": "<p>The first step would be to refactor out the instanciation of the Oauth2 client:</p>\n\n<pre><code># lib/analytics_oauth2_client\nclass AnalyticsOAuth2Client &gt; OAuth2::Client\n def initialize(client_id, client_secret, options = {}, &amp;block)\n client_id || = ENV['GA_CLIENT_ID']\n client...
{ "AcceptedAnswerId": null, "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-07T14:17:25.370", "Id": "27144", "Score": "4", "Tags": [ "ruby", "ruby-on-rails", "oauth" ], "Title": "Rails service + OAuth" }
27144
<p>The working and functional code below is a simplification of a real test application I've written. It acts as a client which asynchronously sends UDP data from a local endpoint and listens for an echo'ed response from a server implemented elsewhere. </p> <p>The requirements are:</p> <ol> <li>Send and listen on the...
[]
[ { "body": "<ol>\n<li><p>I find a better naming convention for private fields on a class is <code>_PascalCase</code> or <code>_camelCase</code>. This makes it easy to see whether you are using a field or a local variable.</p></li>\n<li><p>It is completely unnecessary to create a new <code>Thread</code> to call <...
{ "AcceptedAnswerId": "36339", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-07T15:12:23.760", "Id": "27145", "Score": "5", "Tags": [ "c#", ".net", "multithreading", "socket" ], "Title": "Reusing thread and UdpClient for sending and receiving on same p...
27145
<p>I'll write a small simulation of my actual class first to request insights on unit testing one of the behaviors of this class. Here is my class A:</p> <pre><code>public class A { private final String a; private final String b; private String c; public A(final String a, final String b) { this...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-07T17:27:19.680", "Id": "42131", "Score": "0", "body": "Is this logic in a method of the class or not?" }, { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-07T17:28:43.503", "Id": "42132", "Score": "0", ...
[ { "body": "<p>Delegate the computation of <code>a</code> and <code>b</code> to a method, say, <code>.compute()</code>. Since we want to spy this method's execution, we will suppose that we have Guava and its useful <code>@VisibleForTesting</code> annotation, and make the method <code>protected</code>:</p>\n\n<p...
{ "AcceptedAnswerId": "27147", "CommentCount": "6", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-07T17:21:52.493", "Id": "27146", "Score": "1", "Tags": [ "java", "unit-testing", "dependency-injection" ], "Title": "Unit Test to verify number of times lazy initialization logic ...
27146
<p>I'm creating a simple generic <code>Map</code>-wrapper with multiple keyed values. </p> <p>I'm intending to use it with storing edges in a graph, where an edge goes from one vertex to another. Thus, I can fetch any of the two vertices in constant time instead of having to loop to find either to/from.</p> <pre><cod...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-07T16:57:20.570", "Id": "42144", "Score": "0", "body": "I would use an adjacency matrix, or sparse adjacency matrix." }, { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-07T19:21:38.520", "Id": "42155", ...
[ { "body": "<p>Some notes about the current code:</p>\n\n<ol>\n<li><p><code>extends Objects</code> looks unnecessary in the class declaration:</p>\n\n<pre><code>public class MultiMap&lt;K1 extends Object, K2 extends Object, V extends Object&gt; {\n</code></pre>\n\n<p>I'm not completely sure but the following see...
{ "AcceptedAnswerId": null, "CommentCount": "3", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-07T16:35:36.333", "Id": "27148", "Score": "3", "Tags": [ "java", "generics" ], "Title": "Tips on multiple key Map-wrapper" }
27148
<p><em>This may be a better fit for another board, but here goes:</em></p> <p>I've made <a href="https://github.com/Flambino/week_sauce" rel="nofollow noreferrer">a very simple gem</a> that acts as a days-of-the-week bitmask. To be hip, I registered it on CodeClimate, <a href="https://codeclimate.com/github/Flambino/w...
[]
[ { "body": "<p>Notes:</p>\n\n<ul>\n<li><p>I am afraid I'd propose a very boring solution: don't use meta-programming. I mean, why add methods and more methods, polluting the class, when you can add just one method and use symbols for the days? Add validations if you're worried about invalid values for days.</p><...
{ "AcceptedAnswerId": "27153", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-07T20:30:47.260", "Id": "27150", "Score": "2", "Tags": [ "ruby", "meta-programming" ], "Title": "define_method vs method_missing and CodeClimate scores" }
27150
<p>This code was first critiqued here (without the Card class): <a href="https://codereview.stackexchange.com/questions/22805/please-critique-my-c-deck-class">Card Deck class for a Poker game</a></p> <p>After learning more about data structures both online and in class, I wanted to revisit my <code>Card</code> and <co...
[]
[ { "body": "<p>No pointing defining methods where the compiler generated versions does the same:</p>\n\n<pre><code>Card::Card(const Card &amp;obj)\n{\n rankValue = obj.rankValue;\n rank = obj.rank;\n suit = obj.suit;\n card = obj.card;\n}\n\nCard::~Card() {}\n\nCard &amp;Card::operator=(const Card &a...
{ "AcceptedAnswerId": "73671", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-07T21:39:44.287", "Id": "27154", "Score": "3", "Tags": [ "c++", "c++11", "classes", "playing-cards" ], "Title": "Card Deck class for a Poker game - version 2" }
27154
<p>I'm a newbie to javascript, and day after day, I try to write better code with jQuery.</p> <p>For example, I wrote this code earlier:</p> <pre><code>$$foo= $(".foo"); $$foo.mouseenter(function() { $(this).find('h1').addClass("hover"); }); $$foo.mouseleave(function() { $("h1").removeClass("hover"); }); </...
[]
[ { "body": "<p>First of all you can use <a href=\"http://api.jquery.com/hover/\" rel=\"nofollow\">jQuery hover</a> and passing 2 functions (mouseenter, mouseleave)\nand u can shorten it also with <a href=\"http://api.jquery.com/end/\" rel=\"nofollow\">jQuery end</a></p>\n\n<pre><code>$('.foo').hover(\n functi...
{ "AcceptedAnswerId": "27175", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-07T23:39:48.720", "Id": "27155", "Score": "2", "Tags": [ "javascript", "beginner", "jquery", "event-handling" ], "Title": "Attaching both mouseenter and mouseleave event handl...
27155
<p>I am just beginning to dabble in C++. Would you implement this as a <code>class</code> like I did, or a <code>struct</code>? What are the pros and cons?</p> <pre><code>class Point { private: double _x; double _y; public: Point(); Point(double x, double y); ~Point(); Point(Point&amp;...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-10T06:22:13.773", "Id": "42248", "Score": "1", "body": "This should give you an idea of the differences between the two: [When should you use a class vs a struct in C++?](http://stackoverflow.com/questions/54585/when-should-you-use-a-c...
[ { "body": "<p>If you are using point as a property bag (like this).</p>\n\n<p>Then just make it a struct (leave the members public). If you think there is a slight possibility of modifying the implementation then use get/set (ers) to access the memebrs but this class is so simple it seems over kill.</p>\n\n<p>I...
{ "AcceptedAnswerId": "27236", "CommentCount": "2", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-08T01:27:37.443", "Id": "27156", "Score": "4", "Tags": [ "c++", "beginner" ], "Title": "Should this Point class remain a class, or become a struct?" }
27156
<p>I have this PHP script below that requests data from the Twitter API. The script then displays it on a page according to the information that I have in my local database. I stored Twitter real usernames so I can pass them to the API and display the profile images of those twitter users. The script works fine to disp...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-11T14:42:23.000", "Id": "42349", "Score": "0", "body": "Please consider using PDO prepared statements instead of the now deprecated mysql_* functions" }, { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-13T14:00...
[]
{ "AcceptedAnswerId": null, "CommentCount": "2", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-08T08:34:08.023", "Id": "27166", "Score": "1", "Tags": [ "php", "performance", "php5", "api", "twitter" ], "Title": "Requesting data from the Twitter API" }
27166
<p>I built a simple PHP spellchecker and suggestions app that uses PHP's similar_text() and levenshtein() functions to compare words from a dictionary that is loaded into an array.</p> <ul> <li>How it works is first I load the contents of the dictionary into an array.</li> <li>I split the user's input into words and s...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-12T16:49:36.987", "Id": "42447", "Score": "0", "body": "Any reason why you aren't using an existing spellchecker? Spellcheck for PHP has already been solved a number of times." }, { "ContentLicense": "CC BY-SA 3.0", "Creati...
[ { "body": "<p>Here are a couple of tweaks that could help performance:</p>\n\n<ol>\n<li><p>Rather than storing the dictionary in-memory, <strong>offload that to a database</strong> (potentially even caching commonly misspelled words as an optimization)</p></li>\n<li><p><strong>Ignore words under a certain lengt...
{ "AcceptedAnswerId": null, "CommentCount": "5", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-08T15:04:40.573", "Id": "27173", "Score": "5", "Tags": [ "php", "strings", "php5" ], "Title": "PHP spell checker with suggestions for misspelled words" }
27173
<p>I want to implement in my code a cache mechanism with a fixed duration. For example an iframe will be cached for one hour, or a script file will be cached for 24 hours.</p> <p>This is how I implemented it, with a rounded timestamp (simplified code assuming that the url doesn't have any query or hash part):</p> <pr...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-08T20:28:35.980", "Id": "42193", "Score": "0", "body": "Cache at what level?" }, { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-08T21:17:19.727", "Id": "42194", "Score": "0", "body": "@fge this is ...
[ { "body": "<p>I suggest you read more about rules on <a href=\"http://blog.httpwatch.com/2007/12/10/two-simple-rules-for-http-caching/\" rel=\"nofollow\">simple rules for HTTP caching</a>, and <a href=\"http://blog.httpwatch.com/2009/08/07/ajax-caching-two-important-facts/\" rel=\"nofollow\">simple rules for AJ...
{ "AcceptedAnswerId": null, "CommentCount": "2", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-08T18:27:53.400", "Id": "27177", "Score": "1", "Tags": [ "javascript", "ajax", "cache" ], "Title": "Cache mechanism with duration" }
27177
<p>I think I'd like to learn Clojure eventually, but at the moment am having fun with <a href="http://docs.hylang.org/en/latest/" rel="nofollow">Hy</a>, a "dialect of Lisp that's embedded in Python." </p> <pre><code>(import socket) (defn echo-upper (sock) (.send c (.upper (.recv c 10000))) (.close c) ) (def s so...
[]
[ { "body": "<p>algernon in freenode/#hy says:</p>\n\n<blockquote>\n <p>Looks lispy enough to me, except for the <code>__getitem__</code> line.</p>\n \n <p>...and the closing braces.</p>\n</blockquote>\n\n<p>I'm think this is fix for the braces and using <code>get</code> instead of <code>__getitem__</code>:</p...
{ "AcceptedAnswerId": "27378", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-08T19:25:32.130", "Id": "27179", "Score": "1", "Tags": [ "python", "lisp", "socket", "hy" ], "Title": "Echoing back input in uppercase over a socket with Hy, a Python Lisp dia...
27179
<p>My program will fork, launching $n children. When a child finishes, the parent will launch a new one, if we have more to launch.</p> <pre><code>#!/usr/bin/perl use strict; use warnings; use POSIX ":sys_wait_h"; #function, that every child will run sub func # {{{ { my $i = shift; my $delay = shift; ...
[]
[ { "body": "<p>I think this can be improved by using queues as the main construct to convey what is going on.</p>\n\n<p>First, you have a pending queue:</p>\n\n<pre><code>my @pending = (8, 8, 5);\n</code></pre>\n\n<p>Second, you have an empty queue with the currently active workers:</p>\n\n<pre><code>my @workers...
{ "AcceptedAnswerId": "27198", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-08T21:18:10.070", "Id": "27183", "Score": "2", "Tags": [ "multithreading", "perl" ], "Title": "Perl mutiple children creation" }
27183
<p>I'm new to SQLite but have programmed in BASIC (and other languages) for pushing 50 years.</p> <p>Given the sample code extract, please offer suggestions for improvement:</p> <pre><code> Sub addAuthor(ByVal authorlnf As String, ByVal authorfnf As String) Dim objConn As SQLiteConnection Dim objCo...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-09T12:39:15.823", "Id": "42208", "Score": "0", "body": "You should consider opening a database connection to be an extremely costly operation. You really don't want to do that often at all. Also look into prepared statements and bulk i...
[ { "body": "<p>You aren't closing the connection inside of a <code>finally</code> statement which means that in the case your application hits one of those exceptions it will not close the connection. this isn't good.</p>\n\n<p>I recommend using a <code>using</code> block to surround the code that you need per...
{ "AcceptedAnswerId": null, "CommentCount": "2", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-09T02:31:14.473", "Id": "27184", "Score": "6", "Tags": [ "vb.net", "sqlite" ], "Title": "Adding to an index of authors" }
27184
<p>I've building a multi-step form that saves data in multiple models. The "base" or parent model is the Venue, and <em>has_one</em> or <em>has_many</em> other models. In the screen below, the "Basic Information" tab is the only one which is mandatory to fill. Once that is saved, a row is generated in the Venue table i...
[]
[]
{ "AcceptedAnswerId": null, "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-09T10:11:53.713", "Id": "27188", "Score": "2", "Tags": [ "beginner", "ruby", "ruby-on-rails", "form", "ajax" ], "Title": "Multi-page form with each step loaded via Ajax" }
27188
<p>I am fairly new at writing jQuery plugins. Moreover, I am a huge fan of best practice. </p> <p>I have written a plugin which centers an element inside its parent. I would love some feedback on how I could have written it better.</p> <pre><code>(function( $ ){ $.fn.verticalCenter = function(options) { var s...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-12T17:28:02.157", "Id": "42453", "Score": "0", "body": "Could you set up a jsFiddle? Just include an example of how you see this plugin being used." } ]
[ { "body": "<p>Interesting question, from a once over:</p>\n\n<ul>\n<li>Consider a <code>'use strict'</code> after <code>(function( $ ){</code></li>\n<li>You are mixing lowerCamelCase and snake_case, you should stick to lowerCamelCase</li>\n<li>I am not a fan of <code>remove_style</code>, it might cause all kind...
{ "AcceptedAnswerId": null, "CommentCount": "1", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-09T10:16:50.247", "Id": "27189", "Score": "2", "Tags": [ "javascript", "jquery", "beginner", "plugin" ], "Title": "jQuery plugin authoring: Vertical Center example" }
27189
<p>I am currently writing a simple PHP script that uses themes and I found myself writing these methods as part of a class that handles theme config files:</p> <pre><code>public function getArrayOfThemeNames() { $this-&gt;loadUnfilteredDirectoryListing(); $this-&gt;loadThemeFiles(); return $this-&gt;theme_...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-11T14:31:07.863", "Id": "42344", "Score": "1", "body": "I'm no PHP guru, but is there any reason why you're using class members to pass values around instead of just local variables?" }, { "ContentLicense": "CC BY-SA 3.0", ...
[ { "body": "<p>Well, the function kind-of sort-of violates SRP...but according to <a href=\"http://butunclebob.com/ArticleS.UncleBob.SrpInRuby\" rel=\"nofollow\">Uncle Bob</a>, I think your class violates the SRP moreso than the function. </p>\n\n<p>Your class above, has three functions within it: <code>getArray...
{ "AcceptedAnswerId": "27269", "CommentCount": "3", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-09T12:19:48.260", "Id": "27194", "Score": "2", "Tags": [ "php" ], "Title": "Loading array of themes" }
27194
<p>I have recently written the following Pong game in Java:</p> <p><code>Pong</code> class:</p> <pre><code>import java.awt.Color; import javax.swing.JFrame; public class Pong extends JFrame { private final static int WIDTH = 700, HEIGHT = 450; private PongPanel panel; public Pong() { setSize(WI...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-09T13:13:12.200", "Id": "42209", "Score": "0", "body": "There are many instance variables which can be made `final`, you can start with this" }, { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-09T15:40:02.103",...
[ { "body": "<p>Overall I find your code quite good as it stands. :) You've got sensible classes with sane, short methods. However, there were a few points that you could improve. Let's look at your solution class by class:</p>\n\n<p><strong>Pong class:</strong></p>\n\n<p>When you turn on strict enough compiler w...
{ "AcceptedAnswerId": "27211", "CommentCount": "5", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-09T12:54:28.910", "Id": "27197", "Score": "16", "Tags": [ "java", "game", "swing", "pong" ], "Title": "Pong game in Java" }
27197
<p>I've created <a href="http://jsfiddle.net/Cone/fAz7p/1/" rel="nofollow">this script</a> and I want to use it for website navigation in my project. As my knowledge of jQuery is quite poor, I'm not sure that code is correct and enough optimized. Could you help me to find out if there are any ways to optimize the code,...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-09T14:02:55.920", "Id": "42212", "Score": "1", "body": "I've read your code a couple of times and must say that I don't really understand what it is doing. It would probably be more readable if you used more verbose variable names in s...
[ { "body": "<p><strong>- Cache your selectors:</strong> As a rule of thumb, if you use a selection more than once, you should cache it. What happens when you use <code>$(\"someElem\")</code> is jQuery has to jump into the DOM and look through all elements that would match that selection. So you should really do ...
{ "AcceptedAnswerId": "27238", "CommentCount": "2", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-09T13:39:46.597", "Id": "27199", "Score": "2", "Tags": [ "javascript", "jquery" ], "Title": "jQuery single page navigation" }
27199
<p>I'm trying to learn learning javascript and jQuery plugin standards, and after some googling and testing I've come up with this pattern:</p> <pre><code>;(function(window, document, $, undefined){ var plugin = function(element){ var instance = this, somePrivateFunc = function(){ console.log(i...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-09T16:46:18.033", "Id": "42220", "Score": "0", "body": "I suggest you take a look at [jQuery Boilerplate](http://jqueryboilerplate.com/)." }, { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-09T17:24:21.800", ...
[ { "body": "<p>Here are some suggestions:</p>\n\n<ol>\n<li><p>Use <code>$.data(this, '_myplugin')</code> &amp; <code>$.data(this, '_myplugin', myplugin)</code>. They're SO MUCH faster.</p></li>\n<li><p>Since you call <code>.publicFunc</code> every time you instantiate your plugin, you should probably move the ca...
{ "AcceptedAnswerId": "27208", "CommentCount": "4", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-09T15:28:33.700", "Id": "27200", "Score": "1", "Tags": [ "javascript", "jquery", "plugin" ], "Title": "Review my jQuery plugin structure" }
27200
<p>There are 10 threads and I have 15 tasks. Now I have submitted all these tasks to these threads. I need to write all these threads output to a file which I was not successful.</p> <p>I am getting output by running all the threads.</p> <p><strong>ThreadPool.java</strong> (Creates a Thread pool and adds all the task...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-09T10:29:16.533", "Id": "42227", "Score": "0", "body": "-1. You are pretty much asking for a code review..." } ]
[ { "body": "<p>I think you've over complicated matters a little.</p>\n\n<p>Some things to be aware of:</p>\n\n<ol>\n<li>new FileWriter(fileName) does not append, new FileWriter(fileName, true) does append.</li>\n<li>If you want multiple threads to write to the same file, you need to synchronize the write.</li>\n...
{ "AcceptedAnswerId": null, "CommentCount": "1", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-09T06:40:25.477", "Id": "27201", "Score": "3", "Tags": [ "java", "multithreading", "concurrency", "file" ], "Title": "Reading output from multiple threads and writing to a file" ...
27201
<p>I am trying to start learning Python using <a href="http://learnpythonthehardway.org" rel="nofollow"><em>Learn Python the Hard Way</em></a>. I wrote my first game that works as far as tested. It was done as an exercise for <a href="http://learnpythonthehardway.org/book/ex45.html" rel="nofollow">this</a>.</p> <p>Ple...
[]
[ { "body": "<p>Some notes:</p>\n\n<ul>\n<li><p>You took a OOP approach. I won't say that's wrong (maybe it was even encouraged by that guide), but when learning a language like Python, where OOP is not compulsory, I'd say it preferable to take a non-OOP approach first.</p></li>\n<li><p><code>return ('addition', ...
{ "AcceptedAnswerId": "27210", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-09T19:13:56.720", "Id": "27203", "Score": "8", "Tags": [ "python", "game", "quiz" ], "Title": "\"MathChallenge\" game for sums with 4 operators" }
27203
<h3>TL;DR</h3> <p>I need a way to refactor a complex user dashboard with several objects and some complex data to display a accounting chart. I have read about both presenters (also called decorators or view models) and service object, but I'm not sure if I should use one or the other, or both? And how to implement th...
[]
[ { "body": "<p>There is a lot here and without diving into the pattern based approach, my first question would be have you considered using <a href=\"http://guides.rubyonrails.org/active_record_querying.html#scopes\" rel=\"nofollow\">rails scopes</a>? Maybe that answer is overly simplistic, but using scopes woul...
{ "AcceptedAnswerId": null, "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-09T20:24:38.357", "Id": "27205", "Score": "1", "Tags": [ "ruby", "design-patterns", "ruby-on-rails", "haml" ], "Title": "Complex dashboard using presenters and/or service objects...
27205
<p>This is my first real attempt at a Scala program. I come from a predominantly Java background, so I'd like to know if the program sticks to Scala conventions well. </p> <p>Is it well readable or should it be formulated differently? To me, there is a lot of lines in the main function which doesn't quite bode right....
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2014-08-15T10:44:29.190", "Id": "108687", "Score": "0", "body": "@Jamal I can't agree with the tag edits. This may be a case of reinventing the wheel but the purpose of it is learning. The focus of the question isn't about how to prevent that....
[ { "body": "<p>Here are a few things, going from smallest to the more significant.</p>\n\n<ol>\n<li>Try to avoid using return. It isn't idiomatic, and for <a href=\"http://scalapuzzlers.com/#pzzlr-018\" rel=\"nofollow\">good reason</a>. So instead of writing <code>if (cond) return a; return b</code> you should p...
{ "AcceptedAnswerId": null, "CommentCount": "2", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-09T20:28:46.843", "Id": "27206", "Score": "5", "Tags": [ "parsing", "unit-testing", "scala", "reinventing-the-wheel", "csv" ], "Title": "Reading and parsing CSV files" }
27206
<p>I've been studying compiler construction theory. Right now, I'm studying <a href="http://en.wikibooks.org/wiki/Compiler_Construction/Lexical_analysis#Finite_State_Automaton" rel="nofollow noreferrer">Finite State Automaton</a> and I've tried to create my own implementation. I'm not sure if my implementation is right...
[]
[ { "body": "<p>It's quite good, I especially like the <code>$current_state</code> explanatory variable. Actually, I'd create another one to remove some duplication:</p>\n\n<pre><code>$transitions = $transition_table[$current_state];\nif (isset($transitions[$character])) {\n $current_state = $transitions[$char...
{ "AcceptedAnswerId": null, "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-09T22:11:37.543", "Id": "27209", "Score": "5", "Tags": [ "php", "state" ], "Title": "Finite State Automaton implementation" }
27209
<p>A small <code>struct</code> is sent beforehand to the client that specifies <code>package_size</code> amount of data should be received which is what <code>p-&gt;data_value_1</code> contains.</p> <p>Server side:</p> <pre><code>void Server::sendDataSingleUser(char *data_in, int user, unsigned int package_size){ ...
[]
[ { "body": "<p>In both read and write you should leave the loop after an error (unless you have explicitly tested and fixed the error).</p>\n\n<pre><code>while(totalRecieved &lt; readSize)\n{\n dataRecieved = recv(socket, date + totalRecieved, readSize - totalRecieved);\n if (dataRecieved &gt; 0)\n {\n ...
{ "AcceptedAnswerId": null, "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-09T23:42:29.587", "Id": "27213", "Score": "3", "Tags": [ "c++", "tcp" ], "Title": "Sending large data package with TCP - winsock" }
27213
<p>I have recently gotten into Java in a Computer Science class at my high school and I am trying to learn more than just the basics I have learned in school. Yesterday, I designed a very simple text editor I named Aqua that is written with Swing. For some reason, my computer drags a little bit when I run these methods...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-10T07:01:43.767", "Id": "42251", "Score": "3", "body": "Use [jvisualvm](http://docs.oracle.com/javase/7/docs/technotes/guides/visualvm/profiler.html) to find what actually causes the slowness. Also do not use `+` or `concat` in a loop ...
[ { "body": "<p>There are formatting and whitespace issues in your code, fix them! You're also switching between declaring variables and initializing them in the next line and initializing them in the same line, be consistent!</p>\n\n<hr>\n\n<pre><code>if(i&lt;1){\n</code></pre>\n\n<p>That hurts! What is <code>i<...
{ "AcceptedAnswerId": "27221", "CommentCount": "3", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-10T03:18:23.953", "Id": "27216", "Score": "4", "Tags": [ "java" ], "Title": "Java Save/Load Optimization" }
27216
<p>I'm going to be working on a much larger version of this program and I just wanted to see if there was anything I should change in my style of coding before I made anything larger.</p> <p>If it wasn't obvious, this code goes through each comic on <a href="http://asofterworld.com/" rel="nofollow">A Softer World</a> ...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-10T06:27:56.390", "Id": "42249", "Score": "0", "body": "Can you tell us something about the much larger version, to give more context to this code?" }, { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-10T06:38:1...
[ { "body": "<ul>\n<li>The names <code>Worker</code> and <code>run</code> are very general, while what they are doing is actually very specific (crawl a single web site for images matching a pattern). It would be better if the names reflected their content.</li>\n<li>The magic numbers and strings in the code shou...
{ "AcceptedAnswerId": "27239", "CommentCount": "3", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-10T04:15:11.483", "Id": "27217", "Score": "2", "Tags": [ "python", "multithreading" ], "Title": "Criticize my threaded image downloader" }
27217
<p>I'm trying to write a multi-threaded utility that logs some audit trails to the DB every 30 minutes, or whenever my storage data structure (a list in this case) exceeds a certain limit.</p> <p>The code works well, but having said that, my manager said there <em>could</em> potentially be some issues in the method Lo...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-12T04:25:19.340", "Id": "42387", "Score": "0", "body": "Apologies for not mentioning in the question early on. I am using .NET 3.5. ConcurrentQueue<T> won't be available here. Plans are to move our projects to 4.5, but only later this ...
[ { "body": "<h2>DO NOT LOCK ON EMPTY STRING</h2>\n\n<p>In .NET every string is stored only once in the AppDomain so if you are locking on empty string everything will stop until you release the lock.</p>\n\n<p>The correct syncroot can be:</p>\n\n<pre><code>private readonly object _flag = new object();\n</code></...
{ "AcceptedAnswerId": null, "CommentCount": "1", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-10T06:27:26.030", "Id": "27219", "Score": "1", "Tags": [ "c#", ".net", "multithreading", "locking", "synchronization" ], "Title": "Multi-threaded utility that logs some audit...
27219
<p>I am learning JavaScript and I just wrote a Rock, Paper, Scissors game that will first prompt the two players for their choices and validate them. In the event of a tie, each player will choose their answer again and it will once again be validated. Everything works fine, but it feels like I have a lot of repetition...
[]
[ { "body": "<p>Just build an object with the associativity.</p>\n\n<pre><code>// what wins to what\nvar winsTo = {\n 'rock': 'scissors',\n 'paper': 'rock',\n 'scissors': 'paper',\n};\n\n// invalid selected\nif (!(choice1 in winsTo &amp;&amp; choice2 in winsTo)) {\n alert(\"bad choice !\");\n return;\n}\n\n/...
{ "AcceptedAnswerId": "27226", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-10T06:44:11.503", "Id": "27225", "Score": "6", "Tags": [ "javascript", "beginner", "game", "rock-paper-scissors" ], "Title": "Is there a way to make my Rock, Paper, Scissors g...
27225
<p>Basically I have a function of the type <code>'a -&gt; 'a</code> (an optimization function on a AST) and I want to call it (passing the previous result) until it returns the same thing as the input.</p> <p>Right now I have this:</p> <pre><code>let performOptimizations ast = Seq.initInfinite (fun _ -&gt; 0) |&...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-10T17:19:27.987", "Id": "42290", "Score": "0", "body": "Is this the sort of situation that could be solved in C# via the `yield` keyword?" }, { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-10T17:23:29.530", ...
[ { "body": "<p><a href=\"http://blogs.msdn.com/b/fsharpteam/archive/2011/07/08/tail-calls-in-fsharp.aspx\" rel=\"nofollow\">F# supports tail call recursion.</a></p>\n\n<p>Your algorithm would seem a perfect candidate for the approach, something along the lines of:</p>\n\n<pre><code>let performOp ast =\n let r...
{ "AcceptedAnswerId": "27237", "CommentCount": "4", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-10T13:30:55.183", "Id": "27230", "Score": "7", "Tags": [ "f#" ], "Title": "What's the most idiomatic way to call a function an arbitrary number of times with the previous result?" }
27230
<p>I've created this adjacency list, but I don't like the design of the graph. The issue is that there is one pointer in the node to its adjacency list and there is a pointer in an <code>adj_list</code> node to the actual node. I am also using <code>next</code> pointers to traverse the graph in the order in which nodes...
[]
[ { "body": "<blockquote>\n <p>e.g. of adding a new node to the graph</p>\n</blockquote>\n\n<p>Well for starters I don't see the graph:</p>\n\n<p>I would expect the code to be:</p>\n\n<pre><code> Graph graph;\n graph.add_node(20,q); // Add nodes to graph.\n</code></pre>\n\n<p>Stop passing pointers around. </...
{ "AcceptedAnswerId": "27250", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-10T17:51:28.393", "Id": "27241", "Score": "4", "Tags": [ "c++", "algorithm", "graph" ], "Title": "Creating an adjacency list" }
27241
<p>I have the following ajax call:</p> <pre><code>function getPreviewText() { $.ajax({ type: 'POST', url: '@Url.Action("PreviewWiki")', dataType: 'json', data: 'source=' + $('#markItUp').val(), success: function (data) { $('#previewMode').html(data.RenderedSource...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-11T02:03:07.373", "Id": "42304", "Score": "0", "body": "I can't vouch for the .NET code, but I don't see anything wrong with the JavaScript. You definitely don't want to use GET, because you'd have to include the data in the URL's quer...
[ { "body": "<p>Well there's not much code to review here, but for what's here, I'd say it looks okay. I would only make one suggestion. If all you're going to do in your controller action is return single string wrapped up in a JSON object, why not dispose of the JSON and just return the HTML as content?</p>\n\n...
{ "AcceptedAnswerId": "27246", "CommentCount": "1", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-10T20:01:43.563", "Id": "27245", "Score": "3", "Tags": [ "javascript", "jquery", "ajax", "asp.net-mvc-4" ], "Title": "Using AJAX to interact with MVC" }
27245
<p>I'm learning ASP.NET and think I figured out ViewState. Can you tell me if I have it right?</p> <p>The goal of the ViewState in this page is simply to keep the value in a DropDownList.</p> <p>So first, let's have the aspx page markup:</p> <pre><code>&lt;%@ Page Title="Parameters" Language="C#" MasterPageFile="~/S...
[]
[ { "body": "<p>You don't have to maintain the DropDownList's seelcted item becouse it will do it by it's self. The problem will only occur when you have disabled the ViewState for the control.</p>\n", "comments": [ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-11T12:09:19...
{ "AcceptedAnswerId": null, "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-10T21:00:54.533", "Id": "27247", "Score": "2", "Tags": [ "c#", "asp.net" ], "Title": "Using ViewState, start-to-end" }
27247
<p>I recently learned Haskell, and I am trying to apply it to the code I use in order to get a feeling for the language.</p> <p>I really like the Repa library since I manipulate a lot of multi-dimensional data. Yet it is, in my sense, missing a lot of signal processing tools. For instance, I use quite a lot of Gaussia...
[]
[]
{ "AcceptedAnswerId": null, "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-10T21:25:14.517", "Id": "27249", "Score": "6", "Tags": [ "optimization", "performance", "array", "haskell", "recursion" ], "Title": "Implementing recursive filters with Haske...
27249
<p>I'm refactoring a responsive report builder in JavaScript. Here's what it looks like: <img src="https://i.stack.imgur.com/g4Djj.png" alt="report builder UI"></p> <p>This started as a small set of objects that transformed data, rendered the graphs with D3.js, and managed the layout all in the same object. The graphs...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-13T02:16:53.323", "Id": "42487", "Score": "0", "body": "That's a beautiful example of some libraries that I have never used before. I am curious though, what do the double braces do in `var properties = {{ properties | json_encode | ra...
[ { "body": "<p>I ended up using the <a href=\"http://addyosmani.com/resources/essentialjsdesignpatterns/book/#decoratorpatternjavascript\" rel=\"nofollow\">decorator pattern</a>. The report view and each graph object now implement a <code>decorate()</code> method. When the report view's <code>decorate()</code> i...
{ "AcceptedAnswerId": "27330", "CommentCount": "3", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-11T01:38:48.840", "Id": "27252", "Score": "1", "Tags": [ "javascript", "backbone.js", "require.js", "lodash.js" ], "Title": "Layout manager: decouple DOM insertion from constr...
27252
<p>I have two class:</p> <p>InputForm.java</p> <pre><code>public class InputForm { private String brandCode; private String caution; public String getBrandCode() { return brandCode; } public void setBrandCode(String brandCode) { this.brandCode = brandCode; } public Strin...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-11T07:29:19.560", "Id": "42307", "Score": "1", "body": "Actually reflection came to mind when I read this. I am not very good at Java, so I haven't heard why using it is a bad idea. Why is this so?" }, { "ContentLicense": "CC B...
[ { "body": "<p>You may want a static method in your <code>InputForm</code> class, named, for instance, <code>.fromCopyForm()</code> taking a <code>CopyForm</code> as an argument and returning an <code>InputForm</code>:</p>\n\n<pre><code>public static InputForm fromCopyForm(final CopyForm copyForm)\n{\n final ...
{ "AcceptedAnswerId": null, "CommentCount": "3", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-11T06:27:43.360", "Id": "27254", "Score": "3", "Tags": [ "java", "classes", "form" ], "Title": "Copy object properties without using many if statements" }
27254
<p>In my current project, I am working with a lot of JSON objects that contain arrays, er.. <code>lists</code> so I setup a decorator for convienece when the list is one item or multiple. Even though this is convienent in the current project, is it reusable code?</p> <pre><code>def collapse(function): @functools.w...
[]
[ { "body": "<p>If you expect a list of length 1, raise an exception when that is not the case:</p>\n\n<pre><code>if isinstance(call, (list, tuple)) and (len(call) == 1):\n return call[0]\nelse:\n raise ValueError(\"List of length 1 expected\")\n</code></pre>\n\n<p>A function that may return either a list o...
{ "AcceptedAnswerId": null, "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-11T07:42:03.317", "Id": "27255", "Score": "-1", "Tags": [ "python" ], "Title": "Proper use or convenience, or both?" }
27255
<p>I have made the following custom defined function in Excel. It works somehow like <code>VLOOKUP()</code>, but it takes two criteria. I think the code is a bit of a mess. Does anyone has any comments, suggestions, or improvements?</p> <pre><code>Public Function VLOOKUPMC(ByVal return_col_num As Long, ByRef table_arr...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-11T17:42:25.753", "Id": "42362", "Score": "0", "body": "Do you mean to search through all columns in the range `table_array_1`, or just the first, like a standard `VLOOKUP`?" }, { "ContentLicense": "CC BY-SA 3.0", "Creation...
[ { "body": "<p>Here's a different version, though I won't claim that it is significantly better. The code seemed to work just fine and was mostly easy to understand (see my comment on the original question). My only big suggestion would be to add some comments to better describe what you are doing and how this w...
{ "AcceptedAnswerId": "27275", "CommentCount": "4", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-11T10:28:45.677", "Id": "27261", "Score": "5", "Tags": [ "vba", "excel", "lookup" ], "Title": "Multiple criterias in a VLOOKUP in Excel VBA" }
27261
<pre><code>Declare @IsExistRoleAR As bit Declare @IsExistRoleEN As bit Declare @IsExist AS bit set @IsExistRoleAR=(SELECT CASE WHEN COUNT(RoleID) &gt; 0 THEN 1 ELSE 0 END AS isExists FROM Roles WHERE RoleDescAR='Arabic Name') set @IsExistRoleEN=(SELECT CASE WHEN COUNT(RoleID) &...
[]
[ { "body": "<p>You could just use <code>OR</code>:</p>\n\n<pre><code> SELECT CASE WHEN COUNT(RoleID) &gt; 0 THEN 1 ELSE 0 END AS isExists\n FROM Roles\n WHERE RoleDescAR='Arabic Name' OR RoleDescEN='English Name'\n</code></pre>\n", "comments": [ { "ContentLicense": "CC BY-SA 3.0", "C...
{ "AcceptedAnswerId": null, "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-10T15:33:02.760", "Id": "27262", "Score": "0", "Tags": [ "sql", "sql-server" ], "Title": "How to refactor the following sql statement?" }
27262