body stringlengths 25 86.7k | comments list | answers list | meta_data dict | question_id stringlengths 1 6 |
|---|---|---|---|---|
<p>CoffeeScript is an alternative syntax for JavaScript. Unlike other languages that compile to JavaScript, it deliberately retains the exact semantics of JavaScript. Furthermore, it provides syntax help to enable you to write correct, verbose, JS code concisely.</p>
<p><a href="http://coffeescript.org/" rel="nofoll... | [] | [] | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-04T09:13:36.897",
"Id": "3864",
"Score": "0",
"Tags": null,
"Title": null
} | 3864 |
CoffeeScript is a little language that compiles into JavaScript. Underneath all of those embarrassing braces and semicolons, JavaScript has always had a gorgeous object model at its heart. CoffeeScript is an attempt to expose the good parts of JavaScript in a simple way. | [] | [] | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-04T09:13:36.897",
"Id": "3865",
"Score": "0",
"Tags": null,
"Title": null
} | 3865 |
<p>In my model, I have things called Reports that have known workflow associated with them. </p>
<p>I've got a requirement to output at which state a report currently is.</p>
<pre><code>public abstract partial class Report{
public virtual string StateStatus(){
if (Check.IsSuccessful) return "Approved";
... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-04T13:53:39.423",
"Id": "5805",
"Score": "0",
"body": "Are those calculated properties you are checking?"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-04T14:39:01.127",
"Id": "5808",
"Score": "0",... | [
{
"body": "<p>A state machine. This tidies up all of those messy methods into a single property which stores the current state of your object. The downside to this is you'll need to tidy up your other objects, too...</p>\n\n<pre><code>public partial class Report {\n\n public enum State {\n NotReceiv... | {
"AcceptedAnswerId": null,
"CommentCount": "6",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-04T13:04:50.227",
"Id": "3872",
"Score": "2",
"Tags": [
"c#"
],
"Title": "Refactoring a series of if-return statements."
} | 3872 |
<pre><code>public static DateTime ObjectToDateTime(object o, DateTime defaultValue)
{
if (o == null) return defaultValue;
DateTime dt;
if (DateTime.TryParse(o.ToString(), out dt))
return dt;
else
return defaultValue;
}
</code></pre>
<p>The code feels too wordy and smells ba... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-04T17:55:25.910",
"Id": "5816",
"Score": "1",
"body": "You could get rid of the \"else\" (because in the true case it has already returned), but that's not much of an improvement. Other than that, this looks pretty optimal (clarity-wi... | [
{
"body": "<p>I like @ChaosPandion's solution however I find the following a bit quicker to read. </p>\n\n<pre><code>public static DateTime ObjectToDateTime(object o, DateTime defaultValue)\n{ \n DateTime result;\n if (DateTime.TryParse((o ?? \"\").ToString(), out result)) {\n return result;\n ... | {
"AcceptedAnswerId": "3886",
"CommentCount": "4",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-04T17:45:45.603",
"Id": "3879",
"Score": "24",
"Tags": [
"c#",
"datetime",
"converting"
],
"Title": "Convert Object to a DateTime"
} | 3879 |
<p>I created this class to keep track of a user's role:</p>
<pre><code>class UserRoleHelper extends AppHelper
{
public $helpers = array('Session');
private $role = NULL;
const CUSTOMER = 'customer';
const ASSOCIATE = 'associate';
const MANAGER = 'manager';
const ADMIN = 'admin';
co... | [] | [
{
"body": "<p>Use an <a href=\"http://www.php.net/manual/de/class.splenum.php\" rel=\"nofollow\">enumerator</a>?</p>\n\n<p>And use it like this:</p>\n\n<pre><code>if($this->isInRole(Roles::MANAGER)\n</code></pre>\n",
"comments": [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": ... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-04T19:45:05.047",
"Id": "3884",
"Score": "3",
"Tags": [
"php"
],
"Title": "Keeping track of user role's"
} | 3884 |
<p>I have designed this model which is very flexible. For example, you can create asset types, assets and combinations of them infinitely. It is the front end to a Python Pyramid website, so all the validation and business logic is handled by the web app.</p>
<p>However, not being a db guy, I have this sneaking suspic... | [] | [
{
"body": "<p>You've reinvented a database inside a database. Basically, the Asset/AssetType is a simulation of a database inside a database which will as a result be slow. Also, you are going to spend a lot of effort reimplementing database features.</p>\n\n<p>You could do this by using a NoSQL database which ... | {
"AcceptedAnswerId": "3890",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-05T01:04:52.997",
"Id": "3888",
"Score": "3",
"Tags": [
"python",
"pyramid"
],
"Title": "Elixir db model"
} | 3888 |
<p>A naïve quicksort will take O(n^2) time to sort an array containing no unique keys, because all keys will be partitioned either before or after the pivot value. There are ways to handle duplicate keys (like one described in Quicksort is Optimal). The proposed solution only works for the Hoare partition, but I've imp... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-05T04:02:33.343",
"Id": "5828",
"Score": "1",
"body": "This is code review, not pseudocode review. If you want reviews of your code, please post your actual code."
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "201... | [
{
"body": "<ol>\n<li>Use a swap function rather then repeating the three lines required to swap elements three times</li>\n<li>Put spaces around your operators</li>\n<li>You avoid recursing for the second partition. I don't think the performance gain from this is sufficient for the extra complexity in your code... | {
"AcceptedAnswerId": "3891",
"CommentCount": "6",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-05T02:55:43.463",
"Id": "3889",
"Score": "4",
"Tags": [
"java",
"quick-sort"
],
"Title": "Handling duplicate keys in quicksort"
} | 3889 |
<p>I am trying to create an extension method that builds a POCO object (copies all the fields) for an Entity Object.</p>
<p>When Entity object is simple (no navigation, no sub collections), it works fine.
I want to improve it so it could also deal with sub-entities. </p>
<p>For this example I take Nortwind database ... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-04T08:22:32.793",
"Id": "5850",
"Score": "0",
"body": "Well for better speed I would cache the PropertyInfo[], so you don't have to use reflection everytime, this will speed up your application a lot, if you are calling this extension ... | [
{
"body": "<p>For this task, i would use <a href=\"http://automapper.codeplex.com/\" rel=\"nofollow\">AutoMapper</a>, it does exactly what you need, and can work with complex entities.</p>\n\n<p>If you are writing this mapper yourself, consider using expression compilation feature.\nSuch code will execute at th... | {
"AcceptedAnswerId": null,
"CommentCount": "5",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-04T08:18:23.447",
"Id": "3895",
"Score": "2",
"Tags": [
"c#",
"entity-framework",
"reflection",
"extension-methods"
],
"Title": "Creating Extension Method to map entity with sube... | 3895 |
<p>In the index view a user can search with any combination of one to all of the following parameters: <code>firstname</code>, <code>lastname</code> or <code>ssn</code>. The found search results are displayed in the search results view. In my <code>HomeOfficeController</code> I have written the code like this, but most... | [] | [
{
"body": "<p>This is roughly how I would refactor it (note: I have not tried to compile this so I'm sure some tweaking will be needed).</p>\n\n<pre><code>public ActionResult SearchResults(HomeOfficeViewModel viewModel, UserSessionContext sessionContext)\n{\n TempData.Keep();\n if (TempData[\"FirstName\"]... | {
"AcceptedAnswerId": "3907",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-05T14:51:55.150",
"Id": "3899",
"Score": "4",
"Tags": [
"c#",
"asp.net-mvc-3",
"controller"
],
"Title": "Controller for searching with certain parameters"
} | 3899 |
<p>I was asked to make a Java application with main class q2.</p>
<p>How can I make it better and more efficient?</p>
<pre><code>import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
class Sort extends q2
{
public static double SWITCH;
}
class q2 extends JFrame
{
public static... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-01-18T16:26:07.507",
"Id": "12567",
"Score": "1",
"body": "please learn java naming conventions and stick to them"
}
] | [
{
"body": "<p>Unless you absolutely <em>must</em> keep everything in <code>main</code>, this should probably be split up into a number of separate functions. At the very least, I'd think of:</p>\n\n<ol>\n<li>One function for the UI.</li>\n<li>One function for each sort.</li>\n<li>One function to randomize the d... | {
"AcceptedAnswerId": "3910",
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-05T15:58:17.120",
"Id": "3902",
"Score": "3",
"Tags": [
"java"
],
"Title": "Java application for comparing sorting methods"
} | 3902 |
<p><a href="http://jsfiddle.net/caimen/kMkrW/" rel="nofollow">Here</a> is a link to the code on JSFiddle.</p>
<p>This is my first attempt at playing with canvas. Before I move on doing anything else, it would be nice to have insight from somebody who knows canvas and JavaScript better than me.</p>
<p>Things I am loo... | [] | [
{
"body": "<p>It's more personal preference than any hard requirement, but I always prefer object literal syntax to individual value assignments. That would turn your initial declarations into this:</p>\n\n<pre><code>var ship = {\n name: \"Enterprise\",\n x: 0,\n y: 0.\n width: 50,\n left: false,\n right... | {
"AcceptedAnswerId": "4001",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-05T18:00:40.730",
"Id": "3903",
"Score": "7",
"Tags": [
"javascript",
"object-oriented",
"canvas"
],
"Title": "Simple JavaScript canvas game"
} | 3903 |
<p>While this is working very well for storing setting variables for my applications, I've been using it for a number of years and really feel there is something better. I'm perhaps just looking to use some newer functions from the framework. </p>
<pre><code> public string Value { get; set; }
public string Name... | [] | [
{
"body": "<p>It look spretty good to me, but there are a couple of things I would likely change, none of which have to do with the use (or non-use) of a hashtable data structure as it is perfectly suited for this sort of thing.</p>\n\n<p>I would probably change the <code>LoadSettings</code> method a bit as it ... | {
"AcceptedAnswerId": "3905",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-05T19:33:46.187",
"Id": "3904",
"Score": "3",
"Tags": [
"c#",
"asp.net",
"cache"
],
"Title": "Possible solutions to remove the use of a Hashtable"
} | 3904 |
<p>I am trying to solve an exercise in which I am given the following function:</p>
<pre><code>/* squeeze: delete all c from s */
void squeeze(char s[], int c)
{
int i, j;
for (i = j = 0; s[i] != '\0'; i++)
if (s[i] != c)
s[j++] = s[i];
s[j] = '\0';
}
</code></pre>
<p>the exercise is:</p>
<blockquot... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-06T00:03:17.410",
"Id": "5869",
"Score": "0",
"body": "Are you restricted to only writing code in one single function or could you add helper functions? Does it have to be optimized for speed (or other metric)? Case sensitive? Do you r... | [
{
"body": "<p>Although there are quite a few ways to do this, I think I'd represent the \"set\" of characters to be skipped as an array of \"boolean\"s -- which is to say an integer for each possible character, saying whether to copy that character or not:</p>\n\n<pre><code>char copy[CHAR_MAX];\n\n// by default... | {
"AcceptedAnswerId": "3913",
"CommentCount": "3",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-05T22:58:28.930",
"Id": "3908",
"Score": "3",
"Tags": [
"c",
"strings"
],
"Title": "Delete each character in one string that matches any character in another"
} | 3908 |
<p>For a personal project, I'm working on a Java profiler that specifically targets <a href="https://docs.oracle.com/en/java/javase/13/docs/api/java.base/java/util/concurrent/ThreadPoolExecutor.html" rel="nofollow noreferrer">ThreadPoolExecutor</a> and its subclasses and provides statistics about throughput of executed... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-09T13:50:07.017",
"Id": "5957",
"Score": "1",
"body": "What's the point of the buckets?"
},
{
"ContentLicense": "CC BY-SA 4.0",
"CreationDate": "2019-12-21T18:47:43.390",
"Id": "458480",
"Score": "0",
"body": "C... | [
{
"body": "<p>Method <code>fillBucketFromAccumulator()</code> is not <a href=\"https://en.wikipedia.org/wiki/Thread_safety\" rel=\"nofollow noreferrer\">thread-safe</a>. Two threads can write to the same slot in the array, and then both advance by one.</p>\n",
"comments": [],
"meta_data": {
"Comme... | {
"AcceptedAnswerId": null,
"CommentCount": "2",
"ContentLicense": "CC BY-SA 4.0",
"CreationDate": "2011-08-06T03:45:42.087",
"Id": "3912",
"Score": "5",
"Tags": [
"java",
"multithreading"
],
"Title": "Concurrent event counter/averager utility classes"
} | 3912 |
<p>I have several utility classes who should only ever have one instance in memory, such as <code>LogHelper</code>, <code>CacheHelper</code>, etc. One instance to rule them all.</p>
<p>I've never implemented <a href="https://en.wikipedia.org/wiki/Singleton_pattern" rel="nofollow noreferrer">Singleton</a> in Java befor... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-06T02:50:29.483",
"Id": "5887",
"Score": "0",
"body": "Make the constructor private."
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-09T07:21:18.720",
"Id": "5955",
"Score": "0",
"body": "In mos... | [
{
"body": "<p>Unfortunately, there is no way (that I know of) to prevent a subclass from exposing a public constructor - as a result, creating a <a href=\"https://en.wikipedia.org/wiki/Singleton_pattern\" rel=\"nofollow noreferrer\">Singleton</a> base-class may not be in your best interest. If your goal is to e... | {
"AcceptedAnswerId": "3921",
"CommentCount": "3",
"ContentLicense": "CC BY-SA 4.0",
"CreationDate": "2011-08-06T02:34:45.310",
"Id": "3920",
"Score": "5",
"Tags": [
"java",
"object-oriented",
"singleton"
],
"Title": "Request for Comments: Singleton pattern implemented in Java"
} | 3920 |
<p>The following implementation is of a <em>repository proxy</em>.</p>
<p>I will post only the code for <code>NHibernate</code> Repository here. Everything else (including configurers and tests) can be found on the <a href="http://pastebin.com/kjSHgDnR" rel="nofollow">pastebin</a>.</p>
<p><strong>P.S. :</strong> I c... | [] | [
{
"body": "<p>Two things i notied:</p>\n\n<p>Wrap the transaction in a using-statement and also log the exception.</p>\n\n<pre><code> // Performs the entire specified action within a single unit of work.\n private void WithinTransaction(Action action)\n {\n using(var transaction = session.BeginTran... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-06T21:37:21.043",
"Id": "3926",
"Score": "1",
"Tags": [
"c#",
"beginner",
"repository"
],
"Title": "Repository wrapper"
} | 3926 |
<p>Please review this.</p>
<pre><code>public int InsertQuery(string Query)
{
int LastInsertedID = -1;
MySqlCommand MySqlCmd = new MySqlCommand(Query, this.sqlConn);
try
{
MySqlCmd.ExecuteNonQuery();
LastInsertedID = int.Parse(MySqlCmd.LastInsertedId.ToString());
}
catch (Excepti... | [] | [
{
"body": "<p>Don't take shortcuts. Build a proper insert statement using a parameterized query and validate each of the form inputs that they're also of the proper type (ie., not sending a string into an integer field). </p>\n\n<p>Do this to protect the integrity of your data, both from improper types and from... | {
"AcceptedAnswerId": "3930",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-06T21:59:22.133",
"Id": "3927",
"Score": "3",
"Tags": [
"c#",
"mysql"
],
"Title": "Inserting an entire form into MySQL database"
} | 3927 |
<p>I wonder what is better code practice or just what looks better in Java:</p>
<p>Version 1:</p>
<pre><code>protected Boolean doSomething(int amount) {
if (amount < 1) return false;
return insertToDb(fillImages(fetchInfo(REQUEST_URL, amount)));
}
</code></pre>
<p>Version 2:</p>
<pre><code>protected Boo... | [] | [
{
"body": "<p>I definitely prefer the second version. The steps taken just \"leap out\" of the code and the method is easier to read. It will also be a lot easier to debug (or to pinpoint the problem in a stack trace).</p>\n\n<p>Always go for readability, it makes for easier understanding when going back to you... | {
"AcceptedAnswerId": "3929",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-06T22:17:39.263",
"Id": "3928",
"Score": "8",
"Tags": [
"java"
],
"Title": "Different functions in Java"
} | 3928 |
<p>I am currently reading <em>The C++ Programming Language</em> (Special Edition) and am learning about using templates. One of the exercises asks you to program a generic version of quicksort. I am hoping to build a namespace called <code>Sorters</code> that has a class for each sorter, just for learning purposes. I w... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2015-05-25T05:03:07.627",
"Id": "166383",
"Score": "0",
"body": "Explaining the unaccepting of the answer doesn't belong in the original post. Instead, provide comments to the answerer."
}
] | [
{
"body": "<p>First thing your swap is going to kill you on expensive swaps:</p>\n\n<pre><code>template<class T>\nvoid QSorter<T>::Swap( typename vector<T>::iterator l, typename vector<T>::iterator r )\n{\n T tmp = (*r);\n (*r) = (*l);\n (*l) = tmp;\n}\n</code></pre>\n\n<p>Use s... | {
"AcceptedAnswerId": null,
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-07T00:01:03.263",
"Id": "3932",
"Score": "4",
"Tags": [
"c++",
"algorithm",
"quick-sort",
"template"
],
"Title": "More generic sorting algorithm: using namespaces"
} | 3932 |
<p>This is a class from my rails application. It is used as non-database model in my RoR application and uses syntax similar to AR design pattern. I wonder is there a way to make this look more like Ruby code?</p>
<pre><code>class Role
def self.all
role_list = Array.new
r1 = Role.new
r1.id = 1
r1.s... | [] | [
{
"body": "<p>For starters:</p>\n\n<pre><code>def initialize(attributes = {})\n attributes.each do |attr, value|\n self.send \"#{attr}=\", value\n end\nend\n</code></pre>\n\n<p>Would allow you to turn:</p>\n\n<pre><code>r1 = Role.new\nr1.id = 1\nr1.symbol = :owner\nr1.name = \"Owner\"\nr1.has_all_permissio... | {
"AcceptedAnswerId": "4472",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-07T06:05:11.073",
"Id": "3934",
"Score": "3",
"Tags": [
"ruby",
"ruby-on-rails",
"classes"
],
"Title": "How would you ruby-fy this non-AR model class for Ruby on Rails app?"
} | 3934 |
<p>I have something like this in my program:</p>
<pre><code> private void tspBrush_Click(object sender, EventArgs e)
{
currentTool = new Brush(tileLayers);
UncheckToolstripButtons();
tspBrush.Checked = true;
}
private void tspBucket_Click(object sender, EventArgs e)
{
... | [] | [
{
"body": "<p>I'm calling the common item \"Tool\" but I'm sure you have another name for it...</p>\n\n<pre><code>private void tspBrush_Click(object sender, EventArgs e)\n {\n setTool(tspBrush);\n }\n\nprivate void tspBucket_Click(object sender, EventArgs e)\n {\n setTool(tspBucket);\n ... | {
"AcceptedAnswerId": "3940",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-07T16:00:18.837",
"Id": "3938",
"Score": "5",
"Tags": [
"c#"
],
"Title": "This code looks really repetitive. Any way to shorten it?"
} | 3938 |
<p>I am preparing myself for some job interviews. This is a simple array (with the error handling left out for brevity). Any input or suggestions on style and the use of templates would be appreciated.</p>
<pre><code> #include <iostream>
template <class T>
class Array {
public:
explici... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-07T19:53:36.880",
"Id": "5912",
"Score": "0",
"body": "White space is your friend when it comes to readability."
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-07T22:41:12.080",
"Id": "5917",
"Score... | [
{
"body": "<p>Any reason why you pass integers as const references - it will merely slow down your code, and possible confuse the reader, for no benefit.</p>\n\n<p>I'm rusty on templates, so I could be wrong, but doesn't </p>\n\n<pre><code> Array(const Array&);\n</code></pre>\n\n<p>Allow you to try to cons... | {
"AcceptedAnswerId": "3946",
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-07T19:50:04.700",
"Id": "3944",
"Score": "7",
"Tags": [
"c++",
"array",
"template"
],
"Title": "Simple array implementation without bounds-checking"
} | 3944 |
<p>I am attempting to learn R programming by going through the questions in <a href="http://projecteuler.net/index.php?section=problems" rel="nofollow">Project Euler</a>. </p>
<p>The code below is my solution to <a href="http://projecteuler.net/index.php?section=problems" rel="nofollow">problem 5</a> which asks for th... | [] | [
{
"body": "<p>You should change the algorithm in order to make your program run faster. The while loop is performed over 200 million times (printing out information in every iteration) until it reaches the desired number.</p>\n\n<p>If you calculate the answer using LCM (taking the least common multiple of all n... | {
"AcceptedAnswerId": "3960",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-08T02:23:55.140",
"Id": "3953",
"Score": "4",
"Tags": [
"project-euler",
"r"
],
"Title": "How can I get this R code to pass the \"1 minute\" test for this Project Euler question?"
} | 3953 |
<p>I'm writing GUI controls and there are many places where there are many nested <code>if</code>s checking for some result.</p>
<pre><code>function TMyObject.GetCursor: TCursor;
begin
if CanDragX then
begin
if CanDragY then
Result := crSizeAll
else
Result := crSizeWE;
end
else if CanDragY ... | [] | [
{
"body": "<p>I would do this:</p>\n\n<pre><code>function TMyObject.GetCursor: TCursor;\nbegin\n if CanDragX and CanDragY then\n Result := crSizeAll\n else if CanDragX then\n Result := crSizeWE\n else if CanDragY then\n Result := crSizeNS\n else\n if CanClick then\n Result := crHandPoint\n ... | {
"AcceptedAnswerId": "3957",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-08T05:51:59.267",
"Id": "3954",
"Score": "5",
"Tags": [
"delphi"
],
"Title": "Nested IF code in GUI Controls"
} | 3954 |
<p>Is this code too cryptic? Simple yes or no question. Feedback is optional.</p>
<pre><code>int recursive_euclidean( int num1, int num2 )
{
int gcd=0;
if( num1 == num2 || num1 < 0 || num2 < 0 )
( ( num1 == num2 ) ? ( gcd = num1 ) : ( ( num1 < 0) ? ( gcd = num2 ) :
( gcd = num1 ... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-08T11:15:05.210",
"Id": "5933",
"Score": "0",
"body": "Mathematically this is not quite correct, `gcd(2, -2) = 2` is a better choice. `gcd(2, -2) = -2` is also correct, as in terms of divisibility `n` and `-n` are essentially the same... | [
{
"body": "<p>I don't like the multiple different assignments inside the operator.<br>\nThey currently all assign to <code>gcd</code> but you need to study it in detail to work that out.<br>\nI would make the assignment explicit and the ternary operator return the correct value.</p>\n\n<p>The other thing with t... | {
"AcceptedAnswerId": "3958",
"CommentCount": "4",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-08T06:10:37.797",
"Id": "3955",
"Score": "17",
"Tags": [
"c++",
"algorithm",
"recursion",
"iteration"
],
"Title": "Recursive and iterative Euclidean algorithms"
} | 3955 |
<p>Here is a class that I made to validate forms data. I will really appreciate any criticism and hints.</p>
<pre><code> <?php
class formvalidator {
public $filtered, $errors,$db,
$fields_type = Array(), $error_msgs = Array();
public function validate($form, $fields, $error_msgs) {
$this-&... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-10-12T01:36:53.637",
"Id": "8021",
"Score": "0",
"body": "Comment to self: Rename `valure_class()` method with `attributes()` as it either returns class or value."
}
] | [
{
"body": "<p>One thing that I see right off the bat, is the <code>clean()</code> method. Why are you escaping things arbitrarily?</p>\n\n<pre><code>public function clean($input) {\n $clean = Array();\n foreach ($input as $field => $data) {\n $clean[$field] = mysql_real_escape_string($data);\n ... | {
"AcceptedAnswerId": null,
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-08T13:47:27.897",
"Id": "3965",
"Score": "2",
"Tags": [
"php",
"php5",
"form",
"validation"
],
"Title": "Validating forms data"
} | 3965 |
<p>I've been reading <em>Cracking the Coding Interview</em> and at the very beginning I got to the strange statement that the next complexity will be \$O(n^2)\$.</p>
<pre><code>public String makeSentence(String[] words){
String sentence = "";
for (String w:words){
sentence+=w;
}
return sentence;
}
</code></pr... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-08T20:53:18.810",
"Id": "5952",
"Score": "6",
"body": "This would be more appropriate for [SO] I would think."
}
] | [
{
"body": "<p>For every call to <code>String.concat()</code>, a new string instance is created. To create that instance, the contents of the previous string needs to be copied to the new one plus the contents of the concatenated string. This is done for every string in the collection. So if you look at in in... | {
"AcceptedAnswerId": null,
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-08T20:47:54.107",
"Id": "3972",
"Score": "5",
"Tags": [
"java",
"algorithm",
"strings"
],
"Title": "String manipulation complexity"
} | 3972 |
<p><img src="https://i.stack.imgur.com/Jwz0G.gif" width="100"></p>
<p>This tag is used for questions about the <a href="http://www.lua.org/" rel="nofollow noreferrer">Lua programming language</a>.</p>
<p>From <a href="http://www.lua.org/about.html" rel="nofollow noreferrer">Lua's About page</a>:</p>
<blockquote>
<... | [] | [] | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-08T23:14:04.850",
"Id": "3977",
"Score": "0",
"Tags": null,
"Title": null
} | 3977 |
<p>I have the need to programatically create a context menu using names for the menu items which aren't known until the program loads user data.</p>
<p>Here's how I have implemented it. I'm not happy with the cast to get the menu item text to find out which item they have clicked on. Is there a better way that I'm mi... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-09T13:32:12.013",
"Id": "5956",
"Score": "0",
"body": "I believe what you are looking for is dependency injection."
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-09T16:22:52.287",
"Id": "5965",
"Sc... | [
{
"body": "<p>Though there are other things in this I am not sure are done in an ideal fashion, I'm not sure the whole scope so I will stick to just what you asked about:</p>\n\n<p>This cast from sender to a specific type which is the only type that will fire the event, is the standard way of doing exactly what... | {
"AcceptedAnswerId": null,
"CommentCount": "5",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-09T13:19:02.503",
"Id": "3983",
"Score": "6",
"Tags": [
"c#",
"winforms"
],
"Title": "Programatically creating context menu"
} | 3983 |
<p>I have a windows forms application in which a backgroundworker is called again and again.
I need to avoid concurrent access of the code in dowork method for the backgroundWorker; but also need to ensure that the code in the dowork method is called; hence I cannot simply avoid running the backgroundworker altogether ... | [] | [
{
"body": "<p>If you can't enter the lock, this implies that the \"work-queue\" is getting backed up, right? This means that work is not occurring fast enough, so it's possible that <code>tmrEnsureWorkerGetsCalled</code> might get overwritten if <code>tmrCallBgWorker</code> fires twice while a single long-runni... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-09T14:44:41.353",
"Id": "3985",
"Score": "6",
"Tags": [
"c#",
"locking"
],
"Title": "Using Timer with Backgroundworker to ensure the doWork method is called"
} | 3985 |
<p>I am working on a script that collects all comments from my DB and inserts them into a hash. I then do a collect on the comments hash and split the comments into two separate comment hashes (vid_comments & wall_comments)</p>
<pre><code>w_hash = Hash.new
w_hash["comments"] = []
contender.subscriber.video... | [] | [
{
"body": "<p>well, first of all your first query is extremely ineffective</p>\n\n<pre><code>contender.subscriber.videos.collect { |v| w_hash[\"comments\"] << v.comments.where(\"created_at > DATE_SUB( NOW(), INTERVAL 1 DAY)\") }\n</code></pre>\n\n<p>this will query you db for each video you've got</p>\... | {
"AcceptedAnswerId": "3991",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-09T15:24:42.200",
"Id": "3987",
"Score": "5",
"Tags": [
"ruby",
"array"
],
"Title": "Ruby: working with arrays"
} | 3987 |
<p>Presented for critique are a pair of classes which automate Sizer Creation and Layout in wxPython.</p>
<pre><code>import wx
from wx.lib.combotreebox import ComboTreeBox
from wx.lib.agw.floatspin import FloatSpin
wx.ComboTreeBox = ComboTreeBox
wx.FloatSpin = FloatSpin
class FormDialog(wx.Dialog):
def __init__(se... | [] | [
{
"body": "<pre><code>wx.ComboTreeBox = ComboTreeBox\nwx.FloatSpin = FloatSpin\n</code></pre>\n\n<p>Modifying the contents of another module is suspicious and likely to cause trouble. Your changes might not be there when another module is importing and could cause trouble.</p>\n\n<pre><code> ds = wx.GridBagSiz... | {
"AcceptedAnswerId": "3994",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-09T16:15:04.373",
"Id": "3989",
"Score": "3",
"Tags": [
"python",
"layout",
"user-interface"
],
"Title": "wxPython Form Builder - Sizer Automation"
} | 3989 |
<p>Each product in my e-commerce website has Arabic and English values for the title, description and excerpt. I have this method <code>EditProduct</code> to update those values based on current culture (Arabic or English)</p>
<pre><code>public void EditProduct(string element, string text1, string text2, bool edit1, b... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-09T22:00:59.073",
"Id": "5979",
"Score": "0",
"body": "What are Title and TitleAr for? What do edit1 and edit2 mean?"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-10T07:59:32.710",
"Id": "5991",
"... | [
{
"body": "<p>You should try using a switch statement for the languages. Also move the languages to be the first statements to check instead of the element. This combined should reduce the size of your code.</p>\n\n<p><a href=\"http://msdn.microsoft.com/en-us/library/06tc147t%28v=vs.80%29.aspx\" rel=\"nofollo... | {
"AcceptedAnswerId": "3998",
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-09T21:30:40.330",
"Id": "3993",
"Score": "7",
"Tags": [
"c#"
],
"Title": "Editing e-commerce website values"
} | 3993 |
<p>I am still uncomfortable and new with working with classes.</p>
<p>I have made a User class that will return stuff about the user.</p>
<pre><code><?
class User {
public function age($id) {
$birth = new DateTime("$id");
$now = new DateTime();
$age = $now->diff($birth)->format("... | [] | [
{
"body": "<p>In short : its an extremely bad piece of code.</p>\n\n<p>Here is a list or \"whys\":</p>\n\n<ul>\n<li>you are mixing data access with logic ( sql queries should be handled by different object )</li>\n<li>you are using global state for DB access </li>\n<li>there is some mystical parameter <code>$fu... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-09T23:35:22.730",
"Id": "3996",
"Score": "2",
"Tags": [
"php"
],
"Title": "Improving my user class"
} | 3996 |
<p>The point of this algorithm is to see if an element exists in a NxN matrix that has its rows and columns sorted.</p>
<p>What would you change? What did I do well? Both perspectives help so I am not left guessing.</p>
<pre><code>#include <iostream>
#include <iomanip>
#include <cmath>
#include <... | [] | [
{
"body": "<p>I would replace:</p>\n\n<pre><code> int **matrix;\n</code></pre>\n\n<p>with:</p>\n\n<pre><code> std::vector<std::vector<int> > matrix; /* or Boost::Matrix */\n</code></pre>\n\n<p>Then your constructor/destructor become:</p>\n\n<pre><code>RandomOrderedMatrix::RandomOrdere... | {
"AcceptedAnswerId": "4004",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-10T05:47:38.573",
"Id": "4003",
"Score": "6",
"Tags": [
"c++",
"algorithm",
"matrix",
"search"
],
"Title": "Determine if an element exists in a sorted NxN matrix"
} | 4003 |
<p>I have implemented mutex and conditional variables using the <code>futex</code> syscall. I believe that my implementation is correct, but would like it to be verified by someone else. Any suggestions for further improvements in the performance of the mutex and conditional variables would also be appreciated.</p>
<p... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-10T12:51:39.657",
"Id": "5996",
"Score": "0",
"body": "Doesn't pthreads map the mutex implementation onto a futex on Linux platforms for you for free?"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-10T20:2... | [
{
"body": "<p>I have no particular experience of Futex code, so take this with caution:</p>\n\n<p>Looking at <code>mutex_lock</code>, I think some comments would have been useful. As I\nunderstand it, this is what it does:</p>\n\n<ul>\n<li><p>Firstly let's define what your values 0, 1, 2 mean. I understand th... | {
"AcceptedAnswerId": null,
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-10T08:02:09.327",
"Id": "4005",
"Score": "4",
"Tags": [
"c",
"multithreading",
"linux"
],
"Title": "Mutex and condition variable implementation using futex"
} | 4005 |
<h2>Aim:</h2>
<p>Use C++0x features to make function interposition safer. The problem is that it's easy to make a typo when wrapping and interposing on functions.</p>
<h2>Prototype Implementation:</h2>
<pre><code>#define MAKE_WRAPPER(x) static const wrapper<decltype(::x), ::x> x(#x)
namespace {
template<... | [] | [
{
"body": "<p>[ I'm assuming your example is meant to <code>return x(dpy, drawable);</code> rather than call itself recursively. ]</p>\n\n<ol>\n<li><p>It's worth it if you have a use for it I guess.</p></li>\n<li><p>Printing to <code>std::cerr</code> is not error handling, it's error logging. Pick a real error ... | {
"AcceptedAnswerId": "4062",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-10T10:49:16.157",
"Id": "4006",
"Score": "4",
"Tags": [
"c++",
"c++0x"
],
"Title": "Typesafety with dlsym function loading"
} | 4006 |
<p>I have the following (extremely simplified) database structure:</p>
<ul>
<li>Table: <code>Competitions</code>
<ul>
<li><code>Id</code>: string, unique</li>
</ul></li>
<li><p>Table: <code>Persons</code></p>
<ul>
<li><code>Id</code>: string, unique</li>
<li><code>Gender</code>: string</li>
</ul></li>
<li><p>Table: ... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-10T14:41:02.820",
"Id": "6001",
"Score": "0",
"body": "If I'm reading this correctly, you want to throw out the lowest female score in each competition?"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-10T19... | [
{
"body": "<p>Try this one:</p>\n\n<pre><code> var filteredResults =\n (from r in results \n where r.Average > 0 &&\n r.EventID == \"333\" &&\n r.RoundID == \"f\"\n select r).ToList();\n\n List<Results> bestRes... | {
"AcceptedAnswerId": "4012",
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-10T13:08:26.747",
"Id": "4008",
"Score": "3",
"Tags": [
"c#",
"performance",
"linq",
"database"
],
"Title": "Database of competition information"
} | 4008 |
<p>Unlike most(?) smart pointers, boost::intrusive_ptr has a non-explicit constructor. Given that, one could write</p>
<pre><code>typedef boost::intrusive_ptr<Foo> FooPtr;
FooPtr MyFactory()
{
return new Foo(a, b, c);
}
</code></pre>
<p>Or one could write</p>
<pre><code>FooPtr MyFactory()
{
return Foo... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-10-25T22:10:39.793",
"Id": "8465",
"Score": "0",
"body": "On a side note, I subscribe to [Linus Torvalds' opinion on typedefs](http://lkml.indiana.edu/hypermail/linux/kernel/0206.1/0402.html) in as much as they are overused and not always... | [
{
"body": "<p>When in doubt, be explicit. While both of the functions given will work, the second expresses programmer intent more clearly. Adding a comment along the lines of \"This will call intrusive_ptr's implicit constructor\" to the first can help, though at that point it's more concise to call the cons... | {
"AcceptedAnswerId": null,
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-10T14:15:54.920",
"Id": "4009",
"Score": "2",
"Tags": [
"c++"
],
"Title": "Which is the better way to return a new instance of a boost intrusive_ptr"
} | 4009 |
<p>I have two snippets that are going to be used in a walkthrough and I'd like some feedback on which one is more easily understandable</p>
<h1>1</h1>
<pre><code> property int hours, minutes, seconds
property real shift: 0.0
property bool night
function timeChanged() {
var date = new Date();
... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2014-07-01T23:02:10.703",
"Id": "98038",
"Score": "1",
"body": "What's the difference between `shift` and `clock.shift`? Where is `clock` defined? Finally, why can one version work without using the non-UTC versions. In short, I see far more d... | [
{
"body": "<p>I don't know QML and I can pretty well understand what is going on in sample #1. The use of the <code>? :</code> syntax could be confusing to someone new to the language. </p>\n\n<p>Also, the example to define <code>shift</code> is a good one and is very readable.</p>\n",
"comments": [
... | {
"AcceptedAnswerId": "4034",
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-10T14:45:51.457",
"Id": "4013",
"Score": "3",
"Tags": [
"beginner",
"datetime",
"comparative-review",
"qml"
],
"Title": "Two code snippets involving time-change"
} | 4013 |
<p>This working script directs incoming traffic to different locations depending on the value present in the session for referring URI. If there is a present value, it sends the user back to their original location. If not, then a random number is generated, checked against and used to direct the visitor to one of two ... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-11T07:15:49.400",
"Id": "6030",
"Score": "1",
"body": "is there any way \"$_SERVER['REQUEST_URI']\" wont be set?"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-14T03:13:56.663",
"Id": "6104",
"Scor... | [
{
"body": "<p>null coalesce does exist in php according to the google (per: <a href=\"https://stackoverflow.com/questions/1013493/coalesce-function-for-php\">https://stackoverflow.com/questions/1013493/coalesce-function-for-php</a> )</p>\n\n<pre><code>$s = $_SERVER[\"HTTPS\"] ?: \"\";\n$protocol = strleft(strto... | {
"AcceptedAnswerId": "4081",
"CommentCount": "3",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-10T16:33:53.020",
"Id": "4018",
"Score": "5",
"Tags": [
"php",
"url"
],
"Title": "Redirect incoming traffic based on referrer or random number"
} | 4018 |
<p>This is my implementation of quicksort (algorithm taken from Cormen book). This is an in place implementation. Please let us know issues with this or any ideas to make it better. It performs at logN.</p>
<pre><code>import java.util.ArrayList;
public class MyQuickSort {
/**
* @param args
*/
publi... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-17T16:17:30.847",
"Id": "33099",
"Score": "4",
"body": "This is O(n log n) not log n"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-12-12T15:54:45.250",
"Id": "61399",
"Score": "0",
"body": "It wi... | [
{
"body": "<p>If the algorithm was taken from a book, then chances are it will be as good as it could possibly be. So as long as you followed it to the letter, there really shouldn't be any issues in your implementation.</p>\n\n<p>There is however one thing I think could be improved upon, the interface to init... | {
"AcceptedAnswerId": "4023",
"CommentCount": "3",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-05T07:04:26.963",
"Id": "4022",
"Score": "26",
"Tags": [
"java",
"sorting",
"quick-sort"
],
"Title": "Java Implementation of Quick Sort"
} | 4022 |
<p>I have an application in which I am querying an XML web service continuously every 2 seconds in a thread. The returned XML is very big and I am retrieving lots of stuff from them using XPath and plugging the values into labels in the GUI, using delegates for thread safety.</p>
<p>But the way I've done this, it look... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-11T04:45:26.093",
"Id": "6028",
"Score": "4",
"body": "I haven't looked at your code thoroughly yet but I can tell you one thing, switching over to using LINQ to XML and the `XDocument` could cut the code down to a much more reasonable... | [
{
"body": "<p>One minor change you can do: </p>\n\n<pre><code>new Thread(new ParameterizedThreadStart(SetUnits)); \n</code></pre>\n\n<p>can be written as</p>\n\n<pre><code>new Thread(SetUnits);\n</code></pre>\n",
"comments": [],
"meta_data": {
"CommentCount": "0",
"ContentLicense": "CC BY-SA... | {
"AcceptedAnswerId": null,
"CommentCount": "4",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-11T04:17:52.230",
"Id": "4030",
"Score": "6",
"Tags": [
"c#",
"multithreading",
"winforms",
"xpath"
],
"Title": "Returning a large number of values from a thread"
} | 4030 |
<p>Below is what I have come up with for a router/dispatcher system for my personal framework I am working on. Can you please review and tell me any improvements that could be made? </p>
<p>The first part is an array of URI -> to class/method/id_number/page_number using regex. I have only included a partial list of r... | [] | [
{
"body": "<p>If you can limit your URI structure to using a delimiter - <code>/</code> comes to mind - you could avoid the regex.</p>\n\n<p>Here's a rough example (not with any configuration, but shows the concept):</p>\n\n<pre><code>$uri = 'forums/viewforum/1';\n$parts = explode('/', $uri);\n\n$controller = $... | {
"AcceptedAnswerId": "4053",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-11T04:52:31.457",
"Id": "4031",
"Score": "8",
"Tags": [
"php",
"mvc",
".htaccess",
"url-routing"
],
"Title": "MVC router class"
} | 4031 |
<p>I have got this working ok, I just think that it can be improved and reduce the amount of code. If anybody could help that would be brilliant. </p>
<pre><code>// Max Length Input
$('input[maxlength]').each(function() {
var maxCharLength = $(this).attr("maxlength");
if(maxCharLength < 0 || max... | [] | [
{
"body": "<p>There are a few things I would suggest:<br>\n1. Replace you numbers with constants and revert the condition to get rid of 'else':</p>\n\n<pre><code>var minTextLength = 0;\nvar maxTextLength = 5000;\nif (maxCharLength >= minTextLength || maxCharLength <= maxTextLength){\n //Your code...\... | {
"AcceptedAnswerId": "4037",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-11T14:38:36.857",
"Id": "4036",
"Score": "2",
"Tags": [
"javascript",
"jquery"
],
"Title": "Jquery Max character with character count for input and textarea"
} | 4036 |
<p>I'm spec'ing out a API for a task scheduler and I would be thankful for your thoughts. This is what I've got so far:</p>
<pre><code>public void Configure(Context ctx) {
// Single tasks
ctx.Run(() => Tasks.First()).Every.Midnight;
ctx.Run(() => Tasks.Second()).Every.Day(8,0));
// Multiple task... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-12T01:59:37.820",
"Id": "6043",
"Score": "1",
"body": "I wouldn't bother with all your various every overloads, if you want fluent style replace the everys with a single method `.AndReoccur(int numberOfTimes, Timespan interval)` also I... | [
{
"body": "<p>I'd like to be able to use a non-functional approach to running the api. Perhaps I'm a new developer and don't understand lambdas. Give me the option to go.</p>\n\n<pre><code>ctx.Run(//Task, or List of tasks, //How often, //Time to start, //Possible skip conditions - maybe skip weekends);\n\n//Ut... | {
"AcceptedAnswerId": null,
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-11T20:30:33.773",
"Id": "4038",
"Score": "4",
"Tags": [
"c#",
"api",
"scheduled-tasks"
],
"Title": "Task scheduler API"
} | 4038 |
<p>There's another exercise from Thinking in C++. <br />
This time it asks this:</p>
<blockquote>
<p>Write a program that uses two nested for loops and the
modulus operator (%) to detect and print prime numbers
(integral numbers that are not evenly divisible by any
other numbers except for themselves and 1)... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-11T23:23:25.770",
"Id": "6040",
"Score": "4",
"body": "`for(j = 2; j <= i; j++)` should be `for(j = 2; j * j < i; j++)`"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-11T23:23:46.780",
"Id": "64481",
... | [
{
"body": "<p><code>for(j = 2; j <= i; j++)</code> should be <code>for(j = 2; j * j <= i; j++)</code>. You only need to test for divisors less than the square root of i (if there is a divisor greater than sqrt(i), there is one less than sqrt(i)).</p>\n\n<p>The loop should therefore read:</p>\n\n<pre><code... | {
"AcceptedAnswerId": "4046",
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-11T23:22:09.000",
"Id": "4043",
"Score": "3",
"Tags": [
"c++",
"primes"
],
"Title": "Prime number finder"
} | 4043 |
<p>I'm an amateur programmer (self-taught). Anyway, this class provides a few methods to access Google's unofficial weather API. I'm having trouble on how to go about handling parsing errors in <code>parse_xml()</code> so any suggestions there would also be helpful.</p>
<pre><code><?php
class weather {
private ... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-13T17:00:39.890",
"Id": "6084",
"Score": "1",
"body": "amateur doesn't mean self-taught, it just means unpaid. There's plenty of us who went from amateur to professional self-taught :)"
},
{
"ContentLicense": "CC BY-SA 3.0",
... | [
{
"body": "<p>I'm a C# developer myself but I like this altogether, very clean and easy to read and understand even though it's not a language I've worked in.</p>\n\n<p>The layout of the class is clean with the members and methods nicely ordered and clear to their purpose.</p>\n\n<p>The only suggestions I have ... | {
"AcceptedAnswerId": "4161",
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-13T01:06:13.670",
"Id": "4063",
"Score": "7",
"Tags": [
"php",
"parsing",
"xml",
"api"
],
"Title": "Google Weather API wrapper"
} | 4063 |
<p>Essentially I'm taking a number of checkboxes off of my view, adding them to a <code>Dictionary<string, bool></code>, and writing out the <code>Keys</code> of any checkbox that is checked.</p>
<p>I'm guessing there must be a better way of doing this.</p>
<pre><code>// We need to build a list of what the user... | [] | [
{
"body": "<p>Without knowing if you need the dictionary for later use, you can absolutely make the 2nd step better (and remove the trailing \", \" that would get added) by using String.Join with a lambda (don't forget to add the System.Linq using if it is not there):</p>\n\n<pre><code>// Second step is to loop... | {
"AcceptedAnswerId": "4066",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-13T03:15:18.073",
"Id": "4064",
"Score": "9",
"Tags": [
"c#",
"asp.net",
"asp.net-mvc-3"
],
"Title": "Conditional string building"
} | 4064 |
<p>I'm looking for ways to improve this code (more readable, less redundant and maybe cleaner/faster).</p>
<p><em>The problem I needed to solve:</em></p>
<p>I was designated to implement a software that is going to validate an 96 column Excel file and if there is no error on it, create an XML file from it. In case th... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-13T20:38:55.170",
"Id": "6090",
"Score": "2",
"body": "Your presented code is fine, the parts that you say are a problem are in code you didn't present, I'm having a hard time understanding what extra code you made that you don't like ... | [
{
"body": "<p>If you find yourself repeating methods that often, then yes, you need to find a pattern that simplifies your work. That said, I think there would be a better solution than loading up collections of cells in columns. </p>\n\n<p>Take a look at this sample: <a href=\"http://social.msdn.microsoft.com... | {
"AcceptedAnswerId": "4074",
"CommentCount": "3",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-13T19:37:29.810",
"Id": "4073",
"Score": "14",
"Tags": [
"c#",
"validation",
"excel"
],
"Title": "Validating Excel file columns"
} | 4073 |
<p>Consider the following reproducible example: </p>
<pre><code># note that lh is a standard ts dataset that ships with R
lh
# fit an R model
ar.mle<-ar(lh,method="mle")
# now get the min AIC, this is the relevant line:
ar.mle$aic[ar.mle$aic==min(ar.mle$aic)]
</code></pre>
<p>This works fine and gives back th... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-13T14:14:10.117",
"Id": "62788",
"Score": "0",
"body": "Try which([blahblah], arr.ind=TRUE)"
}
] | [
{
"body": "<p>perhaps the function which.min() would do the trick?</p>\n\n<pre><code>which.min(ar.mle$aic)\n</code></pre>\n\n<p>it won't shorten your code all that much:</p>\n\n<pre><code>ar.mle$aic[which.min(ar.mle$aic)]\n</code></pre>\n",
"comments": [
{
"ContentLicense": "CC BY-SA 3.0",
... | {
"AcceptedAnswerId": "4084",
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-12T20:40:24.580",
"Id": "4083",
"Score": "5",
"Tags": [
"r"
],
"Title": "Is there a better way to get the index of a minimum?"
} | 4083 |
<p>How could I improve this code?</p>
<pre><code>var to = "http://forum.";
if (!RedirectPermanent("http://www.", to))
if (!RedirectPermanent("http://blog.", to))
if (!RedirectPermanent("http://forum.", to))
if (!RedirectPermanent("http://tracker.", to))
if (!RedirectPermanent("... | [] | [
{
"body": "<p>Went for this:</p>\n\n<pre><code>var arr = new[] { \"http://www.\", \"http://blog.\", \"http://forum.\", \"http://tracker.\", \"http://wiki.\", \"http://\" };\nvar to = \"http://forum.\";\n\nforeach (var from in arr)\n{\n if (RedirectPermanent(from, to))\n break;\n}\n</code></pre>\n",
... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 4.0",
"CreationDate": "2011-08-14T16:28:56.053",
"Id": "4087",
"Score": "5",
"Tags": [
"c#",
".net",
"url"
],
"Title": "Issue a redirect, trying several string replacements for the domain"
} | 4087 |
<p>Please review this connection pool. I tested it and it works but can we improve it from design or performance perspective?</p>
<pre><code>public class ConnectionPool {
private static final int MAX_SIZE=10;
private static final BlockingQueue<Connection> bq;
static{
bq= new ArrayBlockingQu... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-10-25T20:51:37.097",
"Id": "60212",
"Score": "0",
"body": "Like another poster, I highly suggest bonecp: http://jolbox.com/ You should have VERY good reasons to write your own db connection pool. Furthermore, Spring http://projects.spring... | [
{
"body": "<p>First of all I would suggest you to look at this library <a href=\"http://commons.apache.org/dbcp/\" rel=\"nofollow\">DBCP</a></p>\n\n<p>As for the code you have showed - It will be better to avoid using of static state and behavior in this class. The way to control number of instances of this cla... | {
"AcceptedAnswerId": null,
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-14T17:01:33.583",
"Id": "4090",
"Score": "7",
"Tags": [
"java",
"performance",
"jdbc"
],
"Title": "Connection pool in Java"
} | 4090 |
<p>I'm working on a Cocoa project that uses OpenGL. I'm trying to keep things easily cross-platformable for later (which is the primary reason for my GL singleton; I hope to implement Linux versions of the <code>IRGL</code> methods that currently use <code>NSOpenGL...</code>). When doing Cocoa & OpenGL stuff, thoug... | [] | [
{
"body": "<ol>\n<li><p><code>CVDisplayLink</code> is asynchronous and in general <code>AppKit</code> is not threadsafe. </p></li>\n<li><p>Using <code>setNeedsDisplay</code>, which gets you a refresh at some point in the future, in the draw callback misses the point of display links, which is drawing something ... | {
"AcceptedAnswerId": "6520",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-14T19:36:14.263",
"Id": "4093",
"Score": "5",
"Tags": [
"objective-c",
"opengl",
"cocoa"
],
"Title": "OpenGL game edit view"
} | 4093 |
<p>Wondering if this is fully in C++, and if not can someone help me tell the differences. This was submitted by me last semester, and received a good grade. I'm currently trying to ensure I can tell C++ and C apart.</p>
<pre><code>#include <fstream>
#include <stack>
#include <stdlib.h> //Not necces... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-14T23:41:31.157",
"Id": "6113",
"Score": "1",
"body": "For C++ code, you're using an awful lot of C functions and paradigms. You usually could tell by the excessive use of `printf()`, pointers, for loops, lack of classes and so on. G... | [
{
"body": "<p>using printf is C like.</p>\n\n<p>using a function like readGraph that does a lot of magic as well as printing things is C like.</p>\n\n<p>For this kind of thing to be C++ like, then you'd consider how to write these functions generically. Giving your graph an STL like feel. </p>\n\n<p>This cod... | {
"AcceptedAnswerId": "4097",
"CommentCount": "3",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-14T23:24:15.043",
"Id": "4095",
"Score": "5",
"Tags": [
"c++",
"c",
"algorithm"
],
"Title": "depthFirstTraverse fully in C++?"
} | 4095 |
<p>I'm primarily a C++/Java programmer, but I've recently started using Python at work and decided to write a Lottery Simulator at home. I wrote it to test out different combinations of lottery numbers against a set of winning numbers to calculate how much I would have won (I also run the lotto pool at work). The onl... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-15T06:20:05.973",
"Id": "6118",
"Score": "1",
"body": "Did not know you could leak in python (but then I am not that good at python). Would be interested in the bug you did fix."
},
{
"ContentLicense": "CC BY-SA 3.0",
"Crea... | [
{
"body": "<pre><code>#!/usr/bin/python\n\nimport random, ConfigParser, ast, copy\nfrom optparse import OptionParser\n\n# Global variables.\ng_config = {} # Stores GameType config options.\ng_prizes = {} # Stores prizes from config file.\ng_prize_amounts = {} # Stores prize am... | {
"AcceptedAnswerId": "4125",
"CommentCount": "12",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-15T03:07:49.667",
"Id": "4098",
"Score": "3",
"Tags": [
"python",
"memory-management",
"simulation"
],
"Title": "Memory leaks in lottery simulator"
} | 4098 |
<p>I'm working on a small winforms app, the goal of which is to capture data and write to xml. I'm still a child where programming is concerned so could you guys please take a look and suggest changes as I'm pretty sure that this is not a good approach.
I'm using a masked-textbox for the invoice number which should onl... | [] | [
{
"body": "<ol>\n<li>Don't use String.IsNullOrEmpty to validate required values.</li>\n<li><p>You can create a method</p>\n\n<pre><code>private bool IsEmpty(string value, string errorMessage, Control controlToValidate)\n{\n if ((value ?? string.Empty).Trim().Length == 0)\n {\n MessageBox.Show(error... | {
"AcceptedAnswerId": "4112",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-15T08:15:54.273",
"Id": "4101",
"Score": "4",
"Tags": [
"c#",
"winforms"
],
"Title": "Is this validation sufficient to ensure that the vals are not empty?"
} | 4101 |
<p>Not long ago I tried to get a game developer job (casual games). There was a task to write a simple game like this <a href="http://chainrxn.zwigglers.com/" rel="nofollow">one</a>. I was supposed to use an existing game engine (given in the task) and only create a game logic. After writing the solution I got the answ... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-15T12:28:27.730",
"Id": "6129",
"Score": "0",
"body": "I'm a C# dev and not familiar with C++ standard practices, but to me I'm thinking the 'style' is probably not the issue as it all appears clean and consistent. They probably had mo... | [
{
"body": "<p>Two underscores reserved for compilers, so it is bad header guard: <code>#ifndef __BUBBLEGAME_H__</code></p>\n\n<p>Using <code>using namespace ...</code> directive in header is not recommended, you may use it in source file.</p>\n\n<p>Very much static variables, but i cant see, that you really nee... | {
"AcceptedAnswerId": "4114",
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-15T08:54:28.393",
"Id": "4102",
"Score": "7",
"Tags": [
"c++",
"game"
],
"Title": "Simple chain reaction game"
} | 4102 |
<p>How to write and design a simple (but still proper and secure) login class for PHP?</p>
<p>Currently I'm checking whether there is a login request (user entered data into login form) or the session is already existent and contains <code>$_SESSION['authenticated'] == TRUE;</code>. If both checks failed the user is n... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-15T09:54:31.457",
"Id": "6130",
"Score": "0",
"body": "Have you tried using oAuth?"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-15T10:06:49.790",
"Id": "6131",
"Score": "1",
"body": "Tip: Don... | [
{
"body": "<p>At the first look, no big hole shines through. Some points that can be considered, on the order of appearance:</p>\n\n<ol>\n<li><p>Instead of directly using the <code>$_POST</code>, you can take these as parameters, thus making it look like more of an API. Example usage: when you want to directly ... | {
"AcceptedAnswerId": null,
"CommentCount": "7",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-15T09:51:28.350",
"Id": "4109",
"Score": "5",
"Tags": [
"php",
"security"
],
"Title": "Writing a proper (and simple) auth/login class in PHP"
} | 4109 |
<p>I have a form and need to be able to highlight/decorate fields as they gain focus (change their background color, border). I'm also decorating their corresponding 'labels'.</p>
<p>All works fine where it needs to: FF, Safari, IE 7, 8 and 9</p>
<p><strong>Problem 1</strong> cropped up with IE7 and changing backgrou... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-16T13:52:48.167",
"Id": "6192",
"Score": "1",
"body": "The \"cleanest\" and probably easiest way would be to use the CSS `:focus` selector: `input:focus {background-color: red}`, which is widely supported, except IE7 and earlier."
},... | [
{
"body": "<p>I'm not quite sure if this will do quite what you want, but here is my rehashing of the code:</p>\n\n<pre><code>$(document).ready(function() {\n $('table[id^=\"block\"] input, select').bind(\"focus blur\", function() {\n var $this = $(this);\n $this.closest(\"td\").prev().toggleCl... | {
"AcceptedAnswerId": null,
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-15T13:32:58.787",
"Id": "4115",
"Score": "2",
"Tags": [
"javascript",
"jquery-ui"
],
"Title": "jQuery and cross browser input fields focus handling"
} | 4115 |
<p>I'm an iPhone Developer mainly, and I'm very new to web development - especially jQuery.</p>
<p>My task: I have three divs, which each contain a short biography about a certain person. (I have three people: liz, chris and michael). Those divs are hidden at the start of my code, because they all belong to the class ... | [] | [
{
"body": "<p>Look at <a href=\"http://docs.jquery.com/UI/Accordion\" rel=\"nofollow\">JQuery Accordion</a></p>\n\n<p>Or if you don't want to use the Accordion then here is a variant:</p>\n\n<pre><code>function imgClickHandler(divBiographyId, divTooltipId){ \n var divBiographySelector = \"#\" + divBiographyId;... | {
"AcceptedAnswerId": "4124",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-15T15:59:08.893",
"Id": "4123",
"Score": "2",
"Tags": [
"javascript",
"beginner",
"jquery"
],
"Title": "Three biographical profiles with photos"
} | 4123 |
<p>I am writing a PHP application that works with a MySQL database. As I am fairly new to PHP I am looking for ways to improve my code. Below are two functions, <code>create_ct_query</code> and <code>create_ct_query2</code>. <code>create_ct_query2</code> is the result of my refactoring <code>create_ct_query</code>. ... | [] | [
{
"body": "<p>Stylistically, it looks okay to me. But technically, you will want to embrace all your table and column names in backtick quotes, or else your queries will often fail if they are coming from user input. E.g. table and column names need backticks if they contain dashes, and also for a lot of other ... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-15T23:02:06.770",
"Id": "4129",
"Score": "1",
"Tags": [
"php",
"sql",
"mysql"
],
"Title": "Creating SQL queries for a MySQL database"
} | 4129 |
<p>I wrote the following code to give an HTML element max width/height in its parent container.
I think I address the box model issues but I am wondering if anyone can see issues and or shortcomings.</p>
<p>Are there other solutions out there that accomplish this? I want to make sure I didn't re-invent the wheel.</p>
... | [] | [
{
"body": "<p>What is</p>\n\n<pre><code>parseInt((control.outerHeight(true)) - control.height());\n</code></pre>\n\n<p>supposed to achieve?</p>\n\n<p>The result of <code>-</code> is always going to be a number, so what this does is convert the number to a string and back to a number. This can only lose precisi... | {
"AcceptedAnswerId": "4146",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-16T01:48:26.117",
"Id": "4130",
"Score": "2",
"Tags": [
"javascript",
"jquery",
"html",
"css"
],
"Title": "Javascript to compute max width and height for a nested HTML element"... | 4130 |
<p>As an exercise in learning bash, I wrote this script designed to automate the process of populating a drive with uncompressed .aiff files copied directly from a CD. It saves me having to do a bunch of separate commands to create subdirectories, check before clobbering, etc.</p>
<p>It stores them in this fashion:</p... | [] | [
{
"body": "<p>Some comments:</p>\n\n<ol>\n<li>You script does not work if you have spaces in the filenames, but I guess this cannot happen.</li>\n<li>You do not need to restore IFS as the script will be run in a subprocess, and will not impact parent environment.</li>\n<li>You could run <code>ls -1 /Volumes | g... | {
"AcceptedAnswerId": "4142",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-16T06:09:05.410",
"Id": "4131",
"Score": "3",
"Tags": [
"bash"
],
"Title": "Is this shell script for copying files sensible, readable?"
} | 4131 |
<p>I know that nested namespaces are used in C++ very rarely. But I think it's a nice solution to exclude types from global scope and sometimes it helps to write programs faster when we use things like "IntelliSense" or search for documentation (maybe it's similar for C# developers).</p>
<p>I've tried to organize my s... | [] | [
{
"body": "<p>I don't think there is anything inherently wrong with nested namespaces.</p>\n\n<p>I also disagree that it is rare. It is just not exposed directly.</p>\n\n<pre><code>std::tr1\nCompanyName::ProductName\nCompanyName::ProductName::Details\n</code></pre>\n\n<p>But like all features they can be abused... | {
"AcceptedAnswerId": "4133",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-16T06:17:49.513",
"Id": "4132",
"Score": "9",
"Tags": [
"c++",
"library",
"windows",
"graphics",
"namespaces"
],
"Title": "Use of nested namespaces in 3D graphics engine li... | 4132 |
<p>I have a <code>Sprite</code> class and an <code>AnimatedSprite</code> subclass, and I'd like to decouple these them and maintain substitutability, according to the Liskov Substitution Principle. I find that when dealing with pointers to Sprites I always use <code>GetFrameWidth</code> and <code>GetFrameHeight</code> ... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-16T09:30:46.013",
"Id": "6183",
"Score": "2",
"body": "I'd prioritize Single Responsibility Principle and Interface Segregation Principle before LSP in this case."
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "201... | [
{
"body": "<p><strong>Solution 1</strong>:</p>\n\n<ul>\n<li>have a texture/img class, with GetImgWidth/GetImgHeight. these return\nthe dimensions of the physical texture, so in case of a sprite sheet,\nthe size of the whole sheet</li>\n<li>make sprite extend image. getwidth/getheight return the size of the\nspr... | {
"AcceptedAnswerId": "4150",
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-16T07:16:35.687",
"Id": "4134",
"Score": "4",
"Tags": [
"c++",
"object-oriented",
"animation"
],
"Title": "Sprite and AnimatedSprite"
} | 4134 |
<p>I have a basic cache set up. Whenever a user requests a bitmap, it fetches or loads it from disk if it isn't already loaded, significantly reducing load times.</p>
<p>Currently, the design explicitly tells the user that they are responsible for freeing the individual bitmaps when they are done. That's fine, but th... | [] | [
{
"body": "<p>You initialization is lazy, so there is no need for an explicit <code>Initialize</code>. A <code>Release</code> Method would be one option to clean the memory after usage and setting pool to NULL again. I guess your code is intended for single thread only so there are no real problems. Another opt... | {
"AcceptedAnswerId": "4137",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-16T07:29:15.113",
"Id": "4135",
"Score": "4",
"Tags": [
"c++",
"classes",
"cache",
"static"
],
"Title": "Static class member destruction in C++"
} | 4135 |
<p>I needed a mutable priority queue (the priorities can be changed) for my currect project, and started by simply wrapping a class around a <code>std::vector</code> and make/push/pop_heap. However, it is not nearly fast enough, profiling shows ~70% of processing time is spent in the queue. I need some input on how to ... | [] | [
{
"body": "<p>The standard already has a <a href=\"http://www.sgi.com/tech/stl/priority_queue.html\" rel=\"nofollow\">priority queue</a>.</p>\n\n<pre><code>std::priority_queue\n</code></pre>\n\n<p>Internally it uses std::vector<> (by default) but the elements in the vector are organized into a binary tree st... | {
"AcceptedAnswerId": "4152",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-16T17:10:02.807",
"Id": "4148",
"Score": "2",
"Tags": [
"c++",
"performance"
],
"Title": "Slow mutable priority queue"
} | 4148 |
<p>We have some developers in house that believe it is best practice to use exception handing as flow control, as well as, thinking that catching and re-throwing exceptions is effective error handling. </p>
<p>In an effort to educated, I attempted to come up with a simple sample to demonstrate to them the negative ef... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-16T21:41:38.870",
"Id": "6204",
"Score": "0",
"body": "You should also include a *proper* `try-catch` block that actually does something with a **specific** exception."
}
] | [
{
"body": "<p>The while loop you are using is impacting performance. Sleep that check a bit beforehand to give your scenarios some time to think. </p>\n\n<pre><code>do { Thread.Sleep(3000); } while (!tasks.All(t => t.IsCompleted));\n</code></pre>\n\n<p>You may want to suggest some best practices reading to... | {
"AcceptedAnswerId": "4158",
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-16T20:50:25.210",
"Id": "4155",
"Score": "2",
"Tags": [
"c#",
"exception-handling"
],
"Title": "Is This a Sufficient Demonstration of the Effects of Exception Handling"
} | 4155 |
<p>This is the code that I'm using. I'm working on LINQ TO SQL and I'm using this model in my program:</p>
<p><a href="https://stackoverflow.com/questions/7072819/how-can-i-model-this-class-in-a-database/7072841#7072841">How can I model this class in a database</a></p>
<p>The person must have to enter the level, for ... | [] | [
{
"body": "<p>A few comments: </p>\n\n<ol>\n<li>You should move this code from the <code>AddButton_Click</code> to some business logic class. </li>\n<li>Maybe you should add some validation of <code>LevelTextBox.Text</code>, before using its value. </li>\n<li><p>You should split this code into different met... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-17T00:00:41.043",
"Id": "4160",
"Score": "3",
"Tags": [
"c#",
"linq",
"wpf"
],
"Title": "Determining if a level exists"
} | 4160 |
<p>I know this is a pretty simple class for loading a config file into an object, and then accessing it's properties. I think config options should be lightweight and a class like this seems to adds a lot of overhead IMO but I really like being able to access the config properties with $config->propertyName. So do yo... | [] | [
{
"body": "<blockquote>\n <p>Mainly performance improvements?</p>\n</blockquote>\n\n<p>There doesn't seem to be much oppurtunity for performance improvements. This class basically consists of (1) loading and parsing the configuration and (2) accessing data. For loading, parse_ini_file and json_decode are built... | {
"AcceptedAnswerId": "4169",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-17T02:51:06.613",
"Id": "4162",
"Score": "4",
"Tags": [
"php"
],
"Title": "PHP Config file loader class"
} | 4162 |
<p>I feel a need to rewrite a rather large Python class that "does its job" but it looks somewhat terrible since it was pasted together while learning Python and the platform Google App Engine. It works so that there are no bugs towards user but readability and structure are poor. This is also the largest class of the ... | [] | [
{
"body": "<p>My first impulse would be to take care of repeating </p>\n\n<pre><code>try:\n ...\nexcept Exceptiopn:\n pass\n</code></pre>\n\n<p>structures that take a lot of space e.g.</p>\n\n<pre><code>def value_of(self, param):\n try:\n return self.request.POST.get(param)\n except Exception:\n ... | {
"AcceptedAnswerId": "4165",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-17T04:25:04.247",
"Id": "4164",
"Score": "6",
"Tags": [
"python",
"google-app-engine"
],
"Title": "Add lister class"
} | 4164 |
<p>I need a version of the mvc RouteValueDictionary that I can chain Add calls to, ie:</p>
<pre><code>new RouteValueDictionaryExtended()
.AddValue("controller", "Home")
.AddValue("action", "Index")
.AddValue("id", 3)
</code></pre>
<p>Is there a more description name I can use for this instead ... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-18T14:12:34.320",
"Id": "6248",
"Score": "0",
"body": "Two questions:\n1. Maybe I miss something, but how does 'new RouteValues().WithValue(\"controller\", \"Home\").WithValue(\"action\", \"Index\");' returns 'RouteValues'? The return... | [
{
"body": "<p>If you need this class only to be able to chain 'Add' calls, maybe it would be better to just create an extension method:</p>\n\n<pre><code>public static class RouteValueDictionaryExtensions\n{\n public static RouteValueDictionary AddValue(this RouteValueDictionary routeValueDictionary, string ... | {
"AcceptedAnswerId": "4188",
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-17T14:58:10.867",
"Id": "4170",
"Score": "5",
"Tags": [
"c#"
],
"Title": "Is there a better name for this class?"
} | 4170 |
<p>Would a join be quicker here? I tested a join and it's almost the same time! Basically its a ray casting mechanism that finds all properties in a square on GMaps, and then there's a javascript function that finds the point in polygon. So at any time, the records fetched could be in the tens of thousands. </p>
<p>Th... | [] | [
{
"body": "<p>Personally I would go with a join even if they are currently the same speed.</p>\n\n<p>This way your code automatically gets upgraded when the DB gets upgraded (ie when a new index is added to the DB or they decide to partition the DB by latitude/longitude). In this situation if you are using a Jo... | {
"AcceptedAnswerId": "4177",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-17T15:31:12.483",
"Id": "4173",
"Score": "0",
"Tags": [
"php",
"mysql"
],
"Title": "Make two MySQL statements & loop quicker"
} | 4173 |
<p>I'm looking for a better way to create objects based off of a value of a string. </p>
<p>Consider the following:</p>
<p>Input file:</p>
<pre class="lang-none prettyprint-override"><code>//each Vehicle type has a list of different options
//whose length varies, I provided a simple case
van*2006*dodge*grand caravan... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-17T16:09:08.147",
"Id": "6217",
"Score": "0",
"body": "What's a `Vechicle`?"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2014-04-21T03:07:06.800",
"Id": "83706",
"Score": "0",
"body": "have you consi... | [
{
"body": "<p>You could use the abstract factory pattern and pass the vehicle type to the create method of the factory. That would definitely be cleaner and allow for easier exstensibility when someone wants to create a new type of vehicle (crossover, suv, etc).</p>\n\n<p>An example can be found <a href=\"http:... | {
"AcceptedAnswerId": "4176",
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-17T15:34:15.730",
"Id": "4174",
"Score": "7",
"Tags": [
"c#"
],
"Title": "Better way to create objects from strings"
} | 4174 |
<p>My page contains: GridView1, GridView2, Button1, Button2, DropDownList1 I bind Gridviews to the table selected in dropdown like this:</p>
<pre><code>Dim results as DataTable
Select Case ddl1.SelectedValue
Case 0
Dim cl as ClassZero = new ClassZero()
results = cl.GetClassZeroNames()
Case 1
... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-17T19:55:25.610",
"Id": "6224",
"Score": "0",
"body": "I don't think there is much different you can do here."
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-17T23:16:42.207",
"Id": "6232",
"Score":... | [
{
"body": "<p>I like to use Dictionaries for stuff like this. So in your first instance you could create a <code>Dictionary<int,DataTable></code> and use that instead of the switch statement.</p>\n",
"comments": [],
"meta_data": {
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",... | {
"AcceptedAnswerId": "4184",
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-17T19:00:52.993",
"Id": "4178",
"Score": "1",
"Tags": [
"asp.net",
"vb.net"
],
"Title": "Case statement in multiple methods"
} | 4178 |
<p>The following code has a lot of conditionals. I am trying to write
it in a functional programming way.</p>
<pre><code>val basePrice = {
var b = 0.0
if (runtime > 120)
b += 1.5
if ((day == Sat) || (day == Sun))
b += 1.5
if (!isParquet)
b += 2
if (is3D)
b += 3
b
}
</code></pre>
<p>I t... | [] | [
{
"body": "<p>Maybe a little bit more readable:</p>\n\n<pre><code>val basePrice = List((runtime > 120, 1.5), \n (day == Sat || day == Sun, 1.5),\n (!isParquet, 2.0),\n (is3D, 3.0)).collect{case (true, b) => b}.sum\n</code></pre>\n",
"comments": [
{
"ContentLicense": "CC... | {
"AcceptedAnswerId": "4186",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-17T21:18:22.837",
"Id": "4181",
"Score": "2",
"Tags": [
"functional-programming",
"comparative-review",
"scala"
],
"Title": "Calculating a base price with surcharge conditions"
} | 4181 |
<p>Currently I have this code:</p>
<pre><code>float tileX = (float)rectangle.X / (float)newTileSize;
float tileY = (float)rectangle.Y / (float)newTileSize;
int xOrigin = (int)Math.Round(tileX) * newTileSize;
int yOrigin = (int)Math.Round(tileY) * newTileSize;
// Same idea for the next 4 lines
float tileWidth = (flo... | [] | [
{
"body": "<p>So you're looking for</p>\n\n<pre><code>int result = (int)Math.round((float)n/(float)d) * d;\n</code></pre>\n\n<p>(assuming you are only interested in the intermediate values for this particular calculation)</p>\n\n<p>Here's a test case with an alternative:</p>\n\n<pre><code>import junit.framework... | {
"AcceptedAnswerId": "4195",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-18T17:03:32.393",
"Id": "4192",
"Score": "2",
"Tags": [
"c#",
"casting"
],
"Title": "A way to do this without a lot of variables and casting?"
} | 4192 |
<p>I'm writing a perl script which reads logs from a file and looks for matches from another file before sending the matches off by email. The problem is, I'm dealing with files which could be changed (a log written to them by syslog) at any time. I discovered that files can be locked in perl, however I'm not sure if i... | [] | [
{
"body": "<p>Locking is advisory which mean unless syslog is also doing flock() before write (which is doubtful) your locking scheme will have no effect. Some operating systems like AIX do have manditory/enforced locking mechanisms but I don't believe they're standard at the filesystem level across any UNIX sy... | {
"AcceptedAnswerId": "4198",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-18T19:08:41.127",
"Id": "4197",
"Score": "1",
"Tags": [
"perl",
"security",
"locking"
],
"Title": "Security concerns with locking files"
} | 4197 |
<p>Ruby code below seems to be not so dry. Can you please help me reduce the code.</p>
<pre><code>self.value = case self.type
when 'fast'
Increment.first.max_hours * Incrementor.first.fast_completion_day
when 'super_fast'
Increment.first.max_hours * Incrementor.first.super_fast_completion_day
when 'ludicrous'
I... | [] | [
{
"body": "<p>You can try:</p>\n\n<pre><code>day = case self.type\n when 'fast' then :fast_completion_day\n when 'super_fast' then :super_fast_completion_day\n when 'ludicrous' then :ludicrous_completion_day\n else :budget_completion_day\nend\n\nself.value = Increment.first.max_hour... | {
"AcceptedAnswerId": "4207",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-18T20:25:49.203",
"Id": "4199",
"Score": "2",
"Tags": [
"ruby",
"ruby-on-rails"
],
"Title": "ruby code case when not so dry"
} | 4199 |
<p>I wrote the following function to determine whether a string ends with one of a few given options.</p>
<p>I'm sure it can be written more elegantly, probably avoiding the loop.</p>
<pre><code>bool EndsWithOneOf(string value, IEnumerable<string> suffixes)
{
foreach(var suffix in suffixes)
{
i... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-18T17:48:30.240",
"Id": "6253",
"Score": "0",
"body": "My previous answer was given multiple times, but this works too. Using a method group:\n\n bool EndsWithOneOf(string value, IEnumerable<string> suffixes)\n {\n ... | [
{
"body": "<p>You can LINQify it to improve readability:</p>\n\n<pre><code>bool endsWithOneOf = suffixes.Any(x => value.EndsWith(x));\n</code></pre>\n\n<p>Note that this doesn't \"avoid the loop\", since <code>Any()</code> will iterate through the suffixes (stopping when it hits a match.) But that's ok, sinc... | {
"AcceptedAnswerId": null,
"CommentCount": "5",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-18T17:43:01.227",
"Id": "4200",
"Score": "5",
"Tags": [
"c#",
"strings"
],
"Title": "Determining whether a string ends with one of a few given options"
} | 4200 |
<p>There are some things I am not sure of from a professional standpoint about the "correct" way to write C++ code. I have been looking through source code produced by various opensource projects, as well as other code posted here and on Stack Overflow. </p>
<p>So let's just leave it at this. Let's say I am interviewi... | [] | [
{
"body": "<p>Looks good:</p>\n\n<p>Here typename is not required:</p>\n\n<pre><code>typedef typename std::vector<int>::iterator v_it;\n</code></pre>\n\n<p>So this:</p>\n\n<pre><code>typedef std::vector<int>::iterator v_it;\n</code></pre>\n\n<p>Here I would not specify the size:</p>\n\n<pre><code>st... | {
"AcceptedAnswerId": "4222",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-19T07:54:45.580",
"Id": "4209",
"Score": "6",
"Tags": [
"c++",
"interview-questions",
"mathematics",
"graph"
],
"Title": "Print pair representing objects from sequence of nonne... | 4209 |
<p>I am using code like this everywhere. How can I reduce such this code so that my Ruby code looks a lot cleaner?</p>
<pre><code>Fabricate(:tl, :when =>Date.yesterday.to_s,:work => 266,:type => "fast" )
Fabricate(:tl, :when =>Date.yesterday.to_s,:work => 100,:type => "super_fast" )
Fabricate(:tl, :w... | [] | [
{
"body": "<p>I found that I can use hash({}).each method to dry up the code. It is :</p>\n\n<pre><code>{\"fast\"=>266, \"super_fast\" => 100, \"ludicrous\" => 50, \"budget\" => 900}.each{ |key, value|\n Fabricate(:tl, :when =>Date.yesterday.to_s,:work => value,:type => key )\n}\n</code></... | {
"AcceptedAnswerId": "4213",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-19T07:57:25.817",
"Id": "4210",
"Score": "5",
"Tags": [
"ruby",
"datetime",
"ruby-on-rails"
],
"Title": "Fabricate with work dates"
} | 4210 |
<p>Here is my code, I use it to verify whether an input path is correct or not. I assume that the input path is encoded in <code>UTF8</code>. Is there any way to make it better?</p>
<pre><code>#define MAX_PATH_LEN MAX_PATH
#define IN
#define OUT
typedef int Bool_T;
static
int GetCharsNumInPath( IN const char * path... | [
{
"ContentLicense": "CC BY-SA 4.0",
"CreationDate": "2021-03-16T02:05:56.370",
"Id": "507983",
"Score": "0",
"body": "You need to check for invalid device names too, such as AUX, COM1, LPT1"
}
] | [
{
"body": "<p>Personally if the preconditions to the function are not met then I like to return quickly and this not add to the nesting:</p>\n\n<pre><code>Bool_T PathValid( IN const char * path )\n{\n if( path != NULL )\n {\n // wrong argument\n return false;\n }\n\n if( GetCharsNumIn... | {
"AcceptedAnswerId": "4239",
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-19T13:53:41.580",
"Id": "4214",
"Score": "2",
"Tags": [
"c"
],
"Title": "Function to check whether a path is valid or not needs your critique"
} | 4214 |
<p>I'm investigating some feature for the ScalaDoc tool, which would allow library writers to link to documentation created by third party tools like JavaDoc.</p>
<p>My idea is to have some (XML) configuration which specifies which package prefixes belong to which vendor and how to create a valid URL from the the clas... | [] | [
{
"body": "<blockquote>\n <p>Any idea on how to improve lookUp?</p>\n</blockquote>\n\n<p><a href=\"https://gist.github.com/brikis98/5842766\" rel=\"nofollow\">Use a concurrent cache</a>. The point is, regardless how good or bad you lookup function is, nothing beats a cache-hit performance wise..</p>\n\n<block... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-19T13:57:38.400",
"Id": "4215",
"Score": "5",
"Tags": [
"strings",
"regex",
"scala",
"xml",
"lookup"
],
"Title": "Processing XML configuration which stores regular expression... | 4215 |
<p>I have a set of classes that abstract away calls to a set of web services. I have 6 classes in this particular group, 4 of which contain a simple function that, while small, is still duplicated. What happens is that if some exception or business rule violation happens in the service, they package it up as a fault ob... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-19T18:18:54.013",
"Id": "6345",
"Score": "0",
"body": "Does AlphaResponse, BravoResponse etc all inherit the same interface or class or abstract class?"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-19T18:... | [
{
"body": "<p>If AlphaResponse, BravoResponse etc all inherit an interface \"IResponse\" then you could do this.</p>\n\n<pre><code>internal static class FaultThrowerExtension\n{\n internal static void ThrowIfContainsFault<T>(this T response) where T : IResponse\n {\n if (response.Fault != nul... | {
"AcceptedAnswerId": "4257",
"CommentCount": "8",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-19T16:03:45.810",
"Id": "4216",
"Score": "2",
"Tags": [
"c#"
],
"Title": "Duplicated code in web service consumption"
} | 4216 |
<p>I was applying for a position, and they asked me to complete a coding problem for them. I did so and submitted it, but I later found out I was rejected from the position. Anyways, I have an eclectic programming background so I'm not sure if my code is grossly wrong or if I just didn't have the best solution out th... | [
{
"ContentLicense": "CC BY-SA 2.5",
"CreationDate": "2010-03-14T16:16:34.690",
"Id": "6278",
"Score": "52",
"body": "As a starting advice: \"commnent less\""
},
{
"ContentLicense": "CC BY-SA 2.5",
"CreationDate": "2010-03-15T01:49:03.653",
"Id": "6279",
"Score": "3",
"bod... | [
{
"body": "<p>It's been a long while since I used C++, so I couldn't write it write now, but I know there were algorithms in the STL that would make very short work of this. Implementing your own binary search is probably a sign to the prospective employer that you're something of a beginner. The fact that yo... | {
"AcceptedAnswerId": null,
"CommentCount": "6",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2010-03-14T01:17:52.977",
"Id": "4223",
"Score": "149",
"Tags": [
"c",
"array",
"interview-questions",
"search",
"binary-search"
],
"Title": "Searching an element in a sorted array"
... | 4223 |
<p>I have been working on a script for almost a month now, which takes in an XML file and uses it to either download or upload files from an ftp site. It also copies the files to a history directory for historical purposes.</p>
<p>This thing has to be flawless so any suggestions to make the script more stable, reliabl... | [] | [
{
"body": "<ol>\n<li><p>Please indent consistently. It makes it easier for others to read your code, and it makes it easier for <em>you</em> to read your code, preventing mistakes. Seeing three close braces in a row lined up along the left side of the terminal makes me sad.</p></li>\n<li><p>Don't use Switch. Us... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-19T22:35:40.547",
"Id": "4244",
"Score": "4",
"Tags": [
"xml",
"file-system",
"perl"
],
"Title": "Script for performing tasks with acquired XML files"
} | 4244 |
<p>I'd appreciate any and all criticism about this code. I didn't have a particular use case in mind when I wrote it; I just wanted to try implementing linked lists. My comments are not very useful, I think. I'm not a very experienced programmer, so there's probably plenty of things I could improve or fix here.</p>
... | [] | [
{
"body": "<pre><code>// Initialize a new, empty LList, in dynamically allocated memory.\nLList *llist_new(LList **self) {\n</code></pre>\n\n<p>Its odd to return the new list as a return value and by reference.</p>\n\n<pre><code> *self = malloc(sizeof (LList));\n if (self == NULL) { return NULL; }\n</code... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-20T00:15:09.747",
"Id": "4246",
"Score": "3",
"Tags": [
"c",
"linked-list"
],
"Title": "Criticize my Linked List implementation"
} | 4246 |
<p>I've been trying to learn different patterns of programming, and the "Chain of Responsibility" sort of eludes me. I've been told that my specific code snippet would be a good candidate for chain of responsibility, and I'm wondering if someone could show me how to get there?</p>
<pre><code>Public Overrides Sub OnAc... | [] | [
{
"body": "<p>This method is not a good candidate for the Chain of Responsibility pattern, but it definitely can be implemented using it (just for the educational purpose):</p>\n\n<pre><code> public override void OnActionExecuting(ActionExecutingContext filterContext)\n {\n //Init\n var refe... | {
"AcceptedAnswerId": "4261",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-20T02:00:40.930",
"Id": "4250",
"Score": "1",
"Tags": [
"c#",
"design-patterns",
"asp.net",
"vb.net"
],
"Title": "Refactor IF into Chain of Responsibility Pattern"
} | 4250 |
<p>I have a set of Class methods which is :</p>
<pre><code>class << self
def increment_value
self.first.increment_value
end
def max_work_hours_per_day
self.first.max_work_hours_per_day
end
def fast_completion_day
self.first.fast_completion_day
end
def super_fast_completion_day
self.first.super_fast_co... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-20T04:38:31.220",
"Id": "6368",
"Score": "1",
"body": "[This](http://ruby-doc.org/stdlib/libdoc/delegate/rdoc/index.html) and [this](http://ruby-doc.org/stdlib/libdoc/forwardable/rdoc/index.html) look like reasonabl nice ways to handle... | [
{
"body": "<p>Ruby already has a module which allows you to forward specific methods to a given object in its standard library: <a href=\"http://ruby-doc.org/stdlib/libdoc/forwardable/rdoc/index.html\">the Forwardable module</a>.</p>\n\n<p>Using it you can write code like this:</p>\n\n<pre><code>class << ... | {
"AcceptedAnswerId": "4327",
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-20T03:16:05.957",
"Id": "4253",
"Score": "7",
"Tags": [
"ruby",
"ruby-on-rails"
],
"Title": "using method missing to reduce the code in ruby"
} | 4253 |
<p>This function checks if a file name is valid or not. I think it's not good enough for integration (not optimized, maybe I forgot about some checks), so need your comments.</p>
<pre><code>static
int ReservedName( const char * name )
{
char * reservedFileNames[] = { "CON", "PRN", "AUX", "NUL",
... | [] | [
{
"body": "<p>Every one has their own side of the holy war about where the '{' should be.</p>\n\n<p>But one thing everybody agrees upon is that you must be consistent.<br>\nIn your code you align the open and close (which I think is perfect) apart from when you use else</p>\n\n<pre><code> }else{\n</code></pre... | {
"AcceptedAnswerId": "4262",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-20T11:01:42.053",
"Id": "4258",
"Score": "2",
"Tags": [
"c",
"strings",
"validation"
],
"Title": "Function to validate a file name"
} | 4258 |
<p>I'm attempting to create a function that will give me a "fish" at random, though a bias random depending on the weight value for the "fish".</p>
<pre><code>var FISH = {
"level1": [
//["name", xp, weight]
["Shrimp", 10, 95],
["Sardine", 20, 85],
["Herring"... | [] | [
{
"body": "<p>I'd suggest you select a random number that's from 0 to the total weight of your fish. Then, each fish can have a piece of that random range according to it's weight which gives it the weighting you want. This ends up giving each fish a number of buckets that corresponds to it's weighting value ... | {
"AcceptedAnswerId": "4265",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-20T17:11:32.013",
"Id": "4264",
"Score": "3",
"Tags": [
"javascript",
"optimization",
"random"
],
"Title": "JavaScript: Weighted Random Generator"
} | 4264 |
<p>I'm coming from a Python background(look at indentation :) ) and trying to learn basics of C++. I wrote a simple prime generator, I know there is better algorithms, but I want advices about coding style, inconsistencies, or better language use.</p>
<p>Here is my main.cpp:</p>
<pre><code>#include <iostream>
... | [] | [
{
"body": "<p>You don't need to manage resources manually. \nYou are declaring a pointer to the new PrimeGenerator instance and releasing it at the end of the main function, but what happens if one of the calls inbetween (for example, addPrime()) fails? You end up with a memory leak.</p>\n\n<p>Instead of </p>\n... | {
"AcceptedAnswerId": "4270",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-20T19:36:11.553",
"Id": "4267",
"Score": "8",
"Tags": [
"c++",
"beginner",
"primes"
],
"Title": "Prime generator"
} | 4267 |
<p>I made this sketch in Processing (basically Java) that consists of a bunch of particles moving around a Perlin Noise field. Each particle is aware of the position of one other particle and this creates a rope like effect. The sketch is intended to be aesthetic, like a screensaver, and I in that way it might be diffi... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-21T05:57:54.720",
"Id": "6394",
"Score": "0",
"body": "Is this code complete? If this is in Java why are there a bunch of functions and variables defined in the middle of nowhere that are not inside a class?"
},
{
"ContentLicen... | [
{
"body": "<p>Some small observations:</p>\n\n<pre><code>//maybe it works here, but generally you should use Math.abs(a-b) < epsilon \n//with a very small epsilon instead a == b for double comparision\nif(maxAlph == .05) maxAlph = 1;\nelse maxAlph = .05; \n-->\nmaxAlph = (maxAlph == .05) ? 1 : 0.05; \n\n... | {
"AcceptedAnswerId": "4301",
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-21T05:17:38.843",
"Id": "4271",
"Score": "6",
"Tags": [
"animation",
"processing"
],
"Title": "Particles moving around a Perlin Noise field"
} | 4271 |
<p>I was wondering if anyone would be so kind as to look at this code I've written for practice. Tell me what am I doing wrong, where can I optimize, anything.</p>
<p>What this code does is (in terminal) draw a bunch of random 1's and 0's, but I added an effect kinda like a lattice. The bulk of this I got help on he... | [] | [
{
"body": "<p>A few pointers:</p>\n\n<p>First, variable length arrays are only allowed in C99:</p>\n\n<pre><code>char buffer1[width + 1];\n</code></pre>\n\n<p>If the code is to be strictly ANSI C, one would make an pointer to an array and use a function like <code>malloc</code> or <code>calloc</code> to allocat... | {
"AcceptedAnswerId": "4276",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2009-06-21T11:01:15.753",
"Id": "4275",
"Score": "13",
"Tags": [
"c",
"random",
"console",
"ascii-art"
],
"Title": "Draw random 1's and 0's"
} | 4275 |
<pre><code>if(env['REQUEST_METHOD'] == 'POST')
$post = {}
post = env['rack.input'].read.split('&')
post_split = Array.new
post.each{|x| post_split << x.split('=')}
post_split.each{|x|
$post[x[0]] = x[1]
}
end
</code></pre>
<p>What would be a nice way to do this? I have a string like:</p>
<pr... | [] | [
{
"body": "<p>better way of such parsing is not to reinvent the wheel and use existing ruby functionality</p>\n\n<pre><code>require \"cgi\"\npost = CGI.parse \"a=b&c=d\"\n# => {\"a\"=>[\"b\"], \"c\"=>[\"d\"]}\n</code></pre>\n\n<p>hash values are arrays here, but it makes sense, as you can have seve... | {
"AcceptedAnswerId": "4457",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-21T15:00:38.020",
"Id": "4282",
"Score": "5",
"Tags": [
"beginner",
"ruby",
"strings",
"array"
],
"Title": "Split post string into hash"
} | 4282 |
<pre><code>(cn.PublishEnd == null || cn.PublishEnd < DateTime.Now)
</code></pre>
<p>vs</p>
<pre><code>(cn.PublishEnd ?? DateTime.MinValue < DateTime.Now)
</code></pre>
<p>Which is more readable?
I'm inclined towards the second form, but something tells me I'm wrong.</p>
<p>Context:</p>
<pre><code>namespace D... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-21T18:45:43.883",
"Id": "6415",
"Score": "0",
"body": "In light of seeing Ivan's answer, I think we'll need a bit more context like, what's that actual type of `cn.PublishEnd`? If it happened to be of type `bool?` (very odd), then tha... | [
{
"body": "<p>Within a conditional, it would depend largely on the type of the object. In general, I'd favor the first form, particularly if it is a non-string reference type that doesn't offer a nice default value. Otherwise if it was a <code>string</code> or nullable structure, I would prefer the second.</p... | {
"AcceptedAnswerId": null,
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-21T15:57:08.873",
"Id": "4283",
"Score": "7",
"Tags": [
"c#",
"datetime",
"linq",
"comparative-review",
"null"
],
"Title": "Enumerating new articles whose publication start d... | 4283 |
<p>I need to get this program reviewed. It is counting the lines of code in all files in the
given directory. </p>
<p>I'm new to python and need some advice on pretty much everything.</p>
<pre><code>#!/usr/bin/python
import os
import sys
def CountFile(f):
counter = 0
f = open(f, "r")
for line in f.read(... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2014-02-02T21:15:54.457",
"Id": "68626",
"Score": "2",
"body": "What was wrong with `wc -l *`?"
}
] | [
{
"body": "<p>You can iterate over a file object line by line without calling <code>.read()</code> or <code>.split('\\n')</code>\nThat should save you a bunch of function calls.</p>\n\n<p>Maybe the built in <code>sum()</code> function would be faster? </p>\n\n<pre><code>counter = sum(1 for line in f)\n</code></... | {
"AcceptedAnswerId": "4289",
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-21T17:53:12.863",
"Id": "4286",
"Score": "4",
"Tags": [
"python"
],
"Title": "Program to count the number of lines of code"
} | 4286 |
<p>I have written this implementation of Huffman coding. Please suggest ways to make this code better, i.e. more Pythonic.</p>
<pre><code>from collections import Counter
#####################################################################
class Node(object):
def __init__(self, pairs, frequency):
self.pairs = ... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-21T14:44:01.500",
"Id": "6418",
"Score": "2",
"body": "Also, if you want to compare methods, there is an early version of a Huffman coding module I wrote as an exercise [on pastebin](http://pastebin.com/QEK3WdbE). The core is the same ... | [
{
"body": "<p><strike>Generally, loops aren't encouraged in python, since it is interpreted rather than compiled, thus the loop body will be \"recompiled\" at every iteration.</strike></p>\n\n<p>using map/reduce is one good way to avoid loops. another is using the <code>for.. in</code>, e.g.:</p>\n\n<pre><code>... | {
"AcceptedAnswerId": "4293",
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-08-21T12:58:13.197",
"Id": "4290",
"Score": "8",
"Tags": [
"python",
"algorithm"
],
"Title": "Is there a better way to implement this Huffman Algorithm in Python?"
} | 4290 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.