body stringlengths 25 86.7k | comments list | answers list | meta_data dict | question_id stringlengths 1 6 |
|---|---|---|---|---|
<p>I have a class that I inherited from someone else. It is doing a P/Invoke on <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/aa363852%28v=vs.85%29.aspx" rel="nofollow">CopyFileEx</a> to copy a file (we are using UNC shares if that matters). The code is marked <code>unsafe</code> but I thought you on... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-04T21:16:50.130",
"Id": "38200",
"Score": "0",
"body": "Are you sure just using `System.Environment.OSVersion.Version.Major > 5` is okay? What happens if the program is run on another OS (say Unix)?"
},
{
"ContentLicense": "CC... | [
{
"body": "<p><code>unsafe</code> is purely a compile-time check. If it were needed the code wouldn’t compile. The runtime errors are caused by something else.</p>\n\n<p>Furthermore, <code>unsafe</code> is needed if and only if you are using unmanaged pointers (e.g. inside a <code>fixed</code> block).</p>\n",
... | {
"AcceptedAnswerId": "24731",
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-04T20:30:24.780",
"Id": "24730",
"Score": "2",
"Tags": [
"c#"
],
"Title": "Is this \"unsafe\" declaration unnecessary?"
} | 24730 |
<p>In my Blackjack game so far, I have multiple classes that access each other frequently. For example, this is my <code>hit()</code> function for the <code>Game</code> class:</p>
<pre><code>void Game::hit(unsigned playerNum)
{
Card newCard = deck.deal();
players[playerNum].getPlayerHand()[0].getHandCards().p... | [] | [
{
"body": "<p>This code:</p>\n\n<pre><code>void Game::hit(unsigned playerNum)\n{\n Card newCard = deck.deal();\n players[playerNum].getPlayerHand()[0].getHandCards().push_back(newCard);\n}\n</code></pre>\n\n<p>should read:</p>\n\n<pre><code>void Game::hit(unsigned playerNum)\n{\n Card newCard = deck.de... | {
"AcceptedAnswerId": "24736",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-04T21:28:29.560",
"Id": "24732",
"Score": "3",
"Tags": [
"c++",
"object-oriented",
"playing-cards"
],
"Title": "Accessing multiple data members in Blackjack classes"
} | 24732 |
<p>I run into this often. I have two functions very similar, that if I combine would be more DRY but if I didn't would be easier to read and make more sense.</p>
<p>In this case I have two implementations of fade, one using <code>setInterval</code> and one using <code>setTimeout</code>.</p>
<p>How do I not get stuck... | [] | [
{
"body": "<p>It's generally a good idea to remove duplication wherever possible. If you have two functions that are <strong>logically</strong> similar, but don't share code, you run the risk of <strong>code rot</strong> - updating one function and forgetting to apply similar changes to the other.</p>\n\n<p>The... | {
"AcceptedAnswerId": "24737",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-04T21:39:44.003",
"Id": "24734",
"Score": "2",
"Tags": [
"javascript"
],
"Title": "Concept - DRY code vs. Readable code?"
} | 24734 |
<p>This is a follow-up to <a href="https://codereview.stackexchange.com/questions/24499/code-tidying-up">this</a> question my code has evolved since so I'm reposting my question.</p>
<p>What is the best way to tidy this up? How can this be refined? I am finding it's thrashing the layout quite a lot and the scroll func... | [] | [
{
"body": "<p>Interesting question:</p>\n\n<p><strong>Naming</strong></p>\n\n<ul>\n<li>JavaScript uses lowerCamelCase variable and function names. ( <code>tblcontentstwo</code> -> <code>TblContentsTwo</code> or <code>TabelContents2</code></li>\n<li>Some of your prefixes like <code>sb</code> are not adding anyth... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-04T23:52:57.337",
"Id": "24739",
"Score": "2",
"Tags": [
"javascript",
"jquery",
"animation",
"wordpress"
],
"Title": "jQuery eye candy for a WordPress site"
} | 24739 |
<p>I wrote an HTTP server for the management of scores for users and levels.</p>
<p>It can return the highest score per level. It has a simple login with session-key.</p>
<p>What do you think could be improved in this code? What's wrong? And particularly, are there any multi-threading issues that I didn't take into ... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-06T18:08:29.677",
"Id": "38320",
"Score": "0",
"body": "Your singleton getInstance() method is not thread-safe. See double-check-locking pattern and so on"
}
] | [
{
"body": "<h2>Volatile</h2>\n<p>I don't have time to write much here, but I did want to comment on the use of <code>volatile</code> members in both of your singletons.</p>\n<p>In my experience, <em>the majority</em> of the time that I see <code>volatile</code> in code, it's being misused. <code>volatile</code... | {
"AcceptedAnswerId": null,
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-05T00:02:57.733",
"Id": "24740",
"Score": "14",
"Tags": [
"java",
"multithreading",
"http"
],
"Title": "HTTP server and multi-threading optimization"
} | 24740 |
<p>I've implemented a very simple in-memory "database" just as an exercise. I wanted to see if there's anything obvious I should be doing that fits more of the Haskell way of doing things.</p>
<pre><code>import qualified Data.Map as M
type History = [String]
data Result = Result (Maybe String) Bool DB
data Command ... | [] | [
{
"body": "<p><code>runSetCmd</code>, <code>runGetCmd</code>, and <code>execCmd</code> don't need to be in the IO monad.</p>\n\n<p>It feels a bit wrong to have <code>runGetCmd</code> returning a \"new DB\". Of course, it really returns the same DB, but the caller has no guarantee.</p>\n\n<p><code>cmdKey</code> ... | {
"AcceptedAnswerId": "24751",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-05T01:44:01.833",
"Id": "24741",
"Score": "6",
"Tags": [
"beginner",
"haskell"
],
"Title": "Very simple in-memory database"
} | 24741 |
<p>I am trying to simply present a table in HTML that is stored in a MySQL database. I would like to use Object Oriented PHP to access and fetch the data for the table. I have spent some time learning the different elements and have tried to put together a generic template I can use to access the tables in the database... | [] | [
{
"body": "<ol>\n<li>The <a href=\"http://php.net/manual/en/pdostatement.fetch.php\" rel=\"nofollow\"><code>fetch()</code></a> call only calls a single row from result set.</li>\n<li>Your <code>foreach</code> loop will, as a result, just go in the first result and do nothing. It may even produce an error/notice... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-05T04:36:33.730",
"Id": "24743",
"Score": "6",
"Tags": [
"php",
"mysql",
"html",
"pdo"
],
"Title": "Create a table from MySQL using PHP PDO"
} | 24743 |
<p>Only these formats are accepted.</p>
<ol>
<li>1.1.1</li>
<li>1.1.1-r</li>
<li>1.1.1-b</li>
<li>1.1.1-r1</li>
<li>1.1.1-b1</li>
</ol>
<p>I wrote this code. What don't I like in it? I used parentheses and now I have two groups. In fact, I don't need to do anything with groups. And there might be some bugs as well.</... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-06T07:05:01.233",
"Id": "38310",
"Score": "0",
"body": "Your last quantifier, `{0,1}` is the same as `?`."
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-08T10:29:05.993",
"Id": "38374",
"Score": "0... | [
{
"body": "<p>It seems to me that you're using * when it looks like that's not the behaviour you want.</p>\n\n<p>i.e. this bit <code>-(b|r)*</code> do you really mean a dash followed by either a 'b' or an 'r' zero or more times? I think it's more likely that you meant more along the lines of: there can be a das... | {
"AcceptedAnswerId": "24750",
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-05T09:07:24.250",
"Id": "24749",
"Score": "2",
"Tags": [
"java",
"regex"
],
"Title": "Regular expression for application version"
} | 24749 |
<p>Each class of my project has several inputs and outputs. Each input, the same as output, depends on it's own set of value-datatype.</p>
<p>I need to provide a mechanism to forward data streams from one instance to another, with ability to split that streams and to combine several streams into one.</p>
<p>There are... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-05T11:57:32.443",
"Id": "38231",
"Score": "0",
"body": "my first question (more to come) is why do you have Context as `abstract` but have no abstract methods in it? abstraction layers help to combine same processes, or have a common m... | [
{
"body": "<p>I think the single biggest issue with your design is that it's not type-safe and relies on strings a lot.</p>\n\n<p>C# is a statically-typed language, you should take advantage of that and let the compiler check for possible errors. This means:</p>\n\n<ol>\n<li>Making <code>Table</code> generic, s... | {
"AcceptedAnswerId": "24765",
"CommentCount": "13",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-05T11:10:07.957",
"Id": "24753",
"Score": "2",
"Tags": [
"c#",
"object-oriented",
"design-patterns"
],
"Title": "Design with Context, Table and Functions"
} | 24753 |
<p>How would you write this?</p>
<pre><code>string halfADayOff = "Half a day off";
string oneDayOff = "One day off";
string days = "NoDaysOff";
if (typeOfDelegation)
{
if ((differenceInDays == 0) && (arrivalDay.TimeOfDay.Hours > 22))
days = halfADayOff;
else if ((difference... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-05T13:00:19.257",
"Id": "38238",
"Score": "1",
"body": "Can `differenceInDays` be negative? Can `sosire.TimeOfDay.Hours` be greater than 23?"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-05T13:03:26.263",... | [
{
"body": "<p>You could change the logic so it is smaller, but in its current form, it looks more readable and readability has a value. However, I think a few more comments might make it even more readable. Especially comments that show some examples.</p>\n\n<pre><code>//for example: from 10pm to midnight\nif... | {
"AcceptedAnswerId": "24757",
"CommentCount": "5",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-05T12:44:33.327",
"Id": "24755",
"Score": "11",
"Tags": [
"c#"
],
"Title": "Complex if else"
} | 24755 |
<p>I develop my first async TCP socket server and client program in c# and would like to review the first parts of it. I like to get some information’s about smelly code that I missed and what I could improve in the program. I know it's a little bit much code, but it would awesome if you help me to improve my skills!</... | [] | [
{
"body": "<p>White space and Casing is good.</p>\n\n<p>Just a few things to think about:</p>\n\n<ol>\n<li><code>#region</code> is not very well accepted. If you have to use <code>#region</code>, then you should look at moving the code out into a method or its own class.</li>\n<li>Use var instead of explicit d... | {
"AcceptedAnswerId": "24766",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-05T14:04:38.687",
"Id": "24758",
"Score": "10",
"Tags": [
"c#",
"asynchronous",
"socket",
"server",
"client"
],
"Title": "TCP async socket server client communication"
} | 24758 |
<p>I am writing a CSV file generator that's filtering through about seven million database entries (MySQL backend). This part is especially slow and I was wondering if there is a way to make it much faster. I was thinking of wiring to a temp file first before serving it and then deleting the file.</p>
<p>The <code>St... | [] | [
{
"body": "<h3>1. Introduction</h3>\n\n<p>By coincidence, I'm working on a similar problem right now, so here's a chance for me to write up an experiment I ran today, in the hope that it might prove useful. However, the solution presented below is far from ideal (see section 3).</p>\n\n<h3>2. The idea</h3>\n\n<... | {
"AcceptedAnswerId": "24763",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-05T14:50:29.263",
"Id": "24761",
"Score": "10",
"Tags": [
"python",
"performance",
"csv",
"http",
"django"
],
"Title": "Faster Django CSV generation for several million da... | 24761 |
<p>I've been playing around with 8051 assembly lately and thought I would make a little project of implementing RC4, since it is pretty interesting and the algorithm doesn't seem too hard. Plus, taking <code>mod 256</code> is REALLY easy when you're only working with single bytes.</p>
<p>Below I've included my (hopeful... | [] | [
{
"body": "<p>I'm not familiar with 8051 assembly in particular, but there are some readability things I want to point out.</p>\n\n<p><strong>Commenting</strong></p>\n\n<p>It would be more readable to vertically-align all the comments provided for the individual lines. With the code and comments condensed like... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-05T16:09:15.203",
"Id": "24762",
"Score": "7",
"Tags": [
"optimization",
"beginner",
"cryptography",
"assembly"
],
"Title": "RC4 in 8051 assembly optimization"
} | 24762 |
<p>I am writing Python code for a Tic Tac Toe game. I need to write a function that takes in three inputs: board, x, and y. Board being the current display of the board and then x and y being values of 0, 1, or 2. The game is set up to ask the user for coordinates.</p>
<pre><code>def CheckVictory(board, x, y):
... | [] | [
{
"body": "<p>I would start by removing duplication. If you pass in the mark that the player being checked is using, then you can eliminate 1/2 your code.</p>\n\n<pre><code>def CheckVictory(board, x, y, mark):\n\n if board[x][0] == (mark) and board[x][1] == (mark) and board [x][2] == (mark):\n ret... | {
"AcceptedAnswerId": "24890",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-05T16:58:50.477",
"Id": "24764",
"Score": "4",
"Tags": [
"python",
"tic-tac-toe"
],
"Title": "Tic Tac Toe victory check"
} | 24764 |
<p>I'm trying to get the most out of this code, so I would understand what should I look for in the future. The code below, works fine, I just want to make it more efficient. </p>
<p>Any suggestions?</p>
<pre><code>from mrjob.job import MRJob
import operator
import re
# append result from each reducer
output_words ... | [] | [
{
"body": "<p>Since the reduce function in this case is commutative and associative you can use a combiner to pre-aggregate values.</p>\n\n<pre><code>def combiner_count_words(self, word, counts):\n # sum the words we've seen so far\n yield (word, sum(counts))\n\ndef steps(self):\n return [self.mr(mappe... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-05T20:40:26.903",
"Id": "24776",
"Score": "3",
"Tags": [
"python"
],
"Title": "How to improve performace of this Map Reduce function, Python mrjob"
} | 24776 |
<p>Looking for a review of my first published jQuery Plugin. It's for TreeViews, very basic example demo can be seen at: <a href="http://goodcodeguy.github.io/demos/goodtree/index.html" rel="nofollow noreferrer">Demo</a></p>
<p>Everything works fine, just looking to see if I can get some feedback on things that I may ... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-05T21:30:49.827",
"Id": "38289",
"Score": "0",
"body": "Just a question why are you doing ` var tree_parent = this;` if you do not use the var tree parent after!"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013... | [
{
"body": "<p>First off it'd be good to run the code through <a href=\"http://jslint.org\" rel=\"nofollow\">jslint</a> or <a href=\"http://jshint.org\" rel=\"nofollow\">jshint</a>. It identifies a few issues, such as an extra comma in the default options list; that'll break the code in older IE versions. Also, ... | {
"AcceptedAnswerId": "24812",
"CommentCount": "2",
"ContentLicense": "CC BY-SA 4.0",
"CreationDate": "2013-04-05T21:10:39.197",
"Id": "24778",
"Score": "1",
"Tags": [
"javascript",
"jquery",
"plugin"
],
"Title": "Is this jQuery Plugin for TreeView correct?"
} | 24778 |
<pre><code>public static void listPrimesThree(int maxNum){
long startTime = System.currentTimeMillis();
boolean[] booleanArray = new boolean[maxNum+1];
int root = (int)Math.sqrt(maxNum);
for (int m = 2; m <= root; m++){
for (int k = m*m; k <= maxNum; k+=m){
booleanArray[k] = true;
}
}
for (int m = 3; m ... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-06T00:54:31.833",
"Id": "38291",
"Score": "1",
"body": "You can improve the efficiency by skipping sieving numbers that are already known to be composite. For example, inside your first outer for loop, you could do `if (booleanArray[m... | [
{
"body": "<p>In your second, output loop, <code>m</code> enumerates only odd numbers: <code>m = 3, 5, 7, 9, ...</code>. So we don't need to mark any evens in the first loop, as we skip them over anyway:</p>\n\n<pre><code>for (int m = 3; m <= root; m+=2) {\n if (!booleanArray[m]) { // if ... | {
"AcceptedAnswerId": null,
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-06T00:36:56.157",
"Id": "24781",
"Score": "4",
"Tags": [
"java",
"algorithm",
"primes"
],
"Title": "Prime sieve: improve efficiency while keeping it reasonably simple?"
} | 24781 |
<p>There may not be a "right" way to do it, but I'm inexperienced in functional programming. I'd like some feedback on how the code can be written in a more idiomatic way.</p>
<p>Here it is:</p>
<pre><code>import Data.Maybe (fromJust, isJust)
sepInt n = if n >= 10
then ( sepInt ( n `div` 10 ) ) ++ ((... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-06T03:29:07.693",
"Id": "38294",
"Score": "0",
"body": "Also, I highly recommend that when you share code with other people, whether on Stack Overflow or otherwise, that you annotate your functions with types. It makes it much easier ... | [
{
"body": "<p>Ok, so here are some comments while I'm fixing up your code:</p>\n\n<ul>\n<li><p>In <code>sepInt</code>, you can combine <code>div</code> and <code>mod</code> into one computation using <code>quotRem</code></p></li>\n<li><p>An efficient trick for building up a list in the \"wrong order\" is to bui... | {
"AcceptedAnswerId": null,
"CommentCount": "1",
"ContentLicense": "CC BY-SA 4.0",
"CreationDate": "2013-04-06T01:10:55.277",
"Id": "24782",
"Score": "5",
"Tags": [
"strings",
"haskell",
"integer"
],
"Title": "Haskell functions to extract and reverse integers hiding in strings"
} | 24782 |
<p>I have completed my homework with directions as follows:</p>
<blockquote>
<p>Declare and implement a class named Binary. This class will have a
method named printB(int n) that prints all binary strings of length n.
For n = 3, it will print</p>
</blockquote>
<pre><code>000
001
010
011
100
101
110
111
</code><... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-04T01:51:43.883",
"Id": "38296",
"Score": "1",
"body": "+1 for posting your code and not just the assignment. Nice to see someone actually trying to do the work before asking here. :-)"
},
{
"ContentLicense": "CC BY-SA 3.0",
... | [
{
"body": "<p>Speaking from a straight Java perspective, one thing you can do is change your loop header. You evaluate the power every iteration and could do something like:</p>\n\n<pre><code>for (int i = 0, end = Math.pow(2, n); i < end; i++)\n</code></pre>\n\n<p>You also use standard String concatenation w... | {
"AcceptedAnswerId": null,
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-04T01:20:54.573",
"Id": "24783",
"Score": "7",
"Tags": [
"java",
"homework",
"combinatorics",
"formatting",
"number-systems"
],
"Title": "Printing all binary strings of lengt... | 24783 |
<p>I'm new to Haskell, and here is my first not-totally-trivial program. It's the first program I tend to write in any language -- a Markov text generator. I'm wondering what I can change to make it more idiomatic, or what language features I could make better use of.</p>
<pre><code>import Data.List
import System.Envi... | [] | [
{
"body": "<p>First of all, it seems like you never change the MarkovMap in your States. So, why don't you take it out of MarkovState and change the type of, say, transition, to MarkovMap -> State MarkovState Char?</p>\n\n<p>Try to use more combinators and less pattern-matching. For example, the generate functi... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-06T04:58:15.190",
"Id": "24791",
"Score": "4",
"Tags": [
"beginner",
"haskell",
"markov-chain"
],
"Title": "Haskell markov text generator"
} | 24791 |
<p>Please review and recommend improvements. It looks horrible to me, difficult to read, too lengthy, and just plain old inelegant. But I don't see the potential improvements.</p>
<pre><code>private boolean useCachedReportBean(BeanReport cachedReportBean, String strRequestedReportID)
throws ExceptionInvalidRepo... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-07T22:05:11.287",
"Id": "38351",
"Score": "0",
"body": "If you `throw` in an `if`, there is no need for an `else`"
}
] | [
{
"body": "<p>This is not really very different from what you had, but here goes.</p>\n\n<pre><code>private boolean useCachedReportBean(BeanReport cachedReportBean, String strRequestedReportID) \n throws ExceptionInvalidReportRequest \n{\n if (isEmpty(strRequestedReportID)) {\n if (cachedReportBean... | {
"AcceptedAnswerId": "24805",
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-06T10:54:00.133",
"Id": "24793",
"Score": "2",
"Tags": [
"java",
"cache",
"null"
],
"Title": "Checking for a cache hit"
} | 24793 |
<p>I'm implementing <a href="http://en.wikipedia.org/wiki/Kruskal%27s_algorithm" rel="nofollow">Kruskal's algorithm</a> in C++11 and would like feedback on style and performance on my graph data structure and algorithm for educational purposes (for production code, I'd use a pre-existing library). The main design quest... | [] | [
{
"body": "<p>To be honest I am having a hard time following your code.<br>\nSo I can't comment on the efficiency.</p>\n\n<p>But as an engineer I will make comments on maintainability (which I think is much more important (because if you understand the code you can optimize the slow parts once you have measured... | {
"AcceptedAnswerId": "24827",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-06T17:02:55.640",
"Id": "24801",
"Score": "2",
"Tags": [
"c++",
"algorithm",
"c++11",
"graph"
],
"Title": "Is this implementation of Kruskal's algorithm maintainable and effic... | 24801 |
<p>Lets imagine we have an <code>Article</code> entity, with data imported from a legacy database. It contains an <code>appendix</code> column with type <code>integer</code>. This column is always filled with values <code>0</code>, <code>1</code> or <code>nil</code>, so we decide to call it <code>in_appendix</code> wit... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-29T17:40:10.047",
"Id": "39683",
"Score": "0",
"body": "hmmm... why not do this in raw sql or Arel then ? as a side note, you have to call `Article.reset_column_information` before using the model or your operations will fail."
}
] | [] | {
"AcceptedAnswerId": null,
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-06T21:47:08.990",
"Id": "24808",
"Score": "2",
"Tags": [
"ruby",
"ruby-on-rails"
],
"Title": "Converting data in a Rails Migration using the model"
} | 24808 |
<p>I am happy to receive all recommendations for improvement. But below I am mostly interested in a review of my handling of the exception thrown by the <code>close()</code> method of <code>RandomAccessFile</code>.</p>
<p>The <code>FileNotFoundException</code> thrown by the opening of a file (in the constructor of <c... | [] | [
{
"body": "<p>At least in code that I've seen, it is very common to ignore exceptions when closing a resource. In fact Apache Commons IO has a number of <a href=\"http://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/IOUtils.html#closeQuietly%28java.io.Closeable%29\"><code>closeQuietly</code... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-06T22:09:47.660",
"Id": "24809",
"Score": "2",
"Tags": [
"java",
"exception-handling"
],
"Title": "What to do with the Exception when you fail to close() a resource?"
} | 24809 |
<p>Consider this simplified code which successively permutes array elements:</p>
<pre><code>import Data.Word
import Data.Bits
import Data.Array.Unboxed
import Data.Array.Base
import Data.Array.ST
test3 :: UArray Int Word32 -> UArray Int Word32
test3 arr = arr `seq` runSTUArray (change arr) where
change a' = do... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-08T01:43:14.720",
"Id": "38357",
"Score": "0",
"body": "`applyLoop` is not strict in either of its accumulators. You might just be getting lucky with the strictness analyzer when using `test4`. Also, `-O2` is the highest optimization... | [
{
"body": "<p>So I profiled your code using the <code>-prof -auto-all -caf-all</code> options at first and then ran it with <code>./arr +RTS -p</code>, which generates the <code>arr.prof</code> file. It gave me the following measurements:</p>\n\n<pre><code> Sun Apr 7 20:16 2013 Time and Allocation Profilin... | {
"AcceptedAnswerId": "24968",
"CommentCount": "3",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-07T01:22:59.870",
"Id": "24811",
"Score": "3",
"Tags": [
"optimization",
"performance",
"array",
"haskell"
],
"Title": "Optimizing unboxed array operations in Haskell"
} | 24811 |
<p>I have just picked up Scala like 2 hours ago, and I am thinking of printing a series like 1 to 10 but in a recursive fashion. I do not understand what is wrong with this:</p>
<pre><code>def printSeries(x:Int):Int = {
if(x==0) doNothing() else doSomething()
def doNothing() = {}
def doSomething() = {
pri... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-06T19:01:42.353",
"Id": "38328",
"Score": "3",
"body": "Unrelated but maybe of interest to you, Coursera just started a class on Scala last week taught by Martin Odersky (the creator of Scala): [link](https://class.coursera.org/progfun... | [
{
"body": "<p>First of all, you are promising to return Int from <code>printSeries</code>. Why? If method is printing, isn't much more natural* to make it return Unit (aka void). This is usually can be done either explicitly </p>\n\n<pre><code>def printSeries(upTo: Int): Unit = { .... } \n</code></pre>\n\n<p>or... | {
"AcceptedAnswerId": "24816",
"CommentCount": "3",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-06T18:56:59.493",
"Id": "24815",
"Score": "1",
"Tags": [
"scala",
"recursion"
],
"Title": "Recursively print in scala"
} | 24815 |
<p>I am learning C from K.N. King and at ch-17, I covered linked list and wrote a code to add data in the ascending order in the linked list which is opposite to found in the book (In the book, it store in reverse order). Here's the code:</p>
<pre><code>#include <stdio.h>
#include <stdlib.h>
typedef struc... | [] | [
{
"body": "<p>Well you can get rid of the if-else by avoiding a malloc of the root node like this:</p>\n\n<pre><code>Node root = {0, NULL};\nNode *tail = &root;\n\nfor (int n = 1; n < 5; n++) {\n Node *node = malloc(sizeof(Node));\n node->value = n;\n node->next = NULL;\n tail->next ... | {
"AcceptedAnswerId": "24825",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-07T16:24:45.633",
"Id": "24823",
"Score": "1",
"Tags": [
"c",
"linked-list"
],
"Title": "Basic Linked list"
} | 24823 |
<p>I am writing a basic image upload script in PHP, and am looking for critiques. The script below is the result of various suggestions I have found online... for now I am running it locally and it works fine, but I would love for someone more experienced to look over it.</p>
<p>Is this secure? Poorly implemented? Too... | [] | [
{
"body": "<p>Why are you using <code>preg_match</code> if you don't need it? The unescaped dot in a regex will match any char. Even if the dot wasn't there, your code is still vulnerable to regex injection.</p>\n\n<p>Also, why are you sanitizing the filename if you're hashing it anyway? Why use hashing at all?... | {
"AcceptedAnswerId": "24857",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-07T16:39:48.357",
"Id": "24824",
"Score": "4",
"Tags": [
"php",
"security"
],
"Title": "Check the security of my image upload script"
} | 24824 |
<p>I have a collection of <code>PlaylistItem</code> objects. They are linked together such that each item knows the ID of its next/previous item. I am iterating over this collection of objects, starting at a known position and working with each object once. I'm not a big fan of how I do this, but I am not seeing an obv... | [] | [
{
"body": "<p>Here's a bit of \"light\" refactoring. I've basically moved the element constructing into its own function to keep the while-block less crowded.</p>\n\n<p>I'm also using some closure magic for the contextmenu, so it doesn't have to re-get the clicked item.</p>\n\n<p>Lastly, I'm going more by the c... | {
"AcceptedAnswerId": "24832",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-07T18:41:41.253",
"Id": "24829",
"Score": "4",
"Tags": [
"javascript",
"linked-list"
],
"Title": "Iterating through a linked-list in a cleaner manner"
} | 24829 |
<p>For my display functions in my Blackjack game, I want to control each player's stats output based on whether a turn is in progress or has ended. Normally, you could only see one card that your opponent(s) hold, while the rest are only revealed at the end of each turn. My code does that well, but I'm having trouble... | [] | [
{
"body": "<p>If you want to make things more consice, you could change a few things:</p>\n\n<ul>\n<li><p>Number of card printed on 2 digits:</p>\n\n<pre><code>if (totalCardsValue <= 9)\n ss << \"(0\" << totalCardsValue << \") \";\nelse\n ss << \"(\" << totalCardsValue &l... | {
"AcceptedAnswerId": "24872",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-07T22:02:24.853",
"Id": "24831",
"Score": "5",
"Tags": [
"c++",
"game",
"classes",
"playing-cards"
],
"Title": "Simplifying member functions in Blackjack game"
} | 24831 |
<p>My program has to decrypt ciphertext. It has to try all possible 26 shifts and store each in an array so that I calculate the frequencies of each array and find the highest frequency, in which that would result in the plain text.</p>
<pre><code>#include <stdio.h>
#define CIPHERSIZE 42
char decryptCiphertext(... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-08T00:29:53.410",
"Id": "38353",
"Score": "0",
"body": "Create a structure for `decrypted<i>[]`?"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-08T00:44:35.210",
"Id": "38354",
"Score": "0",
"b... | [
{
"body": "<p>decryptCiphertext can be reduced to</p>\n\n<pre><code>char decryptCiphertext(char data[], double data2[])\n{\n int i, shift, convert, shiftingLetter;\n char decrypted[26][CIPHERSIZE];\n\n for (shift = 1; shift < 26; shift++)\n {\n //Try all possible shifts from 1-25 and store... | {
"AcceptedAnswerId": null,
"CommentCount": "5",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-08T00:08:24.600",
"Id": "24833",
"Score": "3",
"Tags": [
"c",
"caesar-cipher"
],
"Title": "Shorten this Caesar Cipher cracker"
} | 24833 |
<p>A socket; in network computing; is an endpoint of a bidirectional inter-process communication flow across an Internet Protocol-based computer network, such as the Internet.</p>
<p>An internet socket address is the combination of an IP address (the location of the computer) and a port (which is mapped to the applica... | [] | [] | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-08T01:31:08.897",
"Id": "24834",
"Score": "0",
"Tags": null,
"Title": null
} | 24834 |
An endpoint of a bidirectional inter-process communication flow. This often refers to a process flow over a network connection, but by no means is limited to such. | [] | [] | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-08T01:31:08.897",
"Id": "24835",
"Score": "0",
"Tags": null,
"Title": null
} | 24835 |
<p>I have been working on a project where I needed to analyze multiple, large datasets contained inside many CSV files at the same time. I am not a programmer but an engineer, so I did a lot of searching and reading. Python's stock CSV module provides the basic functionality, but I had a lot of trouble getting the meth... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-07T20:28:50.570",
"Id": "38355",
"Score": "1",
"body": "Also you should investigate [`csvkit`](http://csvkit.readthedocs.org/en/latest/) and [`pandas`](http://pandas.pydata.org), or maybe import CSVs into a relational or key-value data... | [
{
"body": "<p>Some observations:</p>\n\n<ul>\n<li>You expect <code>read</code> to be called exactly once (otherwise it reads the same files again, right?). You might as well call it from <code>__init__</code> directly. Alternatively, <code>read</code> could take <code>location</code> as parameter, so one could ... | {
"AcceptedAnswerId": null,
"CommentCount": "4",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-07T20:15:44.593",
"Id": "24836",
"Score": "5",
"Tags": [
"python",
"parsing",
"csv",
"numpy",
"portability"
],
"Title": "Portable Python CSV class"
} | 24836 |
<p>I am hoping to streamline this bit of code better. This snippet does not cause my any slow downs <strong>in relation</strong> to other parts of the algorithm(?). I would just like guidance on making it better.</p>
<p>Some variables not given by code:</p>
<pre><code>regionDimensions = whatever you want. (256)
hex... | [] | [
{
"body": "<p>You will see a big improvement in performance if you swap the key/value types of your <code>neighborHexes</code> dictionary around..</p>\n\n<pre><code>var neighbourHexes = new Dictionary<int, Hexagon>();\n\nneighborHexes.Add(0, hexagonArray[x, y + 1]); // etc...\n\nprivate void EvaluateNeigh... | {
"AcceptedAnswerId": "24864",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-08T03:04:29.503",
"Id": "24839",
"Score": "3",
"Tags": [
"c#",
".net"
],
"Title": "Quickly Iterate through 2D array manipulating contained structs"
} | 24839 |
<p>The goal of my assignment is simple:</p>
<blockquote>
<p>Write a program that converts a given text to "Pig Latin". Pig Latin
consists of removing the first letter of each word in a sentence and
placing that letter at the end of the word. This is followed by
appending the word with letters "ay".</p>
... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-08T05:40:44.170",
"Id": "38363",
"Score": "1",
"body": "`string.Empty` is generally preferred to the empty string literal, `\"\"`. Also if `pos` only ever equals `1` or `0`, why not use a `bool`?"
}
] | [
{
"body": "<p>This looks pretty decent for a beginner. Some suggestions:</p>\n\n<ul>\n<li><p>Making a variable <code>space</code> instead of using the literal directly is actually a kind of nice idea. You can improve the code by marking the local <code>const</code>.</p></li>\n<li><p>Why is pos an integer? it on... | {
"AcceptedAnswerId": null,
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-08T04:32:14.243",
"Id": "24841",
"Score": "7",
"Tags": [
"c#",
"homework",
"pig-latin"
],
"Title": "Simple Pig Latin Translator"
} | 24841 |
<p>I have a dropdownbox, <code>ActiviteitAardItems</code>, where <code>ActiviteitAard</code> items can be checked (checkbox). If one (or more) are checked the property Opacity will be changed, and the code beneath will be executed. The <code>Tijdblok</code> mentioned below also has a property <code>Activiteit</code> an... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-10T01:45:21.293",
"Id": "38508",
"Score": "3",
"body": "I think a getter that does that much, deserves to be a full-fledged method."
}
] | [
{
"body": "<p>I think there is probably more that could be offered as the code like something that could do with a makeover, but here's a couple of minor points for consideration.</p>\n\n<ol>\n<li><p>To avoid any potential full enumeration of the items to retrieve the count of items in the list use either the <... | {
"AcceptedAnswerId": null,
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-08T07:14:04.063",
"Id": "24843",
"Score": "3",
"Tags": [
"c#",
".net",
"linq"
],
"Title": "Changing the opacity property"
} | 24843 |
<p>The new <a href="http://www.haskell.org/ghc/docs/7.6.2/html/users_guide/syntax-extns.html#multi-way-if" rel="nofollow"><code>MultiWayIf</code></a> extension (available with GHC 7.6) allows guard syntax in an <code>if</code>:</p>
<pre><code>{-# LANGUAGE MultiWayIf #-}
fn :: Int -> String
fn x y = if | x == 1 -... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-08T10:00:52.913",
"Id": "38370",
"Score": "0",
"body": "I am not sure if this question is suited for Code Review, maybe it should get migrated to SO."
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-08T10:06... | [
{
"body": "<p>Unsurprisingly, it's mainly useful when the <code>if</code> is <em>not</em> the top-level expression. Say:</p>\n\n<pre><code>forM_ [1..100] $ \\i ->\n putStrLn $ if | i `mod` 15 == 0 -> \"FizzBuzz\"\n | i `mod` 3 == 0 -> \"Fizz\"\n | i `mod` 5 == 0 ->... | {
"AcceptedAnswerId": "24856",
"CommentCount": "3",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-08T08:40:50.163",
"Id": "24844",
"Score": "1",
"Tags": [
"haskell",
"formatting"
],
"Title": "Haskell MultiWayIf extension: When is it considered useful syntactic sugar?"
} | 24844 |
<p>I really like using the new <a href="http://msdn.microsoft.com/en-us/library/hh873175.aspx" rel="noreferrer">TAP</a> pattern in .Net 4.5. and I am updating some of my older projects to use it.</p>
<p>One of my old patterns was to use <a href="http://msdn.microsoft.com/en-us/library/wewwczdw.aspx" rel="noreferrer">E... | [] | [
{
"body": "<h2>Design Trigger</h2>\n\n<p>I don't understand why you implement a duplex wcf operation to emulate a simplex operation, just to avoid configuring the timeout. Let's assume you have a really good reason why you don't want to change the default timeouts, then your solution can still time out on being... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-08T08:43:54.543",
"Id": "24845",
"Score": "9",
"Tags": [
"c#",
"asynchronous",
"wcf",
"async-await"
],
"Title": "WCF using TAP without worrying about timeouts"
} | 24845 |
<p><em>Coding to the interface, rather than the implementation</em>. Here is what I'm doing in simple terms.</p>
<p><em>Note: Although written using PHP, this is more of a general design / abstraction question that developers using any language could help answer.</em></p>
<p>I'm writing an application that can handle... | [] | [
{
"body": "<p><em><strong>Is it wrong to perform any sort of calculations within the constructor?</em></strong></p>\n\n<p>Opinion is somewhat divided on this matter. I think it is generally accepted that a constructor should not contain business logic because it makes your code much more difficult to mock for t... | {
"AcceptedAnswerId": "24855",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-08T09:45:56.653",
"Id": "24847",
"Score": "3",
"Tags": [
"php",
"object-oriented"
],
"Title": "Abstraction for multiple connection methods"
} | 24847 |
<p>I have to run a python script that enters data into MySQL database table.</p>
<p>My below query runs and inserts data into MySQL database. But I want to optimize it and I have 2 requests:</p>
<ol>
<li><p>I want to use try except error handling inside the for loop. Probably before insertstmt or where it would be mo... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-07T21:13:02.947",
"Id": "38377",
"Score": "0",
"body": "You seem to know what you want and how to do it, so... where's the question?"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-07T21:15:33.127",
"Id... | [
{
"body": "<p>OK, here's the simpliest version of <code>try:except:</code> block for your case:</p>\n\n<pre><code># some code\nfor inrow in res2:\n # some code\n try:\n cur.execute(insertstmt)\n except MySQLdb.ProgrammingError:\n pass\n</code></pre>\n\n<p>That's pretty much it. You probab... | {
"AcceptedAnswerId": null,
"CommentCount": "5",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-07T21:07:54.810",
"Id": "24853",
"Score": "4",
"Tags": [
"python",
"mysql"
],
"Title": "Inserting data into database by Python"
} | 24853 |
<p>To work around the <a href="http://blogs.msdn.com/b/tess/archive/2006/02/15/net-memory-leak-xmlserializing-your-way-to-a-memory-leak.aspx" rel="nofollow">XmlSerializer memory leak thing</a> I created this:</p>
<pre><code>public static class XmlSerializerCache
{
public static XmlSerializer GetXmlSerializer(Type ... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-08T16:32:46.513",
"Id": "38401",
"Score": "0",
"body": "Gotta be honest, I've never actually used that particular `XmlSerializer` constructor overload that doesn't cache."
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDat... | [
{
"body": "<p>According the work around section in the <a href=\"https://web.archive.org/web/20070102071100/http://support.microsoft.com:80/kb/886385/en-us\" rel=\"nofollow noreferrer\">kb article</a> mentioned in the <a href=\"http://blogs.msdn.com/b/tess/archive/2006/02/15/net-memory-leak-xmlserializing-your-... | {
"AcceptedAnswerId": null,
"CommentCount": "4",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-08T16:01:47.643",
"Id": "24861",
"Score": "1",
"Tags": [
"c#",
".net"
],
"Title": "Caching XmlSerializer in AppDomain"
} | 24861 |
<p>I have the following code:</p>
<pre><code>const String sqlSelect = "SELECT * FROM UserPasswords WHERE username='System Administrator';";
const String sqlInsert = "INSERT INTO UserPasswords VALUES (@username,@Password,@startDate,@Expired,@UserPasswordsID)";
using (var con1 = new SqlConnection(System.Configuration.Co... | [] | [
{
"body": "<p><a href=\"http://www.codeproject.com/Articles/18418/Transferring-Data-Using-SqlBulkCopy\" rel=\"nofollow\">Transferring Data Using SqlBulkCopy</a></p>\n\n<pre><code>private static void PerformBulkCopy()\n{\n string connectionString =\n @\"Server=localhost;Database=Northwind;Trusted_C... | {
"AcceptedAnswerId": "24867",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-08T16:57:14.390",
"Id": "24866",
"Score": "1",
"Tags": [
"c#",
"sql",
"asp.net"
],
"Title": "Is there a simpler way to write a row from one table to another?"
} | 24866 |
<p>I have a simple two-class hierarchy to represent U.S. ZIP (12345) and ZIP+4 (12345-1234) codes. To allow clients to allow both types for a field/parameter or restrict it to one type or the other, the specific types inherit from a common generic <code>ZipCode</code> interface.</p>
<p><strong>Update:</strong> A ZIP c... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-08T20:02:01.273",
"Id": "38420",
"Score": "0",
"body": "I'm not sure I understand your interface. Why does the interface know about the concrete objects that implement it, and have functions that return both? Who is using this that t... | [
{
"body": "<p>This is a preliminary answer:</p>\n\n<blockquote>\n <p>Assume for the sake of this question that going that route isn't\n practical.</p>\n</blockquote>\n\n<p>Assuming this is about some group of similar value objects.\nEverything is meaningful in a context. So we need to determine what are the u... | {
"AcceptedAnswerId": null,
"CommentCount": "9",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-08T18:50:47.257",
"Id": "24869",
"Score": "3",
"Tags": [
"java",
"design-patterns",
"polymorphism",
"null"
],
"Title": "Null Object pattern with simple class hierarchy"
} | 24869 |
<p>The goal of this was to be able to enter any number and print out the prime factors of that number. Are there any shorter ways of writing this code? I'm not very familiar with low-level syntax and was hoping somebody could give it a look. </p>
<pre><code>.text
.align 2
.globl main
main:
# compute and display the ... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-08T21:11:16.633",
"Id": "38428",
"Score": "0",
"body": "does it run in MARS or what?"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-08T22:39:07.067",
"Id": "38440",
"Score": "0",
"body": "I use... | [
{
"body": "<p><strong>Procedures</strong></p>\n\n<ul>\n<li><p>You appear to be doing everything in <code>main</code> while just branching into single-character procedures. Consider renaming them to specify their purpose, while separating them from <code>main</code> with proper indentation and such. I cannot t... | {
"AcceptedAnswerId": "40709",
"CommentCount": "5",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-08T21:00:55.927",
"Id": "24874",
"Score": "6",
"Tags": [
"primes",
"assembly"
],
"Title": "Calculating prime factors in MIPS assembly"
} | 24874 |
<p>The class does some database abstraction operations. And while I could have just used an ORM, I prefer to get my hands dirty when learning something new. There are no bugs (not as far as I know anyway) and the code is working as intended. But, since I'm learning Python, I want to make sure I'm thinking and working i... | [
{
"ContentLicense": "CC BY-SA 4.0",
"CreationDate": "2020-01-21T16:48:17.287",
"Id": "462076",
"Score": "0",
"body": "Glancing through, I don't quite see your rationale of using `**kwargs**` If `_username` and others are mandatory fields, why not just have the argument explicitly passed through ... | [
{
"body": "<p>There's quite a lot of code so I'm going to comment only this small piece of code :</p>\n\n<pre><code>results = cur.fetchall();\nn = len(results);\ncols = self.table_columns(None, cur);\nretval = [];\nfor i in range(0,n):\n aux = results[i];\n row = {};\n for j in range(0,len(cols)):\n ... | {
"AcceptedAnswerId": "24880",
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-08T22:34:04.393",
"Id": "24875",
"Score": "1",
"Tags": [
"python",
"database"
],
"Title": "Database abstraction class"
} | 24875 |
<p>I recently built an improved version of a bank account program I made. It has no GUI or any interaction with users, but I simply built it to test it and play with it via <code>main()</code>. I'll post all my classes below and you can run the program yourself or just critique my code. I'll take any and all suggestion... | [] | [
{
"body": "<p>Over-all, you say you are a beginner programmer, but you are off to a very nice start! All my suggestions focus around one concept: the Interface.\nYou are definitely ready to read <a href=\"http://www.informit.com/articles/article.aspx?p=1216151&seqNum=2\" rel=\"nofollow noreferrer\">Joshua ... | {
"AcceptedAnswerId": "24885",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-08T23:04:12.337",
"Id": "24877",
"Score": "3",
"Tags": [
"java",
"beginner",
"finance"
],
"Title": "Virtual bank program"
} | 24877 |
<p>I have started working with Cassandra database recently and I was trying to insert some data into one of my column family that I have created. Below is the code by which I am trying to insert into Cassandra database.</p>
<p>In my case I have around <code>20 columns</code> in my column family so that means I need to... | [] | [
{
"body": "<p>It looks like the pattern is alway the same, I am right? Then you could just do:</p>\n\n<pre><code>String commonColumnValue = \"{\\\"lv\\\":[{\\\"v\\\":{\\\"regSiteId\\\":null,\\\"userState\\\":null,\\\"userId\\\":\" + userId + \"},\\\"cn\\\":1}],\\\"lmd\\\":20130206211109}\"\nString[] columns = {... | {
"AcceptedAnswerId": "24915",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-08T23:05:19.587",
"Id": "24878",
"Score": "2",
"Tags": [
"java",
"cassandra"
],
"Title": "Inserting data into column family"
} | 24878 |
<p>I'm trying to create a wordpress plugin but checking for the existence of a value doesn't really feel right because there's a bunch of values that I have to check:</p>
<pre><code>$flickr_key = '';
if(!empty($ecom['ecom_options_flickr_api'])){
$flickr_key = $ecom['ecom_options_flickr_api'];
}
$flickr_secret = '';... | [] | [
{
"body": "<p><strong>Using ternary operators to removed un-necessary lines</strong></p>\n\n<p>Well you could use the <a href=\"http://www.php.net/manual/en/function.isset.php\" rel=\"nofollow noreferrer\">function isset</a> with the use of <a href=\"http://php.net/manual/en/language.operators.comparison.php#la... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-09T01:36:17.680",
"Id": "24882",
"Score": "1",
"Tags": [
"php"
],
"Title": "Alternative for checking the existence of a value in PHP"
} | 24882 |
<p>There are not many available resources for refactoring go code. I'm a novice gopher who would appreciate any feedback on a small program which reads a .csv file, string converts and parses a few rows in order to perform a sub method. </p>
<pre><code>package main
import (
"encoding/csv"
"fmt"
"io"
... | [] | [
{
"body": "<pre><code> for i := 0; i < len(record[i]); i++ {\n</code></pre>\n\n<p>... I don't think that does what you intended. It seems like just luck from how the data is that it works, or did I miss something? :-) I think you can just take the for {} loop out altogether.</p>\n\n<p>I'd move the ParseFl... | {
"AcceptedAnswerId": "24919",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-09T02:50:52.063",
"Id": "24884",
"Score": "3",
"Tags": [
"beginner",
"parsing",
"csv",
"go"
],
"Title": "String converting and parsing a few rows of a .csv file"
} | 24884 |
<p>I came across the following piece of code in a UI application I need to maintain. </p>
<pre><code>int tool_unhex( char c )
{
return( c >= '0' && c <= '9' ? c - '0'
: c >= 'A' && c <= 'F' ? c - 'A' + 10
: c - 'a' + 10 );
}
void unescape2QString(const char *sOrg, QStri... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-10T02:30:05.537",
"Id": "38509",
"Score": "4",
"body": "Well, not really optimising, but for your four `if ( *++s != '\\0' )` conditions, if any but the last one fails, the loop will go off the end of the string and keep going - until ... | [
{
"body": "<ul>\n<li><p>I can't quite tell what <code>tool_unhex()</code> is supposed to do. It looks like it converts a character (presumably a hex value) to a base-10 value (an <code>int</code> in this case). You may need to change the name to reflect on its primary purpose.</p>\n\n<p>The ternary is a littl... | {
"AcceptedAnswerId": "40708",
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-09T07:22:12.900",
"Id": "24888",
"Score": "6",
"Tags": [
"c++",
"optimization",
"strings"
],
"Title": "Implementing HTUnEscape"
} | 24888 |
<p>I have a few Python codes like this:</p>
<pre><code> workflow = []
conf = {}
result = []
prepare_A(workflow, conf)
prepare_B(workflow, conf)
prepare_C(workflow, conf)
result.append(prepare_D_result(workflow, conf))
prepare_E(workflow, conf)
#.... about 100 prepare functions has the same parameter list
</co... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-09T14:47:09.967",
"Id": "38489",
"Score": "0",
"body": "Your question isn't following the FAQ in that your don't appear to be presenting real code. It would be better if you could show us an actual example."
},
{
"ContentLicens... | [
{
"body": "<p>Here's an idea, assuming your logic is really that straightforward:</p>\n\n<pre><code>workflow = [] \nconf = {}\nresult = []\n\nsteps = ((prepare_A, False),\n (prepare_B, False),\n (prepare_C, False),\n (prepare_D, True),\n (prepare_E, False))\n\nfor func, need_resu... | {
"AcceptedAnswerId": "24892",
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-09T07:23:59.597",
"Id": "24889",
"Score": "0",
"Tags": [
"python"
],
"Title": "Is it worthy to create a wrapper function like this?"
} | 24889 |
<p>I use this code to Load and Insert data to a table using a <code>DataGridView</code> in a C# windows application.</p>
<pre><code> SqlCommand sCommand;
SqlDataAdapter sAdapter;
SqlCommandBuilder sBuilder;
DataSet sDs;
DataTable sTable;
private void form1_Load(object s... | [] | [
{
"body": "<p>It looks like your code should work and output rows if your database contains records corresponding to your condition.</p>\n\n<p>Since we are on a code review site I would like to highlight several code issues not related to your question:</p>\n\n<ul>\n<li>You should dispose all objects implementi... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-09T07:39:12.260",
"Id": "24891",
"Score": "-1",
"Tags": [
"c#",
".net",
"sql",
"winforms"
],
"Title": "Insert to datagridview when SELECT query has WHERE condition"
} | 24891 |
<p>I've made an algorithm solving Hanoi Tower puzzles, for n disks and m pegs.</p>
<p>It uses lists as pegs, each list's element contains disks array - <code>{0, 0, 0}</code> means the peg is empty, <code>{1, 2, 0}</code> means the peg contains "1" and "2" disks, <code>{3, 0, 0}</code> means the peg contains the bigge... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-09T10:51:22.663",
"Id": "38477",
"Score": "0",
"body": "Your link to 'the working algorithm' leads to code that does not compile."
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-09T11:42:07.947",
"Id": ... | [
{
"body": "<p>The biggest problem is that there is very little abstraction going on here. There are plenty of places where a helper function would come in handy, but isn't used, and an object or abstract data type would be good for the individual rods or the set of rods as a whole. We can't get the big pictur... | {
"AcceptedAnswerId": null,
"CommentCount": "8",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-09T09:52:52.510",
"Id": "24897",
"Score": "-1",
"Tags": [
"c++",
"algorithm",
"c",
"tower-of-hanoi"
],
"Title": "Hanoi Towers solver"
} | 24897 |
<p>I need concurrent HashMap of List as value with following behavior:</p>
<ul>
<li>count of read/write ops approximately equals</li>
<li>support add/remove values in lists</li>
<li>thread safe iterations over lists</li>
</ul>
<p>After some research I implemented ConcurrentMapOfList, but I'm not sure in his correctne... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-09T17:41:49.177",
"Id": "38496",
"Score": "0",
"body": "Is there a reason you do not use the wrapper interface `synchronizedMap` from `Collections`? It seems to me that this would be the easier and safer solution. From a quick look, yo... | [
{
"body": "<p>Ok, after the hint from the comment it makes more sense for me. I felt a bit bad because of the wrong comment, so I have investigated this deeper.</p>\n\n<p>As far as I see it, there is only one problem which I will explain in the next paragraph. But I have to admit, the implementation is rather c... | {
"AcceptedAnswerId": "24972",
"CommentCount": "5",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-09T10:17:59.760",
"Id": "24898",
"Score": "1",
"Tags": [
"java",
"multithreading",
"collections"
],
"Title": "Java concurrent Map of List"
} | 24898 |
<p>I got this answer here: <a href="https://stackoverflow.com/questions/15902299/selecting-an-item-in-a-listbox-to-assign-it-as-a-temp-variable">https://stackoverflow.com/questions/15902299/selecting-an-item-in-a-listbox-to-assign-it-as-a-temp-variable</a></p>
<hr>
<p>Suppose you have loaded the <code>lisBox1</code> ... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-09T14:09:15.190",
"Id": "38484",
"Score": "0",
"body": "Winforms? Wpf? Webforms? Who gave you the code?"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-09T14:15:10.317",
"Id": "38485",
"Score": "0",... | [
{
"body": "<p>Not sure if this question really fits here, but I'll try to help anyway.</p>\n\n<blockquote>\n <p>With the first 2 lines of code (At the top of the answer), where would\n i place this (I'm new to C# so .Datasource & DisplayMember are new\n things to me).</p>\n</blockquote>\n\n<p>DataSource ... | {
"AcceptedAnswerId": null,
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-09T13:33:32.907",
"Id": "24903",
"Score": "1",
"Tags": [
"c#",
"winforms"
],
"Title": "selecting an item in a listbox to assign it as a temp variable - Review Code"
} | 24903 |
<p>I'm no stranger to JS, but I think I still have a lot to learn. I originally wrote this code to manipulate a "help" bar, which would be positioned as a thin ribbon on the side of the browser window, which when hovering over or clicking would open up and display it's contents. Nothing major, but an okay example to us... | [] | [
{
"body": "<p>A couple of pointers not in any particular order:</p>\n\n<p>If you use a single main object with methods it'll be easier to follow along later and add functionality etc. A pattern you can use is the observer pattern for setting up and triggering your events. Also if you use a single main object yo... | {
"AcceptedAnswerId": "24923",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-09T14:38:05.857",
"Id": "24904",
"Score": "1",
"Tags": [
"javascript",
"jquery"
],
"Title": "Improving JavaScript - using objects vs. just functions"
} | 24904 |
<p>I have the following dataset in numpy</p>
<pre><code>indices | real data (X) |targets (y)
| |
0 0 | 43.25 665.32 ... |2.4 } 1st block
0 0 | 11.234 |-4.5 }
0 1 ... ... } 2nd block
0 1 }
0 2 ... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2018-01-23T00:48:33.853",
"Id": "354174",
"Score": "0",
"body": "Could you please edit the title of your question to be less generic?"
}
] | [
{
"body": "<p>That <code>W</code> was tricky... Actually, your blocks are pretty irrelevant, apart from getting the right slice of <code>W</code> to do the <code>np.dot</code> with the corresponding <code>X</code>, so I went the easy route of creating an <code>aligned_W</code> array as follows:</p>\n\n<pre><cod... | {
"AcceptedAnswerId": null,
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-09T15:00:43.700",
"Id": "24905",
"Score": "4",
"Tags": [
"python",
"optimization",
"numpy"
],
"Title": "Eliminate for loops in numpy implementation"
} | 24905 |
<p>I was wondering if this is the correct implementation of a times task to destroy a thread after it overruns a predefined time period:</p>
<p>It works by creating a getting the thread from <code>runtime.getRuntime</code>, which it then uses to create a new process to run a command with the <code>exec()</code> method... | [] | [
{
"body": "<p>The main idea looks ok for me. Use a timer with a given time to call the destroy.</p>\n\n<p>I would separate both concerns. One class is responsible for external program handling and it will provide a stop method. The stop method could be called from outside (e.g. the timer, but from manual intera... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-09T15:33:46.063",
"Id": "24907",
"Score": "4",
"Tags": [
"java",
"strings",
"multithreading",
"timer",
"child-process"
],
"Title": "Timer/timertask to destroy a process that ... | 24907 |
<p>I have a merge statement that takes around 10 minutes to process 5 million or more records.</p>
<p>The merge statement is part of a stored procedure that takes my newly bulk loaded staging table then runs data integrity checks, transforms the data into the proper format, and inserts that data where it should go.</p... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-17T21:34:15.617",
"Id": "38995",
"Score": "1",
"body": "Why is your merge condition `ON 1 = 0`? It would help if you post index DDL as well as the execution plan for this query. And why do you say that you \"tried\" using a temp table ... | [
{
"body": "<p>Your <code>Staging</code> Table doesn't have a Primary Key in it, if it did that might speed things up a little bit, you gave your Temporary table a Primary key but not your Permanent <code>Staging</code> Table.</p>\n\n<hr>\n\n<p>Another thing that I noticed is that inside of your <code>Using</c... | {
"AcceptedAnswerId": null,
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-09T15:53:21.060",
"Id": "24908",
"Score": "1",
"Tags": [
"sql",
"sql-server"
],
"Title": "SQL Server merge statement"
} | 24908 |
<p>This is my first attempt at using Python to send http requests. I want to keep a zone record on cPanel pointed at my home network's public IP. I'm just looking for some general feedback/suggestions. Is there an easier way to do this? Is there a more Pythonic way? My intent is to run this as a cronjob to stay ah... | [] | [
{
"body": "<p>Before anything else, I'd like to point out that there is probably an error with your indentation. I've fixed it in my code assuming that only one line was not indented properly..</p>\n\n<p><strong>Simple is better than complex.</strong></p>\n\n<ul>\n<li>You don't need the classes (you can also ha... | {
"AcceptedAnswerId": "24913",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-09T16:17:12.057",
"Id": "24910",
"Score": "1",
"Tags": [
"python",
"https"
],
"Title": "Python script to update a cPanel zone record with my public IP"
} | 24910 |
<p>This is my attempt at constructing a singly-linked-list with basic functions. I initially tried to build the whole list as a series of nodes, but ran into problems when trying to remove or pop index-0. This time I created a series of Node structs and a single List struct as their head. This allowed me to modify t... | [] | [
{
"body": "<p>Nice code, compiles almost cleanly. I'm impressed that this is your code <strong>before</strong> starting courses. I've met people with years of experience who couldn't write such good code. </p>\n\n<p>I have a few comments, starting with the concept. You have a list structure that you initiali... | {
"AcceptedAnswerId": "24921",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-09T17:16:28.170",
"Id": "24911",
"Score": "5",
"Tags": [
"c",
"linked-list"
],
"Title": "Singly Linked List (strings only)"
} | 24911 |
<p>I have 3 fields:</p>
<ul>
<li>Net Price (ex. tax)</li>
<li>tax amount</li>
<li>Total price (price ex. vat + tax amount)</li>
</ul>
<p>The <code>NetPrice</code> and the <code>Total</code> are writable (i.e. you can change either of them and the other 2 values must be auto-calculated).</p>
<p>The way I've done it i... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-10T09:32:50.767",
"Id": "38537",
"Score": "0",
"body": "Just in any case anybody also didn't knew knockout like me: http://knockoutjs.com/"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-11T15:15:03.973",
... | [
{
"body": "<p>I will review and contrast the SO answer that I liked best: </p>\n\n<pre><code>function viewModel() {\n var self = this;\n\n self.NetPrice = ko.observable(100);\n\n self.TaxRate = 0.2;\n\n self.TaxAmt = ko.computed(function() {\n return parseFloat(self.NetPrice()) * self.TaxRate... | {
"AcceptedAnswerId": "44797",
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-09T22:15:26.260",
"Id": "24925",
"Score": "5",
"Tags": [
"javascript",
"knockout.js"
],
"Title": "Two Knockout computed dependent on each other"
} | 24925 |
<p>I have constructed a simple implementation of a parallel loop:</p>
<pre><code>#include <algorithm>
#include <thread>
#include "stdqueue.h"
namespace Wide {
namespace Concurrency {
template<typename Iterator, typename Func> void ParallelForEach(Iterator begin, Iterator end, Func f) {
... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-10T10:36:07.653",
"Id": "38540",
"Score": "0",
"body": "I can see a few errors that will stop this from even compiling. It should be `std::lock_guard<std::mutex>` for one. Depending on your compiler, it may barf at `std::vector<std::th... | [
{
"body": "<p>First, that <code>begin/end</code> range, <code>its</code> and <code>its_queue</code> are duplicates of each other.<br>\nIf we change <code>Iterator</code> to <code>RandomAccessIterator</code>, we can get rid of them:</p>\n\n<pre><code>template<typename RA_Iterator, typename Func> void Paral... | {
"AcceptedAnswerId": null,
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-09T23:19:58.713",
"Id": "24927",
"Score": "1",
"Tags": [
"c++",
"multithreading",
"c++11"
],
"Title": "parallel_for_each"
} | 24927 |
<p>I have a functionality that imports data into a database, based on an Excel workbook and some meta data (both user-supplied). The functionality implements interface <code>IFunctionality</code> which essentially specifies an <code>AuthId</code> string property (used for fetching authorized AD groups for a functionali... | [] | [
{
"body": "<p>The points you've specified in your question actually show that you (almost) see issues in your code yourselves, so thumbs up for that. I'll only expand on your points plus add a couple:</p>\n\n<ul>\n<li>your class is doing too much. You've noted that it has to be accessible from COM, but it doesn... | {
"AcceptedAnswerId": "24946",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-10T01:30:40.743",
"Id": "24930",
"Score": "8",
"Tags": [
"c#",
"design-patterns",
"unit-testing"
],
"Title": "Unit-testing the importing of data into a database"
} | 24930 |
<p>The problem I am facing is:
I need to interate through a bunch of lists, and there are separated conditions which needs to be satisfied by the list. conditons are not independent.</p>
<p>I care about the situation when all the conditions are matched. </p>
<p>The solutions I come up is nested interations mixed with... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-11T14:58:29.423",
"Id": "38670",
"Score": "0",
"body": "The code you've provided appears to only print a single line, 1 2 3 3, in which case you should do away with the loops entirely. You should change your example code to give a more... | [
{
"body": "<p>In your example you iterate over the same collection. In that case I'd write:</p>\n\n<pre><code>[1, 2, 3].repeated_permutation(4).each do |n1, n2, n3, n4|\n if n1 == 1 && n2 == 2 && n3 == 3 && n3 == n4\n p [n1, n2, n3, n4]\n end\nend\n</code></pre>\n\n<p>If instead you... | {
"AcceptedAnswerId": null,
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-10T02:19:17.793",
"Id": "24932",
"Score": "0",
"Tags": [
"ruby",
"iteration"
],
"Title": "How to flatten the nested for loops?"
} | 24932 |
<p>I'm very new to coding and have been diving into the skill with Python as my first language. One of the first programs I wrote for a class was this Tic Tac Toe game. I re-wrote this game using classes and have just recently refactored it looking for different <em>smells</em>. The person who's been teaching the class... | [] | [
{
"body": "<p>For someone new to coding, there's a lot here that is good. Naming of classes, functions and variables are generally good. Comments follow convention, with simple, concise Docstrings for everything. Methods are generally simple and do only one thing, which is nice.</p>\n\n<hr>\n\n<p>Let's go throu... | {
"AcceptedAnswerId": "24938",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-10T04:11:09.560",
"Id": "24934",
"Score": "5",
"Tags": [
"python",
"optimization",
"beginner",
"game"
],
"Title": "Help with refactoring my Tic Tac Toe game"
} | 24934 |
<p>Here's a part of my controller and it's getting quite lengthy (the code works).</p>
<p>Would this code slow down the performance of my website? Can it be cleaned up and be written more efficiently? </p>
<pre><code>def create
@post = current_user.posts.build(params[:post])
if @post.save
if @post.verify
... | [] | [
{
"body": "<p>You could create a method on your post model that converts a post to a tweet. If you do that you can clean up your controller a lot:</p>\n\n<p>Model:</p>\n\n<pre><code>class Post < ActiveRecord::Base\n def to_tweet\n if image.present?\n \"#{content.truncate(120)}... #{image.path.to_s}\... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-10T02:27:51.947",
"Id": "24939",
"Score": "1",
"Tags": [
"ruby",
"ruby-on-rails",
"twitter"
],
"Title": "Saving posts, then tweeting them"
} | 24939 |
<p>I have just started to try and learn 'OOP' but it appears I'm doing this wrong, according to the people on Stack Overflow. The code below is far from object-orientated, but I'm finding it hard as I'm self teaching my self and everyone does everything differently. </p>
<p>I'm building a shopping cart and I need to b... | [] | [
{
"body": "<p>First of all don't use global variables or constants. Use comments to explain your intention not your code. Put every class in a separate file.</p>\n\n<p>Afterwards you have to decide if you want to use your Admin/User as a representation of a concrete admin/user (keeping the data of one user) or ... | {
"AcceptedAnswerId": "24950",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-10T09:47:57.377",
"Id": "24947",
"Score": "2",
"Tags": [
"php",
"object-oriented",
"mysql",
"e-commerce"
],
"Title": "Object-oriented shopping cart"
} | 24947 |
<p>I have recently created my first game in C++:</p>
<pre><code>#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
cout<<"\tWelcome to Guess my Number!\n";
const unsigned short kusiLower=1;
const unsigned short kusiHigher=100;
srand(time(0)... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-10T18:03:20.203",
"Id": "38583",
"Score": "0",
"body": "Is it...... 42?"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-11T15:06:11.883",
"Id": "38672",
"Score": "0",
"body": "`using namespace s... | [
{
"body": "<p>a few remarks:</p>\n\n<ul>\n<li>Use int instead of unsigned short</li>\n<li>Don't use Hungarian naming ('kusi/usi' prefix) - nobody uses that.</li>\n<li>Add argc/argv parameters to main</li>\n<li>Dont import everything from std with <code>using namespace std</code></li>\n<li>Add spaces around oper... | {
"AcceptedAnswerId": null,
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-10T09:50:05.153",
"Id": "24948",
"Score": "1",
"Tags": [
"c++",
"optimization",
"game"
],
"Title": "\"Guess my Number\" game in C++"
} | 24948 |
<p>I have just started learning erlang...I am just practicing the recursion and all in erlang...I tried to write a few of the list processing functions on my own in erlang...Can someone review the code and give some advice on it...</p>
<pre><code>-module(listma).
-export([duplicates/1,reverse/1])
%% Remove the duplica... | [] | [
{
"body": "<p>Your <code>Insert</code> lambda doesn't use any context so you could rewrite it as a top-level function or move its body to the clause.\nAnd you could use <code>lists:member/2</code> instead of <code>--</code>.</p>\n\n<p>I'd write first clause like this:</p>\n\n<pre><code>duplicates([Head | Tail],... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-10T11:05:40.107",
"Id": "24949",
"Score": "1",
"Tags": [
"erlang"
],
"Title": "Feedback on Erlang Lists processing function"
} | 24949 |
<p>I am using jQuery's collapsible lists grouped and named <code>#List01</code>, <code>#List02</code>, <code>#List03</code>, etc. My code captures clicks on list elements and remembers list items' ID for further processing. Each group has identically looking code so I was wondering how I could encapsulate it in one fun... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-10T05:21:35.633",
"Id": "38544",
"Score": "0",
"body": "why dont just use class as jquery selector since the function have no relation with it?"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-10T05:23:15.57... | [
{
"body": "<pre><code>$(document).on('click', '#List01 li, #List02 li, #List03 li', function () {\n var anchor = $(this).find('a');\n sessionStorage.KenID = anchor.attr('id');\n changePage();\n});\n</code></pre>\n\n<p>The <code>,</code> should do the trick.</p>\n",
"comments": [],
"... | {
"AcceptedAnswerId": "24952",
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-10T05:18:02.323",
"Id": "24951",
"Score": "2",
"Tags": [
"javascript",
"jquery"
],
"Title": "Capturing clicks on list items"
} | 24951 |
<p>I'm trying to improve my OOP code and I think my User class is becoming way too fat.</p>
<p>In my program a user has rights over "lists". Read, Write, Update, Delete.
So I made a User class</p>
<pre><code>class User
{
protected $_id;
protected $_email;
protected $_username;
protected $_hashedPassword;
//...Variou... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-11T13:40:40.960",
"Id": "38664",
"Score": "0",
"body": "What exactly does `List` contains?"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-11T14:41:19.770",
"Id": "38668",
"Score": "0",
"body": ... | [
{
"body": "<p>If you have the feeling your classes are too big, you are usually right. Of course there are always different approaches how you could split classes and your idea seems to be reasonable.</p>\n\n<p>So you will have some plain Value Objects (the User), some UserRepository class for getting/updating ... | {
"AcceptedAnswerId": "24986",
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-10T12:00:09.520",
"Id": "24956",
"Score": "1",
"Tags": [
"php",
"object-oriented",
"php5"
],
"Title": "Should a User class only contain attributes and no methods apart from getter... | 24956 |
<p>Take the below code:</p>
<pre><code>public interface ISettingsManager
{
SettingData GetSettingByIdentifierOrCreateIfDoesNotExist(Enum identifier);
T GetSetting<T>(Enum identifier);
}
[IocComponent]
public class SettingsManager : ISettingsManager
{
private readonly ISettingGetItemByIdentifierServ... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-10T12:43:30.730",
"Id": "38561",
"Score": "2",
"body": "If you're following TDD, you should have written the tests `before` the actual code :)"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-10T17:10:17.900... | [
{
"body": "<p>I'm not sure if you need <code>GetSettingByIdentifierOrCreateIfDoesNotExist</code> declaration in your interface... Do you have special use cases where <code>GetSetting<T></code> is not enough?</p>\n\n<p>If I'm right and all you need is just <code>GetSetting<T></code> method then you c... | {
"AcceptedAnswerId": "24960",
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-10T12:39:09.947",
"Id": "24958",
"Score": "1",
"Tags": [
"c#",
"unit-testing",
"tdd"
],
"Title": "unit-testing / mocking a class which contains functionality which depends on itse... | 24958 |
<p>Without changing the semantics and performance, how can I make this where clause more readable?</p>
<pre><code>where
(@Category = 'all' or
(@Category = 'omitted' and Category is null) or
(@Category = Category and (
@SubCategory = 'all' or
(@SubCategory is null and SubCategory is null) or
... | [] | [
{
"body": "<p>This kind of condition may actually hurt performance quite a lot, because query optimizer most likely will be confused by complex filtering condition and would have to do a table scan rather than index seek (assuming that you have index over <code>Category</code> and <code>SubCategory</code>). In ... | {
"AcceptedAnswerId": "25112",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-10T14:14:53.300",
"Id": "24963",
"Score": "2",
"Tags": [
"sql",
"sql-server",
"t-sql"
],
"Title": "How can I make this WHERE clause more readable?"
} | 24963 |
<p>I've combined some of my own functions with a function I found and I'm curious to know what others think of it. It will be used to upload and resize/crop images that are jpeg, jpg, png.</p>
<pre><code><?php
class ImageUploader {
var $multiple_file_upload = true;
var $max_size = 1024;
var $mime_types ... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-10T17:32:42.743",
"Id": "38579",
"Score": "0",
"body": "var is depricated use protected,public or private."
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-10T17:43:43.583",
"Id": "38582",
"Score": "... | [
{
"body": "<p>Some hints for (not) commenting your code.</p>\n\n<p>Change:</p>\n\n<pre><code>/**\n * Landscape Image\n */\nif ($current_ratio > $desired_ratio_after) {\n</code></pre>\n\n<p>To:</p>\n\n<pre><code>$isLandscape=$current_ratio > $desired_ratio_after;\nif ($isLandscape) {\n</code></pre>\n\n<p>O... | {
"AcceptedAnswerId": null,
"CommentCount": "3",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-10T14:45:01.583",
"Id": "24965",
"Score": "1",
"Tags": [
"php",
"optimization",
"object-oriented"
],
"Title": "Image Resize/Crop Class"
} | 24965 |
<p>I've got a string that consists of an arbitrary combination of text and <code>{}</code> delimited python code, for instance, <code>A plus b is {a + b}</code>. However, braces are used for dictionary and set literals in python, so <code>You chose the {{1:"first", 2:"second"}[choice]} option</code> should also be int... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-10T18:35:42.293",
"Id": "38593",
"Score": "0",
"body": "What are you using to execute the Python code? I can't seem to find types like `PythonByteCode` anywhere."
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013... | [
{
"body": "<p>I see an extra unnecessary variable that you can get rid of, or change another variable so that it makes more sense.</p>\n\n<pre><code>int opening = i;\n</code></pre>\n\n<p>you use this inside of a for loop. You can do 2 things with this.</p>\n\n<ol>\n<li>Just use the variable <code>i</code> wher... | {
"AcceptedAnswerId": null,
"CommentCount": "11",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-10T16:29:01.217",
"Id": "24969",
"Score": "3",
"Tags": [
"c#",
"python",
"parsing"
],
"Title": "Interpolating code delimited with character that can appear in code"
} | 24969 |
<p>Of all the macros that I put into heavy rotation these days, this one is running the slowest. ~4-5 seconds depending on the size of the files. It's not a lot but I'd like to know why code 16x as long is running much more instantly.</p>
<p>The code tries to merge documents (usually 2 excel docs out of at most 5) dep... | [] | [
{
"body": "<p>Not for efficiency, but you can start with this... Convert this block:</p>\n\n<pre><code>If FindString(wb.Name, \"Report2\") Then\n wb.Worksheets.Move after:=Workbooks(\"CompanyBook.xlsm\").Sheets(\"Aggregate\")\nElseIf FindString(wb.Name, \"Report1\") Then\n wb.Worksheets.Move after:=Workbo... | {
"AcceptedAnswerId": "25016",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-10T17:58:51.613",
"Id": "24971",
"Score": "3",
"Tags": [
"performance",
"vba",
"excel"
],
"Title": "Merging worksheets and using find/replace"
} | 24971 |
<p>I wish to create audit trails for specific tables and columns in my database, and document who made the change, when it was made, and what the change was.</p>
<p>To do so, I will create the following tables:</p>
<ol>
<li>Audits: Create a record whenever a change is made to any table, and stores the table, the dat... | [] | [
{
"body": "<p>An alternative way I have seen is to create separate audit tables for each table you want to audit and simply have a trigger that copies the entire row into the table plus the action (insert, update, delete), the user that made the change and timestamp of when it happened.</p>\n\n<p>It does involv... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-10T18:06:19.567",
"Id": "24973",
"Score": "9",
"Tags": [
"mysql",
"sql"
],
"Title": "Strategy to create audit trails for a SQL database"
} | 24973 |
<p>I'm learning Ember, and I've come across a situation where I haven't been able to find a pattern online.</p>
<p>My application's main route includes a list of "stories" (think scrum). However, this list also has a <code>StoryController</code> associated with it. I don't want to assign the stories in the <code>Ind... | [] | [
{
"body": "<p>I think you can pass in the controller to the \"stories\" render function and use the model hook :</p>\n\n<p><strong>App.IndexRoute</strong></p>\n\n<pre><code>App.IndexRoute = Ember.Route.extend({\n renderTemplate: function(){\n this.render();\n this.render('stories', {controller:... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-10T19:22:20.313",
"Id": "24975",
"Score": "1",
"Tags": [
"javascript",
"design-patterns",
"ember.js"
],
"Title": "Ember App - Initial State"
} | 24975 |
<p>I want to do some object-oriented programming in Lua, and I decided on something like this:</p>
<pre class="lang-lua prettyprint-override"><code>local B = {} -- in real life there is other stuff in B
B.Object = {
constructor = function() end,
extend = function(this, that, meta)
if not that then that = {}... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2015-05-06T13:51:16.770",
"Id": "163137",
"Score": "2",
"body": "I would suggest adding some comments to describe the intent of your code. The biggest issue is probably private properties. I tried some ideas some time ago, but every option is ... | [
{
"body": "<p><strong>Example Usage</strong></p>\n\n<p>The example that you provide does not really help to explain how the code should work. I trust that it does work, but the variables don't exactly match up.</p>\n\n<p>For example, this code here:</p>\n\n<pre><code>B.request_handlers = {\n GetHandler:new(),\... | {
"AcceptedAnswerId": null,
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-11T06:52:41.173",
"Id": "24979",
"Score": "29",
"Tags": [
"object-oriented",
"lua",
"prototypal-class-design"
],
"Title": "Lua OOP and classically-styled prototypal inheritance"
} | 24979 |
<p>I have this piece of Java (Android) code that adds a list of brokers to a local SQLite database as one single SQL instruction.</p>
<pre><code>public void Add(List<Broker> brokers)
{
if(brokers == null || brokers.size() == 0)
return;
String sql = "INSERT INTO " + TABLE_NAME + " SELECT " + broker... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-11T08:04:43.947",
"Id": "38651",
"Score": "0",
"body": "Add some line breaks ;)"
}
] | [
{
"body": "<p>A classicel one. String concatenation with the <code>+</code> operator is very inefficient, because it creates a new copy of the string every time.<br>\nSee for example item 51 in Effective Java from Joshua Bloch or here: <a href=\"http://www.javapractices.com/topic/TopicAction.do?Id=4\" rel=\"nof... | {
"AcceptedAnswerId": "24981",
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-11T07:18:33.597",
"Id": "24980",
"Score": "3",
"Tags": [
"java",
"optimization",
"sql",
"android"
],
"Title": "Adding a list of brokers to a local SQLite database"
} | 24980 |
<p>I have a abstract class which is extended by many many other classes:</p>
<p><strong>What I have done:</strong></p>
<pre><code>public abstract class AbstractActionHandler {
protected WorkItem currentWI;
protected String status;
protected String field;
protected void init(WorkItem currentWI, Strin... | [] | [
{
"body": "<p>Your question is: <strong>Is it good to use a Singleton here?</strong></p>\n\n<p>My answer is: <strong>If you can avoid</strong> using a singleton, do it! And you answer this yourself: \"Of course I can avoid using a singleton if (...)\".</p>\n\n<p>Passing a <code>DataWorkItem</code> object to the... | {
"AcceptedAnswerId": "35722",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-11T07:55:05.077",
"Id": "24982",
"Score": "2",
"Tags": [
"java",
"singleton"
],
"Title": "Abstract class which uses a abstract factory -> New implementation with Singleton"
} | 24982 |
<p>Here's a fiddle: <a href="http://jsfiddle.net/YF8cg/" rel="nofollow">http://jsfiddle.net/YF8cg/</a></p>
<p>Focussing entirely on the javascript and on the structure of the HTML (<code>.qapair .question and .answer</code>), how could I improve this system?</p>
<p>JS:</p>
<pre><code>$(document).ready(function() {
... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-11T09:50:54.093",
"Id": "38655",
"Score": "1",
"body": "Cache your selectors for starters"
}
] | [
{
"body": "<p>HTML: The only change is that they are wrapped in a div id'ed <code>container</code> which will be used in event handling, explained below.</p>\n\n<pre><code><div id=\"container\">\n <div class=\"qapair\">\n <h4 class=\"question\">First Question</h4>\n &l... | {
"AcceptedAnswerId": "24987",
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-11T09:30:51.663",
"Id": "24985",
"Score": "4",
"Tags": [
"javascript",
"jquery",
"html"
],
"Title": "jQuery - Drop Down QA"
} | 24985 |
<p>I'm new to Rhino mock and I would like to know if the test I did is not fake testing, as I've read a lot about it. Are there any improvements that need to be done?</p>
<pre><code>[Test]
public void GenerateDateTable_Returns_DataTableOfGroup()
{
//Arrange
var groupStub = MockRepository.GenerateStub<Group&... | [] | [
{
"body": "<p>As far as I can understand (I use Moq as an isolation framework and not rhino mocks) in your test you are testing not an implementation of your class FillTableRow, but a mock, generated by isolation framework. You should always use your real class as systam under test (obviously). You use stubs to... | {
"AcceptedAnswerId": "24991",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-11T11:55:26.593",
"Id": "24990",
"Score": "1",
"Tags": [
"c#",
"unit-testing",
"mocks"
],
"Title": "Possible fake testing with Rhino mock"
} | 24990 |
<p>You can find a <a href="http://jsfiddle.net/mAuJr/" rel="nofollow">JSFiddle demo of my tabs</a>.</p>
<p>The jQuery tabs work well as intended. However I was asked to include arrows alongside the tabs so readers could navigate through tabs using them instead of the tab titles alone.</p>
<p>So I used:</p>
<pre><cod... | [] | [
{
"body": "<p>Firstly, the obvious:</p>\n\n<p>Both <code>.addClass</code> and <code>.removeClass</code> can deal with multiple classes at once, so wherever you have something like</p>\n\n<pre><code> $('#div1').removeClass( 'hide' );\n ...\n $('#div1').removeClass( 'show' );\n ...\n</code></pre>\n\n<p>you ca... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-11T12:30:16.657",
"Id": "24992",
"Score": "0",
"Tags": [
"javascript",
"jquery"
],
"Title": "Switching tabs, with previous/next arrows, using jQuery"
} | 24992 |
<p>I'm trying to convert TimeSpan object to text that can be read like a sentence.
e.g. </p>
<pre><code>TimeSpan(2, 1, 0, 0) --> "2 days and an hour"
TimeSpan(1, 2, 1, 0) --> "A day, 2 hours and a minute"
</code></pre>
<p>Some more samples for conversion are in the 'TestCases' object under 'TimeSpanPrettyForma... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-12T09:28:36.550",
"Id": "38710",
"Score": "0",
"body": "duplicate with http://codereview.stackexchange.com/questions/25009/format-a-timespan-with-years"
}
] | [
{
"body": "<p><code>ToPrettyFormat</code> method can definitely be improved. Instead writing the conditional statement for all possible permutations of <code>HasDays</code>, <code>HasHours</code> and <code>HasMinutes</code> it's better to step back and define rules for resulting string:</p>\n\n<ul>\n<li>string ... | {
"AcceptedAnswerId": "24996",
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-11T13:15:00.320",
"Id": "24995",
"Score": "5",
"Tags": [
"c#"
],
"Title": "convert timespan to readable text"
} | 24995 |
<p>numerator: Value being divided.</p>
<p>denominator: Divisor value.</p>
<p>method so far:</p>
<pre><code>def calc_percentage(numerator, denominator)
((numerator/ (denominator.to_f.nonzero? || 1 )) * 100)
end
</code></pre>
<p>What are the bad practices you see in the code above? How can I improve it or write it... | [] | [
{
"body": "<p>Well to me the bad thing is that the function produces an <em>unexpected behavior</em>.</p>\n\n<p>You try to divide by <code>0</code>, it divides by <code>1</code>. Huh?</p>\n\n<p>What I would do is throw an Argument Error Exception (or what ever other exception that you think is appropriate).</p>... | {
"AcceptedAnswerId": "25002",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-11T18:30:29.363",
"Id": "25001",
"Score": "3",
"Tags": [
"ruby"
],
"Title": "Preventing Division by Zero"
} | 25001 |
<p>I have this ugly age printing method. Can you do better?</p>
<pre><code>def age(birth_date)
today = Date.today
years = today.year - birth_date.year
months = today.month - birth_date.month
(months -1) if today.day < birth_date.day
if years == 0 && months == 1
age = "#{months} month"
elsif... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-11T21:33:23.280",
"Id": "38692",
"Score": "0",
"body": "You could consider using `Time.zone.today` or `Date.current` instead of `Date.today` as those methods are time-zone safe"
},
{
"ContentLicense": "CC BY-SA 3.0",
"Creat... | [
{
"body": "<p>Some notes:</p>\n\n<ul>\n<li><code>(months -1) if today.day < birth_date.day</code>. Note that this is doing nothing, <code>months - 1</code> is evaluated and dropped.</li>\n<li>There is a <code>age =</code> in every branch of the conditional, and finally <code>age</code> is returned. That's un... | {
"AcceptedAnswerId": "25006",
"CommentCount": "5",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-11T18:44:50.433",
"Id": "25003",
"Score": "3",
"Tags": [
"ruby",
"ruby-on-rails"
],
"Title": "Nasty Age Printing Method"
} | 25003 |
<p>Before I try to make this work, I wondered if anyone had tried this, whether it was a good idea or not, etc.</p>
<p>I find myself doing this a lot:</p>
<pre><code>if some_result is None:
try:
raise ResultException
except ResultException:
logger.exception(error_msg)
raise ResultExcep... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-11T19:56:36.767",
"Id": "38685",
"Score": "0",
"body": "Why not log the exception when you actually handle it? The traceback will show where it's from."
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-11T20:... | [
{
"body": "<p>This:</p>\n\n<pre><code>if some_result is None:\n with ResultException:\n logger.exception(error_msg)\n</code></pre>\n\n<p>would seem to be equivalent (for most cases) to:</p>\n\n<pre><code>if some_result is None:\n logger.exception(error_msg)\n raise ResultException\n</code></pre>... | {
"AcceptedAnswerId": "25008",
"CommentCount": "4",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-11T18:48:47.783",
"Id": "25004",
"Score": "2",
"Tags": [
"python",
"exception-handling",
"logging"
],
"Title": "Wrapping an Exception with context-management, using the with state... | 25004 |
<p>I have a class with 2 date properties: <code>FirstDay</code> and <code>LastDay</code>. <code>LastDay</code> is nullable. I would like to generate a string in the format of <code>"x year(s) y day(s)"</code>. If the total years are less than 1, I would like to omit the year section. If the total days are less than... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-11T20:51:17.813",
"Id": "38687",
"Score": "1",
"body": "Is there a reason why you are using a solar year (365.242 days) vs a calendar year (365 or 366 days) - not going to make a huge difference, but just looks odd since you are talkin... | [
{
"body": "<p>Overall, your code doesn't look bad. You have good use of white space and indentation. I found your variable names to be a little confusing (what's the difference between totalDays and days? Without actually digging into the code, it's not obvious).</p>\n\n<p>I like that you are using StringBui... | {
"AcceptedAnswerId": "25012",
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-11T20:23:26.477",
"Id": "25009",
"Score": "1",
"Tags": [
"c#",
".net",
"formatting"
],
"Title": "Format A TimeSpan With Years"
} | 25009 |
<p>I want to know if using <code>volatile</code> in this scenario will give better performance than using <code>synchronized</code>, specifically for the paused and running instance variable in the <code>SimulationManager</code> class.</p>
<pre><code>public class SimulationManager {
private List<SimulationPane... | [] | [
{
"body": "<p><code>volatile</code> should perform better and is appropriate in this situation. A volatile read/write is almost as fast as a non volatile read/write on modern architectures. On the other hand, locking with <code>synchronized</code> will have an overhead, especially in a highly contented scenario... | {
"AcceptedAnswerId": "25014",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-11T21:27:05.190",
"Id": "25011",
"Score": "3",
"Tags": [
"java",
"multithreading",
"thread-safety",
"synchronization"
],
"Title": "Using volatile instead of synchronized for a... | 25011 |
<p>Tonight I decided to revamp some code I wrote a few months ago. One part of that was to do with showing (and hiding) tooltips. The functions are called with:</p>
<pre><code>jQuery(element).showTooltip('HTML');
jQuery(element).hideTooltip();
</code></pre>
<p><strong>The old code</strong></p>
<pre><code>$.fn.showTo... | [] | [
{
"body": "<p>You can check out the <a href=\"http://jsfiddle.net/UPAXV/9/\" rel=\"nofollow\">optimized code at work here</a></p>\n\n<p>CSS: removed positioning and moved it to JS to make it dynamic</p>\n\n<pre><code>.tooltip {\n background:#000;\n color:#fff;\n padding:2px 5px;\n line-height:20px;\... | {
"AcceptedAnswerId": "25019",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-11T22:37:22.180",
"Id": "25013",
"Score": "1",
"Tags": [
"javascript",
"jquery"
],
"Title": "Have I put too much into my tooltip showing and hiding functions?"
} | 25013 |
<p>Generally speaking, I try and write my classes so they are highly cohesive.</p>
<p>Sometimes I have accessors (this problem isn't limited to accessors) which derive their value from non-public data only, which lowers cohesion. (<a href="http://www.ndepend.com/metrics.aspx#LCOM">http://www.ndepend.com/metrics.aspx#L... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-12T10:17:22.903",
"Id": "38712",
"Score": "2",
"body": "I'm not a fan of that property/method since it calls `DateTime.Now`. I'd rather pass in a `DateTime`."
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2014-08-... | [
{
"body": "<p>My personal view is create an extension method if:</p>\n\n<ul>\n<li>The class/interface you wish to extend <em>is not</em> created by you.</li>\n<li>The class/interface <em>is</em> created by you but the behavior of the extension method is only required in an assembly that doesn't contain the clas... | {
"AcceptedAnswerId": "25023",
"CommentCount": "3",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-12T03:44:48.197",
"Id": "25017",
"Score": "5",
"Tags": [
"c#",
".net",
"extension-methods"
],
"Title": "Extension methods for methods and properties that don't use non-public data... | 25017 |
<p>A friend of mine asked me a question recently. He was needed to subscribe to an event of some object (window button click) and unsubscribe from it on a first call of a handler. Also he noticed that object is dynamically created and his handler is a delegate.</p>
<p>After some thinking I came up with a solution usin... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-10T20:37:16.470",
"Id": "38697",
"Score": "0",
"body": "@Jesse The question is really very concrete, \"does this code leak X\" is not subjective; it either does or it doesn't, making it appropriate on SO. Were it just, \"make this bet... | [
{
"body": "<p>The code that you have will not result in holding onto a reference to any variables that the anonymous handler closes over once that handler is fired and the <code>handler</code> variable leaves scope or is set to something else (i.e. <code>null</code>), even if the <code>A</code> instance is kept... | {
"AcceptedAnswerId": "25021",
"CommentCount": "4",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-10T20:09:04.103",
"Id": "25020",
"Score": "1",
"Tags": [
"c#",
"delegates"
],
"Title": "Possible memory-leak on a self-removable event handler"
} | 25020 |
<pre><code>public function getStuff($brand)
{
$web=FALSE;
if($this->getWebcorners()):
foreach ($this->getWebcorners() as $webcorner):
if(strtolower($webcorner->getBrand()->getName())== $brand):
return $webcorner;
endif;
... | [] | [
{
"body": "<p>Just cleaned up your method a little: (Guard condition, better variable name, less nesting)</p>\n\n<pre><code>public function getStuff($brand)\n{\n if(!$this->getWebcorners()) return false;\n $fallback=false;\n foreach ($this->getWebcorners() as $webcorner) {\n if(strtolower(... | {
"AcceptedAnswerId": "25026",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-12T08:37:10.473",
"Id": "25024",
"Score": "1",
"Tags": [
"php"
],
"Title": "List a collection of webcorners"
} | 25024 |
<p>I'm fairly new to JavaScript and jQuery and this is my first attempt at creating a plugin. The code below I've written to parse a JSONP feed from a search engine API (funnelback) using ajax() calls. The returned object is used to create a set of results, some pagination links, a facetted navigation etc, everything y... | [] | [
{
"body": "<p>Johntyb, the main problem you will have is with all those vars defined in the <code>$.fn.uclfunnelback</code> namespace; <code>options</code>, <code>defaults</code>, <code>facetsSelected</code>, etc. </p>\n\n<p>Any vars defined there belong to the plugin itself, not to each instance of the plugin,... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-12T11:34:57.633",
"Id": "25029",
"Score": "1",
"Tags": [
"javascript",
"jquery",
"performance"
],
"Title": "jQuery plugin: Parsing a JSONP feed using ajax()"
} | 25029 |
<p>I submitted a previous version of this program but I've completely rewritten it in an Object Oriented style. This is only my second attempt at OO programming so I'm interested in hearing how I can improve things.</p>
<p>Here's the main program:</p>
<pre><code>import tweepy
import sqlite3
import json
import re
impo... | [] | [
{
"body": "<p>using <code>'''...'''</code> creates <code>'/n'</code> at the end of your lines, which you may find unexpected and is not explict.</p>\n\n<p>A pythonic alternative is to use:</p>\n\n<pre><code>'CREATE TABLE tweets ('\n'id int not null unique'\n'text text,'\n'url text,'\n</code></pre>\n\n<p>and so ... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-12T13:12:56.063",
"Id": "25034",
"Score": "1",
"Tags": [
"python",
"object-oriented",
"twitter"
],
"Title": "Object-oriented Twitter filter"
} | 25034 |
<p>I would like feedback on how to optimize this jQuery plugin used for triggering change event on any DOM element.
This plugin doesn't use a timer to check for DOM changes but instead extends some jQuery native functions.
I'm mostly concerned by the <code>.on()</code> method extension because I'm sure there is better ... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-16T14:14:40.047",
"Id": "38894",
"Score": "0",
"body": "Something you should check out: **Pub/Sub**. An awesome way of setting up, turning off, and triggering events. Links:\nhttp://msdn.microsoft.com/en-us/magazine/hh201955.aspx\nhttp... | [
{
"body": "<p>I dont know if there is a better way to do it, but it seems quite smart and minimally intrusive.</p>\n\n<p>From a once over style perspective:</p>\n\n<ul>\n<li><code>function (key, element)</code> <- since you dont use element, you can remove it from the signature</li>\n<li><p>This</p>\n\n<pre>... | {
"AcceptedAnswerId": null,
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-12T13:53:22.340",
"Id": "25035",
"Score": "4",
"Tags": [
"javascript",
"jquery"
],
"Title": "Optimize jquery plugin \"on\" method extension"
} | 25035 |
<p>I'm understanding the question, but I want to know whether or not my implementation is correct.</p>
<p>The question is:</p>
<blockquote>
<p>Write a method that accepts as its argument a BinaryTree object and
returns true if the argument tree is a binary search tree. Examine
each node in the given tree only o... | [] | [
{
"body": "<p>Your implementation looks good but I can spot a some flaws in it.</p>\n\n<p>To begin, you check that <code>tree == null</code> after having already dereferenced it at <code>BinaryNode Node = new BinaryNode (tree.getRootData);</code>.</p>\n\n<p>Moreover I'd like more to see an <code>IllegalArgument... | {
"AcceptedAnswerId": "25042",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-04-12T14:11:46.460",
"Id": "25036",
"Score": "5",
"Tags": [
"java",
"algorithm",
"binary-search",
"tree"
],
"Title": "Check if a Binary Tree <String> is aBinary Search Tree"
} | 25036 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.