body stringlengths 25 86.7k | comments list | answers list | meta_data dict | question_id stringlengths 1 6 |
|---|---|---|---|---|
<p>Is there a way to make this more pythonic?</p>
<pre><code>def money():
current_salary = float(input("What is your current salary? "))
years = int(input("How many years would you like to look ahead? "))
amount_of_raise = float(input("What is the average percentage raise you think you will get? ")) * 0.01... | [] | [
{
"body": "<p>Maybe you could write: </p>\n\n<p>Instead of <code>print('Looks like you will be making', current_salary,' in ', years,'years.')</code> you could write <code>print('Looks like you will be making %d in %d years.') % (current_salary, years)</code></p>\n\n<p>Also, and this one is kind of important, y... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-06T22:09:19.470",
"Id": "28208",
"Score": "2",
"Tags": [
"python"
],
"Title": "Calculate cumulative salary including raises"
} | 28208 |
<p>I wrote this program for an exercise in Bjarne Stroustrup's text Programming -- <em>Principles and Practice Using C++</em>. He recommended using a <code>vector</code> for pseudo randomness, however I opted for <code>srand()</code> and <code>rand()</code> from <code>cstdlib</code>.</p>
<p>Any recommendations regardi... | [] | [
{
"body": "<ul>\n<li><p><a href=\"https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice\">Try not to use <code>using namespace std</code></a>. Using it in local scope (such as a function) is better than having it in global scope, but it's still quite repetitive to have ... | {
"AcceptedAnswerId": "28221",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-07T01:12:48.937",
"Id": "28212",
"Score": "5",
"Tags": [
"c++",
"beginner",
"game",
"rock-paper-scissors"
],
"Title": "Simplification and efficiency suggestions for \"Rock, Pa... | 28212 |
<p>Yes, I know there are plenty of libraries to get this done (Q, step, async), but only for learning I'm wondering if the following code is ok and usable or have major drawbacks and it is only usable at a small programs.</p>
<p>Usage:</p>
<pre><code>var g = new Manager();
g.then (
function() {
console... | [] | [
{
"body": "<p>Some points:</p>\n\n<ul>\n<li><code>g.then(function() { … g.done (\"two done\");})</code> I (personally) don't like that signature, I'm more used to callbacks that get passed as parameters (<code>g.then(function(done) { … done (\"two done\");})</code>). They tend make the chained functions better ... | {
"AcceptedAnswerId": "28214",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-05T08:41:42.573",
"Id": "28213",
"Score": "1",
"Tags": [
"javascript",
"node.js",
"asynchronous"
],
"Title": "Synchronizing async tasks"
} | 28213 |
<p>I am doing some tests with html5 Canvas object. At the same time I am trying to make my javascript code more modular. In the following code, where/how should I declare canvas and context, so that it can be shared with other objects? Please provide general comments/improvements?</p>
<p>Output:</p>
<p><img src="ht... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-05T06:33:14.593",
"Id": "44069",
"Score": "0",
"body": "I'm not sure this will be helpful, but I created a github project called [brushes.js](https://github.com/jimschubert/brushes.js) which somewhat tries to encapsulate *brushes* to d... | [
{
"body": "<p>You can wrap the functionality in an object. You instantiate it with a canvas element and some configuration options.</p>\n\n<pre><code>(function($) {\n\n// config defaults \nvar defaults = {\n LEFT_INDENT: 100,\n START_WORK_HOUR: 8,\n END_WORK_HOUR: 17,\n AXIS_SIDE_MARGIN: 20,\n ... | {
"AcceptedAnswerId": null,
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-05T02:57:10.467",
"Id": "28215",
"Score": "4",
"Tags": [
"javascript",
"html",
"object-oriented",
"canvas"
],
"Title": "javascript canvas -javascript design"
} | 28215 |
<p>This program will show or alter the timestamp header on gzip archives. It was created for people who have issues using hash functions to compare gzip archives as the timestamps would be different. I want my code to really speak its intention without having to use comments outside of initialization (unless it is bett... | [] | [
{
"body": "<p>An interesting program. A few comments</p>\n\n<ul>\n<li><p>there is too much in <code>main</code>. You could usefully split it into several\nseparate functions, one of which would handle argument processing.\nAdmittedly, your use of <code>getopt</code> from <code>main</code> means that you can se... | {
"AcceptedAnswerId": "28233",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-07T01:27:00.880",
"Id": "28217",
"Score": "5",
"Tags": [
"c",
"datetime"
],
"Title": "Showing or altering the timestamp header on gzip archives"
} | 28217 |
<p><strong>Information about my code:</strong></p>
<p>I am following this <a href="http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-046j-introduction-to-algorithms-sma-5503-fall-2005/video-lectures/" rel="nofollow">MIT OCW algorithms course</a>. The first lecture described insertion sort and me... | [] | [
{
"body": "<p>I've slightly reorganised your code to make it easier to follow.</p>\n\n<pre><code>void merge_parts(int arr[], int length)\n{\n /*\n Sorts into increasing order\n For decreasing order change the comparison in for-loop\n */\n int ans[length];\n //This for and next if-else puts the... | {
"AcceptedAnswerId": "28234",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-07T14:25:43.900",
"Id": "28224",
"Score": "6",
"Tags": [
"optimization",
"c",
"recursion",
"mergesort"
],
"Title": "Recursive implementation of merge sort"
} | 28224 |
<p>I am currently going through Codecademy's Python course (I don't come from a coding background but I'm thoroughly enjoying it) and I got to the end of one of the sections and thought, "Well it worked, but that seemed wildly inefficient." I tried shortening unnecessary parts and came up with this:</p>
<pre><code>ll... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-07T14:50:32.037",
"Id": "44104",
"Score": "1",
"body": "Can you add information about the problem you are trying to solve ?"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-07T15:06:50.560",
"Id": "44106... | [
{
"body": "<ul>\n<li>In the function <code>get_letter_grade</code> you were checking more conditions then necessary. Try to follow the logical difference between your function and my code. It is simple math which I used to optimize it. Also structuring it as an if-elif-...-else is better for readability.</li>\n... | {
"AcceptedAnswerId": null,
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-06T22:32:45.643",
"Id": "28226",
"Score": "0",
"Tags": [
"python"
],
"Title": "Calculating student and class averages"
} | 28226 |
<p>I have the following code to open and close some popup divs. I'd like to know whether there is a shorter solution to close the currently opened divs, as soon as another div is opened, so that there is only one open div.</p>
<p>The current solution works as you can see in <a href="http://jsfiddle.net/Gq6eK/2/y" rel=... | [] | [
{
"body": "<p>You can use the <code>not</code> function to get all the details elements except one:</p>\n\n<pre><code>function show(sel) {\n var el = $(sel);\n el.fadeToggle(600);\n $('.details').not(el).fadeOut(\"slow\");\n}\n\n$('.details').hide();\n\n$('#item-a').click(function () {\n show('#detail-1');\... | {
"AcceptedAnswerId": "28238",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-07T20:17:24.210",
"Id": "28235",
"Score": "2",
"Tags": [
"javascript",
"jquery",
"html"
],
"Title": "jQuery fadeToggle/fadeout function"
} | 28235 |
<p>Say I have <code>[1, 2, 3, 4, 5, 6, 7, 8, 9]</code> and I want to spread it across - at most - 4 columns. The end result would be: <code>[[1, 2, 3], [4, 5], [6, 7], [8, 9]]</code>. So I made this:</p>
<pre><code>item_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
item_count = len(item_list)
column_count = item_count if item_co... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-08T09:32:37.943",
"Id": "44142",
"Score": "0",
"body": "What are the constraints ? I assume that : 1) a column cannot be be bigger than a column on its left 2) the maximum size difference between two columns must be 0 or 1 3) the eleme... | [
{
"body": "<p>How about using generators? </p>\n\n<ul>\n<li>It puts the whole thing in a function which in <a href=\"https://stackoverflow.com/questions/11241523/why-does-python-code-run-faster-in-a-function\">itself is a performance boost</a>. </li>\n<li>List comprehension are better compared to list creation ... | {
"AcceptedAnswerId": null,
"CommentCount": "3",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-08T08:27:11.953",
"Id": "28244",
"Score": "1",
"Tags": [
"python"
],
"Title": "Taking a list and using it to build columns"
} | 28244 |
<p><strong>Edit:</strong> Even adding a comment of "looks good to me" and up-voting that comment would be appreciated :) I just want the feedback! Thanks!</p>
<p>For the fun of it as well as professional development, I wrote a small "library" (single 24 line JavaScript function) that will allow a developer to double c... | [] | [
{
"body": "<p>I like the look of this in general. Its short, simple and I'd love to know how well its worked in the field.</p>\n\n<p>I'm not a big fan of <code>document.write</code> but I don't think the alternative is much better. If you look in the jQuery source code you'll see they eval the script.</p>\n\n<p... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-08T11:41:29.893",
"Id": "28246",
"Score": "6",
"Tags": [
"javascript",
"library",
"library-design"
],
"Title": "CDN Fallback \"library\""
} | 28246 |
<p>I came across this interview question and it has been asked to print combinations of the characters in a string. For example: "abc" --> a, b, c, ab, ac, bc, abc. Also it has been mentioned that 'ab' and 'ba' are same. </p>
<p>I want to know if there is any improvement I can make in terms of memory usage/performance... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-08T07:07:31.133",
"Id": "44152",
"Score": "0",
"body": "For big strings you can fall through stackoverflow exception."
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-08T07:15:10.327",
"Id": "44153",
... | [
{
"body": "<p>I can suggest following project for combinations and permutations which is very efficient and easy to use:</p>\n\n<p><a href=\"http://www.codeproject.com/Articles/26050/Permutations-Combinations-and-Variations-using-C-G\">http://www.codeproject.com/Articles/26050/Permutations-Combinations-and-Vari... | {
"AcceptedAnswerId": null,
"CommentCount": "3",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-08T07:01:43.737",
"Id": "28248",
"Score": "8",
"Tags": [
"c#",
"algorithm"
],
"Title": "Implement a function that prints all possible combinations of the characters in a string"
} | 28248 |
<p>After finding myself sometimes needing a map of maps, and as I couldn't find one in existing libs (like Guava), I wrote my own implementation. Do you think this version is okay? Specifically: Is it acceptable that I don't permit empty "inner" maps as content for my "outer" map, hence probably breaking the contract o... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-08T18:26:20.220",
"Id": "44195",
"Score": "0",
"body": "Hi, could you elaborate on what exactly you are trying to accomplish with your map of maps? By doing so, it would help us comment on your questions towards the end of your descri... | [
{
"body": "<p>Rather than making a map of maps (<code>Map<K1,Map<K2,V>></code>), I find it often easier to make a map that uses a composite key : <code>Map<Key<K1, K2>, V></code>.</p>\n\n<p>You could make a generic <code>Key</code> class for this, or make a dedicated class on a per case ... | {
"AcceptedAnswerId": "63654",
"CommentCount": "3",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-08T13:55:52.490",
"Id": "28252",
"Score": "4",
"Tags": [
"java",
"collections"
],
"Title": "Class for Map of Maps"
} | 28252 |
<p>I got bored recently and wrote a tool to re-order out of order classes in Python files. This can occur with some auto-generation code tools for databases and the like; these files can sometimes be thousands of classes big.</p>
<p>I've posted it here to gauge its general utility and get feedback on the code (logic f... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2014-07-04T20:54:56.387",
"Id": "98776",
"Score": "0",
"body": "Can you describe the problem in more detail? What auto-generation code? What databases? What goes wrong? Why does it go wrong? Why is your solution better than fixing the auto-gen... | [
{
"body": "<p>Some tidbits:</p>\n\n<p>Might be anal, but I would have sorted my imports differently. For me it just makes it easier to find what I am looking for, but I can see why you might separate the way you did:</p>\n\n<pre><code>import getopt\nimport inspect\nimport os\nimport sys\nimport types\n\nfrom co... | {
"AcceptedAnswerId": null,
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-08T14:11:49.530",
"Id": "28254",
"Score": "7",
"Tags": [
"python",
"sorting",
"python-2.x",
"inheritance"
],
"Title": "Out of order class resequencer"
} | 28254 |
<p>Building on my previous questions. I've managed to make a GUI that queries a SQL database and displays the result in a table. Christ, it's a lot of work for such a simple activity. </p>
<p>The code below works. Does it adhere to MVC conventions? If I understand correctly, <code>MainFrame</code> and <code>CustomTabl... | [] | [
{
"body": "<ol>\n<li><pre><code>@Override\npublic void actionPerformed(ActionEvent e) {\n try {\n ct.reQuery();\n</code></pre>\n\n<p>Action listeners run on the <a href=\"https://en.wikipedia.org/wiki/Event_dispatching_thread\" rel=\"nofollow noreferrer\">Event dispatching thread</a> and they should b... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-08T17:12:21.987",
"Id": "28256",
"Score": "5",
"Tags": [
"java",
"mvc"
],
"Title": "Combining a JFrame, AbstractTableModel, and SQL Query"
} | 28256 |
<p>I wanted to eliminate some duplication and came up with something I've not seen so I figured I post it and see if others had.</p>
<p>Using a delegate to allow the method to return another call to itself.</p>
<p>The commented out code is what I replaced.</p>
<pre><code>delegate ChainRepositoryCommand ChainReposito... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-08T20:10:42.257",
"Id": "44199",
"Score": "0",
"body": "That... Is rather interesting. And a little disturbing."
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-10T06:56:09.857",
"Id": "44343",
"Sco... | [
{
"body": "<blockquote>\n <p>Using a delegate to allow the method to return another call to itself.</p>\n</blockquote>\n\n<p>You don't necessarily need a delegate to allow another call to itself. For example:</p>\n\n<pre><code>public class SimpleCalculation\n{\n private int result;\n\n public int Result\... | {
"AcceptedAnswerId": null,
"CommentCount": "3",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-08T18:34:52.677",
"Id": "28259",
"Score": "5",
"Tags": [
"c#",
"functional-programming"
],
"Title": "helper method returning a call to itself"
} | 28259 |
<p>We are in the process of creating a book with the purpose of teaching people the art of html and CSS to create a website from scratch. In our book the focal point, so to say, is that the reader is given a webdesign that he converts into a website by himself.</p>
<p>The reader is given the webdesign for the website ... | [] | [
{
"body": "<p>Your logo (resp. site title) should be in a <code>h1</code>. Otherwise your page (→ <code>body</code>) got <s>no heading</s> a wrong heading in the outline (see explanation below): <code><h1><a href=\"/\"><img src=\"images/logo.jpg\" alt=\"Eos Café\" id=\"logo\"></a></h1... | {
"AcceptedAnswerId": "28295",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-08T20:29:06.137",
"Id": "28261",
"Score": "10",
"Tags": [
"html",
"css",
"html5"
],
"Title": "Strict and clean HTML and CSS for teaching website"
} | 28261 |
<p>I'm working on a simple CMS. It's working quite ok, but I think my code can be improved a bit. I work with a section.php, which is included from the index.php with (example):</p>
<pre><code>switch($_GET['key'])
{
case '': $page='section'; $id='1'; break;
case 'home': $page='section'; $id='1'; break;
}
include ('hea... | [] | [
{
"body": "<p>Well,\nfirst of all, OOP is not something you simply use. OOP is a concept of classes and objects. But it doesnt magically make your code better.</p>\n\n<p>Your main problem here is that your business logic is tightly coupled with you displaying of date (the view). You are fetching data, validatin... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-08T20:44:23.947",
"Id": "28262",
"Score": "5",
"Tags": [
"php",
"object-oriented",
"sql"
],
"Title": "Simple content management system"
} | 28262 |
<p>I am attempting to come up with a better method for doing this:</p>
<pre><code>public static String longToPlayerName(long name) {
int i = 0;
char[] nameCharacters = new char[12];
while (name != 0L) {
long ll = name;
name /= 37L;
nameCharacters[11 - i++] = playerNameXlateTable[(int... | [] | [
{
"body": "<p>Pretty good overall, but a few things:</p>\n\n<ul>\n<li>Don't hard-code the 37. Instead, use the translation table's size.</li>\n<li><code>ll</code> is a bad variable name. It's not descriptive at all, and it looks like <code>11</code>.</li>\n<li><p>Your loop in <code>longToPlayerName</code> can b... | {
"AcceptedAnswerId": "28269",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 4.0",
"CreationDate": "2013-07-08T21:56:32.363",
"Id": "28264",
"Score": "4",
"Tags": [
"java",
"strings",
"converting",
"integer"
],
"Title": "Long-to-string and string-to-long while checking characters wi... | 28264 |
<p>As far as I can tell, no gem currently exists to wrap shred, the *nix utility to securely delete files. I created a small class to do this, the key methods are:</p>
<pre><code>def self.run cmd
stdout,stderr,status = Open3.capture3 cmd
raise "Failed executing #{cmd}: stdout: #{stdout}, stderr: #{stderr}, status... | [] | [
{
"body": "<p>Some notes:</p>\n\n<ul>\n<li><code>def self.run cmd</code>: Omiting parentheses on signatures is less and less idiomatic as time goes by, people have realized they are harder to read. I'd put them.</li>\n<li><code>Open3.capture3 cmd</code>: It's also less idiomatic than it was years ago to omit pa... | {
"AcceptedAnswerId": "28286",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-09T01:34:50.480",
"Id": "28265",
"Score": "3",
"Tags": [
"ruby"
],
"Title": "Securely deleting files, while attempting to protect the user against obviously dangerous inputs"
} | 28265 |
<p>I made an example for learning <a href="http://en.wikipedia.org/wiki/Dependency_inversion_principle">Dependency inversion principle</a> in JavaScript OOP programming .</p>
<pre><code>String.prototype.ucfirst = function() {
return this.charAt(0).toUpperCase() + this.slice(1);
}
function textHandler(){
this... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-09T11:53:23.660",
"Id": "44232",
"Score": "0",
"body": "Why is the DOM element created inside `htmlHandle` (rather than injected)? Why are you creating HTML from strings? Why are you joining strings for property lookup? You might want ... | [
{
"body": "<p>From a once over;</p>\n\n<ul>\n<li><code>ucfirst</code> <- not a great name</li>\n<li>Single comma separated <code>var</code> statements <- nice</li>\n<li>Consider using <code>use strict</code></li>\n<li>You need more comments explaining how you made DIP work, honestly I am not convinced tha... | {
"AcceptedAnswerId": null,
"CommentCount": "4",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-09T08:45:22.493",
"Id": "28273",
"Score": "6",
"Tags": [
"javascript",
"jquery"
],
"Title": "OOP Dependency Inversion Principle"
} | 28273 |
<p>I have session controller for mobile devices. I would like to refactor it a little, for example because my session controller is sub class of <code>Devise::SessionsController</code> I had to duplicate error methods from <code>BaseController</code>.</p>
<pre><code>class Api::V1::SessionsController < Devise::Sessi... | [] | [
{
"body": "<p>Put the common error methods in a Module and include it in each Controller where you need them (in a similar way like you already include <code>Devise::Controllers::Helpers</code>, but this time with your own namespace and module name.)</p>\n\n<p>As for the method signatures</p>\n\n<pre><code>def ... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-09T08:48:13.913",
"Id": "28274",
"Score": "2",
"Tags": [
"ruby",
"design-patterns",
"ruby-on-rails",
"api",
"session"
],
"Title": "How can I refactor Api::SessionsController?... | 28274 |
<p>I want to abstract away differences between <a href="http://docs.python.org/2/library/zipfile.html" rel="nofollow">zipfile</a> and <a href="https://pypi.python.org/pypi/rarfile/" rel="nofollow">rarfile</a> modules. In my code i want to call <code>ZipFile</code> or <code>RarFile</code> constructors depending on file ... | [] | [
{
"body": "<p>Classes are first-class objects in Python.</p>\n\n<pre><code>amap = {\n 'zip': zipfile.ZipFile,\n 'rar': rarfile.RarFile\n}\n\n ...\n\nwith amap[archive_type](encode(f)) as archive:\n ...\n</code></pre>\n\n<p>or</p>\n\n<pre><code>with amap.get(archive_type, rarfile.RarFile)(encode(f)) as archi... | {
"AcceptedAnswerId": "28277",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-09T09:02:56.733",
"Id": "28275",
"Score": "1",
"Tags": [
"python",
"constructor"
],
"Title": "Dynamic object creation in Python"
} | 28275 |
<p>I have extension methods to encapsulate query conditions, however I have to separate <code>IQueryable</code> and <code>IEnumerable</code>. Is there any way I can reduce the redundancy and still not lose the performance benefit of <code>IQueryable</code>?</p>
<pre><code>public static class PAYTRACK_PARTNER_Extension... | [] | [
{
"body": "<p>You can use <code>IQueryable<T></code> everywhere, because you can convert to it from <code>IEnumerable<T></code> by using <a href=\"http://msdn.microsoft.com/en-us/library/bb507003.aspx\" rel=\"nofollow\"><code>AsQueryable()</code></a>:</p>\n\n<pre><code>public static IQueryable<PA... | {
"AcceptedAnswerId": "28281",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-09T09:12:26.640",
"Id": "28276",
"Score": "2",
"Tags": [
"c#",
"iterator"
],
"Title": "Refactor IQueryable and IEnumerable that share the same condition"
} | 28276 |
<p>I have, in Java, an <code>arraylist</code> with these values (many lines, this is just an extract):</p>
<blockquote>
<p>20/03/2013 23:31:46 6870 6810 6800 6720 6860 6670 6700 6650 6750 6830 34864 34272
20/03/2013 23:31:46 6910 6780 6800 6720 6860 6680 6620 ... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-09T18:48:41.353",
"Id": "44287",
"Score": "0",
"body": "Please, for future maintainer/international safety, use an ISO-formatted locale-independent 'local'-timezone-ignorant [timestamp](http://en.wikipedia.org/wiki/ISO_8601). In the U... | [
{
"body": "<p>To answer your question.. it depends on how you want to handle the data. Take a step back and put your self in the users shoes. Lets say the user has those numbers saved as bank account transactions.. He would want those numbers saved somewhere, but not necessarily visible. If that was the case yo... | {
"AcceptedAnswerId": "28288",
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-09T11:45:50.450",
"Id": "28284",
"Score": "1",
"Tags": [
"java",
"strings"
],
"Title": "Make an arraylist of unique values"
} | 28284 |
<p>I know that there is a function <code>bsearch</code> present in <code>stdlib.h</code> but still I want to implement this. </p>
<p>This is my code for binary search. Any and all reviews are welcome.</p>
<pre><code>#include<stdio.h>
#include<stdlib.h>
int compare (const void * a, const void * b)
{
r... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-09T18:34:33.790",
"Id": "44285",
"Score": "1",
"body": "Any discussion regarding this question can be done in this [chat room](http://chat.stackexchange.com/rooms/9579/discussion-between-aseem-bansal-and-josay). @codesparkle Just leave... | [
{
"body": "<p>Update <code>bin_search</code> with a best-case scenario <strong>O(1)</strong>, if the array has only 1 element.</p>\n\n<pre><code>int bin_search (int arr[], int min_index, int max_index, int element)\n{\n // this is the best case scenario\n if(min_index == max_index)\n {\n if(a... | {
"AcceptedAnswerId": "28299",
"CommentCount": "3",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-09T11:49:36.997",
"Id": "28285",
"Score": "5",
"Tags": [
"optimization",
"c",
"performance"
],
"Title": "Binary Search in C - Optimization"
} | 28285 |
<p>This is the relevant piece of my code (false is returned if the whole cycle is finished, pattern is a String passed to the function):</p>
<pre><code>for (FileLine fileLine : fileLines) {
itemText = fileLine.getText();
itemStrings = new ArrayList<String>();
itemStrings.addAll(Arrays.asList((itemTe... | [] | [
{
"body": "<p>You should probably be using <code>find()</code> instead of <code>matches()</code> (<a href=\"https://stackoverflow.com/questions/7459263/regex-whole-word#answer-7459291\">source</a>).</p>\n\n<p>I would recommend using <a href=\"http://regexpal.com/\" rel=\"nofollow noreferrer\">regexpal</a> to te... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-09T13:46:23.890",
"Id": "28289",
"Score": "2",
"Tags": [
"java",
"performance",
"regex"
],
"Title": "Matching a pattern in many lines of text"
} | 28289 |
<p>I'm working on figuring out the best way to design this so that it's well organized and it seems like the factory design pattern makes sense. </p>
<p>Updated:
<strong>What I'm building:</strong>
- Pricing calculator
- There are 4 products each having their own specific pricing rules that vary from user input
-... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-09T20:16:28.603",
"Id": "44295",
"Score": "1",
"body": "I love the `return; break;` combos. I always do that too. I type `case ...:` and `break;` and then anything in between. But I always have a `default: break;` too :)"
},
{
... | [
{
"body": "<h2>Flexible approach</h2>\n\n<ol>\n<li>First defining an ultimate abstraction of the factory</li>\n<li>Implement it with product type - class mapping</li>\n<li><p>Creating an entry point to register and use the factory</p>\n\n<pre><code>interface IProductFactory {\n\n function createProduct($type... | {
"AcceptedAnswerId": null,
"CommentCount": "7",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-09T14:15:33.110",
"Id": "28290",
"Score": "1",
"Tags": [
"php",
"design-patterns",
"factory-method"
],
"Title": "PHP design pattern factory input for products and quotes"
} | 28290 |
<p>I'm yet to add garbage cleanup, ID regeneration and the ability to unset sessions, but this is what I have so far.</p>
<ul>
<li>Does this help prevent session hijacking/fixation?</li>
<li>Can you see any vulnerabilities?</li>
</ul>
<p><strong>index.php</strong></p>
<p>
<pre><code>require 'Session.class.php';
//... | [] | [
{
"body": "<p>At this moment $_SESSION would be a lot safer then storing it in the database.</p>\n\n<p>What you are doing is the same as session, but instead of storing everything in a file in the filesystem (like $_SESSION does) you are storing it in the database.</p>\n\n<p>$_SESSION works with a cookie (just ... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-09T14:27:10.340",
"Id": "28291",
"Score": "4",
"Tags": [
"php",
"session"
],
"Title": "My Session Handler"
} | 28291 |
<p>I have written an infix-to-postfix program that I would like reviewed and with a few suggestions on improving it, such as a more modularized approach.</p>
<pre><code>package basicstrut;
import java.util.Stack;
public class Infix {
private String infixStr;
private Stack<Character> oprStack;
pu... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-10T11:30:40.750",
"Id": "44358",
"Score": "0",
"body": "@DaveJarvis What kind of life cycle for `Infix` are you imagining such that it ever needs to be thread-safe?"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2... | [
{
"body": "<p>One small thing you can do is improve the readability of things like </p>\n\n<pre><code>valueOfOpr(opStack) != 5\n</code></pre>\n\n<p>by making an <code>Operator enum</code> like so (notice I took the liberty of re-ordering the square brackets, correct me if I'm wrong):</p>\n\n<pre><code>public st... | {
"AcceptedAnswerId": null,
"CommentCount": "4",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-09T15:05:01.093",
"Id": "28294",
"Score": "2",
"Tags": [
"java",
"performance",
"algorithm",
"math-expression-eval"
],
"Title": "Infix to Postfix conversion in Java"
} | 28294 |
<p>I have a method:</p>
<pre><code> def removeExpiredCookies(jar: CookieJar): CookieJar = {
val currentDate = System.currentTimeMillis / 1000L
jar.filter(_._2._2 > currentDate)
}
</code></pre>
<p>You use said method like so.</p>
<pre><code>var cookieJar = new CookieJar
cookieJar = removeExpiredCookies(... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-09T21:15:31.163",
"Id": "44299",
"Score": "0",
"body": "You could also put `removeExpiredCookies` to `CookieManager`'s (along with the types) companion object. This way, it'd be obvious that it doesn't use or modify any `CookieManager`... | [
{
"body": "<p>(I'm making my comment into an answer.) You can separate everything that is static (not using the state of instances of <code>CookieManager</code>) into a companion object (and import the object at the beginning of the class for convenience). This way, it is clear what functions use <code>this</co... | {
"AcceptedAnswerId": null,
"CommentCount": "3",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-09T16:40:46.573",
"Id": "28297",
"Score": "3",
"Tags": [
"functional-programming",
"scala"
],
"Title": "What takes precedence: readability or elimination of side effects?"
} | 28297 |
<p>this code works fine but i want to know if this can be improved in any manner .</p>
<p>For every user I am fetching the namesList corresponding to that user and for each fetched nameList , i am fetching the ItemsList .</p>
<p>I have tried to replicate the sample using this below standalone program as i cant paste ... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-09T18:37:24.387",
"Id": "44286",
"Score": "0",
"body": "`Names` is orthogonal to `Items`, right? That is, names are related to a user, and items are related to a user, but (user) names aren't really related to items. `getItemsCorresp... | [
{
"body": "<p>Before avoiding to nesting for loop you must have to clean you code ..Here is the same code mentioned in your question with little bit of correction which helps you to write clean code. \nChanges : </p>\n\n<ul>\n<li>Use interfaces instead of implementations</li>\n<li>Use final in methos parameters... | {
"AcceptedAnswerId": "28318",
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-09T18:12:04.293",
"Id": "28302",
"Score": "1",
"Tags": [
"java"
],
"Title": "Can I avoid nested for loop in this case?"
} | 28302 |
<p>I have a Rails app and in it I have two models, widget and votes. Votes are used to keep track of the desirability of a widget.</p>
<p>In my models I have:</p>
<pre><code>class Vote < ActiveRecord::Base
belongs_to :widget
end
class Widget < ActiveRecord::Base
has_many :votes
end
</code></pre>
<p>I ... | [] | [
{
"body": "<p>I would not create custom controller actions. <code>up</code> and <code>down</code> are attributes of a <code>Vote</code>, so I would do the following:</p>\n\n<pre><code>resources :widgets do\n resources :votes, only: [:new, :create]\nend\n</code></pre>\n\n<p>And then send a <code>POST</code> to ... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-09T19:13:08.967",
"Id": "28303",
"Score": "2",
"Tags": [
"ruby",
"ruby-on-rails",
"mvc",
"url-routing"
],
"Title": "Nested routes and proper place for a function?"
} | 28303 |
<p>Is there a better way to implement this class, or does it not constitute implementation in the first place? This is one of my first few tries at OOP. Any suggestions will be much appreciated. </p>
<pre><code>#include <iostream>
#include <vector>
#include <cmath>
class PrimeList
{
public:
... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-09T22:38:36.707",
"Id": "44319",
"Score": "1",
"body": "You have a small typo -- `sqrt` should be `std::sqrt`. Unfortunately, some compilers don't warn about this, and then we're in for a nasty surprise when the code suddenly refuses t... | [
{
"body": "<ul>\n<li><p>As a general rule for classes: data members (variables), such as <code>vPrime</code>, should be <code>private</code>. They can either be declared under <code>private</code> or outside of <code>public</code> (a <code>class</code> is <code>private</code> by default).</p></li>\n<li><p>For ... | {
"AcceptedAnswerId": "28308",
"CommentCount": "4",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-09T19:55:06.117",
"Id": "28306",
"Score": "3",
"Tags": [
"c++",
"object-oriented",
"classes",
"beginner",
"primes"
],
"Title": "Object-oriented design of list of primes"
} | 28306 |
<p>Here's a simple algorithm that walks the DOM given a node:</p>
<pre><code>function walkDOM(n) {
do {
console.log(n);
if (n.hasChildNodes()) {
walkDOM(n.firstChild)
}
} while (n = n.nextSibling)
}
</code></pre>
<p>I wanted to implement it iteratively as an exercise, and c... | [] | [
{
"body": "<p>I know you are doing this as an exercise, and personally I like the recursive function. But just as an alternative, there is also the much forgotten <kbd><a href=\"https://developer.mozilla.org/en-US/docs/Web/API/treeWalker?redirectlocale=en-US&redirectslug=DOM/treeWalker\" rel=\"noreferrer\">... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-09T20:38:31.160",
"Id": "28307",
"Score": "12",
"Tags": [
"javascript",
"recursion",
"dom",
"iteration"
],
"Title": "Implementing an algorithm that walks the DOM without recursio... | 28307 |
<p>I started learning JavaScript a week ago, and I made a sorting function on my own. It does work, but I need reviews and how to write a better one.</p>
<pre><code><body>
<ul id="list">
<li>Art</li>
<li>Mobile</li>
<li>Education</li>
<li>Games</... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-10T01:52:28.010",
"Id": "44335",
"Score": "0",
"body": "What is your purpose for this? What would constitute a \"better\" sorting function? As I see it, it seems to accomplish the purpose fairly nicely..."
},
{
"ContentLicense"... | [
{
"body": "<p>Some notes:</p>\n\n<ul>\n<li><code>var myList</code>: Don't use generic names like <code>myList</code>. My list of what? Variable names are very important, they should inform about the nature of their content, not their type (although it's indeed important to use singular variable names for single... | {
"AcceptedAnswerId": null,
"CommentCount": "3",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-09T21:21:03.977",
"Id": "28310",
"Score": "4",
"Tags": [
"javascript",
"sorting",
"dom"
],
"Title": "My first javascript sorting"
} | 28310 |
<pre><code>var timeToWait = TimeSpan.FromSeconds(20);
var interval = TimeSpan.FromMinutes(5);
var t = new Timer(s =>
{
tracker.ProcessAuditLogs();
}, null, timeToWait, interval);
</code></pre>
<p>I'm looking to run a timer or an indefinite period of time, as long as my program/service is running. Wi... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-10T08:25:52.570",
"Id": "44348",
"Score": "1",
"body": "Could you give us a bit more context? What are your concerns about this code?"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-16T23:51:36.957",
"I... | [
{
"body": "<p>I converted my timer loops to Reactive Extensions a while back to avoid the memory leaks created by event subscriptions and event handlers. A snippet doing the same thing as yours is...</p>\n\n<pre><code> var timer = Observable\n .Timer(TimeSpan.FromSeconds(3), TimeSpan.FromSeco... | {
"AcceptedAnswerId": "28620",
"CommentCount": "3",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-10T04:31:40.063",
"Id": "28314",
"Score": "1",
"Tags": [
"c#",
".net",
"timer"
],
"Title": "Rewriting timers in .NET with lambda"
} | 28314 |
<p>This function code works very slowly. How can I speed it up?</p>
<pre><code>CREATE OR REPLACE FUNCTION bill."ReportIngredients"(
_from date,
_to date,
_beginning_date date,
_has_inventory boolean,
_inventory_id uuid,
_restaurant_id uuid,
_stock_id uuid,
_ingredientIds uuid [],
_s... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-11-15T18:27:53.627",
"Id": "57437",
"Score": "0",
"body": "you have a lot of Nested Select Statements with multiple where statements, is there any way to reduce the Nested select statements? like Temp Tables, Table Variables, joins, etc?... | [
{
"body": "<p>I will give you kudos on being very explicit in your programming. Here are my thoughts:</p>\n\n<ol>\n<li><p>Not performance related but still a factor, I would suggest that you remain consistent with your casing of key words. Sure SQL is not case-sensitive, but it makes the code easier to read esp... | {
"AcceptedAnswerId": null,
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-10T04:44:25.747",
"Id": "28315",
"Score": "5",
"Tags": [
"optimization",
"sql",
"postgresql"
],
"Title": "Optimize postgres function"
} | 28315 |
<p>I am currently learning JavaScript and created this cross-browser event utility as a learning exercise. It was not meant to be used in production code, and I just tried to cover only the cases that I'm aware of, so there might be a lot of edge cases that are not covered. </p>
<p>I'd be happy if you could check it a... | [] | [
{
"body": "<p>Your code looks ok. Nothing really jumps out at me except these two things:</p>\n\n<ol>\n<li><p>Event handlers attached via <code>attachEvent</code> are executed in random order (Source: <a href=\"http://msdn.microsoft.com/en-us/library/ie/ms536343%28v=vs.85%29.aspx\" rel=\"nofollow\">http://msdn.... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-10T10:56:16.600",
"Id": "28320",
"Score": "1",
"Tags": [
"javascript",
"cross-browser"
],
"Title": "Implementing a cross-browser event utility as an exercise"
} | 28320 |
<p>In my ASP.NET MVC code, I like to use controller service classes for my controllers. These service classes contain methods for retrieving viewmodel objects.</p>
<p>Here is an example controller snippet:</p>
<pre><code>public SubscriptionsController(ISubscriptionsControllerService service)
{
_service = service;... | [] | [
{
"body": "<p>Why not name them both the same? The difference is the type of parameter you're passing. This leaves you free to do some method overloading:</p>\n\n<pre><code>public SubscriptionsViewModel GetSubscriptionsViewModel(Guid id)\n{\n //GET Logic here...\n}\n\npublic SubscriptionsViewModel GetSubscri... | {
"AcceptedAnswerId": "28331",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-10T12:38:03.420",
"Id": "28324",
"Score": "2",
"Tags": [
"c#"
],
"Title": "Returning a viewmodel"
} | 28324 |
<p>I am currently learning JavaScript and created this cross-browser AJAX utility as a learning exercise. It was not meant to be used in production code, and I just tried to cover only the cases that I'm aware of, so there might be a lot of edge cases that are not covered.</p>
<p>I'd be happy if you could check it and... | [] | [
{
"body": "<p>I can't specifically comment on your usage of the extra tries for < IE7, as I always make sure my audience is using something better. It seems like there should be a more elegant way to check each of those than a try with an empty catch, but without knowing how IE treats them, I can't say what... | {
"AcceptedAnswerId": "28376",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-10T12:41:53.373",
"Id": "28325",
"Score": "3",
"Tags": [
"javascript",
"ajax"
],
"Title": "Implementing a cross-browser AJAX utility as an exercise"
} | 28325 |
<p>This code fetches reviews for the top 100 products on NutraPlanet, finds the Bayesian estimate for each average review, then prints out all reviews sorted by that Bayesian estimate.</p>
<p>How can I improve this code (correctness, elegance, best practices, etc)?</p>
<pre><code><?php
// echo "Remove exit from s... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-10T16:02:30.840",
"Id": "44368",
"Score": "0",
"body": "What's up with all the `$`s?"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-10T16:27:01.093",
"Id": "44370",
"Score": "1",
"body": "Ummmm... | [
{
"body": "<p>I just put this together, so I haven't tested it or anything, there might be bugs and someone else PLEASE come along and improve upon it, but I think this might be a good starting point to answering this question:</p>\n\n<p>OOP version of OP's code:</p>\n\n<p>\n\n<pre><code>$ReviewPrinter = new Re... | {
"AcceptedAnswerId": null,
"CommentCount": "3",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-10T13:17:41.600",
"Id": "28326",
"Score": "2",
"Tags": [
"php"
],
"Title": "Finding the Bayesian estimates for the top 100 products on NutraPlanet"
} | 28326 |
<p>I have written the following Query to retrieve some object's ID. If there are objects in the list that have a larger <code>numbinbatch</code> than the <code>saveditems</code> number in the batch, then it should take that. If not, then it should take the first number from the lower number in batches.</p>
<p>I am won... | [] | [
{
"body": "<p>What you certainly can/should do is this:</p>\n\n<pre><code>var higherNumInBatch = unsavedItems.Where(i => i.NumInBatch > savedItem.NumInBatch).OrderBy(i=>i.NumInBatch);\n\nif (higherNumInBatch.Any())\n{\n NextItemID = (int)higherNumInBatch.Take(1).First().ID;\n}\nelse\n{\n var lowe... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-10T13:48:37.293",
"Id": "28328",
"Score": "0",
"Tags": [
"c#",
"linq"
],
"Title": "Retrieving an object's ID"
} | 28328 |
<p>I'm currently working on a logfile parser for a pretty old videogame called <em>Team Fortress Classic</em>, which can be compared to <em>Counter-Strike 1.6</em> and is available on Valve's Steam platform.</p>
<p>I started out trying to make a collection of nicknames for each player. Every player joining the server ... | [] | [
{
"body": "<p>Your 3rd question is off-topic for the site, but I do believe that you are using the one-to-many relationships correctly.</p>\n\n<p>When you are finding a player by <code>steam_id</code> you don't actually need to select the <code>steam_id</code> unless you are going to use it for something in an ... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-10T13:57:04.917",
"Id": "28329",
"Score": "7",
"Tags": [
"sql",
"mysql",
"parsing"
],
"Title": "Logfile parser for Team Fortress Classic video game"
} | 28329 |
<p>I'm a C++ beginner and have made a simple class. But I'm not sure if this is well-written. It's basically just a <code>Date</code> class.</p>
<pre><code>#include <iostream>
using namespace std;
class Date{
int d, m, y; //Day, Month, Year
public:
Date(int dd = 1, int mm = 1, int yy = 1);
int ... | [] | [
{
"body": "<p>For built-in types, there is no point in passing it as a <code>const&</code> - prefer a plain <code>int</code> (or a <code>const int</code>) instead.</p>\n\n<p>Other comments:</p>\n\n<ol>\n<li>Your <code>add-</code> functions are said to return <code>int</code> even though you don't have a <co... | {
"AcceptedAnswerId": "28340",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-10T17:47:12.667",
"Id": "28339",
"Score": "7",
"Tags": [
"c++",
"classes",
"beginner",
"datetime"
],
"Title": "Is this Date class well-written?"
} | 28339 |
<p>Suppose I've got a JavaScript function that I'm treating like a class - that is, I want to make many instances of it:</p>
<pre><code>function Blerg() {
this._a = 5;
}
Blerg.prototype.getA = function() {
return this._a;
}
Blerg.prototype.setA = function(val) {
this._a = val;
}
</code></pre>
<p>This class has ... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-10T20:20:29.513",
"Id": "44399",
"Score": "0",
"body": "For reference, here is a quick help from v8: http://pastebin.com/dTk0eegF (this is the ASM generated for this code: http://pastebin.com/F59x1U15)"
}
] | [
{
"body": "<p>related jsperf <a href=\"http://jsperf.com/12312412354\">http://jsperf.com/12312412354</a></p>\n\n<p>Well this is a really bad idea. Objects are always considered having different class if they don't have exactly the same set of properties in the same order. So\na function that accepts these objec... | {
"AcceptedAnswerId": "28360",
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-10T20:06:21.047",
"Id": "28344",
"Score": "17",
"Tags": [
"javascript",
"performance",
"classes",
"prototypal-class-design"
],
"Title": "Should I put default values of attribu... | 28344 |
<p>This was too slow for my computer:</p>
<pre><code>int f(unsigned int x)
{
if(x <= 1) return 1;
else return f(x-1)+f(x-2);
}
/* main */
int main()
{
for(int i = 0; i < 50; i++) cout << f(i) << endl;
return 0;
}
</code></pre>
<p>So I've made a faster implementation. It works, but... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-10T21:00:14.460",
"Id": "44405",
"Score": "0",
"body": "I just noticed that if I call this function again it will print numbers until the program crashes. Do I have to set the static variables back to zero?"
},
{
"ContentLicens... | [
{
"body": "<p><strong>First of all:</strong> If you want execution speed, an iterative approach to fibonacci is faster. That said, implementing a recursive solution is a nice exercise.</p>\n\n<p><strong>Some other remarks:</strong></p>\n\n<ol>\n<li>Code that <em>does</em> something should not perform IO. Separa... | {
"AcceptedAnswerId": "28375",
"CommentCount": "7",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-10T20:49:38.620",
"Id": "28346",
"Score": "2",
"Tags": [
"c++",
"recursion",
"complexity",
"fibonacci-sequence"
],
"Title": "Fibonacci sequence implementation"
} | 28346 |
<p>the purpose of this class file is to design a Saving account that stores a saving account's annual interest rate and balance. I have not wrote the test program yet, seeing that I believe I made a few mistakes with my class file. Also, at the end of classfile, I hid a method because I didn't understand how to word th... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-10T21:46:23.803",
"Id": "44412",
"Score": "0",
"body": "use [Java naming convention](http://java.about.com/od/javasyntax/a/nameconventions.htm) and post full working code for reviewing."
}
] | [
{
"body": "<ul>\n<li>First and most important remark : do not use <code>double</code> or <code>float</code> to represent money. Use <code>BigDecimal</code> instead. And make a <code>Money</code> class that combines the amount with the currency. <strong>edit</strong> : it's also possible to use long or int, if y... | {
"AcceptedAnswerId": "28361",
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-10T20:54:52.627",
"Id": "28347",
"Score": "2",
"Tags": [
"java"
],
"Title": "Java Is my class file methods well written?"
} | 28347 |
<p>I'm just wondering if this code is good against SQL injection attacks. I don't know how to check if is good. Also I would like to know if is good how I'm working or this is just bad practice?</p>
<pre><code><?php
if (isset($_POST['register'])) {
$username = $_POST["username"];
$email = $_POST["email"];
$passw... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-10T21:02:40.517",
"Id": "44406",
"Score": "1",
"body": "Looks good to me"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-10T21:05:05.573",
"Id": "44407",
"Score": "0",
"body": "Since you're usin... | [
{
"body": "<p>Looks O.K. to me as well, as far as the SQL injection goes.</p>\n\n<p>However, I'd suggest checking the return value of <code>$sth->execute();</code>, so you capture possible errors (i.e., DB being down).</p>\n\n<p>You might also consider doing a case insensitive search for the existing usernam... | {
"AcceptedAnswerId": "28369",
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-10T21:00:39.067",
"Id": "28348",
"Score": "4",
"Tags": [
"php",
"mysql",
"pdo"
],
"Title": "Is this code protected against SQL injection attacks?"
} | 28348 |
<p>Please take it easy on my as this is my first OOP web application I plan on converting it to some deeper degree but not sure yet.</p>
<p>The basic functionality is there I think, but I'm looking for some feedback on how I can improve this in terms of OOP and simplicity. Sorry for any simple mistakes only been doing... | [] | [
{
"body": "<p>The code below is good. I only changed the comments a bit to make them a bit clearer and more concise. However, \none glaring issue is that it seems you have your setters and getters mixed up. Note the <code>getimage_*()</code> \nand <code>setimage_*()</code> functions you have in your class. Usua... | {
"AcceptedAnswerId": "28377",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-10T21:17:25.577",
"Id": "28349",
"Score": "3",
"Tags": [
"php",
"object-oriented"
],
"Title": "Photogallery First project in OOP review"
} | 28349 |
<p>I've started reading <em>C++ Primer</em> a few weeks ago and there's an exercise that asks you to compare two arrays for equality. The code I made works, but is it good? What should I fix?</p>
<p>If the number of elements don't match, they're not equal. If the elements are different, they are not equal.</p>
<pre>... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-11T04:37:35.267",
"Id": "44433",
"Score": "1",
"body": "I'm assuming you're doing this for academic reasons, but just in case: there's no need for your loop. You can just do `std::equal(ar1, ar1 + 10, ar2 + ar2 + 19).`"
},
{
"C... | [
{
"body": "<ul>\n<li><p><a href=\"https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice\">Try not to use <code>using namespace std</code></a>.</p></li>\n<li><p>You don't need an explicit <code>return 0</code> at the end of <code>main()</code>. As reaching the end of <co... | {
"AcceptedAnswerId": "28355",
"CommentCount": "4",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-11T00:29:51.130",
"Id": "28354",
"Score": "6",
"Tags": [
"c++",
"array"
],
"Title": "Comparing two arrays"
} | 28354 |
<p>It is usually inefficient to find bugs in a longer code. Unfortunately, after spending too much time in debugging my code, I realize that a good programming habit is important. Please give me some advice about codestyle, design... anything important to write high-quality code.</p>
<pre><code>import numpy as np
from... | [] | [
{
"body": "<pre><code>import numpy as np\nfrom numpy.random import standard_normal, chisquare, multivariate_normal, dirichlet, multinomial\nfrom numpy.linalg import cholesky, inv\n</code></pre>\n\n<p>You are importing a lot of stuff here, and you aren't even using much of it. I guess not importing these names u... | {
"AcceptedAnswerId": "28358",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-11T02:56:03.157",
"Id": "28356",
"Score": "4",
"Tags": [
"python",
"optimization",
"numpy",
"statistics"
],
"Title": "Statistical samples and distributions"
} | 28356 |
<p>I was learning a bit about about observers. Ruby has an <code>Observable</code> module, but I decided to make my own with a few tweaks.</p>
<p>The first important feature is that this module calls one <strong>or more</strong> observer methods when the observed object's state is changed. When you add an observer, <s... | [] | [
{
"body": "<p>A few notes on making this look more like idiomatic Ruby:</p>\n\n<ol>\n<li><p>There's no need to use <code>return unless @observers</code> in <em>every method</em>. You initialize <code>@observers</code> to a known value, which will never be falsy; there is no harm at all in assuming it will still... | {
"AcceptedAnswerId": "28368",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-11T04:56:59.713",
"Id": "28357",
"Score": "0",
"Tags": [
"ruby",
"design-patterns",
"modules"
],
"Title": "A flexible Ruby Observable module"
} | 28357 |
<p>I've been thinking about making Javascript Prototypes that represent HTML elements.
For example a form prototype with build in ajax requests and form element prototypes. Or a list with list item prototypes. </p>
<p>I think that the biggest benefit of this approach is that it reduces repetitive code.</p>
<p>Here is... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-11T12:15:30.937",
"Id": "44465",
"Score": "0",
"body": "How does this \"reduce repetitive code to a minimum\" ?"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-11T12:29:54.163",
"Id": "44467",
"Scor... | [
{
"body": "<p>As dystroy mentioned, the setters and getters are really not JavaScript style, neither are the many <code>try/catch</code> statements in your functions.</p>\n\n<p>However, if you insist on having all these getters and setters, you should create a utility function that generates a getter/setter fo... | {
"AcceptedAnswerId": "48010",
"CommentCount": "9",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-11T12:02:50.780",
"Id": "28365",
"Score": "2",
"Tags": [
"javascript",
"jquery",
"html"
],
"Title": "Javascript Prototypes that represent HTML elements"
} | 28365 |
<p>I've got the following code to monitor the network usage on a network interface. I'm mainly looking for feedback on the design, but if you see any code improvements please let me know! This is also the first time I've actually added 'official' documentation so tell me if I'm forgetting something.
For those that feel... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-11T15:34:23.127",
"Id": "44479",
"Score": "0",
"body": "Is there a reason why everything is Static?"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-11T15:43:24.140",
"Id": "44480",
"Score": "0",
... | [
{
"body": "<p><strong>Style</strong></p>\n\n<ol>\n<li><code>IStatistics</code> is a bit of a generic name (although I guess if you put it in a sensible namespace it might be ok). I'd consider renaming it to something like <code>INetworkStatistics</code>.</li>\n<li>From looking at the code it is not obvious what... | {
"AcceptedAnswerId": "36005",
"CommentCount": "5",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-11T13:00:59.977",
"Id": "28367",
"Score": "6",
"Tags": [
"c#",
"networking"
],
"Title": "Basic network utilisation display"
} | 28367 |
<p>I've built a small library for comparing objects based on their members <a href="https://github.com/alabax/YetAnotherEqualityComparer" rel="nofollow noreferrer">here</a> (<a href="https://stackoverflow.com/questions/17558135/is-there-a-way-to-reduce-amount-of-boilerplate-code-in-equals-and-gethashcode">related SO qu... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-11T16:00:22.937",
"Id": "44484",
"Score": "0",
"body": "\"with multiplications and additions significant bits are shifted away and are lost.\" I'm not buying that argument. The very nature of hashing throws away information, but keeps ... | [
{
"body": "<p>If I'm not wrong, your hashcode starts using the most significant bit only when <code>memberHashCode << leftShift</code> is using it. This may very well never be the case, if memberSetups.Count is low, and the hashcode of the object you're using is not perfect. For example, ints or bool.</p>... | {
"AcceptedAnswerId": null,
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-11T15:15:27.283",
"Id": "28372",
"Score": "2",
"Tags": [
"c#"
],
"Title": "Composite GetHashCode function"
} | 28372 |
<p>I am learning about timing analysis in Python. I used <code>time</code> module before it was pointed out that <code>timeit</code> module would be better for timing analysis.</p>
<p>My main focus on timing analysis is for conducting many test cases on 2 variations of the same function. In this example code I used 2 ... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-17T11:47:45.120",
"Id": "44920",
"Score": "0",
"body": "\"My usage of timeit module gave variable results on every run.\" How many tests/how long runs were they? What was the relative difference?"
},
{
"ContentLicense": "CC BY-... | [
{
"body": "<p>Software timing benchmarks are notoriously noisy because your computer isn't a very good controlled experimental platform.</p>\n\n<ul>\n<li>Some algorithms are nondeterministic. For instance, some quicksort implementations choose a random pivot element.</li>\n<li>While you're running your benchmar... | {
"AcceptedAnswerId": "28629",
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-11T15:35:22.270",
"Id": "28373",
"Score": "8",
"Tags": [
"python",
"optimization",
"unit-testing",
"timer"
],
"Title": "Timing analysis of many test cases in Python"
} | 28373 |
<p>I'm an advanced C# programmer learning F#. As an exercise I'm porting a function that calculates the check digit of a US ABA (routing) number. Here are 2 C# implementations:</p>
<pre><code>int CalcCheckDigit(string rt)
{
var s = new[]{3,7,1,3,7,1,3,7}
.Zip(rt, (m,d) => m * int.Parse(d.ToString()))
... | [] | [
{
"body": "<p>This isn't really F# specific because you could do the same thing in your C# code, but converting each character to a string so you can parse it as an integer seems inefficient to me. I'd be inclined to use <a href=\"http://msdn.microsoft.com/en-us/library/e7k33ktz.aspx\" rel=\"noreferrer\">Char.G... | {
"AcceptedAnswerId": "28387",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-11T18:46:19.393",
"Id": "28378",
"Score": "6",
"Tags": [
"c#",
"f#"
],
"Title": "Learning F# - Porting C# Function to F#"
} | 28378 |
<p>Before I get started I have spent much time searching for the answer. It seems the most referenced way is here, <a href="http://www.developer.com/print.php/3794146" rel="nofollow">http://www.developer.com/print.php/3794146</a>. I am not quite understanding how to optimize. See below.</p>
<pre><code>public void Calc... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-11T19:20:54.810",
"Id": "44499",
"Score": "0",
"body": "This question shows a pretty simple way, provided you first create a IEnumerable<double> from your values. http://stackoverflow.com/a/3141731/736079"
},
{
"ContentLicense"... | [
{
"body": "<p>If the method calculate standard desviation, it should return the value, not assign a global variable or instance variable.</p>\n\n<p>Regards, </p>\n",
"comments": [],
"meta_data": {
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-12T06:31:... | {
"AcceptedAnswerId": null,
"CommentCount": "3",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-11T19:11:26.293",
"Id": "28379",
"Score": "1",
"Tags": [
"c#",
"optimization",
"algorithm"
],
"Title": "How can I simplify the following code that calculates standard deviation"
} | 28379 |
<p>I created a <code>Queue</code> object when <a href="https://stackoverflow.com/a/17528961/561731">answering</a> <a href="https://stackoverflow.com/q/17528749/561731">this question</a> and I was wondering if it could do with some improvement.</p>
<p>Here is the current code:</p>
<pre><code>var Queue = (function () {... | [] | [
{
"body": "<p>Overall, the code is quite simple. That's a good thing already.</p>\n\n<p>Some things can be improved though:</p>\n\n<ul>\n<li><p>Always create the <code>autorun</code> property. It will help your compiler. See <a href=\"https://codereview.stackexchange.com/questions/28344/should-i-put-default-val... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-11T19:58:05.297",
"Id": "28380",
"Score": "4",
"Tags": [
"javascript",
"queue"
],
"Title": "Javascript Queue Object"
} | 28380 |
<p>I wrote this class to ensure that anything done in any of my worker threads can be displayed on the GUI via events, but I ran into non-thread-safe problems.</p>
<p>This class should take an action or a function with one or no params, i.e. the event delegate.</p>
<p>Is this code actually thread-safe, and, how could... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-11T22:04:33.923",
"Id": "44519",
"Score": "7",
"body": "`catch (Exception)\n {\n\n }` is a bad, bad idea."
}
] | [
{
"body": "<p>Your specific usage seems to be thread-safe. But it's relying on implementation details (about lambdas), which you shouldn't do. It's also <strong>extremely</strong> easy to get it wrong.</p>\n\n<p>I believe the only way it will work is if you're closing over <code>this</code>, but not any other v... | {
"AcceptedAnswerId": null,
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-11T21:42:03.277",
"Id": "28384",
"Score": "4",
"Tags": [
"c#",
"multithreading",
"thread-safety"
],
"Title": "Invoking worker threads"
} | 28384 |
<p>I started learning my first functional programming language (Haskell) yesterday and I have been working on a lexer for an expression evaluator. The lexer is now basically feature complete, but I'm not sure what I can do to improve the code.</p>
<pre><code>import Data.Char
import Data.List
data TokenType = Identifi... | [] | [
{
"body": "<ul>\n<li><p>No need of <code>TokenType</code>: <code>data Token = Identifier String | RealNumberLiteral String | PlusSign ...</code></p></li>\n<li><p>No need of nested <code>let</code> statements, see <a href=\"http://learnyouahaskell.com/syntax-in-functions#let-it-be\" rel=\"nofollow\">http://learn... | {
"AcceptedAnswerId": "28538",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-11T22:06:35.757",
"Id": "28385",
"Score": "2",
"Tags": [
"parsing",
"haskell"
],
"Title": "Lexer for expression evaluator"
} | 28385 |
<p>This is my Fibonacci generator:</p>
<pre><code>package main
import "fmt"
func main() {
for i, j := 0, 1; j < 100; i, j = i+j,i {
fmt.Println(i)
}
}
</code></pre>
<p>It's working, but I don't know how I can improve it, so I'd like more expert approaches to solving it.</p>
| [] | [
{
"body": "<p>Your code is quite nice, and can't really be improved. However, let's put the “generator” back into the code.</p>\n\n<p>Go has <em>channels</em> which can be used to write elegant generators/iterators. We spawn of a goroutine that fills the channel with the fibonacci sequence. The main thread then... | {
"AcceptedAnswerId": "28445",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-11T22:16:32.657",
"Id": "28386",
"Score": "10",
"Tags": [
"mathematics",
"go",
"fibonacci-sequence"
],
"Title": "Fibonacci generator with Golang"
} | 28386 |
<p>I am planing to create a small game using C#, so I've decided to create a small logic for that game. I immediately opened Visual studio and started to create some classes to hold on the player's data, so I came up with something like this. However, I don't know whether or not there is better ways to achieve such th... | [] | [
{
"body": "<p>It's hard to review code like this, because it's very obvious it's not “real” code. But I'll try:</p>\n\n<ol>\n<li><p>All your “classes” are basically just C structs. You should probably add some constructors and make some of the setters <code>private</code>. You should also move some of the logic... | {
"AcceptedAnswerId": "28390",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-11T22:57:06.657",
"Id": "28388",
"Score": "1",
"Tags": [
"c#",
"game"
],
"Title": "Small building game logic"
} | 28388 |
<p>I've built a basic image slide show with jQuery, nothing particularly fancy, but there a few specific things I wanted it to do and I managed it - I just don't think I did it the best way possible. Hoping to get some pointers on where I can improve it.</p>
<p>So the basic visual structure is:</p>
<pre><code>_______... | [] | [
{
"body": "<p>You have to think more in terms of <strong>arrays</strong></p>\n\n<p>This:</p>\n\n<pre><code> if(compare === allSlides[0])\n {\n nextSlide = 1\n }\n else if(compare === allSlides[1])\n {\n nextSlide = 2\n }\n else if(compare === allSlides[2])\n {\n next... | {
"AcceptedAnswerId": "47169",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-12T00:33:34.767",
"Id": "28391",
"Score": "3",
"Tags": [
"javascript",
"jquery",
"optimization",
"html"
],
"Title": "Custom slide show script"
} | 28391 |
<p>I am supposed to be creating a doubly circular linked list, but I am now have problems creating the actual linked list. Is there a problem with the classes? Also, for the assignment, the list is suppose to be a template class, but now I just want to get the code to work. </p>
<pre><code>#include <iostream>
#i... | [] | [
{
"body": "<p>You have done the correct thing by putting a sentinel element into the list.<br>\nThis makes adding/removing and generally manipulating the list trivial because you don't need to handle NULL links in the list. </p>\n\n<p>But you have done it using a two phase create. </p>\n\n<ol>\n<li>You have to... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-12T01:06:38.053",
"Id": "28392",
"Score": "1",
"Tags": [
"c++",
"linked-list",
"circular-list"
],
"Title": "Doubly circular link list implementation"
} | 28392 |
<p>This is my interface, all of my transport concrete classes will be implementing this interface.</p>
<pre><code>interface ITransport
{
void Travel()
}
</code></pre>
<p>These are my existing implementation class:</p>
<pre><code>class Horse : ITransport
{
public Horse()
{
}
public void Travel()
... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-12T08:07:47.367",
"Id": "44543",
"Score": "2",
"body": "You should indent your code properly. That makes it more pleasant to read, and increases the chances of a good answer."
},
{
"ContentLicense": "CC BY-SA 3.0",
"Creatio... | [
{
"body": "<p>I assume that this is just an example and I don't need to tell you that you should use <code>enum</code>s instead of <code>string</code>s, remove <code>break</code>s after <code>return</code>s, indent your code, etc. :) So, to answer your question: I think you should refactor your <code>Human</cod... | {
"AcceptedAnswerId": "28405",
"CommentCount": "7",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-12T03:47:40.007",
"Id": "28396",
"Score": "0",
"Tags": [
"c#",
"object-oriented",
"dependency-injection"
],
"Title": "Constructor Injection, new dependency and its impact on code"... | 28396 |
<p>I'm trying to make a MultiClient Chat Application in which the chat is implemented in the client window. I've tried server and client code for the same. I've got two problems:
<strong><em>A.</em></strong> I believe the code should work but, server to client connections are just fine but information isn't transferred... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-12T10:32:59.640",
"Id": "44561",
"Score": "1",
"body": "First of all, try to follow the Java conventions: Class names must start with uppercase. Another problem I see your server is receiving connections but it's doing nothing with the... | [
{
"body": "<p>Currently your <code>innerChat</code> <code>Runnable</code> gets the <code>InputStream</code> and <code>OutputStream</code> but doesn't do anything with them.</p>\n\n<p>The client also simply writes <code>String</code>s to its <code>OutputStream</code>, however it would be more convenient to send ... | {
"AcceptedAnswerId": null,
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-12T04:20:27.760",
"Id": "28397",
"Score": "-1",
"Tags": [
"java",
"multithreading",
"swing",
"socket"
],
"Title": "Stuck with Multi-Client Chat Application"
} | 28397 |
<p>My aim is to sort a list of strings where words have to be sorted alphabetically. Words starting with <code>s</code> should be at the start of the list (they should be sorted as well), followed by the other words.</p>
<p>The below function does that for me.</p>
<pre><code>def mysort(words):
mylist1 = sorted([i... | [] | [
{
"body": "<p>For this snippet, I would have probably written the more compact:</p>\n\n<pre><code>def mysort(words):\n return (sorted([i for i in words if i[0] == \"s\"]) +\n sorted([i for i in words if i[0] != \"s\"]))\n</code></pre>\n\n<p>(You're not supposed to align code like this in Python, b... | {
"AcceptedAnswerId": "28418",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-12T05:46:42.197",
"Id": "28398",
"Score": "2",
"Tags": [
"python",
"sorting"
],
"Title": "Sort a list in Python"
} | 28398 |
<p>The following are an example of methods supported by a proprietary device:</p>
<pre><code>Monitor
ControlTemp
PutPeakInfo
GetPeakInfo
</code></pre>
<p>I have a class that builds the packets for the above corresponding methods:</p>
<pre><code>GetMonitorPacket
GetControlTempPacket
GetPutPeakInfo
</code></pre>
<p>E... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-12T10:30:12.607",
"Id": "44560",
"Score": "0",
"body": "For example, Microsoft doesn't seem to care. \"How do we call method, which returns `MethodInfo` for property getter? `PropertyInfo.GetGetMethod()`, no problem.\" In your case, i ... | [
{
"body": "<p>How about using <code>Generate</code> as prefix?</p>\n\n<pre><code>GenerateMonitorPacket\nGenerateControlTempPacket\nGeneratePutPeakInfoPacket\nGenerateGetPeakInfoPacket\n</code></pre>\n\n<p>Alternatively, you can also create a separate class for each type of packet generator. You've only shown th... | {
"AcceptedAnswerId": "28401",
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-12T07:00:18.960",
"Id": "28400",
"Score": "1",
"Tags": [
"c#",
"tcp"
],
"Title": "Getting a monitor packet"
} | 28400 |
<p>I'm only two weeks into learning Java and just finished creating my first program. I have a feeling I've added unnecessary things, but not sure. I'd like someone to take a look at it and let me know if there are ways to simplify or approach the code. Basically, I'm just trying to create a simple GPA calculator.</p... | [] | [
{
"body": "<p>I would improve your code as following :</p>\n\n<ul>\n<li>Look at your code, you'll notice the repetition of the same piece of code (print a question => scan for input => analyze the input), it means you could write a method for that.</li>\n<li>If you want to ask for one more grade, you'll have to... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-12T08:45:21.010",
"Id": "28406",
"Score": "6",
"Tags": [
"java",
"beginner"
],
"Title": "Simplifying GPA calculator"
} | 28406 |
<p>I have a User class that will throw an exception if the userid provided in the parameter does not correspond with a user.</p>
<p>I've written a helper function that will return true or false depending on whether a userid belongs to a user or not, but was wondering if my use of a try {} catch {} to do so is correct:... | [] | [
{
"body": "<p>I would suggest working the other way around. Expect that a user does not exist and only return true if a user is found.</p>\n\n<pre><code>private function UserExists($userId)\n{\n $userExists = false;\n\n try\n {\n $user = new User($userId);\n if($user !== null)\n ... | {
"AcceptedAnswerId": "28414",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-12T12:52:23.517",
"Id": "28413",
"Score": "1",
"Tags": [
"php",
"exception"
],
"Title": "Function that uses an exception to check if user exists"
} | 28413 |
<p>I'm not sure that I fully understand this pattern. I've written a simple application that uses MVC. Please criticize my approach to the use of this pattern. For convenience, I'll post the source code of the main classes. The source code of the project can be found here: <a href="https://github.com/Leonideveloper/Sou... | [] | [
{
"body": "<p>I've only skimmed through, and all I could find is a <code>bool stopped</code> which I would have called <code>bool isStopped</code>, although <code>stopped</code> is a fairly unambiguous, clear name - as is the rest of this code.</p>\n\n<p>Oh, and maybe the <code>256</code> hard-coded into your <... | {
"AcceptedAnswerId": "35740",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-12T14:43:41.410",
"Id": "28419",
"Score": "5",
"Tags": [
"java",
"design-patterns",
"mvc",
"android"
],
"Title": "First introduction to MVC"
} | 28419 |
<p>I'm getting data to fill out an e-mail template, and I'm wondering if this method is too long. How could I refactor it?</p>
<pre><code>sub getEmailData
{
my $self = shift;
my $defs = QuickContentFunction::Simple::QuerySearchFieldDefinitions->new();
my $phParams = decode_json($self->parameters);
... | [] | [
{
"body": "<p>First off, stuff like <code>my @aFields = split(/~/, $phParams->{sFields});</code> is a sign of something gone awry. You've just deserialized <code>$phParams</code> from a JSON object, so why isn't this just stored in a normal array?</p>\n\n<p>There is no need to write <code>$i < scalar(@aFi... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-12T18:57:39.863",
"Id": "28425",
"Score": "0",
"Tags": [
"perl"
],
"Title": "Preparing search criteria and results to be e-mailed"
} | 28425 |
<p>While working with someone else's code I've isolated a time-killer in the (very large) script. Basically this program runs through a list of thousands and thousands of objects to find matches to an input (or many inputs) object. It works well when the number is below 50 but any input higher than that and it begins t... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-12T21:34:43.993",
"Id": "44608",
"Score": "0",
"body": "Can you clarify a bit further what this is supposed to do and what 'number' you are referring to exactly? The number of input objects? Are the stars in BestX the input objects? Al... | [
{
"body": "<p>Some guesswork here but I think what you want is actually something like this:</p>\n\n<pre><code>def match_something(file, match):\n \"\"\" Finds a line in file starting with 'RESULTS:' then checks the following 50\n lines for match; if it fails returns None, otherwise it returns\n how ma... | {
"AcceptedAnswerId": null,
"CommentCount": "7",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-12T21:21:04.760",
"Id": "28428",
"Score": "0",
"Tags": [
"python"
],
"Title": "Is there a better, more efficient way to write this loop?"
} | 28428 |
<p>I want to verify that I'm correctly handling risk of "Overflowing fixed-length string buffers." I'm also aware that I'm using C-style strings, which falls short of full C++ code.</p>
<p><strong>Main</strong></p>
<pre><code>/*
PURPOSE:
attempt to implement, without using given/known-good code, various concepts
... | [] | [
{
"body": "<p>It is very apparent that you are new. That is fine; we were all new once. I'll try to adjust my feedback accordingly. Let me know if you are not familiar with some of the terminology, and I'll provide an explanation or definition.</p>\n\n<hr>\n\n<h3>Overflow safeguards</h3>\n\n<p>First of all, I'l... | {
"AcceptedAnswerId": "28432",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-12T21:54:06.203",
"Id": "28429",
"Score": "4",
"Tags": [
"c++",
"c",
"strings"
],
"Title": "Manage risk of \"Overflowing fixed-length string buffers\""
} | 28429 |
<p>I'm working on a TCP Server using .NET and the following code is my first attempt to process the received data (from the socket) to create a packet to deliver. Basically, the packet has a header (7 bytes) where the first 3 bytes are always <code>0x12 0x34 0x89</code> and the next 7 bytes are the message length. For ... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-13T03:16:57.220",
"Id": "44634",
"Score": "1",
"body": "TCP servers already exist, ones written in C are faster. If you want uber speed, the code will have to be messy. If you want clarity, then I would separate the process of scanning... | [
{
"body": "<p>There is a bug in that you are not resetting the state when you read an unexpected byte. For example, this byte sequence would be accepted as a valid header by your code:</p>\n\n<p>0x12 0xff 0x34 0xff 0x89</p>\n\n<p>I wonder whether you would get a neater solution if you used a stream:</p>\n\n<pre... | {
"AcceptedAnswerId": "28441",
"CommentCount": "8",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-13T02:42:00.013",
"Id": "28431",
"Score": "2",
"Tags": [
"c#",
"design-patterns",
"socket"
],
"Title": "if-elseif chain. Is there a better pattern?"
} | 28431 |
<p><strong>users_controller.rb:</strong></p>
<pre><code>class UsersController < ApplicationController
def new
end
def create
user = User.create(params[:user])
if user.id
session[:user_id] = user.id
redirect_to '/city/map'
else
redirect_to new_session_path, flash: { error: 'This ... | [] | [
{
"body": "<p>Before drying, make sure your code working.</p>\n\n<p>In the first block, your approach is wrong</p>\n\n<pre><code>user = User.create(params[:user])\n</code></pre>\n\n<p>But the user object is not persisted and no id will be assigned. (only <code>create!</code> will try to save to db and will thro... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-13T09:28:26.173",
"Id": "28435",
"Score": "-3",
"Tags": [
"ruby",
"ruby-on-rails"
],
"Title": "How can I DRY this Rails code?"
} | 28435 |
<p>This is (supposedly) a multi-threaded scheduler for one-time and/or repeating tasks. The tasks are simple <code>std::function<void()></code> objects. I built it to be a crucial part of a larger project I'm working on, but I developed it stand-alone, so no context is missing for a review.</p>
<p>I'm making hea... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-13T18:43:59.410",
"Id": "44655",
"Score": "0",
"body": "Just a comment for now -- note that `std::function` is not free and its type erasure mechanism comes at a run-time cost. Perhaps it's worth considering using templates with perfec... | [
{
"body": "<p>It looks/sounds like you're trying to build a sort of <a href=\"https://en.wikipedia.org/wiki/Thread_pool_pattern\" rel=\"nofollow noreferrer\">thread pool</a>. If that's the case, take a look at this <a href=\"https://softwareengineering.stackexchange.com/questions/173575/what-is-a-thread-pool\">... | {
"AcceptedAnswerId": null,
"CommentCount": "4",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-13T14:34:41.383",
"Id": "28437",
"Score": "7",
"Tags": [
"c++",
"c++11",
"multithreading",
"scheduled-tasks"
],
"Title": "Multithreaded task-scheduler"
} | 28437 |
<p>For my current project, I have elected to represent database rows as objects.</p>
<p>So an individual comment can be retrieved as a class like so:</p>
<pre><code>$comment = new Comment(1);
</code></pre>
<p>Here is the Comment class code:</p>
<pre><code><?php
class Comment
{
private $userid, $postid, $con... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-14T19:41:26.123",
"Id": "44705",
"Score": "0",
"body": "is the code complete? There aren't any defined properties."
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-15T17:47:19.310",
"Id": "44770",
"S... | [
{
"body": "<p>This is not OOP, your comment is hard coupled with mysqli. What if you would write the exact same program but now with PDO? you will have to change the Comment class.</p>\n<p>Then performance wise: you are now opening a mysqli connection for every comment.</p>\n<p>You seem to think that using the ... | {
"AcceptedAnswerId": "28513",
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-13T18:33:02.313",
"Id": "28440",
"Score": "2",
"Tags": [
"php",
"classes"
],
"Title": "Representing database row as a class"
} | 28440 |
<p>I'm getting into jQuery and I figured I'd start with log in / sign up form. (As an aside, I'm fully aware log in / sign up forms exist ad infinitum; this is more as a learning exercise than anything) </p>
<p>The idea is that the form fields will fade in depending on whether the user wants to log in or sign up to th... | [] | [
{
"body": "<p>If your looking to find performance improvements i've always know this to be faster</p>\n\n<pre><code>$('<input>', {\n type: \"button\", \n value: \"\", \n 'class': \"btn btn-primary emptyLabel\"\n});\n</code></pre>\n\n<p>Than this </p>\n\n<pre><code>$('<input type=\"button\" value=... | {
"AcceptedAnswerId": "28451",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-14T04:10:09.230",
"Id": "28450",
"Score": "2",
"Tags": [
"javascript",
"jquery",
"form",
"twitter-bootstrap"
],
"Title": "Log in / sign up form"
} | 28450 |
<p>Each data structure has its own time complexities. The biggest one that jumps out at you is the hash table. The average times for Insertion, Deletion and Searching are all O(1). But it's really just constant time, since there can be multiple reps through each of these to find the right spot. The real question is... | [] | [
{
"body": "<p>First there is no C++ here.<br>\nYou are simply doing C (use class and member functions that way you will not be passing pointers around).</p>\n\n<p>So what you have implemented is a chunked set.\nEach chunk <code>trie</code> uses 2048 integers to hold existence information and somewhere outside t... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 4.0",
"CreationDate": "2013-07-14T05:37:28.720",
"Id": "28453",
"Score": "2",
"Tags": [
"c++",
"algorithm",
"performance",
"binary-search",
"hash-map"
],
"Title": "Fast integer handling"
} | 28453 |
<p>I have a C++ class which acts as an abstraction for an IO device which is controlled through file descriptors. As I am very new to C++, I would be glad if you can give me devastating feedback on things I've done wrong.</p>
<p>This especially covers:</p>
<ul>
<li>Code style</li>
<li>Design patterns</li>
<li>Error h... | [] | [
{
"body": "<h2>Prefer the C++ way</h2>\n\n<p>As you seem to expect, one of the key issues with your code is that it's not very C++y.</p>\n\n<h3>1. Prefer <code>std::string</code> over C-style strings.</h3>\n\n<p><a href=\"http://en.cppreference.com/w/cpp/string/basic_string\"><code>std::string</code></a> is be... | {
"AcceptedAnswerId": "28462",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-14T09:02:51.013",
"Id": "28455",
"Score": "5",
"Tags": [
"c++",
"linux",
"io"
],
"Title": "Abstraction for an IO device"
} | 28455 |
<p>I want to remove not only spaces, but certain characters, as well from the beginning or end of a JavaScript string. </p>
<pre><code>function trim(str, characters) {
var c_array = characters.split('');
var result = '';
for (var i=0; i < characters.length; i++)
result += '\\' + c_array[i];
retur... | [] | [
{
"body": "<p>There are many different ways to tackle trimming <a href=\"/questions/tagged/strings\" class=\"post-tag\" title=\"show questions tagged 'strings'\" rel=\"tag\">strings</a>, a <a href=\"/questions/tagged/regex\" class=\"post-tag\" title=\"show questions tagged 'regex'\" rel=\"tag\">regex</a> is pro... | {
"AcceptedAnswerId": "28465",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-14T14:24:30.347",
"Id": "28464",
"Score": "15",
"Tags": [
"javascript",
"strings",
"regex"
],
"Title": "Trim certain characters from a string in JavaScript"
} | 28464 |
<p>I have multiple different implementations of an object, which implement this custom <code>interface</code> I made called <code>Board</code>.</p>
<p><code>Board</code> contains a method that looks like the following</p>
<pre><code>public void ConvertFromString(String formattedString);
</code></pre>
<p>Each object ... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-14T16:36:22.667",
"Id": "44695",
"Score": "4",
"body": "Voting to close, as I think this question is better suited on StackOverflow. While your code is technically working, in practice it is not. You are not really asking for a code re... | [
{
"body": "<p>The warning you get, is because you call an overridable method from your constructor. This is bad because subclasses may change the implementation of the method to access other parts of the object, while it is not fully initialized yet.</p>\n\n<p>So to get rid of the warning : don't call overridab... | {
"AcceptedAnswerId": "28469",
"CommentCount": "2",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-14T15:33:02.973",
"Id": "28466",
"Score": "1",
"Tags": [
"java",
"interface"
],
"Title": "Defining a Constructor In an Interface Java?"
} | 28466 |
<p>I've received reviews on the last version <a href="https://codereview.stackexchange.com/questions/28455/c-class-needs-a-very-critical-review">here</a>, but as an update would make the answers not fitting anymore, I think it is wise to open a new post.</p>
<p>I followed most of the hints of the last review and now w... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-14T18:48:16.130",
"Id": "44703",
"Score": "0",
"body": "Check this out for argument about explicitly calling close: http://codereview.stackexchange.com/q/540/507"
}
] | [
{
"body": "<p>Some quick remarks:</p>\n\n<ul>\n<li><p>To me, <code>value</code> and <code>direction</code> don't sound like stream names. Maybe they should be called something else? It depends on the domain terminology (which I'm not familiar with).</p></li>\n<li><p>Don't ever pollute the global namespace by us... | {
"AcceptedAnswerId": "28468",
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-14T15:46:28.603",
"Id": "28467",
"Score": "3",
"Tags": [
"c++"
],
"Title": "GPIO wrapper class"
} | 28467 |
<p>I am creating an application just for practicing c# as well as new programming ways. So i came across the idea that i create an application that stores information about "soldiers" ex. Names, ranks, titles, etc... but the trick here is that i want the soldier title to be automatically upgraded when it's rank increas... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-15T17:46:04.213",
"Id": "44769",
"Score": "0",
"body": "Based upon the code provided, you are avoiding the benefits of polymorphism by using enums and switches and string literals. According to Fowler (and his disciples) these can be ... | [
{
"body": "<p>You can change <code>Rank</code> type to some <code>enum</code>.</p>\n\n<pre><code>public enum Rank\n{\n Unknown = -1,\n Novice = 0,\n SecondLieutenant = 1,\n ....\n}\n\npublic static class SoldierRankLibrary\n{\n private static readonly Dictionary<int, string> RankDictionary ... | {
"AcceptedAnswerId": "28518",
"CommentCount": "3",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-14T18:44:20.110",
"Id": "28471",
"Score": "0",
"Tags": [
"c#"
],
"Title": "Automatic Property Upgrading"
} | 28471 |
<p>I'm working on a simple blogsystem (for learning purposes, not to reinvent the wheel). In this system the user can add existing or new tags to a blogpost. I wrote a method to achieve this, but it's ugly, inconvenient and probably of bad performance.</p>
<p>Some facts:</p>
<ul>
<li>I make use of Propel as mapper to... | [] | [
{
"body": "<p>Take a step back and look at your code. Do the different classes/objects ha ve Single responsibility? Or is the same class responsible for adding posts, managing tags and coupling a tag to a post?</p>\n\n<p>I think it is time to go back to the drawing board and see what you actually want/need.</p>... | {
"AcceptedAnswerId": "28543",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-14T19:32:28.973",
"Id": "28473",
"Score": "1",
"Tags": [
"php",
"object-oriented",
"postgresql",
"propel"
],
"Title": "Adding new or existing tags to a blog post"
} | 28473 |
<p><a href="http://jsfiddle.net/Cone/v5jaz/2/" rel="nofollow">Here is my script</a> of a simple jQuery-powered galler I've decided to create while studying JavaScript. Please point out my mistakes and let me know how the script can be optimized!</p>
<p><strong>JavaScript</strong></p>
<pre class="lang-js prettyprin... | [] | [
{
"body": "\n\n<pre class=\"lang-js prettyprint-override\"><code>$(this).hasClass('next_div')\n</code></pre>\n\n<p>is usually preferred over</p>\n\n<pre class=\"lang-js prettyprint-override\"><code>$(this).attr('class')=='next_div'\n</code></pre>\n\n<p>jQuery object variables are usually prefixed with a dollar ... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-14T19:43:49.427",
"Id": "28474",
"Score": "1",
"Tags": [
"javascript",
"optimization",
"jquery",
"html",
"css"
],
"Title": "Optimization of a simple jQuery-powered image gall... | 28474 |
<p>It works fine, but it is slow. </p>
<p>Could anybody help make this faster?</p>
<pre><code>import itertools
from decorators import timer
from settings import MENU, CASH
class Cocktail(object):
def __init__(self, group, name, sell, count=0):
self.group = group
self.name = name
self.se... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-14T20:59:32.400",
"Id": "44709",
"Score": "1",
"body": "Hello, could you please add some more information about what you are trying to achieve here? Thanks"
},
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-14... | [
{
"body": "<p>Take a look at <a href=\"http://wiki.python.org/moin/PythonSpeed/PerformanceTips\" rel=\"nofollow\">Python Performace Tips</a> and search for list comprehension. As far as I can understand your code you can use it.</p>\n\n<p>An example would be this function.</p>\n\n<pre><code>def __create_matrix(... | {
"AcceptedAnswerId": null,
"CommentCount": "5",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-14T20:53:31.733",
"Id": "28477",
"Score": "2",
"Tags": [
"python",
"algorithm",
"combinatorics"
],
"Title": "Find all row combinations which sum elements equal to any given number"
} | 28477 |
<p>I have a query which selects a show from <code>anime_series</code>, then selects corresponding information from other tables (such as studio, which is stored as a SMALLINT reference to another table). The below query works as intended, but I really don't believe my code is as efficient as it can or should be.</p>
<... | [] | [
{
"body": "<p>I would say the <code>DISTINCT</code> keywords in the following quote are superfluous. I would recommend to explicitely use <code>INNER JOIN</code> but even as it is, your <code>JOIN</code> clauses should implicitly compute that already. The only time I would use <code>DISTINCT</code> would be if ... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-14T21:14:01.050",
"Id": "28478",
"Score": "6",
"Tags": [
"mysql",
"sql"
],
"Title": "Optimizing a query with many joins"
} | 28478 |
<p>I am starting out with Java programming and get limited feedback from my professor. Following is an assignment that I handed in, and just re-factored. I wanted to see if people with Java experience could give some pointers on improving how it looks. Last semester I took a C++ course so splitting sections up into pub... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-15T06:16:04.140",
"Id": "44724",
"Score": "0",
"body": "You can read [this](http://programmers.stackexchange.com/questions/119600/beginners-guide-to-writing-comments) and [this](http://programmers.stackexchange.com/questions/173118/sho... | [
{
"body": "<p>Just a few comments:</p>\n\n<ul>\n<li>in Java, comments describing methods are ususally placed right before the method declaration, not after it. Also, use <code>/** ... */</code>, so they are turned into <a href=\"http://en.wikipedia.org/wiki/Javadoc\" rel=\"nofollow\">Javadoc</a> comments.</li>\... | {
"AcceptedAnswerId": "28503",
"CommentCount": "1",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-14T22:03:32.537",
"Id": "28479",
"Score": "4",
"Tags": [
"java",
"homework"
],
"Title": "Grade report builder"
} | 28479 |
<p><strong>Iterative</strong> functions – loop-based imperative repetitions of a process.</p>
<p><strong>Example</strong></p>
<pre><code>//iterative function calculates n!
function factorialIterative(n) {
var sum = 1;
if (n <= 1) {
return sum;
}
while (n > 1) {
sum *= n;
... | [] | [] | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-14T22:50:54.187",
"Id": "28480",
"Score": "0",
"Tags": null,
"Title": null
} | 28480 |
Iteration is the repetition of a block of statements within a computer program, usually with mutable state. | [] | [] | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-14T22:50:54.187",
"Id": "28481",
"Score": "0",
"Tags": null,
"Title": null
} | 28481 |
<p>So I came across an interesting problem awhile back, and I finally got around to solving it. Basically, it needs to allow the user to provide two words, and have them progressively splice together, like so:</p>
<pre><code>Word 1: 123
Word 2: abc
Result: 123abc
12a3bc
1a2b3c
a1b2c3
a... | [] | [
{
"body": "<p>Some notes:</p>\n\n<ul>\n<li><p><code>raise unless word1.is_a?(String) && word2.is_a?(String)</code>: Don't lose a second testing types of arguments, it's the caller's responsability to get it right.</p></li>\n<li><p>Blank lines: You should be more careful with blank lines, used without co... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-15T03:45:14.133",
"Id": "28484",
"Score": "1",
"Tags": [
"ruby",
"strings",
"recursion"
],
"Title": "Ruby string splicer"
} | 28484 |
<p>I've written a program:</p>
<pre><code>import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
import org.apache.commons.io.FileUtils;
/**
*
* @author Mohammad Faisal
*/
public class FileContentMatcher {
public static void main(String[] args) throws IOExcepti... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-15T06:45:31.717",
"Id": "44732",
"Score": "2",
"body": "well, the number of files doesn't tell how many GB/TB of data you need to go through; and how many seconds does you program currently take?"
},
{
"ContentLicense": "CC BY-... | [
{
"body": "<p>A few tips :</p>\n\n<ul>\n<li>Instead of reading the entire file into memory, use a <code>Reader</code>, reading only a small buffer at a time, to check for possible matches. This will improve memory usage, and avoid reading the entire file if the <code>textToMatch</code> is found.</li>\n<li>Separ... | {
"AcceptedAnswerId": "28499",
"CommentCount": "6",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-15T06:31:52.753",
"Id": "28490",
"Score": "1",
"Tags": [
"java",
"optimization",
"search"
],
"Title": "searching word or phrase among files"
} | 28490 |
<p>I am was writing a practice program in javascript and it turned out to work quite alright, however I am not entirely happy with the code. I can't shake the feeling that there is a lot of code which doesn't have to be there, I feel that it can be improved.</p>
<p>The idea of the program is basically, you have a sele... | [] | [
{
"body": "<p>Applying the good ol' <strong>DRY</strong> principle and by adding <code>coef</code> as a parameter, we'd get something like :</p>\n\n<pre><code>function setChoices(coef) {\n var relevantArray = (coef === 0) ? time :\n (coef === 1) ? category :\n ... | {
"AcceptedAnswerId": "28495",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-15T06:59:42.107",
"Id": "28492",
"Score": "2",
"Tags": [
"javascript",
"html5"
],
"Title": "Having less code for the current program"
} | 28492 |
<p>I (ruby beginner) have a performance issue with the following ruby code. The idea is to aggregate a CSV into a smaller, better-to-handle file and calculating the average of some values. Basically the same code in groovy runs about 30 times faster. And I can't believe that Ruby is that slow! ;-)</p>
<p>Some backgrou... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-15T20:11:01.343",
"Id": "44784",
"Score": "0",
"body": "You don't post the groovy code nor the input file (or did I miss the links?) so we cannot compare. Anyway, I don't see anything out of the usual in your code (regarding efficiency... | [
{
"body": "<pre><code>if time_slices[current_time_slice] == nil\n time_slices[current_time_slice] = Hash.new\nend\n</code></pre>\n\n<p>It's more idomatic to write</p>\n\n<pre><code>time_slices[current_time_slice] ||= {}\n</code></pre>\n\n<p>In general don't use <code>Hash.new</code> if you can just write <code... | {
"AcceptedAnswerId": null,
"CommentCount": "4",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-15T09:13:03.397",
"Id": "28501",
"Score": "1",
"Tags": [
"ruby",
"performance",
"csv"
],
"Title": "Ruby CSV reading performance problems"
} | 28501 |
<p>I have a <a href="https://github.com/regebro/svg.path">module in Python for dealing with SVG paths</a>. One of the problems with this is that the <a href="http://www.w3.org/TR/SVG/">SVG spec</a> is obsessed with saving characters to a pointless extent. As such, this path is valid:</p>
<pre><code>"M3.4E-34-2.2e56L23... | [
{
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-15T10:26:14.583",
"Id": "44746",
"Score": "1",
"body": "It's not just you having trouble with slow parsing of paths: [`svgwrite` has the same problem](https://bitbucket.org/mozman/svgwrite/issue/7/parsing-of-paths-is-very-time-consumin... | [
{
"body": "<p>With cred to @VedranŠego for kicking me in the right direction, my current solution is simply to split the two regex-parts into separate parsings, the first one a split (instead of a match) and the second one a findall on the floats (because doing a split is near impossible, as - both should and s... | {
"AcceptedAnswerId": "28565",
"CommentCount": "5",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-15T09:34:16.747",
"Id": "28502",
"Score": "6",
"Tags": [
"python",
"performance",
"parsing",
"regex",
"svg"
],
"Title": "SVG path parsing"
} | 28502 |
<p>I need to improve this by using <code>switch</code> statement in which I want to set the text and <code>setFullScreen</code>. Is there any more elegant way to implement this?</p>
<pre><code>final String change[] =
{
"Full Screen", "Exit Full Screen"
};
final MenuItem fullScreen = MenuItemBuilder... | [] | [
{
"body": "<p>This can be made a lot more elegant with the State Pattern.</p>\n\n<p>We'll introduce an enum for the two states :</p>\n\n<pre><code>private static enum Mode {\n FULLSCREEN(\"Exit Full Screen\"), NORMAL(\"Full Screen\");\n\n private final String toggleActionText;\n\n private Mode(String t... | {
"AcceptedAnswerId": null,
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-15T11:50:30.200",
"Id": "28505",
"Score": "2",
"Tags": [
"java",
"javafx"
],
"Title": "Menu items to enter and exit full-screen mode in JavaFX"
} | 28505 |
<p>I'm new to Perl, learning on my own. I've wrote many scripts (utilities) for myself but never posted them online. I would really appreciate any feedback on this script before I post it on a <a href="https://bbs.archlinux.org/viewtopic.php?id=56646" rel="nofollow">forum</a>.</p>
<p>The script takes list of files nam... | [] | [
{
"body": "<p>That is a nice and useful script, although a few things could be improved.</p>\n\n<ul>\n<li><p>You specified the <code>-w</code> command line option. You should instead <code>use warnings</code>. While the result is identical for such a small script, the <code>warnings</code> pragma allows for fin... | {
"AcceptedAnswerId": "28578",
"CommentCount": "0",
"ContentLicense": "CC BY-SA 3.0",
"CreationDate": "2013-07-15T13:17:11.660",
"Id": "28506",
"Score": "3",
"Tags": [
"perl"
],
"Title": "Perl script to rename multiple files with vi(m)"
} | 28506 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.