body stringlengths 25 86.7k | comments list | answers list | meta_data dict | question_id stringlengths 1 6 |
|---|---|---|---|---|
<p>Oftentimes people will shorten <code>System.out</code> to just <code>out</code> and make an analog called <code>in</code> (e.g. <code>Scanner in = new Scanner(System.in);</code>). I've seen two methods of doing this</p>
<h2>Method 1:</h2>
<p><code>import static java.lang.System.out;</code></p>
<h2>Method 2:</h2>
<p>... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-17T05:46:34.000",
"Id": "33064",
"Score": "0",
"body": "Consider the scopes of the name `out` in each case. Method one is throughout the file and cannot be easily overridden (doing so will only add to the confusion). Method two restri... | [
{
"body": "<p>With the second approach you hide that <code>out</code> refers to <code>System.out</code>, so if anytime later you want to redirect the output, you need to change only <code>static final PrintStream out = System.out;</code> since the rest of the code is only aware of the fact that it is writing to... | {
"AcceptedAnswerId": "20616",
"CommentCount": "4",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-17T03:37:04.000",
"Id": "20615",
"Score": "4",
"Tags": [
"java"
],
"Title": "Shortening System.out - which is better?"
} | 20615 |
<p>I am designing a class to store primitive types into byte buffer using predefined byte order. I am not going to use Boost.Serialization because I am working with plain types only and I need predefined binary structure without versions and things like that.</p>
<p>Here is draft of the design:</p>
<pre><code>#ifndef... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-17T15:09:22.337",
"Id": "33091",
"Score": "0",
"body": "Why not use (the relatively standard) method of using htonl() and family (unfortunately for you it is network byte order (or big endian)). It is know to work and has optimal effic... | [
{
"body": "<p>It just seems unnecessarily complicated. You're not doing anything to ensure binary compatibility between platforms so why not just write out the memory? Take a pointer to your data, cast it to <code>char*</code> and then read/write <code>sizeof(ValueType)</code> bytes from/to the file. There's no... | {
"AcceptedAnswerId": null,
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-17T08:09:52.347",
"Id": "20621",
"Score": "3",
"Tags": [
"c++",
"template"
],
"Title": "Plain type serializer design"
} | 20621 |
<p>I have this code in <code>LogInService</code></p>
<pre><code>public User isValid(User user) {
if(user == null)
return user;
User db_user = userDao.getUserByUsername(user.getUsername());
if (db_user != null) {
try {
if (PasswordHash.validatePassword(us... | [] | [
{
"body": "<p>You could use a <strong>common interface</strong> for <em>User</em> and <em>Admin</em> and a <strong>factory</strong>/service for the <em>getByUserName</em> method. So you have only a switch in the factory, and all the other duplication is gone.</p>\n\n<p>Btw. I thinks it is a better practice to r... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-17T08:27:43.207",
"Id": "20622",
"Score": "1",
"Tags": [
"java",
"design-patterns",
"hibernate",
"jpa"
],
"Title": "Repetitive code for different Database Entities with same impl... | 20622 |
<p>I'm building a small web application and it's starting to get a bit complex. I have reached to a point where I have to run some tests and load some libraries.</p>
<p>I made it so I can use it in this way:</p>
<pre><code>this.loadDependencies([
{
test : tests.JSON,
polyfill : self.polyfills.json... | [] | [
{
"body": "<p>One of the javascript code conventions I like to follow is to start each function with declaring all variables used in the function. Even the ones used inside For loops. The scope of these is the function body, declaring variables like this just clarify scope.</p>\n\n<p>Your code is fairly readabl... | {
"AcceptedAnswerId": "21160",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-17T08:29:17.743",
"Id": "20623",
"Score": "2",
"Tags": [
"javascript",
"object-oriented",
"jquery"
],
"Title": "Small web application"
} | 20623 |
<p>Cyclic Words</p>
<blockquote>
<p>Problem Statement</p>
<p>We can think of a cyclic word as a word written in a circle. To
represent a cyclic word, we choose an arbitrary starting position and
read the characters in clockwise order. So, "picture" and "turepic"
are representations for the same cyclic wor... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-21T17:59:10.637",
"Id": "33289",
"Score": "1",
"body": "\"efficient\" depends on some requirements and/or further clarification. For readability and form, this solution will work. A simple and easy understandable solution is always bet... | [
{
"body": "<p>There's one trick which you can use to get a reduction in memory usage which is very worthwhile if the strings are long. <code>String.substring</code> returns a new <code>String</code> object but wrapping the same underlying <code>char[]</code> as the original. So your code:</p>\n\n<pre><code> ... | {
"AcceptedAnswerId": "20627",
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-17T08:34:05.400",
"Id": "20624",
"Score": "2",
"Tags": [
"java",
"optimization"
],
"Title": "Count the number of cyclic words in an input"
} | 20624 |
<p>I'm trying to improve some code in order to get a better perfomance, I have to do a lot of pattern matching for little tags on medium large strings, for example:</p>
<pre><code>import re
STR = "0001x10x11716506872xdc23654&xd01371600031832xx10xID=000110011001010\n"
def getID(string):
result = re.search('.*I... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-17T15:17:23.493",
"Id": "33093",
"Score": "5",
"body": "Is this the actual code that you are trying to improve the performance of? As it stands, `print` will be more expensive then anything else and relative to that no other change wil... | [
{
"body": "<p>If you name your function <code>getSomething()</code>, I expect it to return a value with no side effects. I do not expect it to print anything. Therefore, your function should either return the found string, or <code>None</code> if it is not not found.</p>\n\n<p>Your original and revised code l... | {
"AcceptedAnswerId": null,
"CommentCount": "4",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-17T13:42:52.693",
"Id": "20632",
"Score": "1",
"Tags": [
"python",
"regex"
],
"Title": "Python pattern searching using re standard lib, str.find()"
} | 20632 |
<p>I'm just beginning my foray into iOS development and need a code review. I have a custom <code>NSManagedObject</code> that needs some smart setters. I'm new to Objective-C, iOS, and core-data development, so I don't trust myself yet. I come from a Java background, so smart setters are what I know, but might not b... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-17T16:41:33.413",
"Id": "33103",
"Score": "0",
"body": "what do u mean with \"Core Data class\"? is this a subclass of NSManagedObject?"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-17T16:46:59.200",
... | [
{
"body": "<p>without referring to your code, I think there is one pattern, that makes core data more fun but sadly isnt supported by Xcode out-of-the-box:</p>\n\n<p>Have two classes for each model: one for automatic code generation — it will be jsut changed by Xcode — and on that inherits from the first, that ... | {
"AcceptedAnswerId": null,
"CommentCount": "4",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-17T14:21:45.143",
"Id": "20634",
"Score": "3",
"Tags": [
"beginner",
"objective-c",
"ios",
"core-data"
],
"Title": "NSManagedObject in need of smart setters"
} | 20634 |
<p>I have the following code:</p>
<pre><code> Integer cr = 3;
String y = "\"\r\n\"";
for (Integer i = 0; i < cr; i++)
{
for (Integer j = 0; j < cr; j++)
{
for (Integer k = 0; k < cr; k++)
{
for (Integer l = 0; l < cr; l++)
... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-16T02:02:05.143",
"Id": "33086",
"Score": "5",
"body": "What makes you think its inefficient?"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-16T02:02:55.800",
"Id": "33087",
"Score": "2",
"body... | [
{
"body": "<p>Presumably you want to avoid the 8 levels of nested loops? (This isn't an efficiency thing, merely a code cleanliness issue.)</p>\n\n<p>If so, use a length-8 array of integers, and use it to do \"counting\" in a while loop.</p>\n\n<pre><code>int[] cnt = new int[8];\n\nwhile (1) {\n\n // ... Do... | {
"AcceptedAnswerId": "20638",
"CommentCount": "3",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-16T01:59:28.953",
"Id": "20635",
"Score": "2",
"Tags": [
"java",
"android"
],
"Title": "Calculating the number 8 chars long which contains all possible numbers containing the numbers ... | 20635 |
<p>I posted a question few weeks back, on making a PHP Login Script. Most of you guys told me not to use <code>global</code> variables and especially for something like MySQLi connection object as it may be insecure. I was also advised to switch to classes. I did so. And here is my code -</p>
<pre><code>class Page {
... | [] | [
{
"body": "<p>No. The <strong>visibility</strong> has little to do with security with that respect. (<a href=\"http://php.net/manual/en/language.oop5.visibility.php\" rel=\"nofollow\">PHP Document on Visibility</a>)</p>\n\n<p>In short:</p>\n\n<ul>\n<li>Public: Accessible by anything, within the object or outsid... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-17T15:27:24.453",
"Id": "20640",
"Score": "-1",
"Tags": [
"php",
"classes",
"mysqli",
"scope"
],
"Title": "Is declaring a property as `public` insecure?"
} | 20640 |
<p>I don't want to extend the functionality of this method to include say library objects. I'm just looking for feedback on what it currently does.</p>
<p>ES5, section 8.6.2 "exposes" these global objects. I added a few Browser or "Host" objects which I needed for my purposes.</p>
<p>If needed I can add some more B... | [] | [
{
"body": "<p><code>JSON.stringify</code> is a dangerous function. It fails, and thus your function too, on cyclic objects, on very deep or big objects, or simply on objects having some forbidden accessor deeply cached.</p>\n\n<p>This object can't be logged by your function, for example : </p>\n\n<pre><code>var... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-17T15:58:04.517",
"Id": "20643",
"Score": "1",
"Tags": [
"javascript"
],
"Title": "NS.log (ES5 logger) - v0"
} | 20643 |
<p>I have the below code:</p>
<pre><code>public interface IRepositoryService
{
/// <summary>
/// This will add conditions like Published = true, Deleted = false and PublisehdDate smaller than NOW
///
/// </summary>
IRavenQueryable<T> AddConditionsToQueryToLoadOnlyAvailableForFron... | [] | [
{
"body": "<p>Nice thing in participating/answering in forums like this is that you learn while you answer questions. I haven't heard about SpecsFor framework. Looks a bit tricky, but will definitely have a look later. Ok, back to your question :)</p>\n\n<p>About your first question, setting up the mock - you c... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-17T16:01:40.917",
"Id": "20644",
"Score": "4",
"Tags": [
"c#",
"unit-testing",
"dependency-injection",
"moq"
],
"Title": "Mock/unit test for this IRepositoryService method/class"... | 20644 |
<p>I know the code is full of useless data and stylistic and analysis errors and I'd really love and appreciate to have a pro's idea about it so I can learn to code (and think) better.</p>
<pre><code>import os, time
from ConfigParser import SafeConfigParser
configfilename = "imhome.ini"
scriptpath = os.path.abspath(os... | [] | [
{
"body": "<p>Neat idea with that script. It looks good; not bad for the first program. Just a few comments:</p>\n\n<ol>\n<li>I would improve some variable names: <code>file_modification_time</code> -> <code>phone_last_seen_time</code>, <code>file_delta_time</code> -> <code>phone_unseen_duration</code>. When ... | {
"AcceptedAnswerId": "20676",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-17T16:29:00.560",
"Id": "20646",
"Score": "19",
"Tags": [
"python",
"beginner",
"child-process",
"raspberry-pi"
],
"Title": "Automatically turn on Computer from Raspberry Pi (... | 20646 |
<p>I am currently programming, but I messed up my code. What is the best way to clean this?</p>
<p>buttonUitvoeren_Click_1:</p>
<pre><code>private void buttonUitvoeren_Click_1(object sender, EventArgs e)
{
buttonNoodstop.Enabled = true;
buttonPauze.Enabled = true;
buttonUitvoeren.Enabled = false;
//... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-17T18:41:41.113",
"Id": "33122",
"Score": "3",
"body": "You are mixing English with ... Dutch? in comments, variable names and messages. I suppose messages must remain in the original language in the final code, but not knowing what it... | [
{
"body": "<p>I would start by trying to extract methods from your code. My general rule of thumb is that a method should be small enough to fit on the screen without having to scroll up or down.</p>\n\n<p>Here is an example:</p>\n\n<pre><code>if (t > 0 && t < orderLijst.Count)\n{\n if (orderL... | {
"AcceptedAnswerId": null,
"CommentCount": "3",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-17T16:31:08.930",
"Id": "20647",
"Score": "2",
"Tags": [
"c#",
"homework"
],
"Title": "Where to start refactoring?"
} | 20647 |
<p>just I wan to ask how to improve my code especially contact info block </p>
<pre><code>using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Compudata_ProjectManager.CodeFile.BOL;
using System.Web.Security;
namespace Compud... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-02-10T20:09:13.377",
"Id": "34626",
"Score": "0",
"body": "You can use the Model View Presenter pattern to abstract away the GUI - head over to pluralsight-training.net"
}
] | [
{
"body": "<p>A couple minor changes I would make in the loading. I didn't review the rest. I am assuming (probably correctly) that <code>memberUser.ProviderUserKey</code> is always a <code>Guid</code> at runtime.</p>\n\n<pre><code> //get logged in user id UserID from MembershipUser\n Membership... | {
"AcceptedAnswerId": "20693",
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-17T18:04:18.493",
"Id": "20653",
"Score": "1",
"Tags": [
"c#",
"asp.net"
],
"Title": "improve & review NewCustomer page?"
} | 20653 |
<p>So I would be very grateful if you could check these four tasks - I'm a Java newbie and recently coded such things on a Introduction to CS test which I didn't score so well at and would love to know where my mistakes were, what I should do some other way and how can I get better at it to score higher again :)</p>
<... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2015-06-09T04:00:59.100",
"Id": "169139",
"Score": "1",
"body": "It's best to just have one exercise per question, especially if they're unrelated."
}
] | [
{
"body": "<p>Hmmmm...</p>\n\n<p>1> This looks mostly okay. Probably the biggest help for you would be to extract adding the offset (15) to another method. This should change your algorithm from O(2n) to O(n); that is, you only have to loop through the array once, instead of twice:</p>\n\n<pre><code>private s... | {
"AcceptedAnswerId": "20659",
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-17T19:10:22.107",
"Id": "20656",
"Score": "2",
"Tags": [
"java"
],
"Title": "Four basic Java exercises - what should have been done better?"
} | 20656 |
<p>I'm building an authentication system using a combination of PHP, MySQL, jQuery, and AJAX. The way I'm doing it right now is I have a form that takes in a username and password. When the user clicks on the login button I pass the value of those two values along with an action to a controller file. That controller fi... | [] | [
{
"body": "<p>Do all URLs on your site use SSL? If not you'll need to setup your site to at least support the authenticate URL over HTTPS.<br>\nCurrently your passing a plain text Username and Password - anyone can grab them.</p>\n\n<p>On requests after the login, how are you determining whether or not the user... | {
"AcceptedAnswerId": "20658",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-17T20:12:37.030",
"Id": "20657",
"Score": "0",
"Tags": [
"php",
"jquery",
"mysql",
"security",
"ajax"
],
"Title": "Authentication system using AJAX"
} | 20657 |
<p>I use this simple time method to time intervals. It basically just consolidates some simple math.</p>
<pre><code>/*time
** dependencies - none
** browser - unknown
**
*/
NS.time = (function () {
var measurements = [];
return function (control) {
var index,
intervals = [],
... | [] | [
{
"body": "<p>Since <code>start</code>, <code>middle</code> and <code>finish</code> are pretty much \"verbs\", why not make them actions of the object <code>time</code>? With that, an API like this would do:</p>\n\n<pre><code>$.time.start();\n$.time.middle();\n$.time.finish();\n</code></pre>\n\n<p>Further, a de... | {
"AcceptedAnswerId": "20663",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-18T01:13:32.803",
"Id": "20662",
"Score": "1",
"Tags": [
"javascript"
],
"Title": "Release - NS.time"
} | 20662 |
<p>Just curious if this is a reasonable idea. I wouldn't do it all the time. The example I'm in now is that of the data context and repository that uses it.</p>
<p>I kind of like this because, compared to calling <code>new</code> in <code>AssignButtonClicked</code>, it gives my code the same quality of feeling indep... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-18T11:24:23.413",
"Id": "33153",
"Score": "1",
"body": "Is `WithPaymentBatchRepository` going to be called elsewhere or only from `AssignButtonClicked`? Or there gonna be other helper methods of the same style for other user actions?"
... | [
{
"body": "<blockquote>\n <p>it gives my code the same quality of feeling independent of resource allocation as when using constructor injection</p>\n</blockquote>\n\n<p>But it's not. With constructor injection, the caller can easily use another implementation or create the resource with different parameters. ... | {
"AcceptedAnswerId": "20677",
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-18T03:23:28.437",
"Id": "20664",
"Score": "3",
"Tags": [
"c#"
],
"Title": "Do you like this kind of helper, which creates short lived helper objects through a delegate?"
} | 20664 |
<p>I've created this scheme to preserve function calls until all the arguments are available. The <code>stub_op</code> classes will be replaced with classes that implement a <code>forward</code>-like mechanism that receives notifications when a forward is finished.</p>
<p>I wanted to have a way to set up a function ca... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-20T15:43:43.750",
"Id": "33238",
"Score": "0",
"body": "Why out of my league. There are not that many C++ reviewers here (and fewer that I have seen have this kind of knowledge). You may want to try pinging somebody from this list to g... | [
{
"body": "<p>the very first and important question: <strong>what problem you are trying to solve with this code?</strong> Why do you think it's better than simple:</p>\n\n<pre><code>auto call_chain = []()\n{\n return a_function2(a_function());\n};\nstd::cerr << \"result == \" << call_chain() <... | {
"AcceptedAnswerId": null,
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-18T04:48:16.463",
"Id": "20665",
"Score": "3",
"Tags": [
"c++",
"c++11",
"template-meta-programming"
],
"Title": "Could this deferred execution scheme be any simpler?"
} | 20665 |
<p>I'm writing a simple disassembler in C for learning. It is supposed to take a ELF file read in the contents of the ELF header then the individual program headers (potentially multiple) and print out various information on the file and then print each ARM code out.</p>
<p><strong>main.c</strong></p>
<pre><code>#inc... | [] | [
{
"body": "<p>As a first thought, how portable is this? You use standard types (int, short, unsigned int etc) everywhere, but the sizes of these vary with platform. You might be better using types from stdint.h, such as int32_t, int16_t etc, which guarantee size.</p>\n\n<p>I'll comment more thoroughly later...<... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-18T11:04:31.673",
"Id": "20671",
"Score": "2",
"Tags": [
"optimization",
"c"
],
"Title": "ARM disassembler from ELF file in C"
} | 20671 |
<p>While I'm aware that code with callbacks tends to be complex, I'm wondering if there are any patterns or something to improve my situation.</p>
<p>All I do here is check if a file exists and then print its name if it's not a directory:</p>
<pre><code>var fs = require('fs'),
filename = process.args[2];
fs.exis... | [] | [
{
"body": "<p>You can actually omit <code>fs.exists()</code>. The <code>fs.stat()</code> will return an error when the item you are testing is not there. You can scavenge through the <code>err</code> object that <code>fs.stat()</code> returns to see what error caused it. As I remember, when <code>fs.stat()</cod... | {
"AcceptedAnswerId": "20675",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-18T11:47:31.293",
"Id": "20674",
"Score": "4",
"Tags": [
"javascript",
"node.js",
"file-system"
],
"Title": "Checking if a file exists then do something if it's not a directory"
} | 20674 |
<p>I know that is generally expected that <a href="http://blog.mohammadjalloul.com/blogs/mo/archive/2009/12/13/evil-practices-swallowing-exceptions.aspx" rel="nofollow">you should not swallow exceptions</a>. In this code, an <a href="http://help.infragistics.com/Help/NetAdvantage/WinForms/2011.2/CLR2.0/html/Infragistic... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-18T14:49:34.140",
"Id": "33162",
"Score": "1",
"body": "Why do you think it should be an exception to the rule? What kind of exceptions can be thrown here, and in which cases?"
},
{
"ContentLicense": "CC BY-SA 3.0",
"Creati... | [
{
"body": "<p>I agree with <a href=\"https://codereview.stackexchange.com/users/19473/almaz\">almaz</a> - a blanket <code>catch</code> is bad juju in just about every case. Need to know what exception(s) wind up being there. If the column doesn't exist, there should be a better, non-exceptional way of doing tha... | {
"AcceptedAnswerId": "20681",
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-18T14:44:18.907",
"Id": "20680",
"Score": "6",
"Tags": [
"c#",
"exception-handling"
],
"Title": "Handling failed catches"
} | 20680 |
<p>How could this code become cleaner? I think that the way I handle the interfaces and binary search could be improved. I am trying to understand how to structure such a code (and usage of APIs) in a cleaner and more efficient manner. </p>
<p>This code solves the problem of finding the max subset of non-overlapp... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-21T09:38:48.903",
"Id": "33265",
"Score": "1",
"body": "http://docs.oracle.com/javase/6/docs/api/java/util/Comparator.html#compare%28T,%20T%29 \"The implementor must ensure that sgn(compare(x, y)) == -sgn(compare(y, x)) for all x and y... | [
{
"body": "<p>I see three points of improvement:</p>\n\n<ol>\n<li>The isEmpty check is redundant can be removed. The contains will handle the scenario your are checking for.</li>\n<li>The Comparator really clutters things up, and since you already have a SortedSet coming in, I know you have another compareTo me... | {
"AcceptedAnswerId": "20754",
"CommentCount": "6",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-18T15:49:04.923",
"Id": "20683",
"Score": "2",
"Tags": [
"java",
"algorithm",
"api",
"binary-search",
"interval"
],
"Title": "Finding the max subset of non-overlapping int... | 20683 |
<p>Thanks for taking the time to read this first off. I want to say that I have always been very interested in generating random terrain and since I'm new to any sort of graphical programming, I decided to start as simple as I could get.</p>
<p>I am trying to implement the midpoint displacement algorithm to make fract... | [] | [
{
"body": "<p>.... Hrm...</p>\n\n<pre><code>import java.util.Random;\n</code></pre>\n\n<p>I'm assuming this is sufficient for your needs. In the future, it may be preferable to create your own interface, that you can supply different implementations for.</p>\n\n<pre><code>public class MidPointGenerator {\n\n ... | {
"AcceptedAnswerId": "20690",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-18T16:47:19.680",
"Id": "20684",
"Score": "1",
"Tags": [
"java"
],
"Title": "Implementing the midpoint displacement algorithm in Java"
} | 20684 |
<p>I'm currently writing a class:</p>
<pre><code>final class MyTestClass {
const URL = 'http://www.example.org';
private static $MY_ARRAY = array('...');
// since this is not allowed
// const MY_ARRAY = array('...');
}
</code></pre>
<p>So my question is whether I should make <em>URL</em> a <code>private stat... | [] | [
{
"body": "<p>I'm not quite sure that this question is on topic, however:</p>\n\n<p>You have to understand what each variable definition is and what it means for the accesibility of that variable.</p>\n\n<p>Say your class definition is as follows:</p>\n\n<pre><code>class MyTestClass {\n const URL = 'http://www... | {
"AcceptedAnswerId": "20705",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-18T17:20:34.410",
"Id": "20685",
"Score": "2",
"Tags": [
"php",
"object-oriented",
"classes"
],
"Title": "Consistent implementation of PHP class constants (arrays not allowed)"
} | 20685 |
<p>I have a few custom exception classes that I created simply for the sake of having my own exception message:</p>
<pre><code>public class DivideByZeroException extends Exception
{
@Override
public String toString()
{
return "ERROR: Expression cannot divide by 0";
}
}
</code></pre>
<p>I realiz... | [] | [
{
"body": "<p>If you want all your exceptions to follow this style, then you could have one parent Exception class, then make your application exceptions extend it, like so:</p>\n\n<pre><code>class MyException extends Exception\n{\n MyException(String message)\n {\n super(message);\n }\n\n @O... | {
"AcceptedAnswerId": "20689",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-18T19:00:09.393",
"Id": "20688",
"Score": "1",
"Tags": [
"java",
"exception"
],
"Title": "Java Exception Message"
} | 20688 |
<p>I wrote the following in Javascript and jQuery, and I'm wondering if there are any ways to improve the code, organize it better, and increase speed:</p>
<pre><code>function calendar(d) {
$("#calendar").remove();
$("#calendar_container").append("<table border='1' id='calendar'></table>");
t =... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-18T21:20:19.150",
"Id": "33183",
"Score": "2",
"body": "You should cache var `$this = $(this)` to avoid re querying."
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-18T22:02:44.660",
"Id": "33184",
... | [
{
"body": "<p>The code you posted is quite big so I believe that's why no one has answered this question yet. For a better and more specific answer I would suggest you ask several questions with isolated sections you want to improve.</p>\n\n<p>Anyways, here are some basic improvements you could use:</p>\n\n<ul>... | {
"AcceptedAnswerId": null,
"CommentCount": "4",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-18T21:11:56.667",
"Id": "20691",
"Score": "4",
"Tags": [
"javascript",
"jquery",
"performance",
"datetime"
],
"Title": "Calendar code review"
} | 20691 |
<p>I'm trying to solve <a href="http://www.spoj.com/problems/PRIME1/" rel="nofollow">this</a> problem. After a couple of tries, this is what I pulled off:</p>
<pre><code> #include<stdio.h>
#define primeLimit 100000
int prime (long int Start2, long int Stop2 )
{
long int a[primeLimit+... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-02-18T15:19:58.197",
"Id": "126811",
"Score": "0",
"body": "You should use the 'Segmented Sieve' algorithm. If you need the complete explanation of the problem look here: http://zobayer.blogspot.de/2009/09/segmented-sieve.html"
},
{
... | [
{
"body": "<p>The short answer: restrict your sieving to what is necessary (sieve potential factors up to the square root of the upper end of the range, sieve the range itself). The runtime for that is a few milliseconds, compared to several seconds (or even minutes) for sieving <strong>all</strong> numbers up ... | {
"AcceptedAnswerId": null,
"CommentCount": "3",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-19T04:13:26.887",
"Id": "20694",
"Score": "1",
"Tags": [
"c",
"programming-challenge",
"primes",
"time-limit-exceeded"
],
"Title": "SPOJ Problem 2 - Prime Generator"
} | 20694 |
<p>I have the following code below I was wondering if there is a way to clean it up/improve it especially near the if statements. I am new to programming and this is what I came up with and its working fine I just think there might be a better way to accomplish it.</p>
<pre><code> <?php
require 'DB.php';
try ... | [] | [
{
"body": "<p>My own personal preference when outputting things like this is to put the portion where data is actually retrieved in a separate file and then use the alternate PHP structure syntax. While this probably isn't everyone's preferred coding style, in my opinion it makes it easier to structure HTML so ... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-19T04:32:53.567",
"Id": "20695",
"Score": "1",
"Tags": [
"php"
],
"Title": "echoing html tags in php"
} | 20695 |
<p>I've made a small breakout game, so far with one level. My plan is to expand it so that it has a level editor, but for now I wanted to make sure that everything is currently looking well-designed. I don't use js a lot so I'm not always very sure where to put things.</p>
<p>Just a heads up, the collision doesn't w... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-19T19:57:27.400",
"Id": "33210",
"Score": "0",
"body": "Do you know about [requestAnimationFrame](http://creativejs.com/resources/requestanimationframe/)? I haven't used it yet but would probably try it if I was making a new game. That... | [
{
"body": "<p>You might want to put all the canvas stuff into an object or class.</p>\n\n<pre><code>function Canvas() {\n var el = $('<canvas>'),\n ctx;\n this.width = 600;\n this.height = 400;\n el.attr('id', 'canvas');\n el.attr('width', $('#game').width());\n el.attr('height', $('#game').heigh... | {
"AcceptedAnswerId": "20728",
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-19T10:10:30.160",
"Id": "20704",
"Score": "1",
"Tags": [
"javascript",
"collision"
],
"Title": "Breakout-like game in JS"
} | 20704 |
<p>My son (9) is learning Python and wrote this temperature conversion program. He can see that there is a lot of repetition in it and would like to get feedback on ways to make it shorter.</p>
<pre><code>def get_temperature():
print "What is your temperature?"
while True:
temperature = raw_input()
... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-19T12:56:32.623",
"Id": "33194",
"Score": "0",
"body": "Out of curiosity: why didn't your son ask here himself?"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-19T12:59:52.990",
"Id": "33195",
"Scor... | [
{
"body": "<p>He should define functions that he can re-use:</p>\n\n<pre><code>def fahrenheit_from_celsius(temprature):\n return temperature * 9 / 5 + 32\n\n\nprint fahrenheit_from_celsius(get_temperature())\n</code></pre>\n",
"comments": [],
"meta_data": {
"CommentCount": "0",
"ContentLi... | {
"AcceptedAnswerId": "20732",
"CommentCount": "6",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-19T12:17:27.710",
"Id": "20708",
"Score": "3",
"Tags": [
"python",
"converting"
],
"Title": "Temperature conversion program with a lot of repetition"
} | 20708 |
<p>I'm coding a Minesweeper clone using Java and Swing. For my current knowledge, I manage to keep it working but two things are giving me nightmares. Namely, I have a VERY repetitive code so I suppose there should be a way to write it once only and use twice but I don't know how to do it as the things it does when the... | [] | [
{
"body": "<p>Did you thought about using a n+2 grid? Just don't place bombs at the border and don't display the other ring. So you can safely check and update all direction?</p>\n\n<p>But maybe you should abstract your grid in two levels, to get rid of the +-1 stuff and implement a iterator to get all neighbou... | {
"AcceptedAnswerId": "20721",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-19T14:01:45.383",
"Id": "20714",
"Score": "2",
"Tags": [
"java",
"game",
"swing",
"minesweeper"
],
"Title": "Minesweeper clone using Swing"
} | 20714 |
<p>It is a little thing about UIViewController, but it has always bothered me - the boilerplate code needed to setup some of a view controller's default properties (e.g. the tab bar item and navigation item) and the location of this code.</p>
<p>Apple's documentation says the following:</p>
<blockquote>
<p><strong>... | [
{
"ContentLicense": "CC BY-SA 4.0",
"CreationDate": "2019-07-17T23:28:48.130",
"Id": "435246",
"Score": "0",
"body": "option 1 but name the ivar _tabBarItem and don't have a setter. This is what Apple does in their apps, e.g. Podcasts @interface MTDownloadsCollectionViewController : UICollection... | [
{
"body": "<p>A subclass of a vc that otherwise could be used as a tab bar vc or a nav controller vc would probably achieve your goals </p>\n\n<p>Additionally, one could make a category to tuck all the boilerplate in and just set some optional params in for that</p>\n\n<p>In reality, I think few people care to ... | {
"AcceptedAnswerId": "20771",
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-19T15:16:16.767",
"Id": "20716",
"Score": "1",
"Tags": [
"objective-c",
"ios"
],
"Title": "iOS View Controllers - Default Lazy Loaded Properties (Tab Bar Item & Navigation Item)"
} | 20716 |
<p>I've recently picked up the <em>Head First Design Patterns</em> book in an effort to become a more efficient and better Python programmer. Unfortunately, the code examples in this book are in Java. </p>
<p>I'm not the <a href="http://www.velocityreviews.com/forums/t338550-python-design-patterns.html">first one</a> ... | [] | [
{
"body": "<h1>The devil’s in the <code>_details</code></h1>\n\n<p>Your suggestion is good, but you don’t need a property; you can just use a normal attribute. All your setter does is set the variable, so just do that instead. So, this:</p>\n\n<pre><code>self.set_fly_behavior(fly_instance)\n</code></pre>\n\n<p>... | {
"AcceptedAnswerId": "20719",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-19T16:38:44.363",
"Id": "20718",
"Score": "24",
"Tags": [
"python",
"design-patterns"
],
"Title": "Strategy design pattern with various duck type classes"
} | 20718 |
<p>I am trying to improve my C skills, and I hope someone might be able to provide me with some feedback on the following code. I avoid <code>strtok</code> function intentionally.</p>
<pre><code>#define MAX_SIZE 1000
int split_up(char* string, char* argv[])
{
char* p = string;
int argc = 0;
while(*p != '\... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-20T15:34:26.187",
"Id": "33236",
"Score": "0",
"body": "The same affect can be achieved using `sscanf()`. Just use a string format of \" %*s\" (note the leading space in the string)."
}
] | [
{
"body": "<p>Your code looks quite nice, but here are a few nitpicking comments, for what they are worth.</p>\n\n<ul>\n<li>Missing headers, but I assume you just didn't paste them.</li>\n<li>split_up should be static and get a better name</li>\n<li>variable names are odd. argc/argv are the usual names for main... | {
"AcceptedAnswerId": "20734",
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-19T18:21:20.663",
"Id": "20722",
"Score": "1",
"Tags": [
"c",
"strings"
],
"Title": "split up a string into whitespace-seperated fields"
} | 20722 |
<p>I've been working on my first Angular JS app for the past few days.
It's in a very early stage (no real functionality), but that will only make it easier to review what IS there.</p>
<p>The client side is written in CoffeeScript. The app used Requirejs to manage files AMD style (it loads compiled CoffeeScript).</p... | [] | [] | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-19T18:39:03.703",
"Id": "20723",
"Score": "4",
"Tags": [
"html",
"angular.js"
],
"Title": "Angular JS photo app for personal cloud"
} | 20723 |
<p>I was writing my own implementation of BlockingQueue for practice. I am trying to avoid using the synchronized keyword for the methods. I would instead like to use ReentrantLock.</p>
<p>What is the best way to write this implementation? I am not a Java ninja and would greatly appreciate if someone could pinpoint th... | [] | [
{
"body": "<p>These seem mixed up:</p>\n\n<pre><code> while(queue.size() == limit.get()) {\n put_condition.await();\n }\n put_condition.signal();\n</code></pre>\n\n<p>and</p>\n\n<pre><code> while (queue.size() == 0) {\n take_condition.await();\n }\n take_condition.signal();\n</co... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-19T18:44:23.903",
"Id": "20724",
"Score": "4",
"Tags": [
"java",
"thread-safety"
],
"Title": "BlockingQueue Implemetation using ReentrantLock"
} | 20724 |
<p>I am basically getting data from various APIs and using PHP to put them together - like a web mashup. I am currently using 4 <code>foreach</code> statements to insert the gathered data into their individual arrays. I believe that the current code is inefficient because it takes probably around 3 seconds to load the ... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-20T08:40:48.657",
"Id": "33228",
"Score": "0",
"body": "Would you have a few possible values to input so that I can try a few things ? (Of course, I know that the DB query will fail)."
},
{
"ContentLicense": "CC BY-SA 3.0",
... | [
{
"body": "<p>Please add the following method do your code and print the runtime of every section of your code. As indicated by my comment I guess the foreach->simplexml_load_file(\"...\") might take a while.</p>\n\n<pre><code>//PHP <5.0.0\nfunction microtime_float()\n{\n list($usec, $sec) = explode(\" \"... | {
"AcceptedAnswerId": null,
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-19T19:20:03.973",
"Id": "20725",
"Score": "1",
"Tags": [
"php",
"api",
"client"
],
"Title": "Music management app, mashing data from various APIs"
} | 20725 |
<p>The entity framework will only save properties it detects has changed via its proxy classes. I have a situation where I want a property to always be saved no matter if it changed or not.</p>
<p>I wrote a blank attribute called <code>AlwaysUpdate</code> which I apply to the property. I then overrode <code>SaveChan... | [] | [
{
"body": "<ul>\n<li><p>Instead of checking <code>.Count() > 0</code> which needs to iterate over the <code>IEnumerable</code> you can just use <code>.Any()</code> which only checks if at least one item is contained in the <code>IEnumerable</code> </p></li>\n<li><p>you should extract the result of <code>typ... | {
"AcceptedAnswerId": "74068",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-19T19:45:43.010",
"Id": "20726",
"Score": "5",
"Tags": [
"c#",
"entity-framework",
"poco"
],
"Title": "AlwaysUpdate attribute for Entity Framework Code First POCO"
} | 20726 |
<p>Here is my simple example: there is a class A with few primitive members and a class B with few primitive members but also a collection of objects of type A.</p>
<p>This is my A class:</p>
<pre><code>#pragma once
#include<string>
class Hero
{
private:
long id;
std::string name;
int level;
s... | [] | [
{
"body": "<p>Firstly, in your header file, you don't need the <code>Hero::</code> qualification on your move assignment operator. Secondly, you are defining it as <code>Hero& Hero::operator=(const Hero &&hero);</code>. Think about what this is saying - it's saying if you have an rvalue reference to... | {
"AcceptedAnswerId": "20733",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-19T20:52:35.787",
"Id": "20729",
"Score": "7",
"Tags": [
"c++",
"beginner",
"c++11",
"memory-management",
"smart-pointers"
],
"Title": "Am I using copy ctors/move ctors/sh... | 20729 |
<p>I am using Castle Windsor as my IoC container and I registered it as a <code>DependencyResolver</code> to let the MVC framework know about it. </p>
<p>With the Entity Framework I have this <code>DbContext</code>:</p>
<pre><code>public class MyDbContext : DbContext
{
public DbSet<User> Users { get; set; }... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2014-10-07T12:36:12.373",
"Id": "119071",
"Score": "0",
"body": "Why exactly do you say the `MyDbContext` cannot be injected directly? I'm trying to see if I understand what you mean, so I could help you out."
},
{
"ContentLicense": "C... | [
{
"body": "<p>The preferred method of doing this is to use a factory dependency that creates the DbContext for you. So instead of the DbContext as the dependency you have a ContextFactory as the dependency and its consumers can get and manage a local DbContext by calling <code>contextFactory.CreateContext()</co... | {
"AcceptedAnswerId": null,
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-20T11:00:40.653",
"Id": "20735",
"Score": "7",
"Tags": [
"c#",
"validation",
"asp.net-mvc-3"
],
"Title": "MembershipProvider with Entity Framework and IoC"
} | 20735 |
<p>I have been doing TDD since I have started my first job out of university (about 5 months ago), most of which is working with legacy code. I started a personal project today and thought I would TDD it. I don't have much "green field" dev experience so I am unsure if this is the correct way of driving interface desig... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-21T09:52:55.273",
"Id": "33266",
"Score": "2",
"body": "The things you check in your tests are already checked by the compiler. So these tests add no value. Tests are code too, and they need maintenance. These test cases are just liabi... | [
{
"body": "<p>Yes, it is too much. Testing that methods exist on a contract is both overkill and high-maintenance. If you decide to change a method name, it breaks tests, and the test breakage doesn't tell you something useful. Ask yourself what value you are getting out of this test.</p>\n\n<p>I tend to tes... | {
"AcceptedAnswerId": null,
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-20T17:00:41.607",
"Id": "20739",
"Score": "3",
"Tags": [
"c#",
"unit-testing",
"interface",
"tdd"
],
"Title": "Test Driving Interface Design"
} | 20739 |
<p>I have some data that logs kWh. I want to be able to collect the data and produce a bar chart (I'm using the Microsoft ASP.Net Chart control). I have written code that works, but it looks a little clunky to me and I wondered if somebody could point out a method that may be easier to read and/or more efficient.</p>
... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-21T10:58:27.587",
"Id": "33269",
"Score": "0",
"body": "The answer from Svick below is exactly the kind of thing I was looking for. Being new to programming I hadn't even considered the concept of duplicating arrays. Techniques like th... | [
{
"body": "<p>I think that your code to “remove the values that are identical” is quite confusing.</p>\n\n<p>First, it doesn't remove anything, it just sets the values to zero. And unless you filter that out later, it will cause problems in your final calculation.</p>\n\n<p>Second, modifying the indexing variab... | {
"AcceptedAnswerId": "20741",
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-20T17:07:22.977",
"Id": "20740",
"Score": "3",
"Tags": [
"c#",
"performance",
"asp.net"
],
"Title": "Can I improve this code for readability and/or performance?"
} | 20740 |
<p>Just a quick preface, I'm not a web developer. I'm simply doing this as a favor for a friend. My goal is to be done with it as quickly as possible, but still not have the coding be horrendous. With that said, as I was coding this, my spidey senses were going off telling me that what I was doing was really bad progra... | [] | [
{
"body": "<p>I cleaned up your code a bit:</p>\n\n<pre><code><!doctype html>\n<html>\n<head>\n <meta charset=\"utf-8\" />\n <title></title>\n <style type=\"text/css\">\n #cash-amount\n {\n text-indent:50px;\n }\n... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-20T19:05:18.743",
"Id": "20744",
"Score": "2",
"Tags": [
"php",
"jquery",
"mysql",
"html",
"css"
],
"Title": "I've embedded several PHP/HTML/Javascript in one page. How can I... | 20744 |
<p>Here's the situation. I'm writing a simple game and I had two main actors: <code>GameController</code> and <code>GridView</code>.</p>
<ul>
<li><p><code>GridView</code> is a <code>UIView</code> subclass displaying a grid with which the user interacts. It defines its custom delegate protocol (<code>GridViewDelegate</... | [] | [
{
"body": "<p>I think you trying to overengineer solution a bit. First of all View should not do any stuff at all. View is just a View, sheet of paper. Maximum of view's responsibility is layouting himself. So controller should take care of other view-related stuff. </p>\n\n<p>In this case I probably prefer to ... | {
"AcceptedAnswerId": "20811",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-21T01:58:46.990",
"Id": "20749",
"Score": "4",
"Tags": [
"objective-c"
],
"Title": "Code review of forward invocation"
} | 20749 |
<p>Is there a better way to implement my carousel in less lines?</p>
<pre class="lang-html prettyprint-override"><code><div id="myCarousel" class="carousel slide">
<div class="carousel-inner">
<% @photos.each_with_index do |photo, index| %>
<% if index == 0 %>
<div class=... | [] | [
{
"body": "<p>Get rid of unnecessary code:</p>\n\n<pre><code><div id=\"myCarousel\" class=\"carousel slide\">\n <div class=\"carousel-inner\">\n <% @photos.each_with_index do |photo, index| %>\n <% if index.zero? %>\n <div class=\"item active\">\n <img src=\... | {
"AcceptedAnswerId": "20755",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-21T02:30:15.607",
"Id": "20750",
"Score": "6",
"Tags": [
"html",
"erb"
],
"Title": "Showing items as in a Carousel"
} | 20750 |
<p>I use the following code to verify that columns have data and display the appropriate error message.</p>
<p>My code is working but it just doesn't look tidy. Is there any way to refactor all the <code>if</code> statements into a loop to display the error message, possibly another method which checks all? <code>chec... | [] | [
{
"body": "<p>First of all, better names than columnX would be nice. I guess the indentation is a copying-issue, otherwise this would be the next optimization. Furthermore I have the feeling that mixing the && and the || in your condition without parenthesis is not what you want to do?</p>\n\n<pre><code... | {
"AcceptedAnswerId": "20758",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-21T12:30:10.767",
"Id": "20757",
"Score": "3",
"Tags": [
"c#",
"asp.net"
],
"Title": "Importing an Excel document into a project"
} | 20757 |
<p>I have this code and I think it is very slow, about 50ms. I need to make it execute faster:</p>
<pre><code> switch (unit)
{
case 0:
if (u == 2)
{
if (u1 == "piece" || u1 == "gram")
result = quantity * y;
else
result = quantity * z;
}... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-21T15:07:15.800",
"Id": "33279",
"Score": "1",
"body": "Can you give more information about unit, u, u1, res_u, result, quantity, x, y, z?"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-21T15:12:21.290",
... | [
{
"body": "<p>The first thing I would do is refactor your names to something understandable. After that I would try to create a data structure to encapsulate the nested logic.</p>\n\n<p>Without knowing more about the background it is hard to refactor without breaking some kind of functionality. </p>\n",
"co... | {
"AcceptedAnswerId": null,
"CommentCount": "5",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-21T15:03:14.697",
"Id": "20759",
"Score": "2",
"Tags": [
"c#",
"performance"
],
"Title": "Optimizing nested if statements in switch"
} | 20759 |
<p>This is my code:</p>
<pre><code>var test = (from x in myDb.myTable
where (x.name == tmp || x.name == tmp2 || x.name == tmp3) && x.unit == u
select x).FirstOrDefault();
if (test == null)
test = (from x in myDb.myTable
where (x.name == tmp || x.name == tmp2 || x.name =... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-21T15:46:31.107",
"Id": "33285",
"Score": "1",
"body": "Could you explain your logic? Why are you doing it this way? If there are rows with both `tmp` and `tmp2` (which are bad variable names, BTW) as their `name`, why is it okay to ge... | [
{
"body": "<pre><code>var test = (from x in myDb.myTable\n where (x.name == tmp || x.name == tmp2 || x.name == tmp3)\n select x)\n .AsEnumerable()\n .OrderBy(x => x.unit == u ? 0 : 1)\n .FirstOrDefault();\n</code></pre>\n\n<p>I removed the <code>x.unit == u... | {
"AcceptedAnswerId": "20762",
"CommentCount": "7",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-21T15:12:46.830",
"Id": "20760",
"Score": "3",
"Tags": [
"c#",
"optimization",
"entity-framework"
],
"Title": "Optimization of code for searching in db"
} | 20760 |
<p>I have a much needed rewrite of an inherited application ahead of me and have started to sketch out some possible solutions / build some prototypes for different parts of the application.</p>
<p>I'd like to have some feedback if this is a good approach to provide application events.</p>
<p>Goals:</p>
<ul>
<li>Loo... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-21T21:31:23.687",
"Id": "33307",
"Score": "1",
"body": "This looks reasonable. But the same pitfall as with every rewrite applies: you're potentially throwing away hours and hours of bug fixes and experience. Proceed with care, **unit ... | [
{
"body": "<p>The code itself looks good, but I would look into providing a single way of subscribing to events, it will make the job easier for other developers reading and changing the code.</p>\n\n<p>The first method of subscribing (via <code>IEventBroker<T></code> injection) requires subscriber object... | {
"AcceptedAnswerId": "20796",
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-21T21:08:55.243",
"Id": "20775",
"Score": "4",
"Tags": [
"c#"
],
"Title": "Loosely coupled application wide events"
} | 20775 |
<p>I decided to run a few test on js functions on jspref to see which method of using function is better suited for this small particular example, to get a better understanding.</p>
<p><a href="http://jsperf.com/with-func-or-not" rel="nofollow">Page with tested functions</a></p>
<p>Problem:</p>
<p>Getting different ... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-22T00:06:12.923",
"Id": "33311",
"Score": "0",
"body": "To be clear, are you interested specifically in performance (speed)? Can the position of #horiz_line change? If so then the functions will do slightly different things. I imagine ... | [
{
"body": "<p>These four functions are doing different things:</p>\n\n<p>The 1st and 4th set <code>up</code> immediately and <code>down</code> when the user scrolls.</p>\n\n<p>The 2nd and 3rd set both <code>up</code> and <code>down</code> when the user scrolls. (The 2nd seems unlikely to work properly because <... | {
"AcceptedAnswerId": "20780",
"CommentCount": "3",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-21T21:58:01.867",
"Id": "20777",
"Score": "0",
"Tags": [
"javascript",
"jquery",
"comparative-review"
],
"Title": "Different function test result inquiry"
} | 20777 |
<p>Due to a weird server setup on one of my client's websites, I needed to setup a script to load referenced pdf files through a case-insensitive lookup. We originally looked into mod_speling, but it was causing issues with other mod_rewrite declarations. I'm not entirely sure why, but I didn't have much control over ... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-22T05:50:45.717",
"Id": "33326",
"Score": "1",
"body": "Not sure if it's an exploitable issue, but what would happen if someone set `p` to a relative path like `../cgi/example.pdf`? I can't forsee any issues though as the `pdf` extensi... | [
{
"body": "<p>As Jason stated checking for \"..\" is a good idea. If there are only a limited number of path where the pdfs are located on the server, I would even prefer matching against this set.</p>\n\n<p>Nevertheless, you might also want to invert your ifs to get rid of the nesting and use guard conditions.... | {
"AcceptedAnswerId": "20787",
"CommentCount": "3",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-21T20:46:11.733",
"Id": "20782",
"Score": "2",
"Tags": [
"php",
"security"
],
"Title": "Am I missing any security loopholes in this php script that reads and outputs directory/file fr... | 20782 |
<p>I've implemented adding and multiplying arbitrary precision integers.</p>
<p>Also available as a <a href="https://gist.github.com/aba0a2d1194d2cd0967a">gist</a>.</p>
<pre><code>#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
int *data;
int size;
} bi;
bi *bi_... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-22T11:42:05.093",
"Id": "33347",
"Score": "1",
"body": "Have you considered using a base16 or base32 system, rather than a base10 system?"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-22T13:19:54.523",
... | [
{
"body": "<p>The good part is that without any real modifications, I can compile your code with lots of the extra compiler flags to detect errors: <code>-Wall, -Wextra, -Werror, -pedantic</code> under <code>-std=c99</code>. That's a good start. There are a few problems, though:</p>\n<h2>Memory Leaking</h2>\n<p... | {
"AcceptedAnswerId": null,
"CommentCount": "3",
"ContentLicense": "CC BY-SA 4.0",
"CreationDate": "2013-01-22T04:51:32.897",
"Id": "20783",
"Score": "9",
"Tags": [
"beginner",
"c",
"integer"
],
"Title": "BigInteger implementation in C, supporting addition and multiplication"
} | 20783 |
<p>I have the following JavaScript function:</p>
<pre><code>function registerEvents() {
var isTapholding = false;
$(document).delegate('.add-control .add-image', 'tap', function (evt) {
handleAddControlEvent(this);
});
$(document).delegate('.delete-image', 'tap', function (evt) {
evt.st... | [] | [
{
"body": "<p>All calls have different parameters and different bodies. If there is no fancy JQuery trick, there is nothing you can do about this, and even if such a trick exist I have the feeling that this will result in a switch for every call.</p>\n",
"comments": [
{
"ContentLicense": "CC B... | {
"AcceptedAnswerId": "20790",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-22T07:04:27.490",
"Id": "20786",
"Score": "3",
"Tags": [
"javascript",
"jquery"
],
"Title": "How to Make This JavaScript Snippet DRY (Don't Repeat Yourself)?"
} | 20786 |
<p>I'm new to RSpec and testing in general. I've come up with a spec for testing my <code>Content</code> model and I need some feedback because I think there are many improvements that can be done. I don't know if the way I did it is considered over-testing, bloated/wrong code or something. This test is kinda slow but ... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-03T20:26:20.380",
"Id": "62863",
"Score": "0",
"body": "Note that David Chelimsky advises against the use of an explicit subject: http://blog.davidchelimsky.net/2012/05/13/spec-smell-explicit-use-of-subject/"
}
] | [
{
"body": "<p>Nice job. Your tests are nicely compartmentalized. It is indeed good to test the factory independently. Good use of <em>describe</em> and <em>context</em>.</p>\n\n<p><strong>Consider using the shoulda-matchers gem</strong></p>\n\n<p>Tests for many of the rails model associations and validations... | {
"AcceptedAnswerId": "20799",
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-22T08:44:30.660",
"Id": "20792",
"Score": "3",
"Tags": [
"ruby",
"ruby-on-rails",
"rspec"
],
"Title": "Testing a Content model"
} | 20792 |
<p>I feel that my current business view logic is not efficient or very clean. The problem is building the right output, but with less code and more DRY.</p>
<p>I have 3 'static' links, as in, direct tags: </p>
<pre><code>about, contact, search
</code></pre>
<p>I also have three navigation menu's.</p>
<p>The first... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-03-23T23:25:33.043",
"Id": "37460",
"Score": "1",
"body": "You should really take a look at [Symfony's Routing](http://symfony.com/doc/master/book/routing.html), whic as a component, can be used wholly separately from the rest of the proj... | [
{
"body": "<p>The following if..else stack in the controller allows me to load one of the 4 views, while containing the logic in one location, rather than across multiple files.</p>\n\n<p>Still a bit long, but probably the best way:</p>\n\n<pre><code> /** ADD content\n * = add main page content to output *... | {
"AcceptedAnswerId": null,
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-22T10:33:52.057",
"Id": "20795",
"Score": "4",
"Tags": [
"php",
"template"
],
"Title": "Cleaner way to determine and load page template"
} | 20795 |
<p>This is my try at <a href="http://en.wikipedia.org/wiki/Proxy_pattern" rel="nofollow">the proxy pattern</a>.</p>
<p>What do you Pythoneers think of my attempt?</p>
<pre><code>class Image:
def __init__( self, filename ):
self._filename = filename
def load_image_from_disk( self ):
print("load... | [] | [
{
"body": "<p>It seems like overkill to implement a class to represent the proxy pattern. <em>Design patterns are patterns, not classes.</em> What do you gain from your implementation compared to a simple approach like the one shown below?</p>\n\n<pre><code>class Image(object):\n def __init__(self, filename)... | {
"AcceptedAnswerId": "20800",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-22T13:08:25.817",
"Id": "20798",
"Score": "3",
"Tags": [
"python",
"design-patterns"
],
"Title": "proxy pattern in Python"
} | 20798 |
<p>This is a sample class using Rhino (1.7R4). I need it because in the context of JSON Schema, regexes should be ECMA 262. One requirement is that it is thread safe, and it is.</p>
<p>But I'm not sure I actually use it correctly. Can you comment?</p>
<pre><code>public final class RhinoHelper
{
/**
* JavaScr... | [] | [] | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-22T13:42:54.257",
"Id": "20802",
"Score": "3",
"Tags": [
"java",
"multithreading",
"regex",
"rhino"
],
"Title": "Rhino helper class to support ECMA-262 regular expressions"
} | 20802 |
<p>My app communicates with a server over an internal network through HTTPS. The SSL certificate on this server is listed for the host as its external host name. I want to accept this certificate, but I don't want to accept ANY certificate as many workarounds I've seen seem to do. I would also like to have it so that l... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-11-21T03:41:58.560",
"Id": "58244",
"Score": "0",
"body": "Since certificate authorities will [stop issuing certificates with internal hostnames](http://www.digicert.com/internal-names.htm), I see your options as: a) Implement this workar... | [
{
"body": "<p>There are a few items inhere I would suggest can be improved (assuming the security aspects of the code are valid - i.e. legal disclaimer about 'safe' and SSL means speak to someone who's more of a security expert than me).</p>\n\n<p>First, I assume you are using the apache-commons based HttpClien... | {
"AcceptedAnswerId": null,
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-22T17:03:12.487",
"Id": "20806",
"Score": "10",
"Tags": [
"java",
"security",
"android",
"https",
"ssl"
],
"Title": "Safely accepting a known SSL certificate with a different... | 20806 |
<p>I'm still learning JavaScript and I'm building an Ajax website that loads in content for each page from external files (e.g. test1.php, test2.php). I spent several hours cobbling together some code that works, but it feels really clunky. Any recommendations on how to streamline it? Or anything I am doing that is stu... | [] | [
{
"body": "<p>You should really combine code that is the same and put them into functions. Then call the functions.</p>\n\n<p>I put your functionality into two categories.<br>\n 1. Updating the tab links to show the active link. \n 2. Pulling the content from the specified pages.</p>\n\n<p>So in order to do th... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-22T19:45:46.707",
"Id": "20807",
"Score": "1",
"Tags": [
"javascript",
"jquery",
"ajax"
],
"Title": "AJAX navigation"
} | 20807 |
<p>What is the most elegant and readable way to in include (potentially long) <code>image_tag</code> calls inside of <code>link_to</code>?</p>
<p>Example</p>
<pre><code><%= link_to image_tag('buckminsterfullerene.png', width: '210', height: '60', alt: 'Molecular structure of Buckminsterfullerene'), 'some_long_path... | [] | [
{
"body": "<p>You could create a helper to keep your views less bloated. For example:</p>\n\n<p><code>app/helpers/application_helper.rb</code></p>\n\n<pre class=\"lang-ruby prettyprint-override\"><code>...\ndef image_link_to(image_path, url, image_tag_options = { }, link_to_options = { })\n link_to url, link_t... | {
"AcceptedAnswerId": "20821",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-23T02:39:38.770",
"Id": "20812",
"Score": "6",
"Tags": [
"ruby",
"ruby-on-rails",
"erb"
],
"Title": "Best way to include image_tag inside link_to"
} | 20812 |
<p>I am new to programming as you can probably tell from the code below I am curious if there is a way to create a loop so I don't have so many copy's of the same code with only different variable names. The way the code works is that for each variable that is represented as a day of the week the value is a string such... | [] | [
{
"body": "<p>The first thing to do would be to change whatever can be changed in the code so that the behavior doesn't change but the code looks the same everywhere. Here's what I've done :</p>\n\n<pre><code>$stmt = $conn->prepare('SELECT `employeeID`, SEC_TO_TIME(SUM(TIME_TO_SEC(TIMEDIFF(`timeOut`, `timeIn... | {
"AcceptedAnswerId": "20815",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-23T05:09:30.257",
"Id": "20813",
"Score": "0",
"Tags": [
"php",
"mysql"
],
"Title": "Create loop to shorten this code up"
} | 20813 |
<p>I've been taking a stab at implementing test cases using the Jasmine testing framework. To do so, I've made an application which has a User object. This User object is created by the server, but its ID is stored in a couple of client-side data storage locations. When a new User model is initialized I check those sto... | [] | [
{
"body": "<p>From a once over:</p>\n\n<ul>\n<li>The trigger name <code>'loaded'</code> should be a constant, declared on the <code>userIdKey</code> level</li>\n<li>You should not be using <code>console.log</code>, at the very least you should consoledate those calls into a function.</li>\n<li>It seems that if ... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-23T05:41:10.120",
"Id": "20814",
"Score": "4",
"Tags": [
"javascript",
"unit-testing",
"backbone.js",
"require.js"
],
"Title": "User model with BackboneJS and RequireJS with test... | 20814 |
<p>I need to write a function which will do the following functionalities</p>
<p>I would get a string and a array as input parameters</p>
<p>The string would be like one of the following for example </p>
<pre><code>catalog
level1Cats
level2Cats
</code></pre>
<p>The String array would be containing the data for the... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-23T11:10:21.633",
"Id": "33394",
"Score": "0",
"body": "How about using JSON.\n{\n name: \"12605\",\n children: [{\n name: \"For the Home\",\n children: [{\n name: \"Appliances\"\n }]\n }, {\n ... | [
{
"body": "<p>Below code is more readable and no need of using our logic around single quotes.</p>\n\n<pre><code>String[] w = fqField.split(\"\\\"\");\nfor (int i = 1; i < w.length; i+=2) {\n prefixes.add(w[i]);\n}\n</code></pre>\n",
"comments": [],
"meta_data": {
"CommentCount": "0",
... | {
"AcceptedAnswerId": null,
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-23T10:21:25.773",
"Id": "20819",
"Score": "2",
"Tags": [
"java",
"algorithm",
"functional-programming"
],
"Title": "Implementing logic in a different way"
} | 20819 |
<p>I am just about to embark upon a massive job of multi-threading a cost-engine. I could use TPL of which I am very familiar, but would like to leverage the <code>async</code>/<code>await</code> keywords of .NET 4.5 to make life that little bit simpler.</p>
<p>Is my understanding of what is going on correct? How ca... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-23T14:04:37.743",
"Id": "33403",
"Score": "0",
"body": "You may want to rephrase the question to better fit the code-review style described in the [faq]. There are currently a couple of close requests because questions like *\"Help me ... | [
{
"body": "<p>Yes, your understanding is correct. Note that you're not using <code>CancellationToken</code> provided to <code>ProcessScript</code>. If your operations are cancellable and/or you're using other asynchronous API that accepts <code>CancellationToken</code>s it's highly recommended to pass it throug... | {
"AcceptedAnswerId": "20822",
"CommentCount": "3",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-23T11:30:57.667",
"Id": "20820",
"Score": "6",
"Tags": [
"c#",
".net",
"asynchronous",
"task-parallel-library",
"async-await"
],
"Title": "Use and understanding of async/a... | 20820 |
<p>I've been writing a small site which is essentially just a service API for other applications but deals with the caching and serving of files. In an effort to make the site as scalable and maintainable as possible I did some research into the most efficient ways to serve files to the client, using PHP for access con... | [] | [
{
"body": "<h2>That's a lot of code</h2>\n\n<p>It looks like it works but since it's apache specific what's wrong with using <a href=\"http://php.net/manual/es/function.virtual.php\" rel=\"nofollow\">virtual</a>? It's an apache-specific php function for serving files. I.e.</p>\n\n<pre><code>public static functi... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-23T13:32:46.957",
"Id": "20823",
"Score": "1",
"Tags": [
"php",
"optimization"
],
"Title": "PHP File Serving code"
} | 20823 |
<p>I'm implementing a Java version of the game <a href="http://en.wikipedia.org/wiki/Mastermind_%28board_game%29" rel="nofollow">Mastermind</a>. </p>
<p>My version uses numbers to represent colors, for example <code>2315</code>. This function compares a string of numbers to the secret <code>code</code> the object stor... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-23T16:22:10.877",
"Id": "33409",
"Score": "0",
"body": "Why do you return `guess` also?\nAnd what is `guess.length() - correct - match`?"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-23T16:34:09.187",
... | [
{
"body": "<p>The algorithm is short and concise. There might be a more clever way to do it, but it would likely be less readable. </p>\n\n<p>This code will break if the <code>guess</code> is not the same length as the code. An empty string causes an out of out of bounds exception. A string longer than the code... | {
"AcceptedAnswerId": "20831",
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-23T15:07:02.527",
"Id": "20825",
"Score": "1",
"Tags": [
"java",
"optimization",
"algorithm"
],
"Title": "Evaluating result for mastermind comparison"
} | 20825 |
<p>I have a class file that would be used to connect and execute queries into the database. The only thing I am confused about is this: do you really need to drop tables and re-create them everytime you run the application? Or does this depend on your coding or class file? I followed a tutorial on the Internet and kind... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-24T22:38:39.220",
"Id": "33483",
"Score": "1",
"body": "Heck no, you don't need to be constantly creating/dropping the tables. For one, thing, that's terribly 'expensive'. Also, what happens when users come back to the app - they'd c... | [
{
"body": "<p>If you're using one instance of <code>SQLiteOpenHelper</code> across your application (which you should be doing), you should never need to call <code>close</code> on it or the <code>SQLiteDatabase</code> object.</p>\n\n<p>Why do you need the <code>DBConnect</code> class? See <a href=\"https://cod... | {
"AcceptedAnswerId": null,
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-23T15:53:15.007",
"Id": "20827",
"Score": "2",
"Tags": [
"java",
"beginner",
"android",
"sqlite"
],
"Title": "SQLite class to manage recipes for an Android application"
} | 20827 |
<p>I have a view model with properties that represent years and months: </p>
<pre><code>public IEnumerable<SelectListItem> Years
{
get
{
return new SelectList(
Enumerable.Range(1900, 112)
.OrderByDescending(year => year)
.Select(year => new SelectListIte... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-23T19:29:57.780",
"Id": "33422",
"Score": "2",
"body": "You don't need to order anything here, `.Reverse()` is simpler and more efficient. Also, do you really want to have the months backwards, or is that a copy & paste error?"
},
... | [
{
"body": "<p>You're over-creating these objects on each call. Cache static values:</p>\n\n<pre><code> private static readonly IEnumerable<SelectListItem> years = new SelectList(\n Enumerable.Range(1900, 112)\n .OrderByDescending(year => year)\n .Select(year => new SelectList... | {
"AcceptedAnswerId": "20834",
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-23T17:58:58.923",
"Id": "20832",
"Score": "4",
"Tags": [
"c#",
"asp.net",
"datetime",
"asp.net-mvc-3"
],
"Title": "View model with properties that represent years and months"
... | 20832 |
<p>In the interests of improving my Python coding skills, I wanted to post a program I recently built and get it critiqued by you fine folks. Please let me know where you think I might improve this program. I tried to stick to PEP8 and follow other standard conventions mentioned here.</p>
<pre><code>### This program w... | [] | [
{
"body": "<pre><code>### This program will filter a list of tweets by a certain\n### threshold of retweets divided by followers\n\n# Fetch tweets from Twitter list\n# Store them in SQlite3\n# Query database\n# Output an HTML file with the results\n# Clean database of data older than a month\n\nimport json\nimp... | {
"AcceptedAnswerId": "20838",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-23T19:38:50.300",
"Id": "20837",
"Score": "2",
"Tags": [
"python",
"parsing",
"twitter"
],
"Title": "Python Twitter parser"
} | 20837 |
<p>I wrote this class to make it reusable in any project. It uses PDO as the MySQL connection. Beside not using Setters and Getters, is this code secure and good practice for CRUD?</p>
<pre><code>class ContactsRow
{
const PDO_CHARSET = PDO_CHARSET;
const PDO_DBNAME = PDO_DBNAME;
const PDO_DRIVER = PDO_DRIVER;
const PD... | [] | [
{
"body": "<p>The main security concern here I guess is protection from sql injection. Good news. You got that part right by using bound parameters.</p>\n\n<p>Here is what you should improve and why:</p>\n\n<p>Make the name of the class match what it is. In this case, it represents 1 Contact. So, name it Contac... | {
"AcceptedAnswerId": "20887",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-23T21:52:41.250",
"Id": "20841",
"Score": "1",
"Tags": [
"php",
"object-oriented",
"mysql",
"pdo",
"crud"
],
"Title": "Reusable project for CRUD"
} | 20841 |
<p>This technique is more general than just for config files which is why I'm not using Config::Any in this example.</p>
<p>I have a config file with lines like this:</p>
<pre><code>userid foo-admin # foo-comment
directory /some/dir # bar-comment
</code></pre>
<p>And I'm using some very awkward code to get the value... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-24T16:33:45.680",
"Id": "33453",
"Score": "1",
"body": "even though you may not just be using it for \"config\" files, can't you still use a module like Config::Any?"
}
] | [
{
"body": "<p>If you're determined to avoid using a module for this, how about something like the following:</p>\n\n<pre><code>my %config;\nwhile (my $line = <$configfh>) {\n chomp $line; # Strip trailing linefeeds, you probably don't want them there\n my @fields = split(/\\s+/, $line);\n $confi... | {
"AcceptedAnswerId": "20870",
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-23T22:16:27.130",
"Id": "20843",
"Score": "3",
"Tags": [
"perl"
],
"Title": "Reading fields idiomatically"
} | 20843 |
<p>Just looking for some critiques hopefully from haskellers of how I might be breaking monad laws or just monadding all wrong. My <code>or</code> is something like the <code>mplus</code> for <code>either</code> and usable as a catch for failures from <code>then</code> which behaves mostly like the error monad.</p>
<p... | [] | [
{
"body": "<pre><code>liftM2 f m1 m2 = do { x1 <- m1; x2 <- m2; return (f x1 x2) }\nap = liftM2 id\n</code></pre>\n\n<p><code>ap</code> is already defined in <a href=\"http://www.haskell.org/ghc/docs/6.12.2/html/libraries/base-4.2.0.1/src/Control-Monad.html#line-307\" rel=\"nofollow\"... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-23T23:14:02.517",
"Id": "20845",
"Score": "2",
"Tags": [
"javascript",
"haskell"
],
"Title": "Wrote javascript version of error monad / monadplus, did I obey monad laws correctly?"
} | 20845 |
<p>I have an event recurring on the first friday of the month (at 7pm), and I need to output the date of the next first friday coming up.</p>
<p>This is the first bit of PHP I've written, and I was wondering if there is a better way to implement this? Server is running PHP 5.3</p>
<p>Here's what I wrote:</p>
<pre><c... | [] | [
{
"body": "<p>You can improve your ternary operator by using it like this :</p>\n\n<pre><code>$today = new DateTime();\n$this_months_friday = new DateTime('first friday of this month');\n$next_months_friday = new DateTime('first friday of next month');\necho 'Friday, '. (($today < $this_months_friday) ? $thi... | {
"AcceptedAnswerId": "20847",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-23T23:18:17.943",
"Id": "20846",
"Score": "3",
"Tags": [
"php"
],
"Title": "PHP First Friday Display Improvement?"
} | 20846 |
<p>I saw a posting on Hacker News this morning that was ranting about people not being able to solve an interview question. I thought I would give it a shot, but I would like to know how my attempt could be improved.</p>
<pre><code>def get_pascal_row(n):
"""
Returns the nth row of Pascal's Triangle for a given... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-24T01:45:35.923",
"Id": "33431",
"Score": "0",
"body": "Stop half way through and stick a copy of the first half reversed to the end of your return."
}
] | [
{
"body": "<p>You can <a href=\"http://en.wikipedia.org/wiki/Memoization\" rel=\"nofollow noreferrer\">memoize</a> the rows you have already computed previously. </p>\n\n<p>Alternatively, you could compute it in closed form using the <a href=\"http://en.wikipedia.org/wiki/Binomial_coefficient\" rel=\"nofollow ... | {
"AcceptedAnswerId": null,
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-24T01:32:07.507",
"Id": "20850",
"Score": "4",
"Tags": [
"python"
],
"Title": "How can this function be faster? Solving for a row of Pascal's triangle"
} | 20850 |
<p>I am trying to model a domain of metrics for use in a BI application. I recently read the <a href="http://fsharpforfunandprofit.com/posts/designing-with-types-intro/" rel="nofollow">Designing with Types</a> series, and felt that it could be useful, so I wanted to give it a try. What I'm not sure is if I'm trying t... | [] | [
{
"body": "<p>Do you have only a limited number of metric types?</p>\n\n<p>In which case, you might want to make a union case of the possible types.\nThe compound cases can be recursive, referring to the type.</p>\n\n<pre><code>type Metric = \n | Revenue of string * string \n | Volume of string * string\... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-24T04:44:53.620",
"Id": "20852",
"Score": "2",
"Tags": [
"f#",
"mongodb"
],
"Title": "Using Types to Designing Domain"
} | 20852 |
<p>My function converts an improper fraction to a mixed number (it does not AND should not simplify the fraction).</p>
<pre><code>// Improper fraction to mixed number
// n = numerator
// d = denominator
// i = number
function improperFractionToMixedNumber(n, d) {
i = (n / d) >> 0;
n -= i * d;
retur... | [] | [
{
"body": "<p>What you're doing with <code>i = (n / d) >> 0</code> is a sign-propagating right shift. The interesting this is, you're doing a right shift by 0 (I'd imagine because you'd otherwise get <code>NaN</code>. Your method of doing so is a cheap version (read: performance hack?) of doing <code>i = ... | {
"AcceptedAnswerId": "20868",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-24T06:57:34.810",
"Id": "20854",
"Score": "4",
"Tags": [
"javascript"
],
"Title": "Convert Improper Fraction to Mixed Number with JavaScript"
} | 20854 |
<p>From what I understand this site help to check if my solution is proper or not..so forgive me if the header is bad.</p>
<p>This is my solution for the question:</p>
<pre><code>#include <stdio.h>
int main()
{
double num1, num2, different, product, answer;
printf("please enter 2 floatig point numb... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-24T11:53:54.683",
"Id": "33439",
"Score": "0",
"body": "Are you using C99 (the newer version of the language)?"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-24T12:20:07.663",
"Id": "33440",
"Score... | [
{
"body": "<p>For a newbie, your solution is ok. You can simplify it quite a bit though by using the function <code>fabs</code>. It's a fairly simple function: <code>fabs(x)</code> returns <code>x</code> if <code>x >= 0</code>, and returns <code>-x</code> if <code>x < 0</code>. So all of your <code>if</co... | {
"AcceptedAnswerId": "20861",
"CommentCount": "4",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-24T10:30:55.173",
"Id": "20858",
"Score": "1",
"Tags": [
"c"
],
"Title": "Program that requests two floating-point numbers and prints the value of their difference divided by their produc... | 20858 |
<p>For the first time I have to play with Threads in java.</p>
<p>Basically, my code is a simple FIFO job queue.
The main thread regularly puts jobs in the queue wich need to be executed.</p>
<p>The code below is a striped down version of what I am doing :</p>
<pre><code>public class JobPerformer implements Runnable... | [] | [
{
"body": "<p>Why don't you use the classes provided in java.util.concurrent that can handle these requirements? Or is this written for practice?</p>\n\n<p>There is still problem if you don't synchronize, because the <code>ArrayList.add()</code> method is not thread-safe thus it can happen that two threads tryi... | {
"AcceptedAnswerId": "20860",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-24T11:40:57.370",
"Id": "20859",
"Score": "3",
"Tags": [
"java",
"multithreading",
"thread-safety"
],
"Title": "Simple FIFO Job list in Java"
} | 20859 |
<p>The query works fine but I'm just intrigued to know if there is a cleaner solution. The data are results from a survey and I'm obtaining the counts per question within a zone and an overall zone count.</p>
<pre><code>With Cte1 (ZoneId, QuestionId, Count1)
AS
(
Select
tz.ZoneId,
qn.QuestionId,
... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-24T15:31:38.153",
"Id": "33447",
"Score": "1",
"body": "The answer to your question is `rollup`, but that depends on the database that you are using."
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-24T16:48... | [
{
"body": "<p>how about using <code>CASE</code></p>\n\n<pre><code>SELECT tz.ZoneId,\n qn.QuestionId,\n SUM(CASE WHEN Event >= 201201 AND Event <= 201212\n THEN 1 \n ELSE 0\n END) as Count1,\n COUNT(*) AS ZoneTotal\nFROM Customers cust\n ... | {
"AcceptedAnswerId": "138671",
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-24T15:26:48.800",
"Id": "20865",
"Score": "2",
"Tags": [
"sql",
"sql-server",
"t-sql"
],
"Title": "SQL query to obtain totals and subtotals"
} | 20865 |
<p>This is the code I implemented so far to create a single instance WPF application:</p>
<pre><code>#region Using Directives
using System;
using System.Globalization;
using System.Reflection;
using System.Threading;
using System.Windows;
using System.Windows.Interop;
#endregion
namespace MyWPF
{
public partial c... | [] | [
{
"body": "<p>This might be against the spirit of Code Review, but you don't need to write your own single instance manager for WPF! <a href=\"http://blogs.microsoft.co.il/blogs/arik/archive/2010/05/28/wpf-single-instance-application.aspx\">Microsoft has already written code to accomplish this</a>, but it has b... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-24T17:22:48.303",
"Id": "20871",
"Score": "31",
"Tags": [
"c#",
"singleton",
"wpf"
],
"Title": "Single-instance WPF application"
} | 20871 |
<p>I'm creating cards for a game of <a href="https://en.wikipedia.org/wiki/Set_%28card_game%29" rel="nofollow noreferrer">SET</a>.</p>
<p>Can the following be rewritten in a more programmatic way, possibly getting rid of the embedded <code>for</code> loops?</p>
<pre><code>function createCards() {
var cards = [];
... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-24T19:10:23.467",
"Id": "33468",
"Score": "5",
"body": "What is the purpose of the code? Are those playing cards in a game?"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-24T19:11:37.543",
"Id": "3346... | [
{
"body": "<p>If you wanted the number and size of dimensions for your final cards array to be dynamic, you could use a recursive solution like this (<a href=\"http://jsfiddle.net/QshGE/\"><strong>DEMO</strong></a>):</p>\n\n<pre><code>function createCards(dimensions) {\n if (dimensions.length == 0) {\n ... | {
"AcceptedAnswerId": "20877",
"CommentCount": "8",
"ContentLicense": "CC BY-SA 4.0",
"CreationDate": "2013-01-24T19:08:20.623",
"Id": "20874",
"Score": "14",
"Tags": [
"javascript",
"node.js",
"ecmascript-6",
"playing-cards"
],
"Title": "Creating cards for a game of SET"
} | 20874 |
<p>I was solving some <code>Haskell</code> exercises as a way to refresh some of the language concepts.</p>
<p>I encounter the following exercise:</p>
<pre><code>data LTree a = Leaf a | Fork (LTree a) (LTree a)
crossing :: LTree a -> [(a,Int)]
crossing (Leaf x) = [(x,0)]
crossing (Fork e d) = map(\(x,y) -> (x,... | [] | [
{
"body": "<p>I think this is a continuation-passing solution (simple parser) for writing <code>build</code>:</p>\n\n<pre><code>build :: [(a,Int)] -> LTree a\nbuild xs = parseAt 0 xs fst\n\n-- The first Int is the depth of the \"root\" of parseAt\nparseAt :: Int -> [(a,Int)] -> ( (LTree a, [(a,Int)]) -... | {
"AcceptedAnswerId": "20879",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-24T21:18:00.577",
"Id": "20878",
"Score": "1",
"Tags": [
"haskell"
],
"Title": "Ltree inverse crossing function using high order functions"
} | 20878 |
<p>I've been working on converting some legacy code from T-SQL stored procedures. My aim is to use DDD to make the transition. Below is an attempt to do this. Any comments on how well this is following DDD? Also any comments on the AreaGeneration domain service and its CalculateLocationsInArea would be most appreciated... | [] | [
{
"body": "<p>I do like what you've done here. I'm a big fan of immutable objects and interface-based programming (for dependency injection, mocking via unit tests, etc.), so I've done a little squidging to make those happen as per below:</p>\n\n<pre><code> // consumer code\n var locationRepositor... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-24T23:53:10.253",
"Id": "20883",
"Score": "7",
"Tags": [
"c#",
"design-patterns"
],
"Title": "How can I make this C# code better so it more closely follows DDD principles? Am I using fac... | 20883 |
<p>Please tell me if this function is correct (I believe it to be), efficient, and clean.</p>
<p>Function to check:</p>
<pre><code>::CURLcode CURL_DownloadFile( __in ::LPCSTR URL, __in ::LPCSTR Destination )
{
::CURL* Curl;
::CURLcode Return_CurlCode;
std::FILE* File;
if( ( Curl = ::curl_easy_init( )... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-25T03:10:13.060",
"Id": "33490",
"Score": "0",
"body": "One obvious thing that strikes me is `::CURLcode Return_CurlCode;` is never initialized if `::curl_easy_init()` returns false."
},
{
"ContentLicense": "CC BY-SA 3.0",
... | [
{
"body": "<p>Your write function is incorrect:</p>\n\n<pre><code>size_t WriteData( void *ptr, size_t size, size_t nmemb, std::FILE *stream ) {\n size_t written;\n written = fwrite(ptr, size, nmemb, stream);\n return written;\n}\n</code></pre>\n\n<p>CURL will abort the transfer if you do not return <co... | {
"AcceptedAnswerId": null,
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-25T02:18:30.237",
"Id": "20886",
"Score": "3",
"Tags": [
"c++",
"file-system",
"curl"
],
"Title": "DownloadFile Function (using LibCurl)"
} | 20886 |
<p>I wondered if anyone has any ideas to make this cleaner or more efficient. Mostly this was an exercise to generate the data once (So speed really isn't too important, but I'd love to see what techniques you could think of)
This is an algorithm I derived and implemented without external guidance of any kind, so I'm ... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-25T15:35:47.243",
"Id": "33513",
"Score": "3",
"body": "First and foremost: if you have any complicated algorithm in your code, *you need to explain it* in comments. Otherwise, it's hard to understand what your code does, which makes i... | [
{
"body": "<ul>\n<li><p>You have some \"Magic Numbers\". If you wanted to work with a different grid size you'd have to replace <code>9</code> at many different places. Find the relationships between your hard-coded numbers and declare them as <code>const</code>. For example, <code>const int Size = 9;</code> an... | {
"AcceptedAnswerId": null,
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-25T03:34:45.263",
"Id": "20888",
"Score": "4",
"Tags": [
"c#",
"performance",
"algorithm",
"sudoku"
],
"Title": "Sudoku Valid Arangements Permutations Enumerator"
} | 20888 |
<p>I have this code that generally creates directories within a directory.</p>
<pre><code>File userFolder = new File(rootPath+"/media/"+userBean.getUsername());
File userPhotosDir = new File(rootPath+"/media/"+userBean.getUsername()+"/photos");
File userAudioDir = new File(rootPath+"/media/"+userBean.getUse... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-25T18:14:01.600",
"Id": "33521",
"Score": "0",
"body": "what is the question?"
}
] | [
{
"body": "<p>Well, for the purpose of codereview:</p>\n\n<ul>\n<li>There is a <code>mkdirs()</code> methods instead of the <code>mkdir()</code> method</li>\n<li>You can use the <code>new File(String, String)</code> constructor to avoid taking care of slashes</li>\n<li><p>If you have Java7, you can (and probabl... | {
"AcceptedAnswerId": null,
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-25T10:55:01.837",
"Id": "20892",
"Score": "3",
"Tags": [
"java",
"file-system",
"directory"
],
"Title": "Better way to create directories using File"
} | 20892 |
<p>I've recently discovered the wonders of the Python world, and am quickly learning. Coming from <code>Windows/C#/.NET</code>, I find it refreshing working in <code>Python</code> on <code>Linux</code>. A day you've learned something new is not a day wasted.</p>
<p>I need to unpack data received from a device. Data is... | [] | [
{
"body": "<p><em>simple unpacking</em> can be done with the zip built-in as @Lattyware has pointed out:<br>\nthat could be something like: </p>\n\n<pre><code>zip(*[data[idx:idx + num_channels] for idx in range(0, len(data), num_channels)])\n</code></pre>\n\n<p>(note the (*) which is inverting zip by handing ov... | {
"AcceptedAnswerId": "20909",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-25T14:16:14.827",
"Id": "20895",
"Score": "3",
"Tags": [
"python",
"numpy"
],
"Title": "A pythonic way of de-interleaving a list (i.e. data from a generator), into multiple lists"
} | 20895 |
<p>I'm having concerns about the readability of this piece of code:</p>
<pre class="lang-py prettyprint-override"><code> messages_dict = {'error':errors, 'warning':warnings}[severity]
messages_dict[field_key] = message
</code></pre>
<p>and I'm to use this instead:</p>
<pre class="lang-py prettyprint-override"><c... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-25T14:43:51.320",
"Id": "33509",
"Score": "0",
"body": "I don't see the readability concern with the first case."
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-25T14:52:20.773",
"Id": "33510",
"Sco... | [
{
"body": "<p>Since you mention maintainability I think that the consideration can be taken in terms of what next changes may look like. If further changes will take more statements to be executed with the result of one of those specific options, or if more options will be added, I would stick to the dictionary... | {
"AcceptedAnswerId": null,
"CommentCount": "5",
"ContentLicense": "CC BY-SA 4.0",
"CreationDate": "2013-01-25T14:41:37.067",
"Id": "20896",
"Score": "1",
"Tags": [
"python",
"hash-map",
"lookup"
],
"Title": "Readability concerns with literal dictionary lookup vs if-else"
} | 20896 |
<p>I am trying to write an idiomatic trim function in C. How does this look? Should I instead malloc the new string and return it?</p>
<pre><code>void trim(const char *input, char *result)
{
int i, j = 0;
for (i = 0; input[i] != '\0'; i++) {
if (!isspace(input[i])) {
result[j++] = input[i];
}
}
}
<... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-25T15:12:38.673",
"Id": "33512",
"Score": "4",
"body": "There's a number of problems with that code, it's vulnerable to buffer overflow attacks and it doesn't do what a typical \"trim\" function does. Trim removes leading and trailing ... | [
{
"body": "<p>As @JeffMercado pointed out, this removes spaces instead of trimming leading and trailing spaces. Assuming you want to keep the current functionality, let's call it <code>remove_spaces</code>.</p>\n\n<p>There's a really subtle bug here:</p>\n\n<pre><code>... isspace(input[i]) ...\n</code></pre>\n... | {
"AcceptedAnswerId": null,
"CommentCount": "3",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-25T15:01:34.907",
"Id": "20897",
"Score": "5",
"Tags": [
"c",
"strings"
],
"Title": "Trim function in C"
} | 20897 |
<p>I have a view model that I'm migrating to RxUI. It exposes a collection of brokers along with a <code>bool</code> indicating whether there is a market (ie. at least one broker). In addition, it has a property for the currently selected broker (the user can choose a broker via a drop-down):</p>
<pre><code>private IR... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-26T08:39:55.993",
"Id": "33540",
"Score": "0",
"body": "Thank you for this question, Kent. I think you just introduced me to Rx :)"
}
] | [
{
"body": "<blockquote>\n <p>I currently create a new derived collection each time Volatilities changes</p>\n</blockquote>\n\n<p>This was a good idea, but in this case I'd just recreate the brokers list (since it's not a straight <code>volatilities.Select(x => ...)</code>, it's a SelectMany. One thing that'... | {
"AcceptedAnswerId": "20921",
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-25T12:50:20.390",
"Id": "20900",
"Score": "6",
"Tags": [
"c#",
"wpf",
"mvvm",
"system.reactive"
],
"Title": "Can this reactive code be improved?"
} | 20900 |
<p>I need to lock on my <a href="http://msdn.microsoft.com/en-us/library/system.security.cryptography.aesmanaged.aspx" rel="nofollow">AesManaged</a> instance <code>_Aes</code> to call <a href="http://msdn.microsoft.com/en-us/library/bb338332.aspx" rel="nofollow">CreateDecryptor()</a> however the CreateDecryptor() metho... | [] | [
{
"body": "<p>I would say you're not using AES correctly - don't hold on to an instance of it in your class (guessing by the underscore in the name) but rather create it as needed, while holding AES's parameters (the key and IV) in your class:</p>\n\n<pre><code>using (var aes = new AesManaged() { IV = _IV, Key ... | {
"AcceptedAnswerId": "20908",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-25T17:30:12.363",
"Id": "20904",
"Score": "4",
"Tags": [
"c#",
"multithreading",
"locking"
],
"Title": "Getting a decryptor object"
} | 20904 |
<p>Please tell me how bad is the code...in the book he said it's possible with either nested loop, or 1 loop</p>
<p>The user should provide the 8 double numbers for the program to set the cumulative totals in the second array.</p>
<p>This is the code:</p>
<pre><code>#include <stdio.h>
#define ASIZE 8
int main... | [] | [
{
"body": "<p>About the only thing I would change is this loop</p>\n\n<pre><code>for (x = 0,index2 = 0,index = 0; x < ASIZE; x++, index2++)//adding the second array the elements\n{\n cal += array1[index++];\n array2[index2] = cal;\n}\n</code></pre>\n\n<p>You don't need <code>x</code> (its really <code>... | {
"AcceptedAnswerId": "20907",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-25T17:32:03.420",
"Id": "20905",
"Score": "1",
"Tags": [
"c"
],
"Title": "Print 2 double arrrays, the second one should be cumulative totals of the elements of the first array"
} | 20905 |
<p>When the page loads, it checks to see if the user is trying to update an existing book or create a new book.</p>
<p>Something about my code just doesn't seem right. It feels cumbersome and not very logical. Could someone put me out of my misery and let me know that I wrote this bit ok?</p>
<pre><code>Public Prope... | [] | [
{
"body": "<p>The only real issue I can see (it's been a while since I worked in VB, so there may be other things) is using a global value to store the ID of the created record. Make your <code>Create()</code> sub a function and return the ID instead of storing it in the property; that will remove a potential s... | {
"AcceptedAnswerId": "23337",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-25T20:13:43.727",
"Id": "20914",
"Score": "1",
"Tags": [
".net",
"asp.net",
"vb.net"
],
"Title": "Creating a new item or updating an existing item on page load"
} | 20914 |
<p>If I need to create a IDisposeable object from a factory, but if the factory object is not thread safe and requires me to lock on it is this the correct pattern to use?</p>
<pre><code>public void DisposeExample(FactoryClass factoryClass)
{
DispObject dispObject = null;
lock(factoryClass)
{
disp... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-25T22:20:32.367",
"Id": "33536",
"Score": "1",
"body": "CodeReview forum is probably not the best for this type of questions, you'll get more attention on [StackOverflow](http://stackoverflow.com/)"
},
{
"ContentLicense": "CC B... | [
{
"body": "<p>If your question is \"will the dispObject get disposed?\", the answer is \"Yes, that's a valid use of using.\" See the <a href=\"http://msdn.microsoft.com/en-us/library/yh598w02%28v=vs.110%29.aspx\" rel=\"nofollow\">using reference</a>, third sample under remarks. </p>\n",
"comments": [],
... | {
"AcceptedAnswerId": "20918",
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-25T22:00:07.497",
"Id": "20915",
"Score": "1",
"Tags": [
"c#",
"multithreading",
"locking"
],
"Title": "How to correctly get a IDisposeable when you need to lock the factory?"
} | 20915 |
<p>My recipes model is as follows</p>
<pre><code>has_many :quantities
has_many :ingredients, :through => :quantities, :uniq => true
has_many :sizes, :through => :quantities, :uniq => true
</code></pre>
<p>And the controller action im trying to refactor</p>
<pre><code>class Admin::QuantitiesController <... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-26T13:39:22.450",
"Id": "33546",
"Score": "0",
"body": "This is confusing. You are editing a recipe, right? (`edit_admin_recipe_path`), then why are you getting the POST in `QuantitiesController#create`? It should be in `RecipesControl... | [
{
"body": "<p>Some notes:</p>\n\n<ul>\n<li><p>This is a <code>edit_admin_recipe</code> form, so you should POST to <code>Admin::RecipesController#update</code>. </p></li>\n<li><p>Use Rails infrastructure for the models (<a href=\"http://asciicasts.com/episodes/196-nested-model-form-part-1\" rel=\"nofollow\">acc... | {
"AcceptedAnswerId": "20964",
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-25T22:06:53.157",
"Id": "20916",
"Score": "2",
"Tags": [
"ruby",
"ruby-on-rails"
],
"Title": "Updating / validating mass update rails association"
} | 20916 |
<p>I know there are a few recursive ways to solve this problem but the code below is what came to mind for me.</p>
<pre><code>void create_bst(node *root, vector<int> arr)
{
int len = arr.size();
add_nodes(root,len-1); //Make the tree structure
inorder_traversal(root,arr);
}
void add_nodes(node *n, i... | [] | [
{
"body": "<ol>\n<li><p>Your code will only work one time, because of the <code>static int i</code> in your inorder_traversal function. You have no way to reset <code>i</code> to <code>0</code></p></li>\n<li><p>Splitting creation and filling into two parts is not what I'd call efficient. This way you have to tr... | {
"AcceptedAnswerId": "20939",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-26T03:20:45.133",
"Id": "20924",
"Score": "2",
"Tags": [
"c++",
"optimization"
],
"Title": "Given a sorted array in increasing order, create a BST of minimum height"
} | 20924 |
<p>I am a programming novice. I've written this simple PHP script to run a very basic comment form and would appreciate any feedback, especially on these three topics:</p>
<ol>
<li>Efficiency (not sure the right word for this): basically, am I doing anything in a clearly awkward/inferior/inelegant way, in terms of usi... | [] | [
{
"body": "<ul>\n<li>separate the HTML/layout from the PHP</li>\n<li>(don't store the resulting HTML of the comments, a CSV/DB with name, comment and date should be more suitable. This is more effort in first instance, but otherwise you will not be able to change the layout in the future. Also see Robs comment ... | {
"AcceptedAnswerId": "20944",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-26T05:10:49.047",
"Id": "20925",
"Score": "2",
"Tags": [
"php",
"optimization",
"beginner",
"security"
],
"Title": "Basic PHP comment form"
} | 20925 |
<p>I was solving the <a href="https://www.facebook.com/hackercup/problems.php?pid=494433657264959&round=185564241586420" rel="nofollow">"Find the Min"</a> problem on Facebook Hacker Cup.</p>
<p>The code below works fine for the sample inputs given there, but for input size as big as 10<sup>9</sup>, this takes hour... | [] | [
{
"body": "<p>Untested, but using <code>Counter</code> from <a href=\"http://docs.python.org/2/library/collections.html\" rel=\"nofollow\"><code>collections</code></a> may be quicker than forming a set of the last <code>k</code> values each time.</p>\n\n<pre><code>counter = collections.Counter(m)\nfor j in xran... | {
"AcceptedAnswerId": "21055",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-26T07:06:50.447",
"Id": "20926",
"Score": "4",
"Tags": [
"python",
"optimization",
"performance",
"programming-challenge"
],
"Title": "\"Find the Min\" challenge on Facebook H... | 20926 |
<p>I have the following example app: <a href="http://dev.driz.co.uk/jsfunction/" rel="nofollow">http://dev.driz.co.uk/jsfunction/</a></p>
<p>Which runs two types of script files with the same outcome.</p>
<p>Script1:</p>
<pre><code>var script1 = {
showMessage: function( message ) {
var el = document.... | [] | [
{
"body": "<p>Try module-pattern, or somewhat like the module pattern, using an <a href=\"http://benalman.com/news/2010/11/immediately-invoked-function-expression/\" rel=\"nofollow\">IIFE</a></p>\n\n<p>In your case:</p>\n\n<pre><code>(function(ns){\n\n ns.showMessage = function(message){\n var el = document... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-01-26T11:12:31.193",
"Id": "20928",
"Score": "3",
"Tags": [
"javascript"
],
"Title": "Namespaced JavasScript code"
} | 20928 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.