A function (also called a procedure, method, subroutine, or routine) is a portion of code intended to carry out a single, specific task.
0
votes
1answer
32 views
Parsing function review. How to improve it?
I've written a function to parse configuration files, it doesn't handle file opening, instead it takes an array with the file contents, tries to find fields based on an array containing the fields' ...
0
votes
0answers
4 views
AppEngine get/post on webapp2.RequestHandler
I was receiving 405 method not supported errors on get requests to my save controller, so I created a get method which just calls the existing post one as follows:
class Save(webapp2.RequestHandler):
...
2
votes
1answer
37 views
Matrix Transpose Function in C - Improve Performance
I have a matrix transpose function that takes arrays with a possibly different number of rows and columns.
How to improve performance and code quality?
void matrix_transpose(float *matrix, int rows, ...
0
votes
0answers
27 views
Error with variables in different functions and scripts [closed]
My friend and I are making a game in Pygame for fun. I was trying to clean up the code by putting more things into separate scripts as functions. This is the main script, and the only function is ...
0
votes
0answers
25 views
How can I implement a check for anagrams and palindromes on an array? [closed]
This might sound easy at first, or maybe it will sound easy even when you start thinking about how it should be done, but I haven't been able to figure this out yet... I'm not a seasoned programmer, ...
3
votes
1answer
80 views
Python Code Cleanup/Optimization
I am trying to clean up and make the following code more efficient -
https://gist.github.com/eWizardII/6757364
from PIL import Image
import numpy as np
from PIL import ImageChops
import subprocess
...
1
vote
2answers
41 views
What is a better way to organize this Python code in a class?
I wrote a script that will eventually go onto a Raspberry Pi. It takes a picture then uploads it to Twitter every 15 minutes from 5:30-9:00pm.
It works fine as is, but I feel I need to organize ...
2
votes
2answers
62 views
How to optimize a function to get user quota stats?
I have the following function to collect quota information for the given user and return the information on a "ready to use" array:
/* Get User Quota Information
*
* Prepare and array with the ...
3
votes
3answers
73 views
More efficient version of an ID calculator in JavaScript
The following function takes two numbers that are linked with a "user" and calculates an ID number based on that. I have been trying to make this as clean as possible, and would like some advice on ...
1
vote
2answers
72 views
In this script what can I turn into funtions and how can I go about doing it?
I have just made a simple script which spawns an alien that chases the player, but I want to move as much of the script into funtions so as to minimize the amount of code, to make it run better when ...
2
votes
2answers
181 views
Sales tax program
I'm very new to C++, and my instructor has just intoduced us to functions and using variables between them. I've got this sales tax program, that more or less works, but for some reason, in the ...
2
votes
1answer
109 views
WordPress Functions.php file review
I am making a WordPress "framework" for myself. Looking at the functions.php file file, is there redundant or bad code that could be changed or some code that could be good to add?
The functions.php ...
4
votes
2answers
93 views
I know I'm not doing these methods right
I'm having difficulty understanding the implementation of methods in classes. I'm pretty sure I'm not doing this the correct way. Can anyone show me the way it should be done.
This code works by ...
0
votes
1answer
114 views
If statements in validating different form data types
I am using if statements within my functions to validate form fields and then on the isset event.
I want to be able to create many different functions to validate various fields groups and it seems ...
1
vote
2answers
117 views
Scala: What takes precedence: Readability or Elimination of Side Effects
I have a method:
def removeExpiredCookies(jar: CookieJar): CookieJar = {
val currentDate = System.currentTimeMillis / 1000L
jar.filter(_._2._2 > currentDate)
}
You use said method ...
2
votes
1answer
145 views
Should I use a class as a class factory in Python?
As a bit of a learning project, I decided to take the concept of proxying objects a bit further and extend it to creating proxy classes which create proxy'd objects. I originally found the idea of ...
-1
votes
1answer
36 views
How do I make print function refer to something in a function without calling the function again? [closed]
So here is my code to find the number of weeks between two dates. How can I make the output print "There are 'x' weeks until 'future date'" without calling the futureDate() function?
from datetime ...
0
votes
0answers
52 views
Please review this mapreduce code that builds a too large index
There's some problem with this code, it builds a too large index (of 16 000 entities when the expected size is 3700):
def index(entity):
try:
edge = datetime.datetime.now() - ...
1
vote
2answers
75 views
How can I make an easy to understand subtraction accumulator?
I'm currently following the tutorials over at RubyMonk, and one of the problems I need to solve is to write a subtract function that would meet these conditions:
invoking subtract(4, 5) should ...
1
vote
1answer
44 views
Python simple function - evaluating arguments
Think of the numpad number layout.
hvd = horizontal, vertical or diagonal
coord = 1,2 or 3 if horizontal or vertical and 1 or 2 if diagonal (1 is up, 2 is down)...
hello('v', 2) - Asking for the ...
2
votes
0answers
180 views
PHP - faster/better implementation of is_dir function for FTP/FTPS connections?
I'm implementing an is_dir() function to determine if a given item is a directory or a file for FTP/FTPS connections. Currently, I have two methods: one using PHP's FTP wrapper for the is_dir() ...
1
vote
3answers
90 views
Review: Compare two Anagrams words in C
A simple attempt by me to test whether two words are anagrams or not. Here is the code:
#include <stdio.h>
#include <ctype.h>
#define CH_LEN 15
#define N 26
int main(void) {
char ...
2
votes
1answer
101 views
Iterative Collatz with memoization
I'm trying to write efficient code for calculating the chain-length of each number.
For example, 13 -> 40 -> 20 -> 10 -> 5 -> 16 -> 8 -> 4 -> 2 -> 1.
It took 13 iterations ...
0
votes
2answers
103 views
How to name this function?
The sample code is like this:
def a_func(parent_node, child_node):
parent_node.add(child_node)
// check validity
return child_node
node1 = a_func(pnode, Node(attr_x = "a new node")
node2 ...
0
votes
2answers
173 views
PHP PDO Custom class ple
i created a database class from a good tutorial and wanted to put it up here so it would get in some search results. it took me about 2 days to find it. also i added a few custom functions to it.. ...
0
votes
2answers
159 views
A better way of writing a PHP function
As the title says, I'm looking for a better way of writing the following PHP function as it's a very very long one. It's part of a bigger class and I'm trying to keep the amount of code as little as ...
1
vote
3answers
55 views
Function calls as parameters for other functions
I am in two minds about function calls as parameters for other functions. The doctrine that readability is king makes me want to write like this:
br = mechanize.Browser()
raw_html = br.open(__URL)
...
1
vote
2answers
329 views
What design pattern to use on this case
I have this code that converts audio to different file formats.
import java.io.File;
import it.sauronsoftware.jave.AudioAttributes;
import it.sauronsoftware.jave.Encoder;
import ...
1
vote
2answers
65 views
How to improve decode function
I have the next function:
var decode = function(str, result) {
var regex = /\d+/,
number = regex.exec(str);
if (number === null) {
return str;
}
var start = ...
5
votes
3answers
199 views
Calling functions (or invoking methods) in constructor
I had written something like the following,
public class ClassName {
//private instance variables
public ClassName() {
//initialize instance variables
this.someFunction();
}
public void ...
1
vote
1answer
137 views
How can I improve this C style va_args replacement?
I inherited a lot of C code with many ellipsis (variadic) function.
So I have a lots of API with the following signature.
void getXY(int foo, ...) // many parameters
and this is used in this way ...
7
votes
4answers
277 views
Can I use this function to store passwords?
Just wrote this function, wondering if it's secure enough to store passwords in a database:
public static function hash($salt, $str) {
$salt = md5($salt);
$salt = ...
2
votes
1answer
80 views
Single function to read one, read all, or set
I have the below function, it is designed so I can call:
$object->temp('one', 1); //add the key `one` to the temp array with value 1
$object->temp('two', 2); //add the key `two` to the ...
0
votes
2answers
104 views
Refactoring functions that generate string with random letters, numbers or mixed chars?
The bellow functions are used to generate a string with random characters, and they have a parameter to specify the string length.
The string can be composed of letters, numbers or a conjunction of ...
2
votes
2answers
57 views
Naming of overloaded methods - singular or plural? [closed]
This question is about your opinions and/or existing conventions.
Let's say I have an object Basket that store various items in some private property (Basket._items). Now, I want to have a couple of ...
2
votes
1answer
269 views
Arduino based SNES controller wireless transmitter/reciever, code questions
I am building a transmitter and reciever pair for two SNES controllers, as I don't like using long extension cords to get the controllers to reach the couch. I'm using atmega328p's for the avr's, ...
2
votes
2answers
363 views
Best practice for converting minutes and seconds in seconds
I have 2 input fields to input the duration of a CD song. The first input is for minutes and the second is for seconds
When submitting the form, I must insert in the db the duration either in seconds ...
2
votes
1answer
76 views
Is it safe to cast a pointer to non-void function into a pointer to void function?
I thought it was a good idea to use this in my C++ projects:
class CRAIICall
{
public:
typedef void (WINAPI * FnType)(HANDLE);
CRAIICall(HANDLE h, FnType fun)
: m_h(h), m_fun(fun)
...
1
vote
1answer
78 views
How to improve a file access permission/deny function?
I've quickly made a function to deny or allow access to files that are only used thru Ajax Calls.
Key Generator
/**
* Generate a key for file access permission
*
* Function that returns a key to ...
3
votes
1answer
131 views
How to improve function to detect the mime type using an agnostic method?
This function is used to get the mime type of any given file. The intended purpose was to have it working within any of our servers that vary from the latest PHP version down to PHP 4.
/**
* Get the ...
2
votes
1answer
115 views
How can this function to get a remote file improve?
I've prepared a function using cURL to get a remote file to the local storage:
/**
* cURL - Get Remove File
*
* Function using CURL the get a remote file to the local storage.
*
* @param str ...
2
votes
2answers
61 views
Is this function properly structured, leading to a secure row insertion?
I have the following function to insert a new row into a table:
/**
* Insert row into the designated table
*
* Function to add a new row into the designated table
*
* @param str $table ...
2
votes
1answer
78 views
How to optimize function that checks if a directory is empty when having over a million files inside?
I have the following function to confirm if a directory contains files:
/**
* Check if the directory is empty
*
* Function to ascertain if the specified directory contains files.
* Comparing to ...
3
votes
1answer
58 views
How does this function look?
What do you think of my function. It is used to change a cyrillic word with a stress marker into the same cyrillic word but with the stress marker seperately represented as a number. As I want to ...
4
votes
3answers
349 views
C Split function review
I started learning C a week ago, and here is my implementation for the split function:
char** my_split(const char* str, char delim, int* size)
{
int index = 0, start;
char** results = NULL;
...
1
vote
2answers
91 views
How to improve this PHP function to confirm the presence/create directories?
While preparing an automated process to manipulate files, I've made the following function to check for the base directory and sub-directories presence, as to allow or deny the remaining code to be ...
1
vote
1answer
294 views
Better jQuery function argument with if-statement?
function check_itv(key) {
if (key.length){
if(key=="left"){
left_itv = setInterval(left,100);
check_left= false;
check_right=true;
check_up=true;
...
2
votes
5answers
151 views
Buy and Sell game project
it's a buy and sell game, and i revised it, it's still unfinished though, i would appreciate it if someone reviewed again for me.
#include<stdio.h>
#include<conio.h>
...
-4
votes
1answer
161 views
how can i improve/reduce this code? [closed]
any type of suggestion/correction will do, and i can't seem to get my functions to work or my conditional statements.
int buyitem(int x, int y)//* function used to buy an item at any store*//
{ ...
0
votes
2answers
79 views
May be a memory leak somewhere or allocated memory not freed? [closed]
Here is the entire program, please help me, I've tried everything to find out what exactly is going with the memory. The problem is everything runs perfectly, but there are some extra characters ...