body
stringlengths
25
86.7k
comments
list
answers
list
meta_data
dict
question_id
stringlengths
1
6
<p>I have a bit of code that came from <a href="http://www.eggheadcafe.com/tutorials/aspnet/aa5ff306-16a1-4014-a51d-6ffde0894a0d/aspnet-request-logging-with-asynchronous-fire-and-forget-pattern.aspx">Egg Head Cafe</a> a long while ago and it's been working great. But I ran the entire site through Redgate's Profiler. an...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-08T08:17:31.510", "Id": "6986", "Score": "1", "body": "`StatusCode = 0x191`? You think in HEX?" }, { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-08T10:45:18.580", "Id": "6996", "Score": "0", "body...
[ { "body": "<p>The BeginInvoke() call is a hotspot because it will need a thread from the thread pool (and when there are too many used, it will wait for one to be available). </p>\n\n<p>As a micro-optimization, try replacing that with ThreadPool.QueueUserWorkItem and measuring again, but in the end you're start...
{ "AcceptedAnswerId": "7000", "CommentCount": "6", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-08T01:16:48.500", "Id": "4659", "Score": "5", "Tags": [ "c#", "asp.net", "multithreading" ], "Title": "Optimizing a Fire and Forget page tracker" }
4659
<p>My code keeps some <code>int</code> value about the date range, and there is a method to check if all of it is zero. Is there a better way to do it than doing this?</p> <pre><code>public bool IsAllDayZero { get { return Today == 0 &amp;&amp; Days1_2 == 0 &amp;&amp; ...
[]
[ { "body": "<p>I see 3 alternative solutions:</p>\n\n<ol>\n<li>Use array of <code>days</code>, and then: <code>days.All(d =&gt; d == 0)</code></li>\n<li>You can define boolean variable <code>areAllDayZero</code> or something else :-) and change it when you set values to properties</li>\n<li>Use reflection</li>\n...
{ "AcceptedAnswerId": "4663", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-08T06:27:01.080", "Id": "4661", "Score": "2", "Tags": [ "c#", "datetime", "interval" ], "Title": "Comparing each property to the same value" }
4661
<p>Is this an efficient way to do this?</p> <p>the code is designed to find the nearest friday, without passing Sunday. If it is already past Friday, the start date should equal now.</p> <pre><code>function get_weekend(){ $start = time(); $day = date('w',$now); //find friday, without going pa...
[]
[ { "body": "<p>No need for <code>while</code> loops - just some math:</p>\n\n<pre><code>function get_weekend()\n{\n $start = time();\n $end = $start;\n $day = date('w', $start);\n\n if ($day &gt; 0 &amp;&amp; $day &lt; 5)\n {\n $start = $time()+ ((5 - $day) * 86400);\n }\n\n if ($day ...
{ "AcceptedAnswerId": "4672", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-08T09:14:35.350", "Id": "4668", "Score": "1", "Tags": [ "php" ], "Title": "Efficiently working out the nearest weekend" }
4668
<p>Any ideas?</p> <pre><code>public override string ToString() { string returnValue; if (!string.IsNullOrWhiteSpace(Country)) returnValue = string.Format("{0}", Country); else return returnValue; if (!string.IsNullOrWhiteSpace(City)) returnValue += string.Format(", city {0}", C...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-08T15:28:57.357", "Id": "7011", "Score": "2", "body": "You did notice that in all but the first case you are returning strings. But the first case `return Country` you are always returning a NULL or an empty string. In which case you m...
[ { "body": "<p>Using \"&amp;&amp;\" it will stop the evaluation at the first false, so something like this should work for you:</p>\n\n<pre><code>public class MyClass\n{\n public string Item1 { get; set; }\n public string Item2 { get; set; }\n public string Item3 { get; set; }\n public string Long { ...
{ "AcceptedAnswerId": null, "CommentCount": "4", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-08T11:11:06.047", "Id": "4669", "Score": "6", "Tags": [ "c#", "strings" ], "Title": "Simplifying ToString() implementation" }
4669
<p>I have the following code, which replaces " " &lt;-- Spaces with "-" &lt;-- Dash.<br> Because some of the title text will be returned with more spaces, it converts it into 3 dashes. </p> <p>Examples of Possible Titles:</p> <ul> <li>"Hello&nbsp;&nbsp;World&nbsp;&nbsp;From&nbsp;&nbsp;Jquery" &lt;--- Notice 2 Space...
[]
[ { "body": "<p>Easy enough - regular expressions have an \"alternation\" operator, which works a lot like an \"or\".</p>\n\n<pre><code>var dirty_url = ui.item.label;\nvar url = dirty_url.replace(/(\\s+|-{2,})/g,\"-\");\n</code></pre>\n", "comments": [ { "ContentLicense": "CC BY-SA 3.0", ...
{ "AcceptedAnswerId": "4678", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-08T11:27:22.557", "Id": "4670", "Score": "1", "Tags": [ "javascript", "regex" ], "Title": "Refactor 2 regular expressions in Javascript into 1 statement" }
4670
<p>Even in the presence of <code>&lt;fstream&gt;</code>, there may be reason for using the <code>&lt;cstdio&gt;</code> file interface. I was wondering if wrapping a <code>FILE*</code> into a <code>shared_ptr</code> would be a useful construction, or if it has any dangerous pitfalls:</p> <pre><code>#include &lt;cstdio&...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-08T13:45:13.047", "Id": "7007", "Score": "0", "body": "That should work fine. Just out of curiosity, what reasons may there be to prefer cstdio over fstream?" }, { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-...
[ { "body": "<p>Honestly, I was thinking very hard to come up with any real disadvantage this might have, but I cannot come up with anything. It certainly look strange to wrap a C structure into a shared_ptr, but the custom deleter takes care of that problem, so it is just a subjective dislike, and only at first....
{ "AcceptedAnswerId": "5305", "CommentCount": "18", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-08T13:30:33.340", "Id": "4679", "Score": "48", "Tags": [ "c++", "c++11" ], "Title": "shared_ptr and FILE for wrapping cstdio (update: also dlfcn.h)" }
4679
<p>I have the following function:</p> <pre><code>from collections import Counter import itertools def combinationsWithMaximumCombinedIntervals(data, windowWidth, maximumCombinedIntervals): for indices in itertools.combinations(range(len(data)),windowWidth): previous = indices[0] combinedInterval...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2017-12-17T12:29:23.873", "Id": "348187", "Score": "0", "body": "Just wondering what on earth `for i in combinations: pass` is actually adding to the code as it seems to be doing nothing." } ]
[ { "body": "<p>As for your actual algorithm, you need to change it. Filtering down python's generating makes things relatively simple. However, in this case you are going throw away way more items then you keep. As a result, this isn't going to be an efficient method.</p>\n\n<p>What I'd try is iterating over all...
{ "AcceptedAnswerId": "4687", "CommentCount": "1", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-08T16:57:40.790", "Id": "4682", "Score": "3", "Tags": [ "python", "optimization" ], "Title": "Very slow Python Combination Generator" }
4682
<p>I've created a custom timer to satisfy the following requirements:</p> <ul> <li>Millisecond accuracy</li> <li>I want the tick event handler to only be called once the current tick handler has completed (much like the winforms timer)</li> <li>I want exceptions on the main UI thread not to be swallowed up by the thre...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-12T17:45:09.827", "Id": "7138", "Score": "0", "body": "If you found a solution, please post an answer with it and accept it instead of editing your post. That way future visitors can easily see what your solution was." } ]
[ { "body": "<p>System.Threading.Thread.Sleep() has an accuracy (resolution) of around 15 milliseconds, and so I would stay far away from using that in your code if you need millisecond accuracy. Can you use <a href=\"http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.aspx\" rel=\"nofollow noref...
{ "AcceptedAnswerId": null, "CommentCount": "1", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-08T18:59:44.940", "Id": "4684", "Score": "1", "Tags": [ "c#", ".net" ], "Title": "Custom timer code needs reviewing for thread safety" }
4684
<p>Please provide positive and not-so-positive feedback on style, clarity, or any other additional feedback you would like to provide.</p> <pre><code>#ifndef MATRIX_H_ #define MATRIX_H_ #include &lt;string&gt; struct Matrix_error { Matrix_error(std::string error) { m_error = error; } std::str...
[]
[ { "body": "<p>Few remarks about the exception handling in your code - <code>struct Matrix_error</code>. \nConsider the following document - <a href=\"http://www.boost.org/community/error_handling.html\" rel=\"nofollow\">Error and Exception Handling</a> - for a proper design of exception classes in C++:</p>\n\n<...
{ "AcceptedAnswerId": "4696", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-08T23:48:56.667", "Id": "4690", "Score": "1", "Tags": [ "c++", "matrix", "stl" ], "Title": "STL matrix chain multiplication" }
4690
<pre><code>$.fn.linkNestedCheckboxes = function () { var childCheckboxes = $(this).find('input[type=checkbox] ~ ul li input[type=checkbox]'); childCheckboxes.change(function () { var checked = $(this).attr('checked'); checked = checked || ($(this).closest('li').siblings('li').find('input:checked...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-09T04:07:17.477", "Id": "7029", "Score": "0", "body": "Actually this doesn't work properly even in your example. When you click the parent then uncheck both the children then uncheck the oarent it will then mark the children checked." ...
[ { "body": "<p>Firstly use <a href=\"http://api.jquery.com/prop/\" rel=\"nofollow\"><code>.prop</code></a> not <a href=\"http://api.jquery.com/attr/\" rel=\"nofollow\"><code>.attr</code></a> if you are using jQuery v1.6+.</p>\n\n<p>Secondly your code has a bug in it. When you click the parent then uncheck both t...
{ "AcceptedAnswerId": "4695", "CommentCount": "6", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-09T00:16:18.930", "Id": "4691", "Score": "2", "Tags": [ "javascript", "jquery", "html" ], "Title": "jQuery function to link nested checkboxes" }
4691
<p>How can I rework the code up that <code>ema(x, 5)</code> returns the data now in <code>emaout</code>? I'm trying to enclose everything in one def.</p> <p>Right now its in a for loop and a def.</p> <pre><code>x = [32.47, 32.70, 32.77, 33.11, 33.25, 33.23, 33.23, 33.0, 33.04, 33.21] def ema(series, period, prevma):...
[]
[ { "body": "<ol>\n<li>ema uses series in one place, where it references <code>series[bar]</code>. But bar is a global variable which is frowned upon. Let's pass series[bar] instead of series to ema.</li>\n<li>That would be series[bar] in the for loop, but series[bar] is the same thing as close, so let's pass tha...
{ "AcceptedAnswerId": null, "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-09T02:24:57.203", "Id": "4692", "Score": "0", "Tags": [ "python", "numpy" ], "Title": "Moving average of a data series" }
4692
<p>All colors can be represented by an <code>int</code>, and often you want to get the individual red, green and blue values from that <code>int</code>.</p> <p>I have a method <code>int2rgb</code> (and another called <code>rgb2int</code>) that does this. Is this a clear/good name?</p> <pre><code>public static int[] i...
[]
[ { "body": "<p>As far as naming is concerned: Using 2 instead of <code>to</code> is pretty much a no-no - \"to\" is spelled out everywhere in the Java standard library. Further Java's naming convention is to capitalize every word, but the first in method names - except for acronyms which are all-caps no matter w...
{ "AcceptedAnswerId": "4701", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-09T08:30:11.633", "Id": "4698", "Score": "2", "Tags": [ "java" ], "Title": "Is \"int2rgb\" an OK method name?" }
4698
<p>i will give a list of urls, and want to make multi processes to fetch certain url's web content, and quit if all the urls are all fetched.</p> <p>here is my implementation, and not sure if it's the right way to do the stuff:</p> <pre><code>#coding=utf8 import multiprocessing from multiprocessing import JoinableQu...
[]
[ { "body": "<p>You should pass the <code>url_q</code> queue to the constructor instead of using it as a global variable.</p>\n", "comments": [], "meta_data": { "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-09T10:38:53.240", "Id": "4703", "Pa...
{ "AcceptedAnswerId": "4707", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-09T09:00:00.083", "Id": "4699", "Score": "5", "Tags": [ "python" ], "Title": "just implemented multiprocessing and queue demo, wonder if there are any improvements?" }
4699
<p>I've been looking for a solution to the ABA problem for a lock-free stack. Searching the Web revealed patented and complicated hazard pointers and tagged pointers using double compare-and-swap (DCAS, CAS2). I would settle for the DCAS, but it's not available on some older AMD CPUs. So I came up with this helper clas...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2012-08-22T14:36:50.897", "Id": "24258", "Score": "0", "body": "How important is portability? On current x86-64 platforms, pointers only have 48-significant bits. You can play bit tricks to store a tag in the remaining (upper) 16 bits, and s...
[ { "body": "<p>It's just a spin lock. If the thread that invalidated of the tag got suspended right after, no other thread can make progress till the thread is resumed and releases the tag.</p>\n\n<pre><code>if (old_tag &amp; INVALIDATED) {\n return false; // no thread can go further if ...\n}\nif (tag.compar...
{ "AcceptedAnswerId": null, "CommentCount": "1", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-09T12:39:58.050", "Id": "4706", "Score": "4", "Tags": [ "c++", "multithreading", "locking", "thread-safety" ], "Title": "Single word CAS tagged pointer for algorithms susceptible...
4706
<p>I am writing a wrapper around the <code>GeUserArea</code> class of <a href="http://maxon.de" rel="nofollow">Cinema 4D</a>'s Python API to enable creating user interfaces using an object orientated interface.</p> <p>I've already written 2 prototypes, but I am <strong>not</strong> satisfied with my class-design. They...
[]
[ { "body": "<p>One thing you could do is ensure that events only reach those who are interested in that particular type of event. Right now you seem to be sending out a generic events that you then check to see what type they are.</p>\n\n<p>I wrote a similar answer some time ago to another question on Stack Over...
{ "AcceptedAnswerId": "4717", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-05T16:44:17.963", "Id": "4708", "Score": "5", "Tags": [ "python", "user-interface" ], "Title": "Wrapper around a Python API for creating user interfaces" }
4708
<p>I know this is a lot of code, but I'm <strong>not</strong> looking for any kind of detailed review of the code. I'm just hoping some nice Javascript guru can give it a once over and offer any needed advice for using OOP in Javascript better than I am. </p> <p>Here are a few areas of concern that I have, that don't...
[]
[ { "body": "<p>If you want to be safer, wrap the whole thing in a executed closure <code>(function () { /* code goes here */ })()</code>.</p>\n\n<p>I don't think you need <code>var me = this</code> since at quick glance all the functions are called on the instances, which would make the keyword <code>this</code>...
{ "AcceptedAnswerId": null, "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-09T14:40:58.007", "Id": "4709", "Score": "1", "Tags": [ "javascript", "jquery", "object-oriented", "json", "knockout.js" ], "Title": "Book catalog demo using Knockout, jQuery...
4709
<p>This is a plugin for inserting content, which is for fallback for CSS generated content for IE</p> <p>Please review and suggest optimization tips and or improvement of code. See the TODO in code and offer advice.</p> <pre><code>//jQuery plugin for generating content (function( $ ){ $.fn.genContent = function($$opt...
[]
[ { "body": "<pre><code>//jQuery plugin for generating content\n(function( $ ){\nvar $_func = $.fn.genContent = function($$options) {\n //;;;_debug(this);\n var $settings = $.extend({}, $_func.defaults, $$options);\n return this.each(function() {\n ///If metadata plugin present, use it http://plug...
{ "AcceptedAnswerId": "4734", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-09T16:42:46.873", "Id": "4710", "Score": "2", "Tags": [ "javascript", "jquery", "plugin" ], "Title": "genContent plugin" }
4710
<p>The encryption script:</p> <pre><code>import random splitArray = [] def takeInput(): rawText = raw_input("Type something to be encrypted: \n") password = raw_input("Please type a numerical password: \n") encrypt(rawText, int(password)) def encrypt(rawText, password): for c in rawText: div...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-09T19:11:49.330", "Id": "7048", "Score": "0", "body": "Unless you have a specific and very good reason for writing your own encryption algorithm, you most likely shouldn't do it. http://diovo.com/2009/02/wrote-your-own-encryption-algo...
[ { "body": "<pre><code>import random\nsplitArray = []\n</code></pre>\n\n<p>It is best not to store data in global variables. Instead, you should have functions take all the input they need and return the result</p>\n\n<pre><code>def takeInput():\n\n rawText = raw_input(\"Type something to be encrypted: \\n\")...
{ "AcceptedAnswerId": null, "CommentCount": "3", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-09T15:48:47.300", "Id": "4712", "Score": "2", "Tags": [ "python", "cryptography" ], "Title": "XECryption encryption and decryption script" }
4712
<p>How can I improve this?</p> <pre><code>#include "stdafx.h" #include &lt;iostream&gt; /*A program that accepts input of an employees' base salaries and years of service. Then it also calculates their bonus based on the years of service. - 20 or more years of service, bonus = salary * 0.1 - 10 or more years of ser...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-09T22:40:45.083", "Id": "7050", "Score": "3", "body": "At the very least you could copy the formatting that was done for you over on stack overflow so somebody doesn't have to do it for you *again*." }, { "ContentLicense": "CC ...
[ { "body": "<ol>\n<li><p>Don't declare variables before you need them:</p>\n\n<pre><code>int iSalary;\nint iService;\nfloat fBonus;\nint counter = 1;\nbool okcontinue = true;\nchar idontchar; \n</code></pre>\n\n<p>By declaring them at the point of first use you also see that they are correctly initialized. Rathe...
{ "AcceptedAnswerId": null, "CommentCount": "4", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-09T22:23:12.243", "Id": "4715", "Score": "-1", "Tags": [ "c++", "finance" ], "Title": "Calculating an employee's bonus" }
4715
<p>Can anyone check this code of mine for improvements? This was coded using Java with OOP concepts.</p> <p><strong><code>Game</code> Class</strong></p> <pre><code>import java.util.Random; public class Game { Player player = new Player(); Banker banker = new Banker(); private int a = 0; private ...
[]
[ { "body": "<p>There are a few things, I'll expand on this over time. First, always write method and variable names in <strong>lowerCamelCase</strong>. Everything starting with an uppercase letter wil be considered a class or a constant by other programmers.</p>\n\n<p>Then, don't use mechanically getters and set...
{ "AcceptedAnswerId": null, "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-10T08:23:53.637", "Id": "4723", "Score": "5", "Tags": [ "java", "object-oriented", "game" ], "Title": "\"Deal or No Deal\" console game" }
4723
<p>I wrote a search script, now I want a more efficient way of finding the keywords used for the search and make them bold. I used this code but it I feel it could be greatly improved on. Thanks for any suggestion.</p> <pre><code>// I use this code to select the lines to display $lines = explode('.', trim($search_resu...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-10T08:25:10.893", "Id": "7061", "Score": "0", "body": "Please pardon how bad the code looks. I'm using a mobile device for this" }, { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-10T08:25:51.490", "Id": "7...
[ { "body": "<p>A couple of small things...</p>\n\n<p>I would replace this:</p>\n\n<pre><code>if (strstr($lines[$i], $keywords[0]) || strstr($lines[$i], $keywords[1]) || \n strstr($lines[$i], $keywords[2]) || strstr($lines[$i], $keywords[3])\n</code></pre>\n\n<p>with something like:</p>\n\n<pre><code>foreach($...
{ "AcceptedAnswerId": null, "CommentCount": "3", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-10T08:20:58.180", "Id": "4725", "Score": "3", "Tags": [ "php" ], "Title": "Please help and improve this script for displaying search results" }
4725
<p>I have classes representing gates (OR, XOR, AND etc) with the following method that is called whenever a property changes.</p> <p>Here is the XORGate code:</p> <pre><code>public void computeOutput() { if (this.input1 != null &amp;&amp; this.input0 != null) { if (this.input0.getValue() || this.i...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-10T23:13:19.077", "Id": "108887", "Score": "0", "body": "Java has bitwise operators for this. Have a look at this link: [Java bitwise operators](http://download.oracle.com/javase/tutorial/java/nutsandbolts/op3.html) Hope this helps." ...
[ { "body": "<pre><code>public void computeOutput() {\n output.setValue(\n (input1 == null || input0 == null)\n ? false\n : (input0.getValue() ^ input1.getValue()));\n}\n</code></pre>\n", "comments": [], "meta_data": { "CommentCount": "0", "ContentLicense": ...
{ "AcceptedAnswerId": "4739", "CommentCount": "1", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-10T23:01:03.890", "Id": "4729", "Score": "5", "Tags": [ "java" ], "Title": "Ternary gate handling - more succinct way then a bunch of if statements" }
4729
<p>I have a small class of 2D drawing utilities, <code>Draw2D</code> which is the global entry point for anything related to drawing. Currently, the class looks like this:</p> <pre><code>struct Draw2D { std::shared_ptr&lt;D2Label&gt; CreateLabel (...); std::shared_ptr&lt;D2Plot&gt; CreatePlot (...); void ...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-12T06:09:57.133", "Id": "7105", "Score": "0", "body": "I'm not sure if I understand the problem, but can't you overload `Draw2D::Release(std::shared_ptr<D2Element> element) { Release(element.get();) }` (with a cast if necessary) and th...
[ { "body": "<p>Why not separate the 2D element into two parts.</p>\n\n<ul>\n<li>The interface that can be used</li>\n<li>The shared resource\n<ul>\n<li>Used by the Draw2D interface</li>\n<li>Used by the D2Elements.</li>\n</ul></li>\n</ul>\n\n<p>Then D2Elements does not need to depend on an object that can go out...
{ "AcceptedAnswerId": null, "CommentCount": "2", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-11T06:37:06.427", "Id": "4737", "Score": "3", "Tags": [ "c++" ], "Title": "Ownership/Lifetime management issue" }
4737
<p>I recently wrote my first webapp and here is the code. I want to make it better, but I'm not exactly sure how. I believe the first place I should start is with the structure of it. Is this spaghetti code? If yes, how do I change that? You can view it at <a href="http://shiftfrog.com" rel="nofollow">http://shiftfrog...
[]
[ { "body": "<p><code>Doer</code> is not a particularly <a href=\"http://en.wikipedia.org/wiki/Naming_convention_%28programming%29#Potential_benefits\">expressive</a> name. Sure, the class <em>does</em> something -- but what class doesn't? Your impulse to name the class <code>Calendar</code> is definitely on targ...
{ "AcceptedAnswerId": null, "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-11T14:59:27.100", "Id": "4740", "Score": "4", "Tags": [ "ruby" ], "Title": "Is this Spaghetti code?" }
4740
<p>I need to draw a lot of multi-line text to the screen so I first used <code>DrawText</code> but it was getting a bit slow... So I've been looking at a few alternatives: save the drawn text in memory DC's, use Direct3D/Direct2D, write my own function. I don't want my program to be a memory hog so using memory DC's wa...
[]
[ { "body": "<p>I didn't take any time to try to figure out what you method is or does, but I would suggest, when drawing text it almost always (I say amost, because you could run into edge cases where resource limitations make the following untrue) faster to save your text up, and draw in big chunks, depending o...
{ "AcceptedAnswerId": "4743", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-11T15:20:23.800", "Id": "4741", "Score": "9", "Tags": [ "c++" ], "Title": "Drawing multi-line text fast in C++, MLTextOut" }
4741
<p>I'm developing a publishing engine and trying to find better ways to optimize the code. The end result is to have an open-source alternative to publish content on mobile devices.</p> <p>For now, though, I have a touch-scroll-slide system I have created from a modified scroll system found elsewhere. There is a pecul...
[]
[ { "body": "<p>First off, if you have lines of code that are commented out, you should probably either uncomment them, or remove them. Commented out code is an eyesore.</p>\n\n<p>Secondly, some of your variable names are, not good. For example, I have no idea what the variable <code>sX</code>, <code>sWX</code>, ...
{ "AcceptedAnswerId": null, "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-11T16:32:02.347", "Id": "4745", "Score": "6", "Tags": [ "javascript", "jquery", "touch" ], "Title": "Simple touch-scroll-slide system for a magazine" }
4745
<p>As a personal habit I generally prefer to use string <code>replace</code> and <code>format</code> methods, along with string "patterns", in lieu of string concatenation and other methods in most languages I use (typically C# and Javascript).</p> <p>For example, if I want to generate a URL with parameters (C#), I us...
[]
[ { "body": "<p>In C#, you could use the built-in <a href=\"http://msdn.microsoft.com/en-us/library/b1csw23d.aspx\" rel=\"noreferrer\"><code>string.Format</code></a> instead:</p>\n\n<pre><code>string parameterizedUrl = \"page.aspx?id={0}&amp;ref={1}\";\nreturn string.Format(parameterizedUrl, id, referrer);\n</cod...
{ "AcceptedAnswerId": "224226", "CommentCount": "0", "ContentLicense": "CC BY-SA 4.0", "CreationDate": "2011-09-11T21:20:12.397", "Id": "4747", "Score": "6", "Tags": [ "c#", "javascript", "formatting" ], "Title": "Best practice and alternatives for string manipulation with an emphasis on r...
4747
<p>Well I just finished my first go at my own form validation (I always used validate plugin) for simple front end. It's definitely small potatoes as it's only meant for a form with 4 fields; 3 required text and one required e-mail. </p> <p>I'm new to Jquery so I am doing little exercises like these to help me learn ...
[]
[ { "body": "<p>A couple of small changes:</p>\n\n<ol>\n<li><p>near the end:</p>\n\n<pre><code>if (errors == true) {\n return false;\n}\nelse {\n var errors = false;\n alert(\"Submitted\");\n return true;\n}\n</code></pre>\n\n<p>Could be:</p>\n\n<pre><code>if(!errors)\n{\n alert(\"Submitted\");\n}\...
{ "AcceptedAnswerId": "4752", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-12T02:48:41.900", "Id": "4751", "Score": "1", "Tags": [ "javascript", "jquery" ], "Title": "First go at my own form validation, and suggestions?" }
4751
<p>The idea is to apply some function on each element in each list, then compare two lists by the value returned by the function.</p> <p>My current solution works but is not fast enough. running "python -m cProfile" gives sth. like:</p> <pre><code> ncalls tottime percall cumtime percall filename:lineno(functio...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-12T08:58:51.990", "Id": "7109", "Score": "0", "body": "there are smarter ways to flatten a list, using `sum`, for other comparison approaches see http://stackoverflow.com/questions/1388818/how-can-i-compare-two-lists-in-python-and-retu...
[ { "body": "<p>So it seems you primarily want to use your callback functions to pull the value to compare out of an object, if you don't mind simplifying how compared items are returned, a faster and simpler approach would be:</p>\n\n<pre><code>def simple_compare(li1, li2, value_func1=None, value_func2=None):\n ...
{ "AcceptedAnswerId": null, "CommentCount": "6", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-12T08:46:15.343", "Id": "4757", "Score": "6", "Tags": [ "python", "performance", "optimization" ], "Title": "Python list comparison: Can this code run faster?" }
4757
<p>I want to fill a <code>DataSet</code> with 20,0000 records using a <code>SqlDataAdapter</code> by using this code:</p> <pre><code>adapter.Fill(dataset); </code></pre> <p>If I fill a <code>DataSet</code> this way it takes a long time. Is there anything wrong with this?</p> <p>This is my code:</p> <pre><code>conne...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-12T10:08:06.897", "Id": "7119", "Score": "0", "body": "Please, post the complete code which you have tried. Then only we come to know whats your problem is." }, { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-1...
[ { "body": "<p>I would say the C# is fine, it's your database that is the issue. Is the Stored procedure optimized? Are the tables in the proper normalization? Are your primary keys &amp; foreign keys set up properly? (you should get the point by now) </p>\n\n<p>Start there first since really everything is th...
{ "AcceptedAnswerId": null, "CommentCount": "9", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-12T10:05:40.253", "Id": "4760", "Score": "4", "Tags": [ "c#", "performance" ], "Title": "Fill a DataSet with nearly 20,0000 records using SqlDataAdapter" }
4760
<p>I've got a form, for which the "Send" button should only be available, upon each form field being validated. For most of my check, I call a function <code>checkFormValue()</code> which is a function gathering all the simple checks, give warning where check failed, and if at least one of the check fail, disable the b...
[]
[ { "body": "<p>I would approach it slightly differntly.</p>\n\n<p>Have functions to change when the field has been validated or not:</p>\n\n<pre><code>function checkFormValues(){\n\n var invalidFields = 0;\n\n function updateValidFields(isValidField)\n {\n\n invalidFields += isValidField ? -1 : 1...
{ "AcceptedAnswerId": "4778", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-12T12:06:54.713", "Id": "4763", "Score": "2", "Tags": [ "php", "javascript", "ajax" ], "Title": "mixed AJAX/Javascript form validation check" }
4763
<p>I've implemented a polling function with a timer in C, that every 10s checks a given condition (I've just replaced it for a log to stdout for testing purposes only) but I would like to know your opinion on this code, namely if there is a simpler way to achieve the same result, what issues may arise concerning perfor...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-12T14:32:37.610", "Id": "7129", "Score": "0", "body": "Which system is this for? C has no function called sleep() and my answer would be quite different if this was intended for Windows or Linux or something else." }, { "Conten...
[ { "body": "<p>What is a reason for using alarm() with sleep()? Why not just</p>\n\n<pre><code>void sigIntHandler(int signum) {\n fprintf(stdout, \"received interrupt. going to exit.\\n\");\n exit(0);\n}\n\nint main(int argc, char* argv[]) {\n if(initializeEnvironment(argc, argv) == -1) {\n return -1;\n }...
{ "AcceptedAnswerId": null, "CommentCount": "4", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-12T13:05:59.203", "Id": "4764", "Score": "2", "Tags": [ "optimization", "c" ], "Title": "Polling timer function in C" }
4764
<p>I have these MySQL tables:</p> <pre><code>-- Create product table CREATE TABLE `product` ( `product_id` int NOT NULL auto_increment, `price` decimal(10,2) NOT NULL default '0.00', `discounted_price` decimal(10,2) NOT NULL default '0.00', `avail` enum('Y','N') NOT NU...
[]
[ { "body": "<p>I'm not an expert as say, but my only suggestions might be:</p>\n\n<p>1) If the enums are only ever going to be Y, N, you could look at changing these to the bit flag.</p>\n\n<p>2) I would perhaps look at removing the defaults from the fields that are defined as part of the primary key i.e. produc...
{ "AcceptedAnswerId": null, "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-12T13:48:23.690", "Id": "4768", "Score": "1", "Tags": [ "sql", "mysql" ], "Title": "Acquiring product details" }
4768
<p>I used to mostly do this:</p> <pre><code>class Person constructor : (@parent, @data)-&gt; @index = @parent.length class People constructor : -&gt; people = [] for data in database.people people.push new Person(people, data) </code></pre> <p>Lately I have been trying the following:</p> <pre...
[]
[ { "body": "<blockquote>\n <p>The reason I am asking is that it might not be the responsibility of Person to add himself to the list.</p>\n</blockquote>\n\n<p>I think you should ask youself a question \"Why am I doing this?\" Why do you need to keep the <code>@index</code> inside a <code>Person</code>? </p>\n\n...
{ "AcceptedAnswerId": "4902", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-12T22:52:54.650", "Id": "4776", "Score": "4", "Tags": [ "design-patterns", "coffeescript" ], "Title": "Adding push to array inside the class that is being pushed" }
4776
<pre><code> package xml.impl; import java.io.FileWriter; import java.io.IOException; import java.util.HashMap; public class XMLCreator { private String parent; private String child; private Integer ctr = 0; private HashMap&lt;Integer, String&gt; xml; private String root; private HashMap&lt;Integer, String&gt; roo...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-10-24T06:15:13.987", "Id": "53118", "Score": "0", "body": "how does your code work for nested to more than just a single level ?" } ]
[ { "body": "<p>What's this?</p>\n\n<pre><code>public XMLCreator() {\n if (this.xml == null) {\n this.xml = new HashMap&lt;Integer, String&gt;();\n }\n if (this.roots == null) {\n this.roots = new HashMap&lt;Integer, String&gt;();\n }\n if (this.sB == null) {\n this.sB = new St...
{ "AcceptedAnswerId": "4783", "CommentCount": "1", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-13T02:50:31.670", "Id": "4780", "Score": "2", "Tags": [ "java", "xml" ], "Title": "Simple XML Creator in Java" }
4780
<p>This is a simple login script. Thought I'm not sure when it's appropriate to use includes over just embedding the code. I'm also not sure if I should make cookies, but I guess I don't really need to right now. Also looking at my code would it be easy to create a 'Remember Me' function? </p> <pre><code>&lt;?php $val...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-13T18:59:41.487", "Id": "7179", "Score": "1", "body": "I'd really recommend utilizing OpenID, where you would gain the security of the services you accept (i.e. Twitter, Facebook, Google). The Zend Framework has a module available to ...
[ { "body": "<p>Rather than an else statement to set false on invalid username/password why not just default to false and only set to true if it worked. Also do you care unless both are valid?</p>\n\n<pre><code>$validUserAndPassword = false;\n\nif (CheckEmpty($_POST['username']) &amp;&amp; CheckEmpty($_POST['pas...
{ "AcceptedAnswerId": null, "CommentCount": "2", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-13T05:55:24.983", "Id": "4781", "Score": "1", "Tags": [ "php", "html" ], "Title": "How Can I Improve/Secure My Login Script?" }
4781
<p>I'm using Winforms C# .NET 3.5. I'm getting frames, and this is how I handle them:</p> <pre><code> delegate void videoStream_NewFrameDelegate(object sender, NewFrameEventArgs eventArgs); public void videoStream_NewFrame(object sender, NewFrameEventArgs eventArgs) { if (ready) { ...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-21T04:33:56.157", "Id": "7380", "Score": "0", "body": "Use Managed D2D withing WPF window" } ]
[ { "body": "<blockquote>\n <p>I tried to use WPF and still same preformence lose.. that is weird because WPF suppose to use DirectX and be fast in image proccessing.</p>\n</blockquote>\n\n<p>Except, in the WPF version, you are cloning a <code>Bitmap</code> (which has nothing to do with WPF) and then calling <co...
{ "AcceptedAnswerId": "4801", "CommentCount": "1", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-13T09:22:22.550", "Id": "4785", "Score": "3", "Tags": [ "c#", ".net", "image", "winforms", "wpf" ], "Title": "Image processing optimization" }
4785
<p>I have simple code that just has to check if a checkbox is checked and if so enable some fields that, by default, should be disabled.</p> <p>I have written the next code which I know can be severely improved. Could anyone tell me how? I tried getting the siblings of the checkbox also but that just caused some troub...
[]
[ { "body": "<pre><code>$(function(){ \n var $els = $('#amount, #period, #key');\n $('#api').change(function(){\n var unchecked = !this.checked;\n $els.each(function(){\n $(this).prop('disabled', unchecked);\n });\n });\n});\n</code></pre>\n\n<p>And if applicable change...
{ "AcceptedAnswerId": null, "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-13T10:14:08.317", "Id": "4787", "Score": "5", "Tags": [ "javascript", "jquery" ], "Title": "Function that enables fields based on checkbox state" }
4787
<p>How could I improve this algorithm? The goal is to organize matches between sport teams such that:</p> <ul> <li>Each team affront each other</li> <li>A minimum number of team should start a match after just finishing one</li> </ul> <p>The interesting part is here:</p> <pre><code>myPerm [] _ = [[]] myPerm xs thres...
[]
[ { "body": "<p>No algorithmic improvement, just some cosmetics...</p>\n\n<pre><code>import System.Environment\nimport Data.Ord\nimport Data.List\n\n-- The number of items we keeps to only search\n-- from bests local results\nnbToKeep :: Int\nnbToKeep = 10\n\ntype Match = (Int,Int)\ntype MatchSequence = [Match]\n...
{ "AcceptedAnswerId": null, "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-13T12:57:12.493", "Id": "4793", "Score": "2", "Tags": [ "algorithm", "haskell", "combinatorics" ], "Title": "Organizing matches between sports teams" }
4793
<p>This game is explained <a href="http://en.wikipedia.org/wiki/Deal_or_No_Deal" rel="nofollow">here</a>.</p> <p>Could this code be more efficient?</p> <p><strong>The Game Class</strong></p> <pre><code>import java.util.List; import java.util.Arrays; import java.util.Collections; public class Game { private Cas...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2012-07-09T00:39:31.880", "Id": "21733", "Score": "0", "body": "You should google \"oo lecture\" or \"oo design\" [Here's](http://mmiika.wordpress.com/oo-design-principles/) an example of a link you get from it." } ]
[ { "body": "<p><code>gameContinue==true</code> can just be <code>gameContinue</code></p>\n\n<p>It looks like you could just get rid of:</p>\n\n<pre><code>else if(briefCases == 1){\n player.removeCase(1,cases); \n offer = banker.getOffer(turn,cases,myAmount);\n gameContinue = player.dnd();\n cases...
{ "AcceptedAnswerId": null, "CommentCount": "1", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-13T17:53:24.330", "Id": "4800", "Score": "7", "Tags": [ "java", "optimization", "object-oriented", "game" ], "Title": "\"Deal Or No Deal\"-style game in Java" }
4800
<p>I need to read records from a flat file, where each 128 bytes constitutes a logical record. The calling module of this below reader does just the following.</p> <blockquote> <pre><code>while(iterator.hasNext()){ iterator.next(); //do Something } </code></pre> </blockquote> <p>Means there will be a next() ...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-13T22:18:27.010", "Id": "7184", "Score": "0", "body": "If you can use Java 7, its ARM feature would be nice here." } ]
[ { "body": "<ol>\n<li><p>There is no particular advantage to using a direct buffer here. You have to get the data across the JNI boundary into Java-land, so you may as well use a normal ByteBuffer. Direct buffers are for copying data when you don't want to look at it yourself really.</p></li>\n<li><p>Use a ByteB...
{ "AcceptedAnswerId": "4803", "CommentCount": "1", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-13T21:34:15.057", "Id": "4802", "Score": "1", "Tags": [ "java", "performance", "io", "iterator", "file-structure" ], "Title": "Iterating through 128-byte records in a file"...
4802
<p>I recently implemented a cache for a pet project of mine. The main objectives behind this implementation were:</p> <ol> <li><p><strong>Support move-only types for values</strong>: C++11 is here, and some of the objects that will be used with this template are movable but not copyable. Keys are fine with a copyable ...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2014-01-26T16:22:33.560", "Id": "67373", "Score": "0", "body": "Here's [a nice article](http://timday.bitbucket.org/lru.html) comparing two data structures: a) `std::list` and `std::unordered_map` and b) `boost::bimap`. Boost.MultiIndex also h...
[ { "body": "<p>I think you can make the maintenance of the list a lot easier if the list is circular.<br>\nWith a circular list there are no test for NULL when inserting or removing an element (you just need a fake head node so that an empty list points back at itself).</p>\n\n<p>Also you are not using encapsula...
{ "AcceptedAnswerId": "4807", "CommentCount": "1", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-14T01:13:32.067", "Id": "4806", "Score": "4", "Tags": [ "c++", "c++11", "cache" ], "Title": "An LRU cache template" }
4806
<p>I am making an Address Book like application . <br> I have a contactType enum.</p> <pre><code>public enum ContactType { //just a cut down version MOBILE,PHONE,FAX,EMAIL; } </code></pre> <p>Then I have a Contact class </p> <pre><code>public class Contact { private ContactType contactType; private String...
[]
[ { "body": "<p>At first I would suggest to get rid of the contact type enumeration and instead derive concrete contacts from your contact class. This will increase cohesion dramatically as this way you do not need to have fields like area code in your base contact class. </p>\n\n<p>Regarding your problem: You co...
{ "AcceptedAnswerId": "4813", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-14T09:48:37.290", "Id": "4811", "Score": "2", "Tags": [ "java", "object-oriented" ], "Title": "How can I improve this code to display contact numbers?" }
4811
<p>Here is my standard .htaccess file, any suggestions on how I can improve it with regards to page load speed? Would gZip instead of DEFLATE make any sort of difference?</p> <pre><code>ExpiresActive On RewriteEngine on RewriteBase / # compress text, html, javascript, css, xml: AddOutputFilterByType DEFLATE text/plai...
[]
[ { "body": "<p>Honestly, this looks OK, though you could clean it up a little bit. </p>\n\n<p>You have two ways to make this code better: </p>\n\n<ol>\n<li>group related mime types.</li>\n<li>use human-readable syntax</li>\n</ol>\n\n<p>Regarding point #2, here's part of the config I created after watching Illya ...
{ "AcceptedAnswerId": null, "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-14T10:02:13.490", "Id": "4812", "Score": "6", "Tags": [ "optimization", ".htaccess" ], "Title": "Suggestions on how to improve this .htaccess file for page load speed" }
4812
<p>I am trying to create a function similar to Excel's <a href="http://office.microsoft.com/en-us/excel-help/eomonth-HP005209076.aspx" rel="nofollow"><code>EOMONTH</code></a> function in C#. I have written the following, however, I am not entirely sure if the it achieves the equivalent functionality. Is my equivalent o...
[]
[ { "body": "<p>For your implementation, why not use:</p>\n\n<pre><code>return ((new DateTime(dateTime.Year, dateTime.Month, 1)).AddMonths(1 + months).AddDays(-1));\n</code></pre>\n\n<p>To me, that's a little cleaner, but it might just be me. When I'm just creating an object to be returned, I'm not a fan of stori...
{ "AcceptedAnswerId": null, "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-14T10:31:55.470", "Id": "4814", "Score": "1", "Tags": [ "c#", "unit-testing", "datetime" ], "Title": "Is my equivalent of Excel's \"EOMONTH\" function correct?" }
4814
<p>I have two text boxes and one combobox. I am retrieving the data based upon the text entered into the text boxes and combobox selection, by using the following method:</p> <pre><code> private void textboxfillnames() { if (txtlastname.Text != "") { var totalmembers = from tsgentit...
[]
[ { "body": "<p>You should wrap each query into a function that takes in a string and returns your query result. You should not make direct reference to GUI objects in your business logic as you have done.</p>\n\n<p>You should have a controller negotiating the transfer of data between your model (eclipse.members)...
{ "AcceptedAnswerId": null, "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-14T14:32:37.147", "Id": "4817", "Score": "5", "Tags": [ "c#", ".net", "linq", "winforms" ], "Title": "Retrieving data based upon text entered in a textbox" }
4817
<p>I am writing a JavaScript feature where by I need to make a call to a third-party library (pre-1.10 DataTables) depending upon whether or not the user provides an integer as an input to the function during initialization. I am pasting here the snippet. As you can see it has duplicate code in the if/else clauses, the...
[]
[ { "body": "<p>Since all that is different about the two if/else bodies is the arguments to the fnFilter method, you can construct the difference in one if statement and then use that in one copy of the code rather than two.</p>\n\n<pre><code>/*\n * By default this is a global search box events. Global being sea...
{ "AcceptedAnswerId": "4822", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-14T14:41:26.750", "Id": "4818", "Score": "1", "Tags": [ "javascript", "event-handling", "jquery-datatables" ], "Title": "DataTables search filter handler" }
4818
<p>I'm still fairly new with JavaScript, and wrote something to generate some social media links on my page. The idea was to have that script grab the necessary URL info and feed it to the social media APIs.</p> <pre><code>&lt;script type="text/javascript"&gt; // print links titleElement = urlencode(document...
[]
[ { "body": "<p>I suppose a more valid and improved way is to put the JavaScript into an external file</p>\n\n<pre><code>&lt;script type=\"text/javascript\" src=\"path/to/script\"&gt;&lt;/script&gt;\n</code></pre>\n\n<p>rather than putting it inline into a html. In the same html have a <code>span</code> tag with ...
{ "AcceptedAnswerId": "4838", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-14T20:49:22.847", "Id": "4825", "Score": "3", "Tags": [ "javascript", "html", "api" ], "Title": "Generated social media links" }
4825
<p>I'm working with an XML structure that requires booleans to be represented as 1 or 0. </p> <p>So, suppose the XML output has to look like:</p> <pre><code>&lt;valid&gt;1&lt;/valid&gt; </code></pre> <p>Several nodes can be represented this way, so I made a struct to handle this. The solution I found works, but lack...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-15T02:23:57.720", "Id": "7219", "Score": "0", "body": "Do you need the IntValue property? Could you get away with just having a bool Value property and do the appropiate read and writes in the serialization. Not much different but do...
[ { "body": "<p>Here's a possible answer although I'm not sure it goes far enough for you</p>\n\n<pre><code>public struct MyCrappyBool : IXmlSerializable\n{\npublic bool Value\n{\n get; private set;\n}\n\npublic MyCrappyBool(object v)\n{\n bool value = false;\n\n if(Boolean.TryParse(v, out value))\n ...
{ "AcceptedAnswerId": "4857", "CommentCount": "4", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-15T01:11:52.173", "Id": "4829", "Score": "5", "Tags": [ "c#", ".net", "xml" ], "Title": "Presenting a boolean as an int for XMLSerialization in C#" }
4829
<p>Inspired by the motivations expressed in <a href="https://stackoverflow.com/questions/932222/implementing-the-koch-curve">this question</a>.</p> <p>I created some code to generate the point list (and also display a pretty picture) using just analytic geometry (no Logo/Turtle, no trigonometry). Also, instead of recu...
[]
[ { "body": "<pre><code>#!/bin/env python\n# coding: utf-8\n\ndef kochenize(a,b):\n HFACTOR = (3**0.5)/6\n</code></pre>\n\n<p>Stylistically, this would better as a global constant although there'll be some runtime cost to that.</p>\n\n<pre><code> dx = b[0] - a[0]\n dy = b[1] - a[1]\n mid = ( (a[0]+b[0...
{ "AcceptedAnswerId": null, "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-14T17:11:03.023", "Id": "4832", "Score": "4", "Tags": [ "python", "performance", "algorithm", "computational-geometry" ], "Title": "Koch Curve algorithm in Python without using T...
4832
<p>Is it a bad idea for a model class hold the db handler as a property? Example code are as below. I think the second is better, but the first is not so bad in my opinion.</p> <p>First:</p> <pre><code>class ActiveModel { protected $db; public function __construct() { $this-&gt;db = DbFactory::get('F...
[]
[ { "body": "<p>It is fine and very convenient to keep the database handler as a property. However, you should pass it in the constructor via <a href=\"http://en.wikipedia.org/wiki/Dependency_injection\" rel=\"nofollow\">dependency injection</a>, rather than call it's factory method inside the class. This can e...
{ "AcceptedAnswerId": null, "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-15T02:20:05.097", "Id": "4833", "Score": "2", "Tags": [ "php", "object-oriented" ], "Title": "Which way is better for db handler?" }
4833
<p>Use this tag only when you're looking for <strong>constructive feedback</strong> for rewriting parts of your <strong>existing code</strong> into better, cleaner code. Questions that merely ask for newer versions of the code should <em>not</em> use this tag and are also <em>off-topic</em> in accordance with the <a h...
[]
[]
{ "AcceptedAnswerId": null, "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-15T07:03:20.720", "Id": "4835", "Score": "0", "Tags": null, "Title": null }
4835
Refactoring is a disciplined technique for restructuring an existing body of code, altering its internal structure without changing its external behavior. Use this tag only when you're looking for constructive feedback for rewriting parts of your existing code into better, cleaner code.
[]
[]
{ "AcceptedAnswerId": null, "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-15T07:03:20.720", "Id": "4836", "Score": "0", "Tags": null, "Title": null }
4836
<p>I am a biologist. Some time ago, I wanted to learn to program, and since I am fascinated by the 4th dimension and I was fascinated by the rotatation of hypercubes and hypersphere, so I decided to understand it better, and thanks to the work of Steve Hollasch, I understood that humans cannot "see" the 4th dimension,...
[]
[ { "body": "<pre><code>__author__ = \"Mauro Pellanda\"\n__credits__ = [\"Mauro Pellanda\"]\n__license__ = \"GNU\"\n__version__ = \"1.1.0\"\n__maintainer__ = \"Mauro Pellanda\"\n__email__ = \"pmauro@ethz.ch\"\n__status__ = \"Devlopment\"\n\n\n''' In this file is contained the snake definition'''\n</code></pre>\n\...
{ "AcceptedAnswerId": "4856", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-15T12:19:02.167", "Id": "4839", "Score": "6", "Tags": [ "python", "game", "snake-game" ], "Title": "Snake game in Python" }
4839
<p>Any suggestions on cleaning up this code:</p> <pre><code> public override bool Validate(Control control, object value) { bool res = false; string str = string.Empty; try { if (((string)value).Length &gt; 0) str = value.ToString(); } catch (Exception e) ...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-15T13:34:11.700", "Id": "7233", "Score": "0", "body": "Welcome to Code Review! It would help us if your post included at least a high-level description of what your method does or any specific concerns you have. Even if it's clear en...
[ { "body": "<p>I can't believe I'm saying this, but you have too much error handling going on. The first 15 lines or so all deals with converting value to a string.</p>\n\n<p>just do something like...</p>\n\n<pre><code>string str = value as string;\nif(string.IsNullOrWhiteSpace(str))\n{ \n this.ErrorT...
{ "AcceptedAnswerId": "4844", "CommentCount": "1", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-15T12:46:29.280", "Id": "4840", "Score": "2", "Tags": [ "c#" ], "Title": "Validation function code review" }
4840
<p>This is my general database connection class. I am using this class to execute my queries through website.</p> <p>What would your suggestions on how to improve performance be?</p> <p>(<em>Running MSSQL 2008 R2 SP1 - Microsoft Visual Studio 2010 SP1 , C# 4.0 - ASP.net 4.0</em>)</p> <p>Class:</p> <pre><code>using ...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2013-06-18T17:51:50.207", "Id": "42854", "Score": "0", "body": "`SqlCommand` implements `IDisposable`, so those also should be wrapped in `using` (similar to what you're correctly doing with `SqlConnection`)." } ]
[ { "body": "<p>You are not using a DataContext, so presuming you don't want to for this, I'd suggest that you also consider a connection pool, that way you don't need to incur the connection cost for every select/delete.</p>\n\n<p>Additionally, your catches are very heavy, are you getting that many errors that y...
{ "AcceptedAnswerId": "4964", "CommentCount": "1", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-15T13:47:31.850", "Id": "4843", "Score": "2", "Tags": [ "c#", "object-oriented", "asp.net", "sql-server" ], "Title": "Database connection class performance" }
4843
<p>I am looking for best practices as they apply to constructor usage in common Java class construction.</p> <p><strong>Example 1</strong></p> <pre><code>import javax.swing.*; import java.awt.*; public class Painter extends JPanel{ public Painter(){ buildGUI(); } private void buildGUI(){ JFrame fram...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-15T17:20:10.077", "Id": "7239", "Score": "1", "body": "Both of those have serious potential threading problems. You should be using `SwingUtilities.invokeAndWait` or `SwingUtilities.invokeLater` at the very least to wrap the `frame.set...
[ { "body": "<p>no one from them, this should be standard or base</p>\n\n<p>EDIT: added DefaultCloseOperation for Top-Level Container </p>\n\n<pre><code>import javax.swing.*;\nimport java.awt.*;\n\npublic class Painter {\n\n private JFrame frame;\n private JPanel headerPanel;\n private Drawing_panel ;\n\...
{ "AcceptedAnswerId": "4888", "CommentCount": "3", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-15T16:44:44.363", "Id": "4850", "Score": "6", "Tags": [ "java" ], "Title": "What constructor implementaton follows best practice in Java" }
4850
<p>The following is a personal attempt at implementing the <a href="https://en.m.wikipedia.org/wiki/Composite_pattern" rel="nofollow">Composite design pattern</a> in Scala. <code>Observation</code> is abstract...</p> <pre><code>class CompositeObservation(obss: Observation*) extends Observation { val elements: Mutabl...
[]
[ { "body": "<ol>\n<li><code>++</code> returns a <em>new</em> collection, you need <code>++=</code> here</li>\n<li>Your attempt looks fine to me. Maybe you should additionally implement the <code>Traversable</code> trait or so, and delegate the calls to <code>elements</code> in order to make things a little bit m...
{ "AcceptedAnswerId": "4853", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-15T16:49:42.627", "Id": "4851", "Score": "0", "Tags": [ "design-patterns", "scala" ], "Title": "Composite Design Pattern in Scala" }
4851
<p>I wrote one of my first classes in Actionscript 3 and I want someone versed in AS3 to help me fine tune my class. I am sure there are things that could really help with efficiency and I love learning about "Object Oriented Programming" so please take a look and let me know what you think.</p> <p>Project Background:...
[]
[ { "body": "<p>There are a few areas that can be improved but I guess we need some more context as to how this is laid out. My comments and questions below:</p>\n\n<p>1 - In ActionScript, since you can modify the contents of a collection inside a loop querying the length of the collection at each iteration is a ...
{ "AcceptedAnswerId": "4870", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-15T23:25:18.077", "Id": "4859", "Score": "3", "Tags": [ "object-oriented", "actionscript-3" ], "Title": "More efficient way to write this Actionscript 3 class?" }
4859
<p>At the end of the <a href="http://go.googlecode.com/hg-history/release-branch.r60/doc/GoCourseDay1.pdf" rel="nofollow">Day 1 Go Course slides (pdf)</a> there is an exercise stated as follows (NOTE: the course in the linked presentation is considered obsolete. If you are looking to learn Go the suggested route is fir...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-16T14:46:10.820", "Id": "7265", "Score": "0", "body": "Probably the wrong place to ask. Try asking on StackOverflow." }, { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-16T23:29:16.957", "Id": "7285", "...
[ { "body": "<p>The link is dead and the code doesn't compile, but there is still an obvious issue to critique: Part of the challenge was to not save state with \"global variables.\" In Go, package variables of the package main are typically considered \"global variables\". However, only a single instance of a...
{ "AcceptedAnswerId": null, "CommentCount": "4", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-16T06:00:58.850", "Id": "4860", "Score": "4", "Tags": [ "go", "fibonacci-sequence" ], "Title": "Fibonacci implementation in Go" }
4860
<p>I've got a foreach loop, going through each section of an XML document (an example of said XML document can be found in <a href="https://stackoverflow.com/q/4122955/500559">this SO question</a>) and only display them in divs with corresponding classes. </p> <pre><code>&lt;xsl:for-each select="sections/section"&gt; ...
[]
[ { "body": "<p>On the js side, I'm not convinced you need the double looping.\nHave you tried something like this:</p>\n\n<pre><code>var optClass = \"rel\" + selection.options[selection.selectedIndex].value;\nvar divArray = document.getElementsByTagName(\"div\");\nfor (var i = 0; i &lt; divArray.length; i++){\n ...
{ "AcceptedAnswerId": null, "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-16T09:04:30.177", "Id": "4861", "Score": "3", "Tags": [ "javascript" ], "Title": "Foreach loop displaying instruction relative to a release number" }
4861
<p>This code is part of one of the methods. I'm pretty sure it is really bad programming. The third if statement is supposed to be called if all 4 variables were set. Is another method needed? How would you write this piece?</p> <pre><code>int width = img.Width; int height = img.Height; int thumbWidth = 0 , thumbHeigh...
[]
[ { "body": "<p>How about this as a starter?</p>\n\n<pre><code> int width = img.Width;\n int height = img.Height;\n int thumbWidth = 0 , thumbHeight = 0;\n int preWidth = 0, preHeight = 0;\n\n bool valuesSet = false;\n\n //if Landscape\n ...
{ "AcceptedAnswerId": "4869", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-16T13:50:34.270", "Id": "4865", "Score": "6", "Tags": [ "c#", "image" ], "Title": "Checking for minimum image dimensions" }
4865
<p>So I have this SDK I'm working with, which lives in unmanaged land, and when it wants to tell me something in particular it sends me a particular Windows API message and passes a pointer to a Date in the LParam of that message. I figured out that the date is OA-compatible (a double representing a fractional number o...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-16T17:47:06.607", "Id": "7271", "Score": "3", "body": "Doesn't look kludgy to me ...although that doesn't mean there isn't a shorter process. Think of each function as a tool - you need 3 tools to perform the conversion." } ]
[ { "body": "<p>I agree with @IAbstract that the inline code looks fine. I would wrap it and name it differently since nothing in the current code reveals that it is a pointer to an OLE/OA Date.</p>\n\n<pre><code>public DateTime DateTimeFromOLEDatePointer(Int64 oaDate)\n {\n var dateAsULong = Marshal.Re...
{ "AcceptedAnswerId": "7869", "CommentCount": "1", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-16T17:02:24.143", "Id": "4871", "Score": "5", "Tags": [ "c#", "datetime" ], "Title": ".NET - Get a DateTime from a pointer to an OLE Date" }
4871
<p>As part of my implementation of cross-validation, I find myself needing to split a list into chunks of roughly equal size.</p> <pre><code>import random def chunk(xs, n): ys = list(xs) random.shuffle(ys) ylen = len(ys) size = int(ylen / n) chunks = [ys[0+size*i : size*(i+1)] for i in xrange(n)] ...
[]
[ { "body": "<blockquote>\n <p>Is there a library function that could perform this operation? </p>\n</blockquote>\n\n<p>No.</p>\n\n<blockquote>\n <p>Are there pythonic improvements that can be made to my code?</p>\n</blockquote>\n\n<p>A few.</p>\n\n<p>Sorry it seems boring, but there's not much better you can d...
{ "AcceptedAnswerId": "4880", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-16T19:46:13.750", "Id": "4872", "Score": "6", "Tags": [ "python" ], "Title": "Pythonic split list into n random chunks of roughly equal size" }
4872
<p>Just need to know if I'm doing something wrong in this code. my app seem work fast now with this code. I just want to if i really understant that</p> <pre><code> - (void)receive { NSString *post2 = [NSString stringWithFormat:@"expediteur=%@&amp;destinataire=%@", [[expediteurLbl text] ...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-17T00:16:30.233", "Id": "7286", "Score": "0", "body": "i don't have errors but in console somes warnings \"NSAutoreleaseNoPool(): Object 0x4e45ab0 of class NSCFString autoreleased with no pool in place - just leaking\"." }, { "...
[ { "body": "<p>I gather that <code>request2</code> is an instance variable? It's risky at best to set it with an autoreleased value and then expect it to be valid later in your <code>displayView</code> method. </p>\n\n<p>But it probably works because you execute <code>receive</code> in a background task, and b...
{ "AcceptedAnswerId": null, "CommentCount": "7", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-17T00:10:53.543", "Id": "4882", "Score": "1", "Tags": [ "objective-c", "json", "memory-management" ], "Title": "NSAutoreleasePool with Json Data" }
4882
<p>The idea is to lock resources in C# or Java using Qt:</p> <pre><code>lock(obj){/*process with locked obj*/}` </code></pre> <p>Now I see the problem with deleting <code>obj</code> under <code>lock()</code>.</p> <p><strong>resourcelocker.h</strong></p> <pre><code>#ifndef RESOURCELOCKER_H #define RESOURCELOCKER_H ...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-17T14:18:17.720", "Id": "7298", "Score": "0", "body": "C++ is not Java or C#. Stop trying to apply concepts from other languages (in the other languages concept), it will just make learning to do things correctly harder. Learn how they...
[ { "body": "<p>There reason languages like Java has that feature is because they don't have RAII like C++ does.</p>\n\n<p>I don't quite see how that is better than simply:</p>\n\n<pre><code>// Note: Braces for scope. \"mutex\" could optionally be declared static, or as a class member.\n{ \n QMutexLocker lock(...
{ "AcceptedAnswerId": "4894", "CommentCount": "4", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-17T10:20:26.223", "Id": "4886", "Score": "3", "Tags": [ "c++", "multithreading", "locking", "synchronization", "qt" ], "Title": "Resource locker in Qt" }
4886
<p>I'm writing a SQLite wrapper for C++. I have this class representing a database:</p> <pre><code> class Database { sqlite3 *db; public: Database(const std::string&amp; file_path, bool readonly = false) throw(SQLiteException); ~Database(); std::vector&lt;std::map&lt;std::string, Value&gt; &gt; Que...
[]
[ { "body": "<p>Types have to be known at compile time. Unfortunately to be flexable you need to look up arbitory types at runtime for a DB. So either you need to use some form of template-meta programming or you can use a variant type. I would go for something like <a href=\"http://www.boost.org/doc/libs/1_47_0/...
{ "AcceptedAnswerId": "4889", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-17T11:45:30.337", "Id": "4887", "Score": "3", "Tags": [ "c++", "object-oriented", "sqlite" ], "Title": "Database class design" }
4887
<p>In the main class, loops generate numbers (0~100), and when its generated number is > 20, its value is passed to the thread where it simulates some work with this number. Meanwhile, while this number is being processed, other generated numbers > 20 must be skipped.</p> <p>Is this code OK? I am not sure if I'm doing...
[]
[ { "body": "<p>There is no synchronisation here and so all access to the thread's state is subject to data races. It is comprehensively not threadsafe.</p>\n\n<p>From what I can tell all you need is to implement a producer/consumer pattern. Do this with a blocking queue. Please don't use busy loops. They burn CP...
{ "AcceptedAnswerId": null, "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-17T18:09:53.907", "Id": "4890", "Score": "4", "Tags": [ "c#", "multithreading", "delegates", "thread-safety" ], "Title": "Thread-safety and delegates with generated numbers" }
4890
<p>I'm 100% sure improvements can be made. Any help is greatly appreciated. I should add that I am new to OOP and am not sure if I'm using this the right way.</p> <pre><code>&lt;?php defined('_VALID') or die('Restricted Access!'); class email { public function addtemplate($html, $public) { global $conn; $sql...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-19T16:13:53.360", "Id": "7331", "Score": "0", "body": "defined('_VALID') or die('Restricted Access!'); // Restricting access is best done in an OOP way as well. Additionally, it would be better to have this closer to the code that i...
[ { "body": "<p>Not a PHP expert, but things like</p>\n\n<pre><code>if ($conn-&gt;affected_rows() == 1) {\n return true;\n} else {\n return false;\n}\n</code></pre>\n\n<p>should be</p>\n\n<pre><code>return $conn-&gt;affected_rows() == 1;\n</code></pre>\n", "comments": [ { "ContentLicense":...
{ "AcceptedAnswerId": "4903", "CommentCount": "5", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-18T04:44:40.450", "Id": "4895", "Score": "1", "Tags": [ "php", "object-oriented", "mysql" ], "Title": "Class for sending emails" }
4895
<p>I have Model Object Car.</p> <pre><code>public class Car { private String colour; private String make; private String yearOfMake; } </code></pre> <p>Then a managed bean</p> <pre><code>public Class CarSearchManagedBean { private Car car; @EJB private CarFacade carFacade; public String search() { ...
[]
[ { "body": "<p>You'll want to look up the <a href=\"http://en.wikipedia.org/wiki/Strategy_pattern\" rel=\"nofollow\">Strategy Pattern</a>. </p>\n\n<p>In your specific case, you're probably going to want to do something along these lines (note - I've never worked with ManagedBeans, so I don't know what spanners ...
{ "AcceptedAnswerId": "4912", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-18T08:27:01.987", "Id": "4896", "Score": "4", "Tags": [ "java" ], "Title": "What improvements can I make to avoid these If statments?" }
4896
<p>I have two accordions on the same page </p> <p>First, I have a page, Freemarker, that includes two other Freemarkers that have the accordion.</p> <pre><code>[#include "page1.ftl"] [#include "page2.ftl"] </code></pre> <p>On page1:</p> <pre><code>&lt;h3 class="trigger"&gt;&lt;div id="toggle-image"&gt;&amp;nbsp;&am...
[]
[ { "body": "<ol>\n<li>Don't rely on IDs when your cases are general. Rely on classes. </li>\n<li>Use the DOM hierarchy to reference elements</li>\n</ol>\n\n<p>page1 (added a container div, \"toggle-image\" class name):</p>\n\n<pre><code>&lt;div&gt;\n &lt;h3 class=\"trigger\"&gt;\n &lt;div id=\"toggle-image...
{ "AcceptedAnswerId": "4898", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-18T17:26:06.660", "Id": "4897", "Score": "2", "Tags": [ "javascript", "jquery" ], "Title": "Multiple Accordion on one page" }
4897
<p>My goal was to be able to render my view models using a standard <code>Object.cshtml</code> editor and display template. In order to do so I needed to be able to always call <code>Html.Editor(propertyName)</code> or <code>Html.Display(propertyName)</code> in order to render the HTML element.</p> <p>There were plent...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2012-04-27T00:54:35.320", "Id": "18031", "Score": "0", "body": "What exactly are you trying to do? Are you trying to display a drop-down list?" } ]
[ { "body": "<p>Why not just do this?</p>\n\n<pre><code>public class PhoneNumberViewModel\n{\n // ... normal properties and attributes\n [DropDownList(\"ContactTypes\")] // &lt;-- This is the important one!\n public string ContactType { get; set; }\n\n public static IEnumerable&lt;SelectListItem&gt; ...
{ "AcceptedAnswerId": null, "CommentCount": "1", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-18T18:06:24.673", "Id": "4899", "Score": "11", "Tags": [ "c#", "asp.net-mvc-3" ], "Title": "Implementing a generic DropDownList attribute and templating solution" }
4899
<p>This is my first experience of creating a useful tool in Python. I'd appreciate any critics on this. <br> I can also post the config file if needed.</p> <pre><code># !/usr/bin/env python # -*- coding: utf-8 -*- """ A Python script that checks for Portage tree and overlays updates and notifies of the list of packa...
[]
[ { "body": "<pre><code>class Timestamp(tempfile._RandomNameSequence):\n def next(self):\n return time.strftime('%Y%m%d-%H%M%S')\n\ntempfile._RandomNameSequence = Timestamp\n</code></pre>\n\n<p>Names beginning with underscores are considered internal to that module. Monkey patching like this is generall...
{ "AcceptedAnswerId": "4906", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-19T12:39:29.063", "Id": "4904", "Score": "2", "Tags": [ "python" ], "Title": "Gentoo linux updates notifier" }
4904
<p>I have implemented generic doubly-linked list class, which supports <code>IEnumerable&lt;T&gt;</code>,<code>IEnumerator&lt;T&gt;</code> interfaces.</p> <p><code>DoublyLinkedList&lt;T&gt;</code> is fully compatible with standart BCL classes.</p> <p>Targets:</p> <ul> <li>class can be used as a standard replacement ...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-14T20:11:13.083", "Id": "9299", "Score": "0", "body": "As an observation, your unit tests don't seem to be testing... a unit of work. It's testing the entire swath of functionality of the class, once for value types and once for refer...
[ { "body": "<p>More granular unit tests as per my comment above.</p>\n\n<pre><code> [TestClass]\n public sealed class DoublyLinkedListUnitTest\n {\n private IDoublyLinkedList&lt;int&gt; valueDll;\n\n [TestInitialize]\n public void TestEmptyValueDllInitialize()\n {\n ...
{ "AcceptedAnswerId": null, "CommentCount": "2", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-19T18:45:49.590", "Id": "4911", "Score": "6", "Tags": [ "c#", ".net", "performance" ], "Title": "Comparing DoublyLinkedList<T> implementation performance with other BCL (.NET) classe...
4911
<p>I have a function that accepts a big amount of data as a parameter (sometimes 1 megabyte) I was thinking which would be the fastest way to pass that data, dunno if I got this right, but here is what I think (functionality doesn't matter, all are the same in my app, since data is never used again after this function)...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2012-02-01T18:28:53.677", "Id": "13379", "Score": "1", "body": "See also: [Is the use of const dogmatic or rational?](http://stackoverflow.com/questions/5844904/is-the-use-of-const-dogmatic-or-rational)" } ]
[ { "body": "<p>This appears similar to <a href=\"https://stackoverflow.com/questions/1951192/delphi-performance-of-passing-const-strings-versus-passing-var-strings\">StackOverflow question 1951192</a>. If you have a real performance need, then try the suggestion there of using FastMM4. You are right that pass-...
{ "AcceptedAnswerId": null, "CommentCount": "1", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-20T06:29:09.683", "Id": "4917", "Score": "9", "Tags": [ "delphi" ], "Title": "Fastest Parameter Passing in delphi 7?" }
4917
<p>I've developed a log reader which can filter it's output following a few criteria: errorlevel, product, category, id. It is functioning, but when the log file is really big, it becomes very slow, to the point of going beyond the time limit.</p> <p>log_reader.php</p> <pre><code>&lt;?php session_start(); uns...
[]
[ { "body": "<p>I wouldn't say this would affect your performance much, but usually <code>if((!in_array($segments[4],$ids))&amp;&amp;(!empty($segments[4])))</code>\nIs written as <code>if(!empty($segments[4])&amp;&amp;!in_array($segments[4],$ids))</code> so as to short out if the item is empty. </p>\n", "comm...
{ "AcceptedAnswerId": null, "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-20T07:05:42.733", "Id": "4918", "Score": "2", "Tags": [ "php", "javascript", "performance" ], "Title": "Filtered log reader" }
4918
<p>I have a performance issue causing test->code->test cycle to be really slow. The slowness is hard to avoid, since I am doing some heavy image processing, and I am trying to accelerate things a bit by not running functions when it is not needed. For ex: compute some numbers from big images -> serialize results in tex...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-20T15:42:15.330", "Id": "7373", "Score": "0", "body": "\"Am I reinventing a wheel here?\" Yes. \"Is there a tool out there I should use instead of a custom lib?\" Yes. SCons. \"those are for building softwares\". False. They're f...
[ { "body": "<pre><code># path.root is the root of our repo\nhashDir = os.path.join(path.root, '_hashes')\n</code></pre>\n\n<p>Python style guide recommends that global constants are ALL_CAPS</p>\n\n<pre><code>class BaseRule(object):\n '''base object, managing work like checking if results are up to date, and\...
{ "AcceptedAnswerId": "4921", "CommentCount": "4", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-20T12:00:36.990", "Id": "4920", "Score": "5", "Tags": [ "python", "performance", "unit-testing" ], "Title": "Faster tests with dependency analysis" }
4920
<p>I'm looking to improve this class - any suggestions? It checks for empty, name, email, and password. The regex for email is very simple. A very lengthy article from the Linux Journal for improving upon this is <a href="http://www.linuxjournal.com/article/9585" rel="nofollow noreferrer">here</a>. The character set...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-20T02:54:39.927", "Id": "7364", "Score": "1", "body": "You really shouldn't use a regex to validate email addresses. Also, why is this in a class? A namespace would make more sense." }, { "ContentLicense": "CC BY-SA 3.0", ...
[ { "body": "<p>First of all, as mentioned by @user7212, use php <code>filter_var()</code> for email validation. Please see below for example:</p>\n\n<pre><code>/**\n * Validate email\n *\n * @param string $email\n *\n * @return bool\n */\npublic static function email($email)\n{\n return filter_var($email, FIL...
{ "AcceptedAnswerId": null, "CommentCount": "7", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-20T02:51:39.813", "Id": "4922", "Score": "4", "Tags": [ "php" ], "Title": "class design improvement - user validation" }
4922
<p>I'm writing a little Downloader that will look through directories online and download the content. The first prototype of my program is a success, now I just want to refine it and learn some more C#. The task is this:</p> <p>Take this string: </p> <p><code>http://example.free.pl/plus%20violent/Dark%20The%20Suns/A...
[]
[ { "body": "<p>The .NET framework will do this for you pretty cleanly with the right classes.</p>\n\n<p>For example:</p>\n\n<pre><code>string url = \"http://example.free.pl/plus%20violent/Dark%20The%20Suns/All%20Ends%20In%20Silence/\";\n\nDirectoryInfo di = new DirectoryInfo( new Uri(url).LocalPath );\n</code></...
{ "AcceptedAnswerId": "4926", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-20T13:19:01.507", "Id": "4924", "Score": "4", "Tags": [ "c#" ], "Title": "Find a specific Substring" }
4924
<p>This is my first ever attempt at Ajax form validation, and everything works as expected, the final true example will hold a lot more input fields than in my testing scripts which are below.</p> <p>The file is expected to get a lot bigger as in my testing I have only used 3 input fields, how can I improve on this:</...
[]
[ { "body": "<p>Here's a few things I can observe at first glance:</p>\n\n<p><br/></p>\n\n<h2>HTML</h2>\n\n<ol>\n<li><p><code>form</code> elements don't accept <code>input</code> fields as direct child’s, so you should place your submit button inside a wrapper element.</p></li>\n<li><p>I don't know about your vis...
{ "AcceptedAnswerId": null, "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-20T13:21:11.007", "Id": "4925", "Score": "5", "Tags": [ "php", "jquery", "ajax", "form", "validation" ], "Title": "Ajax form validation" }
4925
<p>I wrote a short ruby script to generate various screenshots, and event though I am not completely new to ruby I rarely use it. </p> <p>Full script (41 LOC): <a href="https://gist.github.com/1229115">https://gist.github.com/1229115</a></p> <p>The string substitutions irritate me. They don't look quite right - much ...
[]
[ { "body": "<p>There are other ways you could write this but I'm not sure if you'd find any of them cleaner. One option is plain old <a href=\"http://ruby-doc.org/core/classes/Kernel.html#M001432\"><code>sprintf</code></a>. For example:</p>\n\n<pre><code># assemble file name\nfname = sprintf(\"%s/%s-%i-%s-%ix%i....
{ "AcceptedAnswerId": "4942", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-20T16:12:43.690", "Id": "4927", "Score": "8", "Tags": [ "ruby", "strings" ], "Title": "String substitutions in ruby and coding style" }
4927
<p>I am fairly satisfied with my solution here but I would definitely appreciate any constructive criticism of my style and design. The basic idea is to use the strategy pattern to simplify assembling a custom procedural generator. I'm using modules rather than classes for improved simplicity of 'mixing' everything tog...
[]
[ { "body": "<p>Stop trying to write Ruby like Java. You've got a whole mess of modules to do something fairly simple.</p>\n\n<p>Most (not all) of the Gang of Four patterns are workarounds for lack of flexibility in languages like C++ and Java, and are less necessary in a language like Ruby that has classes that ...
{ "AcceptedAnswerId": "5510", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-20T20:17:36.770", "Id": "4930", "Score": "6", "Tags": [ "ruby", "design-patterns" ], "Title": "Is this an appropriate class design for the strategy pattern in Ruby?" }
4930
<p>I have 2 Dictionaries. I am trying to optimize this code to run as fast as possible.</p> <p>This is for solving Shanks Baby Step Giant Step Algorithm<br> Algorithm:<br></p> <pre><code>Given b = a^x (mod p) First choose n, such that n^2 &gt;= p-1 Then create 2 lists: 1. a^j (mod p) for 0 &lt;= j &lt; n 2. b...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-21T10:45:57.477", "Id": "7384", "Score": "0", "body": "I am not all that familiar with this particular algorithm however what is the reason for the BigIntegers? What precision do you need?" }, { "ContentLicense": "CC BY-SA 3.0"...
[ { "body": "<p>One way you can improve the speed of the search is:</p>\n\n<ul>\n<li>For one of the two dictionaries, construct an array of the values. Sort this array. </li>\n<li>To do the search: </li>\n<li>> Go through sequentially through the values of the other dictionary. </li>\n<li>> For each value, perfor...
{ "AcceptedAnswerId": null, "CommentCount": "3", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-21T08:16:00.057", "Id": "4932", "Score": "4", "Tags": [ "c#" ], "Title": "Optimizing a c# program that creates 2 dictionaries and searches both for a matching value" }
4932
<p>So after answering a question about <a href="https://codereview.stackexchange.com/questions/4691">nested linked checkboxes</a> I mentioned it at work and surprisingly it turned out to be similar to something we needed.</p> <p>The requirements were:</p> <ol> <li>a "Select all" checkbox </li> <li>a nested "Select al...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-26T04:52:24.100", "Id": "7475", "Score": "0", "body": "Adding a bounty on this. If you have even a small improvement for me please post it!" } ]
[ { "body": "<p>I have a suggestion. Basically I'm not sure why you are adding so much to the HTML to handle the the parent and child checkboxes. Isn't the hierarchy of the HTML enough to go on? From the HTML you can already determine the parent and child relationships. In additon why trigger more events. Ju...
{ "AcceptedAnswerId": "5049", "CommentCount": "1", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-22T03:40:10.467", "Id": "4939", "Score": "5", "Tags": [ "javascript", "jquery", "html" ], "Title": "Nested \"Select All\" checkboxes" }
4939
<p>I have function which writes data of a specified type to a buffer. Here is the part of this which writes <code>Uint8</code>, <code>Uint16</code>, <code>Uint32</code> and <code>Uint64</code>, in big and little endian. As you can see that the same code is repeated several times, so I want to make this code more elegan...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-22T14:18:52.173", "Id": "7404", "Score": "2", "body": "This is C++ code not C. RTTI casts are only available in C++." }, { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-22T22:06:22.800", "Id": "7412", "...
[ { "body": "<pre><code>#define CONVERT(T, F, v) T val = *(static_cast(T*, src)); \\\n F( IN val, OUT_FROM &amp;bw-&gt;buffer[bw-&gt;curPos], OUT_TO &amp;bw-&gt;buffer[bw-&gt;curPos] + v ); \\\n bw-&gt;curPos += length;\n\n [...]\n\n case BW_DATA_TYPE_UINT8:\n {\n CONVERT(Uint8_T, Uint8ToLit...
{ "AcceptedAnswerId": "4943", "CommentCount": "6", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-22T04:29:57.953", "Id": "4941", "Score": "7", "Tags": [ "c", "integer" ], "Title": "Writing data of a certain integer type to a buffer" }
4941
<p>The whole idea behind this ,is the user enters : 1. hg commit -m "NO-TIK" and is able to submit the changeset 2. hg commit -m "NO-REVIEW" also does the same as # 1 3. hg commit -m "JIRA-123 blah blah" is also submitted as long as there is a valid issue "JIRA-123" otherwise the commit is reverted with the message "%s...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-22T18:36:07.833", "Id": "7406", "Score": "0", "body": "i know using sys.exit(1) is reverse of what should be used, but that is the only way , this works" }, { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-22T19...
[ { "body": "<p>You seem confused about <code>sys.exit</code> is doing. It exits your program. You function verify_commit_text never exits. Your code after calling verify_commit_text is never executed. The program always hits a sys.exit in verify_commit_text and terminates. The first thing you need to do is elimi...
{ "AcceptedAnswerId": "4948", "CommentCount": "4", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-22T18:27:05.563", "Id": "4947", "Score": "0", "Tags": [ "python" ], "Title": "The logic for re.match() , sys.exit(1) does not make sense, plus why can't i use return True if the re.match r...
4947
<p>I am actually pretty excited about this approach, but for sanity's sake I wanted to hear some thoughts on others on my strategy here. My basic goal is to parse a YAML file and recursively create module constants using <code>eval</code>. The core business logic is here:</p> <pre><code># # configure application bas...
[]
[ { "body": "<p>You use eval, but <a href=\"https://stackoverflow.com/questions/2571401/why-exactly-is-eval-evil\">eval is evil</a>.</p>\n\n<p>I tried a solution without eval. For this I used:</p>\n\n<ul>\n<li><code>Module.const_set</code> to define constants</li>\n<li><code>Module.const_defined</code> to check i...
{ "AcceptedAnswerId": "6045", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-22T23:21:14.707", "Id": "4949", "Score": "0", "Tags": [ "ruby" ], "Title": "What if any design issues are there in this method of loading configuration data from YAML in Ruby?" }
4949
<p>This code is from a flex app and is the PHP to determine if a user has read a section of text and answered a question correctly.</p> <p>I write to a table that is similar to a bookmark, and then update the users points. This code works, but since I am making two calls to the database I am worried that it is too "ex...
[]
[ { "body": "<p>Sorry. You need to use two statement since you are updating two different tables.</p>\n", "comments": [ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-26T23:12:31.627", "Id": "7486", "Score": "0", "body": "Thanks for the reply. I real...
{ "AcceptedAnswerId": null, "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-23T00:27:12.887", "Id": "4950", "Score": "2", "Tags": [ "php", "mysqli" ], "Title": "Evaluating text section answer accuracy" }
4950
<p>In SQL Server, there is two tables: Houses, and their images.</p> <p>I need a list with 20 houses with the first of their images (only one). I tried:</p> <pre><code>SELECT top 20 h.id, h.name, im.id, im.name FROM image im INNER JOIN house h ON im.house_id = h.id WHERE 1=1 AND im.id=(SELECT...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-11-25T14:29:04.990", "Id": "9765", "Score": "0", "body": "In SQL Server 2005 or newer version you could use [ranking functions](http://msdn.microsoft.com/en-us/library/ms189798.aspx \"Ranking Functions (Transact-SQL)\") to fetch top N row...
[ { "body": "<p>You should be using the clause <code>group by</code></p>\n\n<pre><code>SELECT h.id, h.name, im.id, im.name -- What you want to select\nFROM house h,image im -- Tables in join\nWHERE h.id = im.house_id -- The join (equivalent to inner join)\n\nGROUP BY h.id ...
{ "AcceptedAnswerId": "5017", "CommentCount": "1", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-23T12:10:54.440", "Id": "4952", "Score": "3", "Tags": [ "performance", "sql", "sql-server", "join" ], "Title": "Inner join with first result" }
4952
<p>I'm playing around with PHP, trying to write a small ORM. Having worked with Magento quite a bit lately, I've fallen in love with the automagic getters/setters that Magento, I think, inherited from Zend. </p> <p>For those, who've never seen it in action, this is what I'm talking about:</p> <blockquote> <pre><code>...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2012-10-24T10:39:54.750", "Id": "28462", "Score": "0", "body": "There are probably a lot of reasons I'm unaware of as a frontend guy to use this code, but I'm wondering why you would use this instead of existing PHP magic overloading? See http...
[]
{ "AcceptedAnswerId": null, "CommentCount": "1", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-23T14:39:56.843", "Id": "4953", "Score": "2", "Tags": [ "php", "algorithm", "reflection" ], "Title": "ORM with magic getters/setters" }
4953
<p>I'm using some slow-ish emit() methods in Python (2.7) logging (email, http POST, etc.) and having them done synchronously in the calling thread is delaying web requests. I put together this function to take an existing handler and make it asynchronous, but I'm new to python and want to make sure my assumptions abo...
[]
[ { "body": "<p>It would seem to me that writing a class would be better:</p>\n\n<pre><code>class AsyncHandler(threading.Thread):\n def __init__(self, handler):\n self._handler = handler\n self._queue = Queue.Queue()\n\n self.daemon = True\n self.start()\n\n def run(self):\n ...
{ "AcceptedAnswerId": "4958", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-23T15:11:58.413", "Id": "4954", "Score": "2", "Tags": [ "python", "multithreading", "thread-safety", "asynchronous", "python-2.x" ], "Title": "Is this a safe/correct way to...
4954
<p>The purpose of this function is to report processing progress to the terminal. It loops through an array of maps that contain two properties: <code>:sequence</code> and <code>:function</code>. Within each <code>sequence-function-map,</code> it will loop through the sequence, and run the function on each item in th...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2012-01-16T11:52:21.870", "Id": "12448", "Score": "1", "body": "I would probably break this up into more managable chunks." } ]
[ { "body": "<p>I will point out stuff that I'd do differently, though I am by no means a clojure expert, I do have some experience in it. Numbered items will reappear inside the code as comments.</p>\n\n<ol>\n<li>no DEF inside defn body\n<ul>\n<li>def will define the var for your entire namespace as such, you wi...
{ "AcceptedAnswerId": "7936", "CommentCount": "1", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-23T15:42:10.223", "Id": "4956", "Score": "4", "Tags": [ "beginner", "clojure", "lisp" ], "Title": "A crude clojure progress reporting function" }
4956
<p>Since Python's ConfigParser does not throw an exception if the file does not exist, is it fine to do it this way:</p> <pre><code>try: config = ConfigParser.RawConfigParser() if config.read('/home/me/file.conf') != []: pass else: raise IOError('Cannot open configuration file') except IOEr...
[ { "ContentLicense": "CC BY-SA 4.0", "CreationDate": "2018-06-04T09:27:49.427", "Id": "376987", "Score": "0", "body": "Won't that also throw if the file exists, but is empty?" } ]
[ { "body": "<ol>\n<li>you should try to put as little as possible inside the try block</li>\n<li>There isn't a whole lot of point to throwing an exception just to catch it on the next line.</li>\n</ol>\n", "comments": [], "meta_data": { "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", ...
{ "AcceptedAnswerId": "4962", "CommentCount": "1", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-23T15:55:52.227", "Id": "4957", "Score": "-1", "Tags": [ "python" ], "Title": "ConfigParser inexisting file exception" }
4957
<p>Simple really my javascript function for checking my form.</p> <pre><code>var errors = {}; errors.email = true; errors.cemail = true; errors.password = true; errors.cpassword = true; errors.username = true; function joinAjax (id) { var val = $('#' + id).val(); if (id == 'email') { $('#emailMsg').hide(); v...
[]
[ { "body": "<p>Some tips:</p>\n\n<ul>\n<li>you'd probably better have a look at some <a href=\"http://docs.jquery.com/Plugins/validation\" rel=\"nofollow\">jquery validation plugin</a> that would do what you are looking for <em>out of the box</em></li>\n<li>to solve your \"unknown parameters\", you could probabl...
{ "AcceptedAnswerId": null, "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-23T16:03:25.777", "Id": "4959", "Score": "3", "Tags": [ "javascript", "jquery" ], "Title": "My form error javascript code" }
4959
<p>I'm trying to isolate a webservice in its own class, and I plan to add separate classes to each webmethod there is in the webservice. What I have so far works, but I have this feeling tickling that I've missed something (except for the invisible variable declarations down here, I didn't want to clog the page).</p> ...
[]
[ { "body": "<p>I don't see why would you put every method of the service in a separate class. A \"method\" is a function of the class. I imagine you wanted to decouple your code, but doing it this way you will force a lot of overhead:</p>\n\n<ul>\n<li>the service is instantiated for every 'method' called, and th...
{ "AcceptedAnswerId": "15642", "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-23T16:38:31.527", "Id": "4960", "Score": "5", "Tags": [ "actionscript-3" ], "Title": "Actionscript web service and web method call classes" }
4960
<p>Can you suggest how I might make the following code more efficient:</p> <pre><code>from collections import Counter a = [1,1,3,3,1,3,2,1,4,1,6,6] c = Counter(a) length = len(set(c.values())) normalisedValueCount = {} previousCount = 0 i = 0 for key in sorted(c, reverse=True): count = c[key] if not previ...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-25T19:28:27.850", "Id": "7472", "Score": "0", "body": "Your most recent edit changed the contents of the last list but didn't update the `normaizedValueCount`s. Are those the results you would be expecting?" } ]
[ { "body": "<p>I have no idea what you code is doing, it doesn't look like any normalization I've seen. I'd offer a more specific suggestion on restructuring if I understood what you are doing.</p>\n\n<p>You:</p>\n\n<ol>\n<li>Put a in a counter</li>\n<li>Put that counter's values into a set</li>\n<li>sort the ke...
{ "AcceptedAnswerId": null, "CommentCount": "1", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-23T20:13:25.657", "Id": "4963", "Score": "3", "Tags": [ "python", "optimization", "python-3.x", "collections" ], "Title": "Collections.Counter in Python3" }
4963
<p>I have all large file of all the order sold in one week. This file gives one line for every order. We have over 5000 orders a day. It read the file line by line and then adds the sale to a database of sales. to I really need to boost performance.</p> <pre><code> static void sold4weeks1() { string sol...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-23T20:42:37.473", "Id": "7429", "Score": "1", "body": "@joe what is the purpose of this?> if (words[0].Substring(0, 3) == \"S01\") continue;" }, { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-23T20:43:44.197"...
[ { "body": "<p>Don't use <code>ReadAllLines</code>, read it line by line, it will boost performance by a lot</p>\n", "comments": [ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-26T10:19:11.593", "Id": "7477", "Score": "1", "body": "@Dani: as given....
{ "AcceptedAnswerId": null, "CommentCount": "13", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-23T20:39:39.143", "Id": "4965", "Score": "3", "Tags": [ "c#", "performance" ], "Title": "How do I make this function faster?" }
4965
<pre><code>&lt;?php $userinput = $_GET['host']; $e = escapeshellcmd($userinput); $arr = (explode(".",$e)); $num = count($arr); $times = (int)$_GET['times']; $time = (range(1,51)); if (!isset($time[$times])){ $times = 5; } function isValidURL($url){ return preg_match('...
[]
[ { "body": "<p>I would do <code>$times = min( (int)$_GET['times'], 999);</code> where 999 is the maximum number of times.</p>\n", "comments": [ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-23T22:50:06.153", "Id": "7436", "Score": "0", "body": "tha...
{ "AcceptedAnswerId": null, "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-23T21:38:26.817", "Id": "4974", "Score": "3", "Tags": [ "php", "security", "regex" ], "Title": "Pinging the user requested host - is this code insecure?" }
4974
<p>This is basically my first Ruby class, which is to remove all empty lines and blank times from a .txt file. A simple test case is also included.</p> <p>I tested the code and it works fine. But I did not have much idea about if the code style is good, or if there are some potential issues. Can someone please do a qu...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-22T04:16:53.640", "Id": "7437", "Score": "0", "body": "On the method `remove` you have an `if/else` that could be re-written as `stringToReturn.length >= 1 ? stringToReturn : \"\"`, but to check that a string has a size bigger than one...
[ { "body": "<p>I would also remove the following: \\r\\n</p>\n\n<p>Also, this part could be simplified a tad:</p>\n\n<pre><code>if stringToReturn.length &gt;= 1\n return stringToReturn\nelse\n return \"\"\nend\n</code></pre>\n\n<p>to:</p>\n\n<pre><code> stringToReturn.length &gt;= 1 ? stringToReturn : \"\"\n<...
{ "AcceptedAnswerId": "4978", "CommentCount": "2", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-09-22T03:56:09.627", "Id": "4977", "Score": "2", "Tags": [ "beginner", "ruby", "unit-testing", "regex" ], "Title": "Regular Expression to remove blank lines" }
4977
<p>Doing practise questions for a Java exam which have no answers (useful) I have been asked to do the following:</p> <p>Write a class called <code>Person</code> with the following features:</p> <ul> <li>a <code>private int</code> called <code>age</code>;</li> <li>a <code>private String</code> called <code>name</code...
[ { "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-05-30T14:27:22.420", "Id": "7449", "Score": "0", "body": "I don't see any errors :)" } ]
[ { "body": "<p>That's fine, but I would have written the last method as <code>return (age &gt;= 18);</code></p>\n", "comments": [], "meta_data": { "CommentCount": "0", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-05-30T14:26:12.013", "Id": "4980", "ParentId": "497...
{ "AcceptedAnswerId": "4981", "CommentCount": "1", "ContentLicense": "CC BY-SA 3.0", "CreationDate": "2011-05-30T14:23:38.863", "Id": "4979", "Score": "11", "Tags": [ "java" ], "Title": "Java practise exam question" }
4979