body stringlengths 25 86.7k | comments list | answers list | meta_data dict | question_id stringlengths 1 6 |
|---|---|---|---|---|
<p>This is the first time I tried to write a back propagation ANN and I would like to know what more experienced people think of it. The code is meant to distinguish if text is written in English, French or Dutch.</p>
<p>I know my training set isn't very diverse but I just got the data from about 30 different texts, e... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-17T22:06:57.523",
"Id": "9477",
"Score": "0",
"body": "You might want to cut the soundcode from the code, I just used it to warn me when the calculation was running and when it was done so I could test."
}
] | [
{
"body": "<pre><code>import math, time, random, winsound\nglobal Usefull\n</code></pre>\n\n<p>A global statement outside of a function has no effect</p>\n\n<pre><code>LearningRate = 0.001\n</code></pre>\n\n<p>The python style guide recommends that global constants be in ALL_CAPS</p>\n\n<pre><code>InWeight = [[... | {
"AcceptedAnswerId": "6135",
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-17T21:32:46.983",
"Id": "6126",
"Score": "4",
"Tags": [
"python",
"matrix",
"windows",
"natural-language-processing",
"neural-network"
],
"Title": "Back propagation neural ... | 6126 |
<p>I have a user model.</p>
<p>It basically forwards get/create/delete requests into my database client.</p>
<p>The problem I have is the error handling, it screams not DRY to me, but I can't find an elegant way to clean it up.</p>
<pre><code>var UserModel = pd.make(Model, {
get: function _get(id, cb) {
... | [] | [
{
"body": "<p>Based on feedback from <a href=\"http://chat.stackoverflow.com/transcript/message/1900602#1900602\">JavaScript chat</a>. Credit to @Zirak and @Chris</p>\n\n<p>You want a hash of white listed error messages for each method</p>\n\n<pre><code>var whitelistMap = {\n \"get\": [\"not_found\"],\n \... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-17T21:45:57.810",
"Id": "6127",
"Score": "4",
"Tags": [
"javascript"
],
"Title": "Refactoring node.js database code"
} | 6127 |
<p>Basically I'm writing a simple framework in php in order to learn more. This session class is extremely rudimentary. At this point I'm not even checking ip/user_agent to prevent session hijacking, it's just basic right now. Also, my <code>_serialize()</code> and <code>_unserialize</code> functions don't actually do ... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-18T09:52:15.860",
"Id": "9494",
"Score": "3",
"body": "Matthew there are numerous problems with the code. You do abuse static, you really need to understand the concept before applying it. Every static member or function you have shoul... | [
{
"body": "<p>The biggest problem you have is this:</p>\n\n<pre><code>self::$_id = $_COOKIE['session'];\n$statement = 'SELECT * FROM ' . Config::get('session', 'table') . \" WHERE id='\" . self::$_id . \"'\";\n</code></pre>\n\n<p>You are <em>blindly</em> trusting user input. This is not only a vector for a <a h... | {
"AcceptedAnswerId": "6138",
"CommentCount": "4",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-18T00:20:41.200",
"Id": "6133",
"Score": "2",
"Tags": [
"php",
"design-patterns",
"mvc"
],
"Title": "A simple php Session class, is the layout/design correct?"
} | 6133 |
<p>I have a WebPostRequest class which looks like this:</p>
<pre><code>public class WebPostRequest
{
public const string Success = "OK";
private WebRequest _webRequest;
private List<string> _paramsList;
public WebPostRequest(string url)
{
try
{
_webRequest = WebR... | [] | [
{
"body": "<p>I would not show any UI from WebPostRequest. For a fast solution you can just wrap the whole upload part and provide some sort of a listener whose methods will be called to notify about progress.</p>\n\n<p>Thus WebPostRequest will not know nothing about UI layer. UI will not be blocked and be able... | {
"AcceptedAnswerId": "6717",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-18T01:37:27.807",
"Id": "6134",
"Score": "2",
"Tags": [
"c#",
"multithreading"
],
"Title": "How to refactor code to use threads?"
} | 6134 |
<p>I have the following code which is located within a case, in a switch statement. Its goal is to add a Pupil to a Subject using the Collections in the respective class.</p>
<p>I've tried to make the code as readable as possible, but I'm sure there's a better approach.</p>
<pre><code> System.out.println("Adding a P... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-21T15:24:53.347",
"Id": "9579",
"Score": "1",
"body": "Just a small one: what benefit are you gaining from using `Boolean` instead of `boolean`?"
}
] | [
{
"body": "<p>Seems to me like you have a couple functions here that need pulled out:</p>\n\n<ol>\n<li>findSubject</li>\n<li>collect input and validate\n<ul>\n<li>I'd also pull out a function for anything more involved like the date prompts that need validated</li>\n</ul></li>\n<li>create pupil and add</li>\n</... | {
"AcceptedAnswerId": null,
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-18T15:27:44.137",
"Id": "6139",
"Score": "3",
"Tags": [
"java"
],
"Title": "Entering pupil information"
} | 6139 |
<p>I have two functions, one to convert a datetime to a hex word, and one to convert a word to a datetime. I was just wondering if there was a more efficient way to convert back and forth.</p>
<pre><code>/// <summary>
/// Converts a date time to a hexadecimal word
/// </summary>
/// <param name="date"&g... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-18T17:15:41.110",
"Id": "9507",
"Score": "0",
"body": "Out of curiousity, _why_ are you doing this? And you should really see about getting the shifts and masks defined at the _class_ level, and ideally, have one depend on the other (... | [
{
"body": "<p>I note that your functions are not doing what they say they're doing. For example, you don't convert a date to a hexadecimal word, you convert to an integer value, a value that also happens to disregard the century and the time component entirely. In getting the date back, you also perform some bu... | {
"AcceptedAnswerId": "6146",
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-18T15:54:53.543",
"Id": "6140",
"Score": "2",
"Tags": [
"c#"
],
"Title": "Converting a Date to Hexadecimal Word"
} | 6140 |
<p>For a project I'm working on, I need to compare version strings where:</p>
<ul>
<li>version strings are composed only of numbers and periods</li>
<li>version strings are made up of an arbitrary number of segments (major.minor, manjor.minor.build, etc)</li>
<li>1.0 is equivalent to 1.0.0, etc.</li>
</ul>
<p><strong... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-19T07:55:26.433",
"Id": "9519",
"Score": "10",
"body": "Uhh, what's wrong with using [`Version.Parse`](http://msdn.microsoft.com/en-us/library/system.version.parse.aspx) to parse it and comparing `Version` instances?"
}
] | [
{
"body": "<p>I adjusted the compare method slightly, making it more readable (IMO) through the use of an Anonymous type with named properties and by extracting two helper methods.</p>\n\n<p>I'm not checking the result of <code>TryParse</code> anymore since the <code>result</code> is <code>0</code> for failed c... | {
"AcceptedAnswerId": null,
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-18T17:11:42.930",
"Id": "6143",
"Score": "4",
"Tags": [
"c#",
"algorithm",
"linq",
"unit-testing"
],
"Title": "VersionString (eg \"1.0.2\") IComparer algorithm"
} | 6143 |
<p>The last question I have been playing around with is a right rotate. I think it is also called a barrel roll. Please post if you have a better solution please post. I am still learning the art of bit flipping.</p>
<pre><code>/*
* Next write a function rightrot(x,n) that returns the value of the
* integer x rotat... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-18T19:03:57.983",
"Id": "9513",
"Score": "0",
"body": "What are you trying to do with the `~`? If it's to account for sign extension, the `unsigned` already does that. You should be fine with just `x << (size_of_int - n) | x >> n`."
... | [
{
"body": "<p>What is this supposed to be?</p>\n\n<pre><code>size_t size_of_int = sizeof(int) << 3;\n</code></pre>\n\n<p>The shift left by 3 is very cryptic. Multiply by 8 or explain what you are doing. But a better solution is to multiply the number of bytes by the actual bits in a byte. </p>\n\n<p>If y... | {
"AcceptedAnswerId": "6149",
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-18T18:35:49.220",
"Id": "6147",
"Score": "3",
"Tags": [
"c",
"bitwise"
],
"Title": "Print Bits Part III"
} | 6147 |
<p>I have a list of sales records sorted by sales date. I need to fill in the gaps (days without a sales record) in this list with 'inferred sales' records. The method in question calculates an average price for the inferred sales records from its closest valid neighbors that have the same <code>SalesType</code>. If t... | [] | [
{
"body": "<p>Just a few things I want to point out before tackling this:</p>\n\n<ul>\n<li><p>Based on the comment, your first condition is flipped. You're skipping valid entries instead of invalid entries. It should be:</p>\n\n<pre><code>if (!salesRecord.IsValid)\n</code></pre></li>\n<li><p>Your last conditi... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-19T17:31:20.527",
"Id": "6150",
"Score": "2",
"Tags": [
"c#"
],
"Title": "Finding neighbors and returning average value from neighbors"
} | 6150 |
<p>I am trying to find a good way of parsing SQL arguments, the way I am accomplishing it now seems like it can use a lot of improvement. I am trying to convert arguments that are split apart by ', & <= =>' from a string into SQL arguments like name = 'ABC' OR name ='John Doe'.</p>
<p>So </p>
<pre><code>parseS... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-20T06:24:28.577",
"Id": "9535",
"Score": "0",
"body": "Interesting idea. I'll take a look. Give me some time."
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-20T06:39:19.500",
"Id": "9537",
"Score... | [
{
"body": "<p>If this is a whole SQL class of sorts, then I think you can improve your design a bit. What if you want people who aren't archived and names match certain things? <code>'dan,john&steven', 'username'</code> doesn't really work. You need something more like:</p>\n\n<pre><code>$sql\n ->se... | {
"AcceptedAnswerId": null,
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-19T18:05:37.803",
"Id": "6151",
"Score": "3",
"Tags": [
"php",
"sql",
"parsing"
],
"Title": "Parsing Placeholders as SQL Arguments in PHP"
} | 6151 |
<p>The problem I am trying to solve can be described as take <code>binarysearch0</code> and rewrite it as <code>binarysearch1</code> such that <code>binarysearch1</code> uses a single test inside the loop instead of two. The code I used to do this is seen below. Please let me know if you think I accomplished the task, ... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-19T21:30:33.423",
"Id": "9522",
"Score": "0",
"body": "I find `binarysearch0` clearer than `binarysearch1`, and here's no difference efficiency-wise. If this is a homework question, how exactly was it formulated? If not, what do you ho... | [
{
"body": "<p>In <code>binarysearch1</code>, you've replaced one test in the loop body with one test in the loop condition. That doesn't reduce the number of tests, and I agree with Gilles, <code>binarysearch0</code> is clearer. I would recommend keeping that. However, in both searches, the calculation of <code... | {
"AcceptedAnswerId": "6156",
"CommentCount": "3",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-19T20:18:51.123",
"Id": "6152",
"Score": "3",
"Tags": [
"optimization",
"c",
"binary-search"
],
"Title": "Binary search optimization: Kernighan & Ritchie 3-1"
} | 6152 |
<p>I want you to review my JavaScript project.</p>
<pre><code>var geocoder;
var map;
var infowindow = new google.maps.InfoWindow();
var marker;
function initialize() {
document.upload.lat.value = geoip_latitude();
document.upload.lng.value = geoip_longitude();
geocoder = new google.maps.Geocoder();
va... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-20T00:18:26.303",
"Id": "9525",
"Score": "3",
"body": "Add comments, name your functions, make the code less ugly."
}
] | [
{
"body": "<p><strong>Comments:</strong></p>\n\n<ul>\n<li><strong>I highly suggest that you document your functions better.</strong> At the beginning of each function, document:\n<ol>\n<li>The overall purpose of the function</li>\n<li>The parameters it takes and their types</li>\n<li>What is returned.</li>\n</... | {
"AcceptedAnswerId": "6167",
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-19T23:56:47.037",
"Id": "6157",
"Score": "3",
"Tags": [
"google-apps-script",
"google-maps"
],
"Title": "Google Maps project in JavaScript"
} | 6157 |
<p>I'm trying to conform to the Zend Coding Standard as well.</p>
<pre><code>EDIT 2:
<?php
/**
* Input : Query request
* Output : A row or a list of rows
* Notes :
*
* Provides escaping and binding via PDO library
* Consolidates PDO to use a single calling mechanism
* Consolidate... | [] | [
{
"body": "<p>The good thing about this code is your bracing style which is applied consistently and is visually appealing. Now on to some improvements:</p>\n\n<p>This code does not implement a Database. (You have called the class Database, but it does not resemble something that looks like a database). This... | {
"AcceptedAnswerId": "6168",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-20T04:41:02.867",
"Id": "6162",
"Score": "0",
"Tags": [
"php"
],
"Title": "class Database extends OneDatabase"
} | 6162 |
<p>I'm looking for any feedback on this code. If it is too much please pick a random method/group and give me feedback.</p>
<p>This code passes www.jshint.com. I'm trying to shape this collection into a library. The regular expressions have a way to go. Any incremental constructive feedback welcome. Only the objects r... | [] | [
{
"body": "<p>Couple of notes from my side:</p>\n\n<ol>\n<li><p><code>var one</code>'s methods perform DOM lookups without caching. Set a field in <code>one</code> object to cache the access to elements. This will increase the performance.</p></li>\n<li><p>Functions <code>check_generic</code>, <code>check_empty... | {
"AcceptedAnswerId": "6185",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 4.0",
"CreationDate": "2011-11-20T05:31:10.813",
"Id": "6166",
"Score": "1",
"Tags": [
"javascript"
],
"Title": "Shaping collection of methods to MVC pattern"
} | 6166 |
<pre><code>if (isset($_POST['submit'])) {
if (isset($_SESSION['token']) && $_POST['token'] == $_SESSION['token']) {
$errors = array();
$error = 0;
if (!preg_match('/^[A-Za-z](?=[A-Za-z0-9_.]{3,20}$)[a-zA-Z0-9_]*\.?[a-zA-Z0-9_]*$/i', $_POST['username'])) {
$errors[] = 'Yo... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-21T00:05:13.107",
"Id": "9556",
"Score": "0",
"body": "I have some doubt on the username regex"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-21T07:10:06.377",
"Id": "9569",
"Score": "0",
"body... | [
{
"body": "<p>You have two long if blocks:</p>\n\n<pre><code>if (isset($_POST['submit'])) {\n if (isset($_SESSION['token']) && $_POST['token'] == $_SESSION['token']) {\n ...\n }\n}\n</code></pre>\n\n<p>Why not combine them?</p>\n\n<pre><code>if (isset($_POST['submit']) && (isset($_SESSI... | {
"AcceptedAnswerId": "6174",
"CommentCount": "3",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-20T11:27:39.267",
"Id": "6170",
"Score": "2",
"Tags": [
"php"
],
"Title": "Validation code tips"
} | 6170 |
<p>I'm trying to transpose the following data from:</p>
<pre><code>1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
</code></pre>
<p>to:</p>
<pre><code>1 1 ... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-14T01:18:01.683",
"Id": "9545",
"Score": "0",
"body": "Any reason you are limiting the solution to `sed`? What about `awk`?"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-14T01:22:50.527",
"Id": "9546... | [
{
"body": "<p>This yet another way to do it:</p>\n\n<pre><code>sed -r '1{s/$/ /;s/ / \\n/g};:a;$!N;s/$/ /;:b;s/\\n(.*\\n+)(\\S+\\s)/\\2@!@\\1/;tb;s/@!@/\\n/g;${s/ \\n/\\n/g;s/\\n+$//;q};ba'\n</code></pre>\n\n<p>This method is somewhat faster and only uses single delimiter which may be crafted to be unique. i.e.... | {
"AcceptedAnswerId": null,
"CommentCount": "8",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-14T01:14:19.583",
"Id": "6175",
"Score": "4",
"Tags": [
"matrix",
"shell",
"sed"
],
"Title": "Transpose a matrix using sed"
} | 6175 |
<p><strong>Are there any security flaws in what I plan to do?</strong></p>
<p>I need to store the following in my DB:</p>
<ol>
<li>a random string to act as a salt for encrypting a password</li>
<li>the encrypted password that used the salt in #1</li>
</ol>
<p>Here's the PHP code I have to accomplish the above tasks... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-20T17:41:26.737",
"Id": "9552",
"Score": "0",
"body": "I don't think “critique my code” is appropriate on this site. “Critique my protocol” could be, although a precise description of your security objectives would be preferable. There... | [
{
"body": "<p>To summarize what you are doing here:</p>\n\n<p>You use AES in ECB-mode, with some unknown key, to encrypt a password.</p>\n\n<p>You also try to use a random initialization vector as a salt for this encryption.</p>\n\n<p>I see these problems:</p>\n\n<ul>\n<li><p>Usually, <strong>you don't want to ... | {
"AcceptedAnswerId": null,
"CommentCount": "4",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-20T17:17:23.053",
"Id": "6176",
"Score": "4",
"Tags": [
"php",
"strings",
"security",
"random"
],
"Title": "Random string + encrypt/decrypt"
} | 6176 |
<p>Alright, well the title doesn't really explain anything, so I'll show a pared down version of my code:</p>
<pre><code>class Handler {
Dictionary<object, Leaf> AllItems = new Dictionary<object, Leaf>(); //This list
Leaf RootLeaf = new Leaf();
public void Add (object obj)
{
Leaf A... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-21T18:28:56.520",
"Id": "9585",
"Score": "0",
"body": "Are you really going to allow any object to be a key to the `Dictionary<object, Leaf>`?"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-21T22:45:39.167... | [
{
"body": "<p>Well, it's not a tree, it's a linked list, and it won't work properly for more than seven items. After that you will overwrite the <code>SubLeaf</code> variable in the first node, and previously added items will be removed from the list, and <code>AllItems</code> will contain lost items with disow... | {
"AcceptedAnswerId": "6180",
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-21T06:13:38.207",
"Id": "6179",
"Score": "2",
"Tags": [
"c#"
],
"Title": "Hold a list of items stored in recursive class?"
} | 6179 |
<p>I am working a project which uses a function to show a modal dialog. The dialog can be hidden by calling the hideModal() function. This function is triggered by:</p>
<ul>
<li>Pressing the ESC key</li>
<li>Clicking on the modal background</li>
<li>Clicking on the close button</li>
</ul>
<p>My current code is:</p>
... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-21T21:18:38.863",
"Id": "9592",
"Score": "2",
"body": "Since your event sources (elements, document etc) are different and there are custom actions you have to implement each time, there is no better way of doing this, unfortunately."
... | [
{
"body": "<p>Your code isn't all that bad. However because you are binding key events on the document, you need to allow room, for other key events to be bound, for other tasks, so your code doesn't interfere with other ESC functions.</p>\n\n<p>The concept is that you bind on click, and unbind upon hiding your... | {
"AcceptedAnswerId": "6198",
"CommentCount": "3",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-21T13:00:40.453",
"Id": "6181",
"Score": "2",
"Tags": [
"javascript",
"jquery"
],
"Title": "jQuery multiple events execute same function refactoring"
} | 6181 |
<p>I've learned MVVM for a week or so, and I've seen the <a href="http://blog.lab49.com/archives/2650" rel="noreferrer">Jason Dolinger video</a> many times. Step by step, following the Jason Dolinger video, I've created my own application which is almost the same to the one presented by Jason Dolinger. I'm completely s... | [] | [
{
"body": "<p>There are a few things you can improve here. Unless all your classes are in separate files, you do not need to do <code>namespace NamespaceName</code> around each class.</p>\n\n<p>This is most unnecessary:</p>\n\n\n\n<pre class=\"lang-cs prettyprint-override\"><code>public List<ConsoleData>... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-21T18:30:11.747",
"Id": "6183",
"Score": "8",
"Tags": [
"c#",
"mvvm"
],
"Title": "MVVM implementation based on Jason Dolinger's video"
} | 6183 |
<p>How can this solution be improved?</p>
<pre><code>#include <limits.h>
void itoa(int n, char s[])
{
int min_int = 0;
int i, sign;
if (INT_MIN == n) {
min_int = 1;
n++;
}
if ((sign = n) < 0)
n = -n;
i = 0;
do {
s[i++] = n % 10 + '0';
} while (... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-21T21:12:19.813",
"Id": "9591",
"Score": "1",
"body": "How about just use `snprintf()`?"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-22T10:34:05.603",
"Id": "9615",
"Score": "0",
"body": "@Je... | [
{
"body": "<p>Just some general idea about the code (without any effort to create a better algorithm).</p>\n\n<p>First of all, I agree with @Jeff, if there is a library for that use it. You don't have to maintain it and everybody will know what the code does.</p>\n\n<p>So, code. I would use longer variable name... | {
"AcceptedAnswerId": "6192",
"CommentCount": "3",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-21T20:56:05.990",
"Id": "6184",
"Score": "7",
"Tags": [
"c",
"reinventing-the-wheel",
"integer"
],
"Title": "Implementation of itoa()"
} | 6184 |
<p>I'm somewhat curious on peoples opinion about such a method. I Actually didn't find anything similar in librarys I use on regular bases so far.</p>
<p>This is my implementation:</p>
<pre><code>Object.map = function _map( obj, transform ) {
if( typeof obj === 'object' && typeof transform === 'function' ... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-22T01:15:34.917",
"Id": "9596",
"Score": "0",
"body": "[I would implement it as such](http://jsfiddle.net/VP2H8/1). I'd also avoid the word \"map\". And probably use word \"transform\". It's also a rare operation and simple to do witho... | [
{
"body": "<p>First comment, there is no need for the closure inside _forEach which makes the code unnecessarily difficult to read. More importantly I would argue that the function would be much more useful if did not transform the original object and instead returned a new object.</p>\n\n<pre><code>Object.map... | {
"AcceptedAnswerId": null,
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-22T00:57:12.660",
"Id": "6189",
"Score": "3",
"Tags": [
"javascript"
],
"Title": "Object map() / transmute() method"
} | 6189 |
<pre><code>/**
* View_message
* Object Model
*/
var View_message = function(div)
{
this.div = document.getElementById(div);
};
View_message.prototype.messages =
{
empty: 'Please complete all fields',
empty_bm: 'Please enter both a title and url',
name: 'Only letters or dashes for the name... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-22T02:11:48.250",
"Id": "9605",
"Score": "0",
"body": "Your using `.innerHTML` instead of `.textContent`"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-22T04:23:53.050",
"Id": "9609",
"Score": "0",... | [
{
"body": "<p>I believe you are overcomplicating things. Declaring a prototype, will not make your code any more robust or even faster. I'm sure you know, but actually something like the code below will be easier to read, more effective and also faster.</p>\n\n<pre><code>/**\n * View_message\n * Object Mo... | {
"AcceptedAnswerId": "6197",
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-22T01:55:08.253",
"Id": "6190",
"Score": "0",
"Tags": [
"javascript"
],
"Title": "View_messages \"class\""
} | 6190 |
<p>I used to use static or singleton for my DAL class. However, I read some articles saying that singleton is evil and should be avoided. Therefore I try to rewrite my code like this:</p>
<pre><code>public class CustomerBLL
{
private CustomerHelper helper = new CustomerHelper();
private CustomerDAL dal = new C... | [] | [
{
"body": "<p>I believe that some generalizations are to be made lightly. i.e. stating that Singletons are evil in general is not accurate, as there are some cases where Singleton is a proper (and maybe the only) solution. And yes I do believe that it is one of the overly abused patterns out there. Having gott... | {
"AcceptedAnswerId": "6193",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-22T02:38:22.110",
"Id": "6191",
"Score": "2",
"Tags": [
"c#",
"singleton",
"static"
],
"Title": "How can I improve this code without using static or singleton?"
} | 6191 |
<p>I'm combining 2 UISliders made from scratch -- one horizontal and one vertical -- to make a single slider that controls 2 parameters. They are set up so that the vertical slider is perpendicular to the horizontal one. (Like a cross) The thumb knob can move both horizontally and vertically, and if it's moved horizon... | [] | [
{
"body": "<p>Once your control is active, just keep tracking until the touch ends, even if the touch moves outside of the view:</p>\n\n<pre><code>- (BOOL)continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event {\n CGPoint touchPoint = [touch locationInView:self];\n\n CGFloat hLimitLeft = ho... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-22T08:47:35.093",
"Id": "6195",
"Score": "3",
"Tags": [
"objective-c"
],
"Title": "Combining horizontal and vertical sliders into one"
} | 6195 |
<p>For the time being, I am not interested in any recursive solution. The code is not modularized; the whole body is inside the main function.</p>
<pre><code>#include<iostream.h>
#include<stdlib.h>
/*This code is written by NF between any time of 120711 and 210711;modified at 231211*/
int main()
{
int... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-22T10:15:09.263",
"Id": "9614",
"Score": "2",
"body": "Are those the original whitespaces? If yes...those should be changed first."
}
] | [
{
"body": "<pre><code>int length=0,min_heapified=0,start_length=0,index=0;\n</code></pre>\n\n<p>I prefer one declaration per line because it's easier to read and find the type of variables. Furthermore, <code>min_heapified</code> is only used inside the <code>do-while</code> loop, so declare it there:</p>\n\n<p... | {
"AcceptedAnswerId": "6987",
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-22T09:33:20.833",
"Id": "6196",
"Score": "7",
"Tags": [
"c++",
"algorithm",
"sorting"
],
"Title": "Heapsort is not modular"
} | 6196 |
<p>I am trying to validate time entry. <code>ArrTime</code> is the time entered by the user from the UI. I am trying to make it so that when a user attempts to add a time, it must be between 8:45 and 17:30. Is there any thing wrong with my code? I am not very good at JavaScript so I have a feeling it is that, but if yo... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-23T12:28:12.520",
"Id": "9649",
"Score": "0",
"body": "You can remove every 'return false;' and only put it at the end of each method. You could also extract the error message operations to one single method, with the message as parame... | [
{
"body": "<p>I would refactor conceptually and introduce a reusable <code>TimeRange</code> object with methods to check for inclusion:</p>\n\n<pre><code>class TimeRange : Range<DateTime>{\n\n bool Includes(DateTime other){...}\n bool Includes(TimeRange other){...}\n bool Includes(string other){.... | {
"AcceptedAnswerId": null,
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-22T15:26:13.360",
"Id": "6202",
"Score": "2",
"Tags": [
"c#",
"javascript",
"datetime",
"validation"
],
"Title": "Validation of Time"
} | 6202 |
<p>Fiddle: <a href="http://jsfiddle.net/fhBd5/" rel="nofollow">http://jsfiddle.net/fhBd5/</a></p>
<p>So the above is working but I'm having trouble trying to come up with a clean and concise way to do some type of loop on the if or statements. </p>
<p>Any help is very much appreciated! </p>
<pre><code>var checkPosit... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-22T17:07:48.300",
"Id": "9626",
"Score": "0",
"body": "Please post code into question. We can grantee that http://jsfiddle.net will be available in the future and thus the question may becomes meaningless for other people when reading ... | [
{
"body": "<p>Here is my slightly \"mathematical\" version of the function you have posted:</p>\n\n<pre><code>var checkPositionOnLoad = function (e) {\n var currentPos = 0;\n var listItem = document.getElement('li.current');\n var currentSelected = $('list').getChildren('li').indexOf(listItem) + 1;\n\n... | {
"AcceptedAnswerId": "6209",
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-22T16:29:15.663",
"Id": "6205",
"Score": "1",
"Tags": [
"javascript",
"jquery",
"mootools"
],
"Title": "Clean up If or syntax in JavaScript"
} | 6205 |
<p>I am a Scala newbie and attempting to improve my skills. I have decided to be playful about it and work to do a conversion of the spaceinvaders game supplied with the LWJGL. It consists of 10 Java classes of which I have loaded, compiled and successfully ran from Eclipse.</p>
<p>My first Scala step is to replace th... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2015-07-28T17:42:10.660",
"Id": "180248",
"Score": "0",
"body": "I have made huge progress with Scala since I initially posted this. To see an even more effective way to isolate the new operator and to easily implement case class instance cach... | [
{
"body": "<p>Question 1: You answered it yourself.</p>\n\n<p>Question 2: <code>try</code> / <code>catch</code> in Scala is an expression. Also, <code>sys.exit</code> is of type <code>Nothing</code>, which is the bottom type, a sub-type of every other type, hence it will not throw off the type inference. So you... | {
"AcceptedAnswerId": "6245",
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-22T18:20:56.303",
"Id": "6208",
"Score": "1",
"Tags": [
"scala",
"exception"
],
"Title": "Simple factory pattern in scala (attempting to restrict use of new)"
} | 6208 |
<p>I was hoping someone could provide some feedback with regards to this:</p>
<pre><code>import java.util.InputMismatchException;
import java.util.Scanner;
public class Bank {
protected double myBalance = 10.40;
protected double bankBalance = 1000000;
protected double creditRating = 0.1;
private sta... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-22T19:48:01.940",
"Id": "9632",
"Score": "0",
"body": "BTW, you can format stuff as source code by adding four spaces of indentation."
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-22T20:00:16.893",
"I... | [
{
"body": "<p>A number of points:</p>\n\n<ul>\n<li>Do not use floating-point data types (<code>float</code> or <code>double</code>) for amounts of money. Floating-point data types have limited precision, they cannot represent all decimal values exactly, and you're going to get roundoff errors sooner or later. U... | {
"AcceptedAnswerId": null,
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-22T19:29:35.800",
"Id": "6210",
"Score": "12",
"Tags": [
"java",
"beginner",
"finance"
],
"Title": "Bank transaction system"
} | 6210 |
<p>Should I be using <code>this</code> more, instead of <code>slider</code>? Am I making any serious no-no's here, and is there a better design I should implement? I'm up for any pointers :)</p>
<pre><code>slider = {
selector : '#slider',
init : function() {
$('#... | [] | [
{
"body": "<ol>\n<li><p><strong>You never use <code>slider.selector</code></strong>, so what's the point?</p></li>\n<li><p><strong>jQuery instances should be \"cached\"</strong>, otherwise you are creating new jQuery object each time you call <code>$()</code> function.</p></li>\n<li><p><strong>This code permits... | {
"AcceptedAnswerId": "6214",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-22T20:52:08.333",
"Id": "6213",
"Score": "5",
"Tags": [
"javascript",
"jquery"
],
"Title": "Quality of custom slider"
} | 6213 |
<p>I wrote a small class that demonstrates my problem:</p>
<pre><code>class Root
{
private Leaf RootLeaf;
private Dictionary<object, Leaf> AllItems = new Dictionary<object, Leaf>(); //dictionary contains a reference to each item, and the leaf it is assigned to (its parent)
public Root()
{
... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-23T03:00:09.023",
"Id": "9643",
"Score": "0",
"body": "What is your test program that uses these classes that you're using to profile?"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-23T05:19:11.980",
"... | [
{
"body": "<p>What I saw when quickly looking at it:</p>\n\n<p>Naming convetion: Some of your Properties and variables are lowerCamelCase, some are UpperCamelCase, some of your variables have a leading underscore, some don't. Some <code>objects</code> are called <code>o</code>, some <code>obj</code>.</p>\n\n<p>... | {
"AcceptedAnswerId": "6220",
"CommentCount": "5",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-23T02:00:42.160",
"Id": "6217",
"Score": "5",
"Tags": [
"c#",
"recursion"
],
"Title": "Optimize Recursive Fetching of information?"
} | 6217 |
<p>This is a pattern in SQL queries that I've found myself repeating recently:</p>
<pre><code>SELECT
w,
x,
y,
(w + x) / y as z
FROM
(SELECT
<some gigantic and complicated query> as w,
a + b as x,
a - b as y
FROM
basetable) somealias;
</code></pre>
<hr>
<p>The issue ... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-23T15:29:57.363",
"Id": "9652",
"Score": "0",
"body": "Why not just `SELECT (a+b) / (a-b) AS z FROM basetable`?"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-23T15:36:26.517",
"Id": "9653",
"Score... | [
{
"body": "<p>As long as the variables are well-named, I think your way (subquery) will be clear and therefore quite acceptable, though I'm not quite sure whether it will affect performance or to what extent (try benchmarking).</p>\n\n<p>That said, I'd still urge you to use a <a href=\"http://dev.mysql.com/doc/... | {
"AcceptedAnswerId": "6227",
"CommentCount": "6",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-23T15:12:47.073",
"Id": "6221",
"Score": "5",
"Tags": [
"mysql",
"sql"
],
"Title": "Using a subquery to rename columns"
} | 6221 |
<p>I have a class which updates an object. The class takes a <code>String id</code> in its constructor and returns the appropriate class based on the <code>id</code>. I think these two methods should be separated into their own classes as returning an object based on a String <code>id</code> will probably have uses els... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-23T12:57:58.083",
"Id": "9651",
"Score": "0",
"body": "In one sense if you've thought about asking then the answer is \"yes\"."
}
] | [
{
"body": "<p>There's no super-design-pattern that would be beneficial here, but you could definitely benefit from some basic OO concepts like encapsulation and single responsibility.</p>\n\n<p>For starters, rather than making a bunch of utility methods, it's better design to allow each class a static method li... | {
"AcceptedAnswerId": "6223",
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-18T12:37:59.503",
"Id": "6222",
"Score": "1",
"Tags": [
"java",
"design-patterns"
],
"Title": "Class which updates an object"
} | 6222 |
<p>The exercise was to take a file such as this:</p>
<blockquote>
<pre class="lang-none prettyprint-override"><code>You say yes
I say no
You say stop
And I say go go go
CHORUS:
Oh no
You say Goodbye
And I say hello
Hello hello
I don't know why
You say Goodbye
I say hello
#repeat 9 12
Why
#repeat 11 13
I say high
You... | [] | [
{
"body": "<p>First main problem is the code does not compile:</p>\n\n<p>When you implement template classes. The compiler instantiates the templates on use. This means the compilation unit that is instantiating the class must have already seen the source for the template (if it has not then it marks it as unre... | {
"AcceptedAnswerId": "6225",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-23T15:39:37.840",
"Id": "6224",
"Score": "7",
"Tags": [
"c++",
"parsing",
"stack"
],
"Title": "Song repetition manager with Stack"
} | 6224 |
<p>I'm working on a piece of code in my MVC3 application and I'm struggling to decide if what I plan to do is a bad idea or not.</p>
<p>I have a snippet of JavaScript that calls an MVC action that in turn returns an HTML partial that I then insert into a <code>div</code> element on my page. Nothing complicated there.<... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-23T17:44:20.300",
"Id": "9662",
"Score": "0",
"body": "Would you please clarify the reason why you want to post back the partial html to the server side (if I understand correctly)? Also, what would possibly cause the erros on the clie... | [
{
"body": "<p>I would consider returning with a more detailed JSON object:</p>\n\n<pre><code>{\n errorMessage: \"things have gone wrong!\",\n data: [\n {\n link: #,\n linkText: Test,\n size: 0MB\n },\n {\n link: #,\n linkText: Tes... | {
"AcceptedAnswerId": null,
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-23T17:29:11.483",
"Id": "6228",
"Score": "3",
"Tags": [
"c#",
"json",
"mvc"
],
"Title": "Passing HTML back inside a JSON object"
} | 6228 |
<p>The below is a class designed to implement <a href="https://www.rememberthemilk.com" rel="nofollow">Rememember the Milk</a>'s task priorities. I'm using it partly as an exercise in "Pythonic" programming, so it's fairly simple already, but advice on how to make it simpler or more Pythonic would be particularly appre... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-23T19:22:13.417",
"Id": "9671",
"Score": "1",
"body": "I'm curious as to why you're extending `int` -- I don't see any benefit from it here. Maybe I missed something?"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate":... | [
{
"body": "<pre><code>class Pri(int): # Subclass int to get handy comparison functions etc.\n</code></pre>\n\n<p>Abbreviating words in class names makes it harder to follow. Just call it <code>Priority</code></p>\n\n<pre><code> '''Task priority'''\n\n public_from_internal = {1: 1, 2: 2, 3: 3, 4: 'N'}\n... | {
"AcceptedAnswerId": "6243",
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-23T18:05:03.127",
"Id": "6229",
"Score": "3",
"Tags": [
"python"
],
"Title": "Python simple int subclass"
} | 6229 |
<p>I came across this code in our project today. Where possible I'm trying to leave the code base in a better shape than I found it, as I go along, and this method jumped out at me for a number of reasons, mainly the sql string and the try/catch block. I feel there's a less expensive way to do it.</p>
<p>Original Co... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-22T16:34:50.133",
"Id": "9663",
"Score": "0",
"body": "Why in the world would you change noOfRecords (horrible name) to an object and check for null? How is that the same?"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationD... | [
{
"body": "<pre><code>public bool CheckSomething(string paramA, int paramB)\n{ \n using (var conn = new SqlConnection(\"..\"))\n using (var comm = new SqlCommand(\"\", conn))\n {\n conn.Open();\n object noOfRecords = comm.ExecuteScalar();\n return noOfRecords != null;\n }\n}\... | {
"AcceptedAnswerId": "6231",
"CommentCount": "5",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-22T16:15:37.620",
"Id": "6230",
"Score": "7",
"Tags": [
"c#",
"exception-handling"
],
"Title": "Could this ExecuteScalar call be written better?"
} | 6230 |
<pre><code><?php
include_once('../dbInfo.php');
function getReport($user_table) {
$tables = array(
"day" => "p_day",
"month" => "p_month"
... etc. .....
);
$table = $tables[$user_table];
if(!$table) {
die(json_encode(array("error" => "bad tabl... | [] | [
{
"body": "<p>If the output is json you may want to set the content type:</p>\n\n<p>This is what I nievely do:</p>\n\n<pre><code>$type =\"text/json\";\nif ($_GET{\"test\"})\n{\n $type = \"text/plain\";\n}\nheader(\"Content-type: $type\");\n</code></pre>\n\n<p>I also use mysql_query() but that is because ... | {
"AcceptedAnswerId": "6256",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-23T20:03:36.877",
"Id": "6237",
"Score": "4",
"Tags": [
"php",
"security"
],
"Title": "PHP function to access a database and return json"
} | 6237 |
<p><strong>First Implementation</strong></p>
<pre><code>public enum ReviewFlowExample {
Draft {
@Override
public ReviewFlowExample getNext() {
return Review;
}
@Override
public ReviewFlowExample getPrevious() {
return null;
}
},
Re... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-24T03:39:48.093",
"Id": "9686",
"Score": "1",
"body": "The first one. No need to set up boilerplate getters and setters when you don't need any dynamic wiring. The first one is much more concise and clearer. You should trim it down eve... | [
{
"body": "<p>Just to spice up the discussion, you could also do something like this:</p>\n\n<pre><code> public ReviewFlowExample getNext() {\n ReviewFlowExample[] values = values();\n int next = ordinal() + 1 == values.length ? ordinal() : ordinal() + 1;\n return values[next];\n }\n<... | {
"AcceptedAnswerId": "6248",
"CommentCount": "9",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-24T03:34:55.590",
"Id": "6246",
"Score": "5",
"Tags": [
"java"
],
"Title": "Which one of the two enum implementation is better?"
} | 6246 |
<p>I wanted to try experimenting with <a href="http://en.wikipedia.org/wiki/Fluent_interface">fluent interface</a> design in the C programming language. That's why I wanted to ask you, Dear Code Review users, comments on the way I have implement this simple employee record interface.</p>
<p>Please note that for maximu... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-24T14:46:02.073",
"Id": "9719",
"Score": "3",
"body": "The point of fluent is to make the code more readable. This fails on that account. It would be more readable just to use normal methods in this situation (rather than chaining)."
... | [
{
"body": "<p>I would drop the <code>edit</code> function and make <code>add_employee</code> the entry point to the API. I've written a number of fluent APIs in C++ and found that placing the required arguments (e.g., employee name) in the API entry point invaluable. From the usage point of view, something li... | {
"AcceptedAnswerId": "6291",
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-24T14:07:17.440",
"Id": "6254",
"Score": "8",
"Tags": [
"c",
"database",
"fluent-interface"
],
"Title": "Fluent interface for manipulating employee records"
} | 6254 |
<p>I have a piece of code that takes a couple of integers and check if performing an addition on the inputs would result in an overflow.</p>
<p>I was wondering if this code is SOLID:</p>
<pre><code>public static boolean CanAdd(int me, int... args) {
int total = me;
for (int arg : args) {
if (total &g... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-24T19:08:54.340",
"Id": "9728",
"Score": "1",
"body": "Have you done any profiling which showed that this method is a bottleneck in your application?"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-24T20:30... | [
{
"body": "<p>I haven't found any input which isn't handled well by your code. Here are some tests:</p>\n\n<pre><code>assertTrue(CanAdd(0, Integer.MAX_VALUE));\nassertTrue(CanAdd(0, Integer.MIN_VALUE));\nassertTrue(CanAdd(Integer.MIN_VALUE, 0));\nassertTrue(CanAdd(-1, Integer.MAX_VALUE));\nassertFalse(CanAdd(1,... | {
"AcceptedAnswerId": null,
"CommentCount": "6",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-24T15:41:56.477",
"Id": "6255",
"Score": "33",
"Tags": [
"java",
"performance",
"integer"
],
"Title": "Int overflow check in Java"
} | 6255 |
<p>I wrote a quicksort program in C++.</p>
<ol>
<li>Is it okay to implement it this way?</li>
<li>Am I using pointers correctly?</li>
<li>Is my style okay?</li>
<li>Any ideas to speed it up or save memory?</li>
</ol>
<p> </p>
<pre><code>void change(int *i, int *j)
{
int temp = *j;
*j = *i;
*i = temp;
}
int *topla... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-24T16:06:46.363",
"Id": "9721",
"Score": "0",
"body": "I don't see any pointer issues. Nice work!"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-24T16:10:48.407",
"Id": "9722",
"Score": "0",
"b... | [
{
"body": "<p>The interface to quicksort:</p>\n\n<pre><code>quicksort(A, &A[n-1]);\n</code></pre>\n\n<p>In C I would expect to see a pointer and a size.<br>\nIn C++ I would expect to see two iterators (like above), but generally the end point is usually one past the end (if you want to be consistent with C+... | {
"AcceptedAnswerId": "6259",
"CommentCount": "9",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-24T16:04:39.063",
"Id": "6257",
"Score": "15",
"Tags": [
"c++",
"homework",
"sorting",
"quick-sort"
],
"Title": "Is this C++ quicksort program okay?"
} | 6257 |
<p>I did not get the job after submitting this piece of work in an interview, but I have no feedback to know what "BAD" things are inside this block of code.</p>
<p>The requirements are:</p>
<blockquote>
<ul>
<li>Connect to the server on a known port and IP </li>
<li>Asynchronously send a message to ... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-24T21:53:10.880",
"Id": "9732",
"Score": "9",
"body": "Any company that doesn't provide feedback on a candidate's assignment is a company that wouldn't be worth working for."
},
{
"ContentLicense": "CC BY-SA 3.0",
"Creation... | [
{
"body": "<p>I think the biggest problem is the lack of synchronization. You modify the <code>averageTime</code> and <code>count</code> variables in the callback which runs concurrently. You should synchronize the access of this variables. There is a good book on this topic: <a href=\"http://jcip.net/\">Read t... | {
"AcceptedAnswerId": "6263",
"CommentCount": "3",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-24T17:21:42.457",
"Id": "6258",
"Score": "36",
"Tags": [
"java",
"multithreading",
"interview-questions",
"asynchronous",
"callback"
],
"Title": "Asynchronous network callb... | 6258 |
<p>I am just trying to use ASP.NET MVC using repository pattern. Am I doing something wrong here?</p>
<p><strong>Model</strong> - Contains the model</p>
<pre><code>public class Contact
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public stri... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-25T06:43:09.450",
"Id": "9745",
"Score": "2",
"body": "What about moving IContactRepository to a IRepository<T> interface. That way you will not have to implement an interface for every model type you want a repository for?"
},
{
... | [
{
"body": "<p>Couple of things I'd change:</p>\n\n<ol>\n<li><p>Make save work out whether it needs to create or modify itself.</p></li>\n<li><p>Don't delete and re-create in the same. It will mess with relational data as Renato said.</p></li>\n<li><p>In the Retrieve method return <code>SingleOrDefault</code> - ... | {
"AcceptedAnswerId": null,
"CommentCount": "3",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-24T22:53:36.507",
"Id": "6266",
"Score": "5",
"Tags": [
"c#",
"design-patterns",
"asp.net-mvc-3"
],
"Title": "ASP.NET MVC using Repository pattern"
} | 6266 |
<p>I have two small structs (might change to classes later) that get loaded into Generic lists. The loading of the lists is what I'm asking about. Can it be done better or more object-oriented?</p>
<p>You'll notice I'm using MS Enterprise library for data access to a SqlCe database.</p>
<p>I'm just looking for some o... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-25T03:06:20.327",
"Id": "9743",
"Score": "0",
"body": "`Dispose()` of your `IDisposable` type (`IDataReader`) with a `using` block."
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-25T03:16:12.917",
"Id... | [
{
"body": "<p>Firstly:</p>\n\n<h2>Close your DataReaders!!!</h2>\n\n<p><br></p>\n\n<p>Secondly:</p>\n\n<p>You may want to consider abstracting boilerplate code like this:</p>\n\n<pre><code> Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>(\"CeConnectionString\");\n IDataReader rdr ... | {
"AcceptedAnswerId": null,
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-24T23:20:52.967",
"Id": "6267",
"Score": "4",
"Tags": [
"c#",
"object-oriented"
],
"Title": "Comic book conversion"
} | 6267 |
<p>In his <a href="http://blog.stevensanderson.com/2011/08/31/knockout-1-3-0-beta-available/" rel="nofollow">KnockoutJs 1.3 beta</a> post, Steve Sanderson has an example of using Binding providers ("5. Binding providers (and hence external bindings)") where he makes a comment in the JSFiddle JavaScript tab that says "/... | [] | [
{
"body": "<pre><code> ko.bindingConvenrions.conventions \".person-editor\"\n \".person-editor\" : \n 'with' : myViewModel.person\n \".first-name\" : ( person ) ->\n value : person.firstName\n \".last-name\" : ( person ) ->\n value : p... | {
"AcceptedAnswerId": "6308",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-25T00:26:48.800",
"Id": "6269",
"Score": "2",
"Tags": [
"coffeescript"
],
"Title": "Writing binding providers in CoffeeScript?"
} | 6269 |
<p>I have been tasked with implementing a secure account login system for a website's content management system. I have built a solution around an access masterpage that handles all account access functions. Here is the Page_Load</p>
<pre><code>Protected Sub Page_Load() Handles Me.Load
If Not Request.IsSecureConne... | [] | [
{
"body": "<p>What you are doing right now is not fundamentally correct. This link <a href=\"http://learn.iis.net/page.aspx/170/developing-a-module-using-net/\" rel=\"nofollow\">http://learn.iis.net/page.aspx/170/developing-a-module-using-net/</a> explains developing a custom authentication module. </p>\n\n<p>A... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-25T01:18:03.790",
"Id": "6271",
"Score": "2",
"Tags": [
"asp.net",
"security",
"vb.net"
],
"Title": "ASP.NET account login system implemented via Masterpage and Panels"
} | 6271 |
<p>I just made this sort method. It runs fine and the code looks okay in my eyes.</p>
<p>Is is possible to optimize it so it runs faster? If it's \$O(n^3)\$ now, it would be interesting changing it to \$O(n^2)\$ or something like that.</p>
<p>Side question: This is \$O(n^3)\$, right (I only like \$O(n^2)\$ and below... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-25T08:58:10.640",
"Id": "9752",
"Score": "6",
"body": "Why aren't you using any of the built-in sorting methods?"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-25T09:02:23.190",
"Id": "9753",
"Scor... | [
{
"body": "<p>You are just sorting by date, correct? I'd use</p>\n\n<pre><code>using System.Linq;\n// ....\nList<Log> errors = _logHandler.GetErrors(daysBack);\nList<Log> errorsSorted = errors.OrderBy(l => l.Date).ToList();\n</code></pre>\n\n<p>According to <a href=\"https://stackoverflow.com/que... | {
"AcceptedAnswerId": "6281",
"CommentCount": "5",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-25T08:56:44.830",
"Id": "6279",
"Score": "4",
"Tags": [
"c#",
"performance",
"algorithm",
"sorting",
"reinventing-the-wheel"
],
"Title": "Optimizing error-sorting method"
} | 6279 |
<p>I would like some advice to shorten down this snippet, give it more efficient flow and look. Review should focus on keeping it as a compact single method (except the two public props). The <code>DataType</code> and <code>MeasureStatus</code> is an <code>enum</code>. Readability is of course more important than compa... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-28T16:37:52.183",
"Id": "9871",
"Score": "0",
"body": "If there are design decisions that are driving this, then they need to be referenced in comments, probably more than just a 'look here for why' (unless you have a good referencing ... | [
{
"body": "<p>There are some (potentially bizarre to me) things going on here. A slightly larger view of the system may be helpful in further refining this method. In any case, here's how I'd restructure this. I've also included notes about some things I found odd...</p>\n\n<pre><code>public Tuple<DataTyp... | {
"AcceptedAnswerId": "6293",
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-25T11:37:02.607",
"Id": "6282",
"Score": "2",
"Tags": [
"c#",
"floating-point"
],
"Title": "Helper that hold methods for evaluate and calculate data"
} | 6282 |
<p>As part of our sprints, we do peer review each others code. </p>
<p>Here is the code that I am reviewing. </p>
<pre><code>public void SendEmail()
{
string emailAddress = string.Empty;
string managerEmailAddress = string.Empty;
if (//condition)
{
//do something
//Retrieve emailAddr... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-25T19:38:40.530",
"Id": "9772",
"Score": "2",
"body": "More of a standards holy-war which belongs on Programmers SE."
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-12-18T13:19:19.100",
"Id": "62463",
... | [
{
"body": "<p>Read <em>Effective Java, Second Edition, Item 45: Minimize the scope of local variables</em>. It has a good overview on the topic. (Google for \"minimize the scope of local variables\", it's on Google Books too.)</p>\n",
"comments": [],
"meta_data": {
"CommentCount": "0",
"Cont... | {
"AcceptedAnswerId": "6296",
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-25T11:57:14.373",
"Id": "6283",
"Score": "8",
"Tags": [
"c#"
],
"Title": "Variable declaration closer to usage Vs Declaring at the top of Method"
} | 6283 |
<p>I'm currently looking at <a href="http://www.boost.org/doc/libs/1_48_0/libs/crc/crc_example.cpp" rel="nofollow">this Boost::CRC example code</a> which I have also inserted below.</p>
<p>I always try to look for suggestions for improving my own coding style when I encounter well-written and well-formatted code.</p>
... | [] | [
{
"body": "<p>I'd invert the condition and use a <code>continue</code> if there is an error with the stream:</p>\n\n<pre><code>for (int i = 1; i < argc; ++i) {\n std::ifstream ifs(argv[i], std::ios_base::binary);\n if (!ifs) {\n std::cerr << \"Failed to open file '\" << argv[i] <... | {
"AcceptedAnswerId": "6307",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-25T20:13:13.157",
"Id": "6295",
"Score": "8",
"Tags": [
"c++",
"boost"
],
"Title": "Boost CRC example program file"
} | 6295 |
<p>Here it is the best I can do (I am a noob using Haskell):</p>
<pre><code>map (\(y, z) -> y + z) (zip x (tail x))
</code></pre>
<p>I'm looking for a point-free solution.</p>
| [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-26T19:57:20.337",
"Id": "9794",
"Score": "0",
"body": "\"Each element is the sum of itself with the next element of a List\", so in other words, `repeat 0`? :)"
}
] | [
{
"body": "<p>Pointfree, eh? Let's ask lambdabot.</p>\n\n<pre><code><DanBurton> @pl map (\\(y, z) -> y + z) (zip x (tail x))\n<lambdabot> zipWith (+) x (tail x)\n</code></pre>\n\n<p>Assuming that <code>x</code> is simply the input to this \"function\"</p>\n\n<pre><code><DanBurton> @pl \\x -... | {
"AcceptedAnswerId": null,
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-26T00:50:58.017",
"Id": "6299",
"Score": "8",
"Tags": [
"haskell"
],
"Title": "Each element is the sum of itself with the next element of a List: now do this point-free in Haskell"
} | 6299 |
<p>After checking some incoming coding competitions, I feel I may want to develop a little bit more on my coding skills and style using some basic algorithms/data structures.</p>
<p>The following two pieces of Java code are about some <code>LinkedList</code> operation. Can you take a quick look and give me some feedba... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-26T02:50:12.243",
"Id": "9782",
"Score": "0",
"body": "You missed a `&`. `if (head!=null & head.next!=null )` should be `&&`. I assume that was just a copy-paste thing because it wouldn't compile otherwise."
},
{
"ContentLicen... | [
{
"body": "<p>Rename <code>Node.add</code> to <code>setNext</code>, since its a setter method:</p>\n\n<pre><code>public void setNext(final Node node) {\n this.next = node;\n}\n</code></pre>\n\n<hr>\n\n<p>Create a constructor with only the <code>value</code> parameter:</p>\n\n<pre><code>public Node(final int ... | {
"AcceptedAnswerId": "6305",
"CommentCount": "4",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-26T02:23:55.370",
"Id": "6302",
"Score": "2",
"Tags": [
"java",
"algorithm",
"linked-list"
],
"Title": "Linked list operations for a competition"
} | 6302 |
<p>I'm trying to convert from random bytes to integers within a range. Basically converting as such:</p>
<pre><code>byte[] GetRandomBytes(int count) -> int NextInteger(int min, int max)
</code></pre>
<p>Another way to think about it would be: I have a <a href="http://msdn.microsoft.com/en-us/library/system.securit... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-26T06:41:15.020",
"Id": "9787",
"Score": "0",
"body": "Fun part about real randomness: it's entirely possible (though \"unlikely\" is a severe understatement) for you to legitimately get the same number a million times in a row. I'd b... | [
{
"body": "<p>I think that looping until a number within range pops up is a very weird idea. The first potential problem that comes to mind is the possibility that under certain circumstances (say, max = min + 2) you might be looping for a long time. A closer examination of your code shows that such a possibili... | {
"AcceptedAnswerId": "6314",
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-26T04:17:11.553",
"Id": "6304",
"Score": "4",
"Tags": [
"c#",
"random"
],
"Title": "Algorithm to convert random bytes to integers"
} | 6304 |
<p>I've written a simple class that somewhat simulates multiple inheritance using mixins. It allows you to extend the functionality of multiple classes and manage any conflicts between them, should they arise.</p>
<p><strong><code>Mixer</code> class</strong></p>
<pre><code>abstract class Vm_Mixer {
protected $me... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-12T19:11:01.550",
"Id": "137431",
"Score": "0",
"body": "> How can I improve the existing code? Your missing an implementation for `Vm_Mixer_Exception()`. Why were you using a custom `Exception` class? Was there more information than a... | [
{
"body": "<p>It's an interesting approach and it works as expected. My first reaction when reading the code was that you are trying to solve a problem that's commonly solved with the <a href=\"http://en.wikipedia.org/wiki/Composite_pattern\" rel=\"nofollow\">Composite pattern</a>, but then I realised you aren'... | {
"AcceptedAnswerId": "6317",
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-26T09:14:24.053",
"Id": "6306",
"Score": "5",
"Tags": [
"php",
"mixins"
],
"Title": "Multiple inheritance Mixin class"
} | 6306 |
<p>I am writing this Monte Carlo simulation and I am facing this issue when running the code at 10,000,000 iterations. here is the code:</p>
<pre><code>import random as rnd
from time import time
#get user input on number of iterations
numOfIterations = raw_input('Enter the number of iterations: ')
numOfIterations = i... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-28T16:26:05.633",
"Id": "9870",
"Score": "1",
"body": "Maybe switching to a compiled language might help. Not sure how well Python can JIT."
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-11-28T08:18:43.970",
... | [
{
"body": "<pre><code>import random as rnd\n</code></pre>\n\n<p>I dislike abbreviation like this, they make the code harder to read</p>\n\n<pre><code>from time import time\n\n#get user input on number of iterations\nnumOfIterations = raw_input('Enter the number of iterations: ')\nnumOfIterations = int(numOfIter... | {
"AcceptedAnswerId": "6313",
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-26T12:01:57.800",
"Id": "6311",
"Score": "5",
"Tags": [
"python",
"optimization"
],
"Title": "How can I optimize this Monte Carlo simulation running at 10,000,000 iterations?"
} | 6311 |
<p>I need to create a protocol for sending data of various types over a socket connection. so I need to serialise the data to a byte stream. I only need signed and unsigned 32 bit integers, 64 bit integers, string and binary. It is only a start but if anyone can code review this I would really appreciate it.</p>
<p... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-26T22:24:44.997",
"Id": "9805",
"Score": "1",
"body": "look up htonl() and family of functions."
}
] | [
{
"body": "<p>If I were to see a point in converting data types to a message, that could only be to be able to send such messages across the network without having to perform any further interpretation of their internal structure. However, your scheme produces messages that still require interpretation before t... | {
"AcceptedAnswerId": "6328",
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-26T19:19:49.683",
"Id": "6315",
"Score": "5",
"Tags": [
"c"
],
"Title": "Protocol code to encode/decode various data types"
} | 6315 |
<p>This program implements a <a href="http://sourceforge.net/projects/sgrep/" rel="nofollow noreferrer">sorted grep</a>, that is, a specialized version of <code>grep</code> for sorted files. It uses binary search for the lines of a file that begin with a certain string.</p>
<p>You can copy and paste the code in a file... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-26T19:57:29.410",
"Id": "9798",
"Score": "0",
"body": "@Dan I've flagged it for migration. The only thing left now is waiting for someone to see the flag."
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-26T... | [
{
"body": "<p>Use your monads! Your code exhibits the walking-right antipattern. You can avoid it with <code>when</code> and <code>guard</code>. Consider <code>goToBOL</code>. This is how I would write it:</p>\n\n<pre><code>-- Go to beginning of line\ngoToBOL :: Handle -> IO ()\ngoToBOL h = do\n bof &... | {
"AcceptedAnswerId": "6319",
"CommentCount": "4",
"ContentLicense": "CC BY-SA 4.0",
"CreationDate": "2011-11-26T18:24:19.293",
"Id": "6318",
"Score": "6",
"Tags": [
"sorting",
"haskell",
"file",
"search"
],
"Title": "Sorted grep in Haskell"
} | 6318 |
<p><em>Before you say anything: Due to project constraints, we cannot use Boost and we cannot use C++11 (yet;</em> perhaps <em>this will change</em> some <em>day).</em></p>
<p>The fact that I was unable to use smart pointers was nagging on me for some time, so I decided to cough up my own implementation, and it was su... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-26T23:05:46.477",
"Id": "9806",
"Score": "0",
"body": "There should be `reinvent-the-wheel` tag!"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-27T02:23:50.540",
"Id": "9814",
"Score": "2",
"bo... | [
{
"body": "<p>OK. Lets see..</p>\n\n<pre><code>void SharedPtrBase::leave() const {\n left->right = right;\n right->left = left;\n}\n</code></pre>\n\n<p>This removes you from the list, but does not reset your own left and right pointers. This is OK when you call it from the destructor() or join(). But wh... | {
"AcceptedAnswerId": "6321",
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-26T23:05:20.970",
"Id": "6320",
"Score": "1",
"Tags": [
"c++",
"smart-pointers",
"reinventing-the-wheel"
],
"Title": "Shared Pointer implementation"
} | 6320 |
<p>I took a challenge on <a href="http://www.codeeval.com/open_challenges/37/" rel="nofollow">CodeEval</a>. Although the code seems to work for the examples taken from the site, I feel it is not really pretty and must be more complicated than it should be.</p>
<blockquote>
<p><strong>Description:</strong></p>
<... | [] | [
{
"body": "<p>One of Python's greatest strengths is its built-in capability to use sets directly. I don't feel you've used sets to their fullest extent here. I'd also like to point out the <code>with</code> statement, which you should probably use to handle file handles.</p>\n\n<pre><code>from __future__ import... | {
"AcceptedAnswerId": "6324",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 4.0",
"CreationDate": "2011-11-27T02:58:15.993",
"Id": "6323",
"Score": "9",
"Tags": [
"python",
"beginner",
"programming-challenge"
],
"Title": "Pangrams CodeEval challenge"
} | 6323 |
<p>It is advised that such questions be free from criticism such as:</p>
<p><em>Library X already implements this code better; use that instead.</em></p>
<p>However, if it appears that the author is <em>unaware</em> of a better implementation (thus not writing the code for learning/practice), then he/she should be in... | [] | [] | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-27T04:28:21.897",
"Id": "6326",
"Score": "0",
"Tags": null,
"Title": null
} | 6326 |
For when you know you are reinventing the wheel, but are doing it anyways. Questions with this tag involve code that is already fully implemented (such as from a library). | [] | [] | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-27T04:28:21.897",
"Id": "6327",
"Score": "0",
"Tags": null,
"Title": null
} | 6327 |
<p>How could I improve this code?
I used a position interface to avoid code duplication, can it be done better?
Can I reduce the amount of code somehow?</p>
<pre><code>interface PositionInterface
{
double getPosition(PageAreaInterface pArea);
}
private double getMinPosition(Collection<PageAreaInterface> pA... | [] | [
{
"body": "<p>It looks fine. Downvote me, but if there is no more possible comparisons I would write four <em>simple</em> <code>for</code> loops instead of the four inner classes and the interface. I think it would be a little bit more readable and mean less code. </p>\n\n<p>Another possibility is creating four... | {
"AcceptedAnswerId": "6335",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-27T12:02:57.837",
"Id": "6330",
"Score": "3",
"Tags": [
"java"
],
"Title": "Interface for obtaining the bounding box for a collection of elements"
} | 6330 |
<p>I have already made a function of multiplication of long numbers, addition of long numbers, subtraction of long numbers and division of long numbers. But division takes a very long time, how it could be improved? Here is my code:</p>
<pre><code> /// removes unnecessary zeros
vector<int> zero(ve... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-27T14:08:29.067",
"Id": "9827",
"Score": "0",
"body": "If it's production code I would use a 3rd party BigInt/BigNumeric lib. I suppose there are some good, optimized library on the internet."
},
{
"ContentLicense": "CC BY-SA 3... | [
{
"body": "<p>You could try implementing long division.</p>\n\n<p>Example:\n13587643180765 / 153483</p>\n\n<p>1) Find first dividend:</p>\n\n<p><b>1358764</b>3180765 / 153483 #1358764 > 153483</p>\n\n<p>2) Divide it by divisor (e.g by repeated subtraction, like you are doing</p>\n\n<p>1358764 / 153483 = 8</... | {
"AcceptedAnswerId": "6333",
"CommentCount": "5",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-27T12:33:29.630",
"Id": "6331",
"Score": "0",
"Tags": [
"c++"
],
"Title": "very large A divide at a very large B"
} | 6331 |
<p>I've recently discovered the power of sed; regular expressions seem like they'll take a lifetime to master. This tiny script uses both to add a zero before the single digit in file names.</p>
<p>Can this be done more easily without sed? Is the regex sound? </p>
<pre><code>#!/bin/bash
# > addzero.sh <
# Adds... | [] | [
{
"body": "<h2>What's wrong</h2>\n\n<pre><code>for filename in $(ls -1 ./*); do\n</code></pre>\n\n<p>Ouch! As a general rule (with very few exceptions), <a href=\"http://mywiki.wooledge.org/ParsingLs\" rel=\"nofollow noreferrer\"><strong>do not use <code>ls</code> in scripts</strong></a>. What you wrote is almo... | {
"AcceptedAnswerId": "6341",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-27T22:00:44.593",
"Id": "6337",
"Score": "2",
"Tags": [
"regex",
"bash",
"sed"
],
"Title": "Adding a zero to file names"
} | 6337 |
<p>I have the following code for binary search</p>
<pre><code>using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Diagnostics;
namespace BinarySearch
{
public class IntComparer : Comparer<int>
{
public override int Compare(i... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-28T10:26:58.143",
"Id": "9849",
"Score": "0",
"body": "In C# both the System.Generic.List class and the System.Array class already have generic BinarySearch() methods which work with Comparer delegates, or with objects implementing ICo... | [
{
"body": "<p>The best way to implement a public generic method for binary search is by invoking the <code>BinarySearch()</code> method of <code>System.Array</code>. (Better yet, don't implement such a method at all, and call <code>System.Array.BinarySearch()</code> directly.)</p>\n",
"comments": [],
"m... | {
"AcceptedAnswerId": null,
"CommentCount": "4",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-27T22:34:34.250",
"Id": "6338",
"Score": "2",
"Tags": [
"c#",
"algorithm",
"library",
"binary-search"
],
"Title": "Generic binary search"
} | 6338 |
<h2>Regular and Irregular Expressions</h2>
<p><a href="https://en.wikipedia.org/wiki/Regular_expression" rel="nofollow noreferrer">Regular expressions</a> are a powerful form of <a href="https://en.wikipedia.org/wiki/Declarative_programming" rel="nofollow noreferrer">declarative programming language</a>, mainly used f... | [] | [] | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 4.0",
"CreationDate": "2011-11-28T01:57:06.430",
"Id": "6344",
"Score": "0",
"Tags": null,
"Title": null
} | 6344 |
Regular expressions are a declarative language, mainly used for pattern matching within strings. Please include a tag specifying the programming language you are using, together with this tag. | [] | [] | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-28T01:57:06.430",
"Id": "6345",
"Score": "0",
"Tags": null,
"Title": null
} | 6345 |
<p>I am sure you can find many weak spots, please let me know about them.
My main question is if this is a sensible approach to separate the construction phase from the constructed phase. </p>
<p>Basically I have a set of objects that needs to be transformed into similar (but different) objects. Once transformed I wan... | [] | [
{
"body": "<p>First of all, you are right, what you have there is not a builder class, but more like a factory class. Which is roughly equivalent to a static factory method.</p>\n\n<p>Now, the question of using constructors vs static factory methods is one over which there has been a lot of talk, and for the mo... | {
"AcceptedAnswerId": "6349",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-28T02:06:05.083",
"Id": "6346",
"Score": "2",
"Tags": [
"java"
],
"Title": "Separate construction phase from constructed phase"
} | 6346 |
<p>I'm trying to figure out what is best and fastest implementation of a browser-like find and highlight function inside a website using JavaScript.
This function is for <strong>one</strong> HTML element <strong>without</strong> any children. Of course it can be expanded to elements with children with some simple loops... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-28T05:39:56.857",
"Id": "9844",
"Score": "0",
"body": "Is there a point in getting and lowercasing words on each call to `find()`? Why not just do that once on `onload`? My javascript-fu is pretty low, so even if it's something obvious... | [
{
"body": "<p>Let's start by optimising your code. Simply said, loops are the danger here.</p>\n\n<ol>\n<li>A <code>while</code> loop can be quicker than a <code>for</code> loop, especially in cases like this. Get the length once.</li>\n<li>Avoid looking into arrays. Do <code>word = words[i]</code> once, whic... | {
"AcceptedAnswerId": null,
"CommentCount": "6",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-28T04:29:32.010",
"Id": "6347",
"Score": "3",
"Tags": [
"javascript"
],
"Title": "Better \"find and highlight\" implementation in HTML element"
} | 6347 |
<p>I've just finished my solution to the <a href="http://en.wikipedia.org/wiki/Dining_philosophers_problem" rel="nofollow">Dining Philosopher's Problem</a>, but I am not confident with my code because I am still newbie to the concurrency world. I would appreciate it if you could leave me some feedback. </p>
<p>Here is... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-28T13:04:40.367",
"Id": "9852",
"Score": "5",
"body": "Why does Philosopher extend Thread?"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-28T14:47:05.387",
"Id": "9855",
"Score": "0",
"body": "... | [
{
"body": "<p>Just a quick note:</p>\n\n<pre><code>synchronized(_leftChopstick){\n while(_leftChopstick.isUsed() || _rightChopstick.isUsed()) \n</code></pre>\n\n<p>Here you should synchronize on <code>_rightChopstick</code> too since <code>isUsed</code> could be called from other threads concurrently.</p... | {
"AcceptedAnswerId": null,
"CommentCount": "7",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-28T06:30:14.820",
"Id": "6348",
"Score": "10",
"Tags": [
"java",
"multithreading",
"dining-philosophers"
],
"Title": "Dining Philosophers"
} | 6348 |
<p>I'm new to Haskell and functional programming and I did for training purpose a basic program where you can:</p>
<ul>
<li>create a directory (consisting of a name and an array of sub-directories)</li>
<li>create an entry (consisting for a title and a content)</li>
<li>remove and entry or a directory</li>
<li>view an... | [] | [
{
"body": "<p>Here are a few things that could be done in your code.</p>\n\n<pre><code>{-# LANGUAGE TemplateHaskell, ViewPatterns #-}\n</code></pre>\n\n<p>I use template haskell to derive data lenses, which make your directory access a little more succinct. I also use ViewPatterns so that the dispatch on string... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-28T10:18:18.930",
"Id": "6351",
"Score": "3",
"Tags": [
"haskell"
],
"Title": "Basic Directory / 'Text entry' create / delete / traversal (bash inspired) in Haskell"
} | 6351 |
<p>I've put together this extremely simple text diff function a while ago:</p>
<pre><code>function diff($old, $new) {
$old = trim($old);
$new = trim($new);
if($old == $new) {
return $new;
}
$old = explode(" ", $old);
$new = explode(" ", $new);
$result = ""; ... | [] | [
{
"body": "<p>It looks fine. A few notes:</p>\n\n<p>1, Use <a href=\"http://php.net/manual/en/function.max.php\" rel=\"nofollow\"><code>max</code></a> instead of </p>\n\n<pre><code>$length = count($old) > count($new) ? count($old) : count($new);\n</code></pre>\n\n<p>2, Consider testing with newlines, tabs an... | {
"AcceptedAnswerId": "6357",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-28T11:02:24.807",
"Id": "6352",
"Score": "3",
"Tags": [
"php"
],
"Title": "Simple text diff function"
} | 6352 |
<p>A small greedy algorithm to position boxes in a grid.</p>
<p>Version A</p>
<pre><code>int lCurrentIndex = 0;
BoxInchesInterface lPreviousBbox = lSortedBboxes.iterator().next();
double lEndPositionForCurrentIndex = pDimension.getEnd(lPreviousBbox);
for (BoxInchesInterface lBbox : lSortedBboxes)
{
boolean lIsF... | [] | [
{
"body": "<p>Version A is definitely better, as it employs a more familiar pattern to get the job done, and it makes use of bit of trivial code (<code>boolean lIsFirstIteration = lBbox == lPreviousBbox</code>) to save you from duplication of non-trivial code. (<code>lIndexFromBbox.put(lPreviousBbox, lCurrentIn... | {
"AcceptedAnswerId": "6356",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-28T11:41:03.967",
"Id": "6354",
"Score": "5",
"Tags": [
"java"
],
"Title": "Positioning boxes in a grid"
} | 6354 |
<p>I'm struggling for a while now with the readability of my code, I after I tried to get as much insight as possible (for my standards). On my level, I think I understand and use it <em>all right</em> for my level. </p>
<p>But I'm still having big chunks of mixed html/css in the presentation. Often I have a mediocre ... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-28T11:11:48.623",
"Id": "62839",
"Score": "0",
"body": "I would suggest using a PHP framework such as [cakePHP](http://www.cakephp.org). These frameworks use an MVC (model View Controller) architecture which will help you separate your... | [
{
"body": "<p>Thank you for the perfect example of the <strong>real-life</strong> piece of presentation logic.<br>\nMost people pushing some primitive templating solutions just have no idea of such a complex cases existence. </p>\n\n<p>Three rules for you to get it right:</p>\n\n<ol>\n<li>Use PHP as a template ... | {
"AcceptedAnswerId": "6361",
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-28T10:50:55.057",
"Id": "6359",
"Score": "38",
"Tags": [
"php",
"html",
"datetime",
"layout"
],
"Title": "Displaying courses in an HTML calendar"
} | 6359 |
<p>Please review my JavaScript code below. I want to colspan and rowspan automatically if adjacent cells have sames value. For example like this.</p>
<pre><code><table id="BeforeTable" border="1">
<tr><td>A</td><td>B</td><td>B</td><td>C</td><td>D<... | [] | [
{
"body": "<ol>\n<li>I don't see a reason why these functions should be in the global scope. The easiest fix for that is to put everything inside your function called with <code>$(document).ready</code>.</li>\n<li><p><code>isEqualToNextRightCell</code>: Use the identity operation for comparison unless you're ab... | {
"AcceptedAnswerId": "6375",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-28T15:59:40.890",
"Id": "6363",
"Score": "3",
"Tags": [
"javascript"
],
"Title": "Spanning Table Cells automatically between same value cells"
} | 6363 |
<p>For trivial reasons, I decided to have a go at differentiating dates. Low and behold having no idea how much of a non-trivial task it was to become.</p>
<p>It was <em>originally a small</em> sidetrack from a project I'm doing.</p>
<p>And, whilst performance isn't a huge concern here, the code I've posted below per... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-28T21:17:18.193",
"Id": "9878",
"Score": "0",
"body": "Did you get `is_leap_year()` correct? Remember you need to check for 100 and 400 year boundaries."
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-28T21... | [
{
"body": "<p>I think if i was doing it, I'd try to structure it more like the standard code: turn each Y/M/D/H/m/S into seconds since some epoch, then use fairly straightforward subtraction to compute the difference.</p>\n\n<pre><code>unsigned calculate_seconds_between2(unsigned Y1, unsigned M1, unsigned D1, u... | {
"AcceptedAnswerId": "6367",
"CommentCount": "3",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-28T16:19:24.413",
"Id": "6364",
"Score": "6",
"Tags": [
"c++",
"optimization",
"c",
"algorithm"
],
"Title": "Date Time - Seconds Difference"
} | 6364 |
<p>I've written a table of contents parser for a FOSS wiki project I maintain in my spare time. The class takes a HTML string, injects anchor takes before each H1,H2 etc. and then generates the contents for the headers.</p>
<p>My main concern is boundary checks within the code. I haven't got any unit tests for it as i... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-29T16:35:36.293",
"Id": "9914",
"Score": "0",
"body": "The comment beginning `Try parsing all H2 headers` is at best misleading. A more useful comment there would say why `h2` is parsed iff no `h1` was found."
}
] | [
{
"body": "<p>I'd be weary of solutions that builds the HTML by hand. Work with the DOM and let it write out the HTML for you. HAP can do that for you.</p>\n\n<p>Don't create one-time use extension methods. It appears you created an extension method for strings to encode your titles. If you can't use it any... | {
"AcceptedAnswerId": null,
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-28T18:34:43.120",
"Id": "6369",
"Score": "1",
"Tags": [
"c#",
"html"
],
"Title": "HTML Table of contents parser"
} | 6369 |
<p>I am looking for feedback on coding style. I know this function already exists, and I would never use my version over a well tested one. I am just practicing.</p>
<pre><code>double Clib_atof(char s[])
{
double val, power, rtn;
int i, sign;
for (i = 0; isspace(s[i]); i++);
sign = (s[i] == '-') ? -1 :... | [] | [
{
"body": "<pre><code>double Clib_atof(char s[])\n{\n double val, power, rtn;\n int i, sign;\n for (i = 0; isspace(s[i]); i++);\n</code></pre>\n\n<p>Looks too much like the <code>;</code> was an accident. I suggest using <code>{/\\*empty\\*/}</code> instead to make it clear you did it on purpose. Or I ... | {
"AcceptedAnswerId": "6371",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-28T18:43:26.773",
"Id": "6370",
"Score": "5",
"Tags": [
"c",
"converting",
"reinventing-the-wheel",
"floating-point"
],
"Title": "Implementation of the atof() function"
} | 6370 |
<p>I'm using the below code to try and make it easy to do sensible calculations for different variables, i.e. pressure + height is nonsensical, and pressure in atm += pressure in bar should add taking units into account. I've tried playing round with dynamic and static cast as well as CRTP on the advice of other fine m... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-29T08:33:24.323",
"Id": "9894",
"Score": "0",
"body": "Last night I thought that perhaps flattening this out by getting rid of the Pressure class and instead making a friend Pressure function which calls the Variable constructor with a... | [
{
"body": "<p>A first look at the Variable base class:</p>\n\n<p>I assume this must always exist!<br>\nThus you should make it a reference (Unless there is some way to change this dynamically. Given the current interface I can's see a way to change it).</p>\n\n<pre><code>const std::map<std::string, short>... | {
"AcceptedAnswerId": "6378",
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-29T00:48:29.347",
"Id": "6373",
"Score": "3",
"Tags": [
"c++"
],
"Title": "Performing calculations for different variables"
} | 6373 |
<p>I've not posted binding specific messages to the gridview part. This shows updating that particular message or message from a list of messages databound with an XML file. My main concern is omitting redundant code in the <code>else</code> part. Any suggestions?</p>
<pre><code>protected void grdMessage_RowUpdating(o... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-29T13:45:46.930",
"Id": "9907",
"Score": "1",
"body": "Telling us what the code does is a code start."
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-29T16:48:57.450",
"Id": "9916",
"Score": "0",
... | [
{
"body": "<p>Not sure if it's \"optimized\", but here's something of a simplification:</p>\n\n<pre><code> protected void grdMessage_RowUpdating(object sender, GridViewUpdateEventArgs e)\n {\n string messageIdString = Request[\"MessageID\"];\n int messageId = messageIdString == null ? 0 : Co... | {
"AcceptedAnswerId": "6382",
"CommentCount": "3",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-29T13:04:43.017",
"Id": "6379",
"Score": "1",
"Tags": [
"c#"
],
"Title": "Updating messages from a list of messages"
} | 6379 |
<p>I tried creating a quick tooltip plugin using the jquery and jquery ui position. Is the way I have used the enclosure are right and is the use of position right since in ff it seem to have some memory problem..it remembers the previous position when I refresh the page after the first time. below is the code </p>
<p... | [] | [
{
"body": "<p>Try this type of closure</p>\n\n<pre><code> $this.add(tip).hover(function() {\n return function() {\n var timeoutId = $this.data('timeoutId');\n if(timeoutId) {\n clearTimeout(timeoutId);\n }\n tip.fadeIn(\"slow\");\n };\n... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-29T15:05:12.370",
"Id": "6383",
"Score": "2",
"Tags": [
"javascript",
"jquery",
"jquery-ui"
],
"Title": "can we use of $this in closure for jquery plugin"
} | 6383 |
<p>I am new to OOP and have written a products class. All is working fine but I am unsure which of the below version of a method within this class is best?</p>
<p>The first gets the variables from within the object and the second passes the variables into the class. Both work. I originally had it as the first version ... | [] | [
{
"body": "<p>Which is the better should not depend on <em>mainly</em> speed. I think you should post more code (at least the name of class and its responsibilities or the list of its functions).</p>\n\n<p>Some other notes:</p>\n\n<ol>\n<li><p>It's really hard to read code which contains one character long vari... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-29T15:21:14.670",
"Id": "6384",
"Score": "3",
"Tags": [
"php",
"object-oriented"
],
"Title": "OOP php method - pass parameters in or get from inside object"
} | 6384 |
<p>I made a small command line tool to help my Italian studies. Any thoughts on how it could be improved? I can't think of much but would like to be proved wrong.</p>
<pre><code>import random
class RegularVerb(object):
def __init__(self, name):
self._name = name
def name(self):
return self._n... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-29T16:04:28.020",
"Id": "9909",
"Score": "1",
"body": "hmm now after posting.. I see that the class RegularCondizionaleSemplice is actually useless and could easily be refactored into each type of verb."
}
] | [
{
"body": "<p>Maybe this could provide of a more neat way of relating verbs with veb_forms:</p>\n\n<pre><code>verb_binder = dict(are=AreVerb, ire=IreVerb, ere=EreVerb)\n\ndef build_regular_verbs(verbs, verb_forms):\n def make(verb):\n try:\n return verb_binder[verb[-3:]](verb, verb_forms)\n... | {
"AcceptedAnswerId": "6388",
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-29T15:59:44.127",
"Id": "6385",
"Score": "7",
"Tags": [
"python"
],
"Title": "Command line tool for Italian language studies"
} | 6385 |
<p>I'm very new to python and scripting in general. I've come up with the following function to find the first and last IP when an IP range string is inputted. I want to only use base Python (and not download any libraries essentially).</p>
<p>Example:</p>
<pre><code>genRange("1.2.3.*"):
Start IP = 1.2.3.0
End IP = 1... | [] | [
{
"body": "<p>The first thing that jumps out at me is that this is a single function, with no classes and only one possible use. But without context, I couldn't comment on whether or not that's good. Since the snippet is so short and limited, your only motivation for changing it is that you might want to use it... | {
"AcceptedAnswerId": "6387",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-29T18:02:09.077",
"Id": "6386",
"Score": "2",
"Tags": [
"python",
"strings"
],
"Title": "Finding the first and last IP in an IP range string"
} | 6386 |
<p>I am refactoring code which contains many lines of <code>NSString* image = [@"ipad_" stringByAppendingString:imageOrigName];</code> and wondered which is more optimized:</p>
<pre><code>stringByAppendingString:
</code></pre>
<p>or</p>
<pre><code>stringWithFormat:
</code></pre>
<p>In the first, you take one <code>... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-29T20:43:13.717",
"Id": "9924",
"Score": "2",
"body": "Have you tried profiling them? Chances are the difference isn't enough to notice. That said, this seems to be about improving working code, so it'd be better on codereview.SE."
... | [
{
"body": "<p>The only way to know is to measure it.</p>\n\n<p>And, of course, the only reason to measure it is if it actually matters; if you have a real world performance issue that can be tracked to this code.</p>\n\n<p>If not, don't worry about it.</p>\n\n<p>However, there are a few issues to consider:</p>... | {
"AcceptedAnswerId": "6390",
"CommentCount": "3",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-29T20:35:57.567",
"Id": "6389",
"Score": "4",
"Tags": [
"objective-c"
],
"Title": "stringByAppendingString: vs. stringWithFormat:"
} | 6389 |
<p>The basic idea is to replace all special expressions, e.g., <code>[%InvoiceNo%]</code>, <code>[%DueDate%]</code>, in a string with the properties of an object, <code>Invoice invoice</code>.</p>
<pre><code>string str = "Your invoice [%InvoiceNo%] will be due on [%DueDate%]";
Invoice invoice = new Invoice { InvoiceNo... | [] | [
{
"body": "<p>You can do it in one loop instead of getting all the indexes separately. Just off the top of my head:</p>\n\n<pre><code> string str = \"Your invoice [%InvoiceNo%] will be due on [%DueDate%]\";\n\n int startIndex = 0;\n\n while ((startIndex = str.IndexOf(\"[%\", startIndex)) &g... | {
"AcceptedAnswerId": "6398",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-30T03:42:22.040",
"Id": "6397",
"Score": "1",
"Tags": [
"c#",
".net",
"reflection"
],
"Title": "Replace all occurrences with the properties of an object"
} | 6397 |
<p>I recently started learning binary tree. Following is the code for a typical node for a tree (no code review required for that):</p>
<pre><code>typedef unsigned int uint;
template<typename T>
class BinaryNode
{
T t_Data;
BinaryNode *pt_Left, *pt_Right;
public:
BinaryNode (const T &data) : t_Data(d... | [] | [
{
"body": "<p>Did you mean to make this private?</p>\n\n<pre><code>class BinaryNode\n{\npublic:\n T t_Data;\n BinaryNode *pt_Left, *pt_Right;\n</code></pre>\n\n<p>I'm not sure I agree with the interface design presented for this data structure. If you're doing this as just a practice exercise I suppose it's p... | {
"AcceptedAnswerId": "6434",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-30T06:44:49.640",
"Id": "6399",
"Score": "4",
"Tags": [
"c++",
"algorithm",
"binary-search"
],
"Title": "Binary tree traversal algorithms"
} | 6399 |
<p>Here's my attempt to replace first occurrence of <code>pattern</code> in file with spaces. Effectively deleting it, and hopefully will allow me to "delete" it in a large file, without rewriting it.</p>
<pre><code>#define MAX_LINE_LENGTH
int removeFirstOccurenceInFile(const char* fileName, const char* pattern) {
F... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-30T18:44:30.067",
"Id": "9954",
"Score": "0",
"body": "Did you test this code before posting it here?\nDid you test it with an input file consisting of multiple lines with the sought-for string somewhere in the middle?"
},
{
"C... | [
{
"body": "<p>My advice would be to abandon the idea of treating the file as a text file in order to search through it, but then as a binary file in order to write into it. Your calculation of the offset to seek to is flimsy, and guaranteed not to work if the file has non-single-character end-of-line markers, (... | {
"AcceptedAnswerId": null,
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-30T09:51:22.077",
"Id": "6400",
"Score": "2",
"Tags": [
"c",
"file",
"error-handling",
"file-structure"
],
"Title": "Replace first occurrence of pattern in file"
} | 6400 |
<p>I would like tips on my CODE, map structure (How I should seperate the code, more files, etc). What I can do about the func.php file as a file with just a bunch of functions, doesn't seem to be the best approach for it.
And generally just a code review how I can improve on it.</p>
<p>createaccount.php</p>
<pre><co... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-30T10:54:56.233",
"Id": "9938",
"Score": "0",
"body": "Too much code without context. Tell us what you're trying to do..."
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-30T11:02:28.403",
"Id": "9940",
... | [
{
"body": "<p>There seems to be a fair amount of paranoia in your code: </p>\n\n<pre><code>function salt() {\n mt_srand(microtime(true)*100000 + memory_get_usage(true));\n return md5(uniqid(mt_rand(), true));\n}\n</code></pre>\n\n<p>here <code>mt_srand(microtime(true)*100000 + memory_get_usage(true));</co... | {
"AcceptedAnswerId": "6407",
"CommentCount": "5",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-30T09:52:52.293",
"Id": "6401",
"Score": "1",
"Tags": [
"php"
],
"Title": "Review on my current structure and tips on improvement"
} | 6401 |
<p>I have inherited an application which uses LINQ-to-Entities for ORM and don't have a great deal of exposure to this.</p>
<p>There are many examples like this throughout the application. Could you tell me where to start in refactoring this code?</p>
<p>I appreciate that it's a large chunk of code, however the quer... | [] | [
{
"body": "<p>You're right... this is a large chunk of code that is a little difficult to digest. With that said, several things come to mind.</p>\n\n<ul>\n<li><p>First off, this method is way too large. This is not a style issue perse... In this case, it's so large, it will not be JIT Optimized because the res... | {
"AcceptedAnswerId": "6425",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-30T10:59:13.683",
"Id": "6403",
"Score": "1",
"Tags": [
"c#",
"linq",
"entity-framework",
"asp.net-mvc-3"
],
"Title": "Optimising multi-part LINQ to Entities query"
} | 6403 |
<p>To illustrate how to do leader election with Apache Zookeeper I've created an straightforward application and I would like to have to threading part of the application reviewed.
I've created a simple class (<code>Speaker</code>) which outputs an unique identifier to a file on a regular interval. I've implemented the... | [] | [
{
"body": "<p>you could also look at using a fixed thread pool. That way, if one thread becomes blocked for some reason, say the disk became full, the threads would wait in a queue until the blocked thread terminated. That way you wouldn't loose any unique id's or run into memory problems if too many threads be... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-30T11:02:41.733",
"Id": "6404",
"Score": "3",
"Tags": [
"java",
"multithreading"
],
"Title": "Switching functionality on and off in a Scheduled Thread"
} | 6404 |
<p>What do you think about this core EF method ?</p>
<p>Also, Notice that it returns IQueryable: </p>
<p>Could that be a performance danger when extended improperly ? How ?</p>
<p>Please accept the fact that <code>Includes</code> and <code>OrderBy</code> parameters are <code>string</code> type.</p>
<pre><code>publi... | [] | [
{
"body": "<p>I'm not sure how this code works (I had to invent <code>GetPrimaryKeyName</code>) and I can't find a <code>IQueryable<T></code> that has an <code>OrderBy</code> that takes a string parameter. So faking it as much as possible, I came up with this:</p>\n\n<pre><code> public IQueryable<T... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-30T13:32:53.370",
"Id": "6410",
"Score": "1",
"Tags": [
"c#",
"entity-framework"
],
"Title": "Review on core EF repository method"
} | 6410 |
<p>The <a href="http://www.microsoft.com/net/" rel="nofollow noreferrer">.NET Framework</a> is not specific to any one programming language. The <a href="/questions/tagged/c%23" class="post-tag" title="show questions tagged 'c#'" rel="tag">c#</a>, <a href="/questions/tagged/vb.net" class="post-tag" title="show ... | [] | [] | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 4.0",
"CreationDate": "2011-11-30T13:52:27.087",
"Id": "6412",
"Score": "0",
"Tags": null,
"Title": null
} | 6412 |
.NET is a software framework supporting a multi-language paradigm and supporting language interoperability. .NET applications are executed in a virtual machine (CLR) which provides a few important services: security, memory management and exception handling. | [] | [] | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-30T13:52:27.087",
"Id": "6413",
"Score": "0",
"Tags": null,
"Title": null
} | 6413 |
<pre><code>class User {
protected $DBH;
protected $STH;
public function __construct($DBH) {
$this->DBH = $DBH;
}
public function logged_in() {
if (isset($_SESSION['userid']) && isset($_SESSION['hash'])) {
$hash = sha1($_SESSION['userid'] . $_SERVER['HTTP_USE... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-12-01T06:57:05.617",
"Id": "9997",
"Score": "1",
"body": "It's quite impressive that you went from [this](http://codereview.stackexchange.com/questions/6401/review-on-my-current-structure-and-tips-on-improvement) to that so quickly. @vstm... | [
{
"body": "<ul>\n<li><p>Regarding try/catch: this depends on whether your user class <strong>can handle errors</strong> or not.</p>\n\n<ul>\n<li><p>An exception you can't handle is for example if the SQL-statement has a syntax error or the SQL-server being down. You can't do anything about it programatically du... | {
"AcceptedAnswerId": "6421",
"CommentCount": "3",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-30T16:06:20.617",
"Id": "6415",
"Score": "1",
"Tags": [
"php"
],
"Title": "OO Auth lib - On the right track?"
} | 6415 |
<p>I started a hobby project which would be the first thing I do in C that is intended to be seen by someone else. I've been practicing for a while but I'm actually a beginner; I never wrote real software in C/C++ yet.</p>
<p>I wanted to know what is OK from my code, what is wrong and why, if it's clear/readable, and... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-30T19:01:43.917",
"Id": "9956",
"Score": "3",
"body": "c++ or C? Pick one."
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-30T20:52:12.613",
"Id": "9966",
"Score": "0",
"body": "I'd rather C tha... | [
{
"body": "<p><strong>Design</strong></p>\n\n<p>It looks like you have a design in mind, but it isn't all visible from the code posted.\nI'm anyway a little hazy on what you're really trying to model here, so let's step through it.</p>\n\n<ol>\n<li>3D array of voxel instances: this is your actual world state, g... | {
"AcceptedAnswerId": "6472",
"CommentCount": "5",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-30T18:21:05.133",
"Id": "6419",
"Score": "6",
"Tags": [
"c",
"game"
],
"Title": "Game with voxels"
} | 6419 |
<p>I'm currently in the process of creating a online work schedule for internal use. For this, the client needs to show the days of a single week.</p>
<p>I pass a week number and a year.
From that I get the first day of that week, by using <code>strtotime()</code> passing an argument in the format <code>[year]-W[weekn... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2012-03-17T10:12:42.040",
"Id": "16078",
"Score": "3",
"body": "You can find a solution to a similar problem at http://stackoverflow.com/questions/186431/calculating-days-of-week-given-a-week-number"
},
{
"ContentLicense": "CC BY-SA 3.... | [
{
"body": "<p>I'm not 100% sure what the rest of your code is doing, but here is the code to get the days of a specified week using date:</p>\n\n<pre><code><?php \n// Set current date\n$date = '06/29/2014';\n// Parse into a Unix timestamp\n$ts = strtotime($date);\n// Find the year and the current week\n$year... | {
"AcceptedAnswerId": null,
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-30T20:11:19.813",
"Id": "6422",
"Score": "20",
"Tags": [
"php",
"html",
"datetime"
],
"Title": "Show days of single week"
} | 6422 |
<p>I usually spent more time on refactoring than in developing itself. But this little piece of code is really hard to refactor. I'd like to keep it as short as possible, but it's quite undarstandable:</p>
<pre><code>foreach($all_headers as $uid => $h) // Intera gli headers
{
// Is the header valid?
$isInb... | [] | [
{
"body": "<p>This won't make it shorter, but you might consider changing the condition checking to only do the actual check if needed instead of doing all of them at once, making you loose the advantage of the short-circuiting <code>||</code> operator.</p>\n\n<p>E.g.</p>\n\n<pre><code>if (!isInbox()) continue;... | {
"AcceptedAnswerId": "6429",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2011-11-30T20:15:11.727",
"Id": "6423",
"Score": "1",
"Tags": [
"php",
"php5"
],
"Title": "Refactoring a loop with many boolean conditions"
} | 6423 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.