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
2answers
146 views
Return value when the requested variable returns null
I have a method that returns a list from a html page:
private List<HtmlNodes> GetRelevantNodes(HtmlDocument aHtmlDoc, string aID)
{
List<HtmlNode> nodes = new List<HtmlNode>();
...
-1
votes
0answers
26 views
Code to solve quadratic equations [closed]
Please bear with me as this is probably a trivial question! I have no clue what I'm doing wrong. When trying to write a program to solve quadratic equations in a certain way I defined 3 functions: ...
6
votes
1answer
43 views
Functions that converts day of year to month and day and reverse with error checking
There is no error checking in day_of_year or month_day. remedy this defect.
Here is the solution:
int day_of_year(unsigned int year, unsigned int month, int day) {
int leap, i;
leap = ...
0
votes
1answer
34 views
Reducing multiple function calls [closed]
I can't quite figure out the best way to approach this. So I have a validation function
that contains various other functions inside of it for validating email, phone and zip.
This works fine but it ...
1
vote
0answers
37 views
Checkbox Group Function and Usage
In CodeIgniter, I have created function (helper) to populate, store and retrieve checkbox group. I just wonder if I have written code is in proper way and optimized or needs some more finishing?
...
7
votes
1answer
61 views
implementations of strncmp, strncat, strncpy
Write versions of the library functions strncpy, strncat and strncmp, which operate on the most n charachters of their argumen strings. For example, strncpy(s, t, n) copies at most n charachters of ...
8
votes
2answers
95 views
Advice needed for scopes in JavaScript
I would like to connect this "JS" to Bugzilla (example: bugzilla.mozilla.org or landfill.bugzilla.org).
I started to learn JS language today and I would like to ask you:
How can I not do bad ...
5
votes
1answer
43 views
Shorten a sorting function
I have this function:
int sort(book *data, int books, char mode, char sortBy) {
int i, j;
book aux;
aux.cat = malloc(sizeof *(aux.cat));
if(sortBy == 'n') {
for(i = 0; i < books; ...
6
votes
1answer
125 views
Parsing function is 50 lines long
This is a parsing function that will at tildes (~) to end of search terms in certain circumstances.
Example an inputs and outputs:
Input: Output:
name:(john doe) ...
6
votes
3answers
79 views
Functions that return a variable
I'm simple looking for what the general preference is between the below options:
anon function directly to variable
middleware.customer = function(req, res, next){
req.customer = function(){
...
3
votes
2answers
68 views
Merging the results of two functions
I have a class which extends another class and override one of its parent methods.
class parentClass {
public function methodA() {
if (some_condition)
return Array();
...
7
votes
3answers
86 views
Finding an element in a list
I'm kind of new to Python. So, I wonder which method is better to use in a function to find an element in a list.
First:
def search_binary(xs,target):
count = 0
for i in xs:
if i == ...
2
votes
0answers
52 views
C++ class “overload” using variadic templates and wrapped function pointers
I'm using C++11 and I have the following problem (pseudo C++):
template<typename T, R1 (*F1)(Args1...)>
class one_size_fits_them_all {};
template<typename T, typename U, typename V, R1 ...
3
votes
5answers
239 views
How can I reduce repetition of these three similar functions? [closed]
I have 3 functions which look like this (simplified):
public void functionA(List list)
{
// there is some functionA specific stuff here
foreach (Object O in list)
{
if(O.a())
...
2
votes
1answer
52 views
Math Skills Game Advice
I think my code works pretty well, although I'm biased: http://jsfiddle.net/AHKb4/2/
Basic Overview: I'm working on building a math skill game, where the objective is to drag and drop div's to a ...
1
vote
2answers
91 views
Is calling a function from within another to alleviate code duplication the best solution?
I am writing up a class to handle various templates to be used in a web application. Each template contains various placeholders which will need to be replaced at the time of the build. I a wondering ...
3
votes
1answer
98 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
95 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 ...
7
votes
5answers
482 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
71 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
102 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
72 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
41 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
70 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
53 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
131 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
51 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
114 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
87 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
110 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 ...
1
vote
1answer
65 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
47 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
118 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
101 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
76 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
136 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
93 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
81 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 ...
4
votes
2answers
209 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
218 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
210 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
223 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 ...
-2
votes
1answer
41 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
157 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
312 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
109 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
146 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 ...