A function (also called a procedure, method, subroutine, or routine) is a portion of code intended to carry out a single, specific task.
2
votes
1answer
27 views
I'm torn between conditionals and abrupt return functions
Which one of these two is better?
This one:
middleware.response_redirect = function(){
return function (req, res, next){
var redirect = function(){
if(req.form.redirect) return ...
1
vote
3answers
74 views
Checking success on functions in C/C++ [closed]
I am trying to understand best practices for C\C++ functions which retrieve data but must also give success/failure information.
Which is better, or is something different all together the correct ...
6
votes
4answers
430 views
Is `if( hidden ) return;` an antipattern?
Sometimes, when you're doing something like displaying a bunch of elements, you can avoid running a function entirely if an object is hidden.
So you can either write the check from the loop:
for( ...
0
votes
2answers
53 views
Counting the even digits in a number [closed]
I made a function to count the even digits in a number and when I enter a number that doesn't include any even digits. I need some one to tell me why and how to fix this. And please be simple in the ...
2
votes
2answers
73 views
Deeper abstracting of code into functions
I created a quiz using the module pattern. My code is as follows:
; (function ($) {
"use strict";
var s,
QuizDefaults = {
modalOverlay: $(".modal-overlay"),
modalWrap: ...
1
vote
2answers
68 views
Scanning an int variable
I have attemped to define a function which scan an int value in a specified range and returns that value. The lower bound is the lower of the two parameters, the upper bound is the larger of the two ...
2
votes
1answer
29 views
Calling multiple functions in image upload form
I have an image upload form with which I call multiple functions before uploading the file such as checking the size and dimensions, and that it is in a valid image. The code works, but calling the ...
3
votes
1answer
62 views
Help with optimizing current data pulling fuctions
Here is what the below code does:
The first function, sqlPull() connects to a local MySQL database and pulls the last 20 rows from the database every 5 seconds. The data coming in is a list of ...
2
votes
1answer
51 views
Improving my invoice generator
In this function, the copy (Write XML) is inside, but I want it to be separated from the function. How can I improve this function?
Sub ExporttoFolder(ByVal POSPath As String, ByVal dt As DataSet, ...
0
votes
2answers
123 views
Which loop code is better practice?
def minval(xs):
minum = xs[0]
for i in range(len(xs)):
if xs[i] < minum: #difference in code: xs[i]
minum = xs[i]
return minum
or
def minval(xs):
minum = ...
1
vote
2answers
47 views
Should I write my fadeTo function differently?
fadeTo = function(obj, time, to, cChange ){
if ( typeof( time ) == "undefined" )
time = 1000;
if ( typeof( to ) == "undefined" )
to = 1;
if ( time <= 0 )
return ...
4
votes
1answer
100 views
Generalized is() type-checking function for JavaScript
In a complex library I've been working on, I got pretty tired of all of the individual type functions and operators to determine whether I was working with an object or piece of data of a given type. ...
0
votes
2answers
66 views
Extending DOM elements with prototype for easier function execution
So I've creating a small library of JavaScript functions that I will be using. Some are for UI functions others are to return something. Below I will post two codes, I am wondering if this method is ...
1
vote
1answer
90 views
Feedback on a small PHP function that generates JSON data
I wrote a PHP function that generates JSON data to populate a line graph. Through jQuery, my code uses a GET request to the PHP script to fetch the JSON data. An example of the JSON data is as ...
0
votes
1answer
60 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
1answer
27 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
75 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, ...
3
votes
1answer
88 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
57 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
93 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
87 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
80 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
193 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 ...
3
votes
1answer
184 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
94 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 ...
1
vote
1answer
177 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 ...
2
votes
1answer
192 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
39 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 ...
1
vote
2answers
108 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
246 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
101 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
116 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
104 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
220 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
171 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
58 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
373 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
82 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
207 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
162 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
294 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
1answer
107 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
66 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
309 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
405 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
85 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
82 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
178 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 ...