The conditions tag has no wiki summary.
3
votes
2answers
45 views
How to avoid goto without replicating code in my meshing algorithm?
To generate faces of a Minecraft-like world from a three dimensional array, I developed the following meshing algorithm. The faces are given vertices and indices. A vertex consists of a position in ...
0
votes
2answers
86 views
Should I return true or false if both of the IP address strings are empty?
I am validating two IP addresses. I simply wrote a peice of code like
if(string.IsNullOrEmpty(ip1) && string.IsNullOrEmpty(ip2)
return true;
But is this logically correct? What should ...
2
votes
3answers
137 views
Switch improvement
I have the following C# code, it actually works, but as you can see I have some bad coding (I think).
For example...
case "K1":
switch (Convert.ToInt32(tb.Text))
{
case 5: // ...
2
votes
2answers
110 views
iOS/Objective-C “if” best practice
I've got this code that I found in a project and I'm wondering how can it be done better:
- (void) setAlpha:(float)alpha {
if (self.superview.tag == noDisableVerticalScrollTag) {
if ...
0
votes
1answer
55 views
Optimization: Eliminate conditional expression within common code
Background
The following code (part of a natural language processor) was written to eliminate duplicate code by using isLeft as a conditional throughout the method:
private void ...
5
votes
2answers
190 views
How can I make this method less verbose?
I point to my problem in the comments of my code.
I have a class RomanNumeral:
public class RomanNumeral {
private String romaNumera;
private String romans = "IVXLCDM";
public ...
1
vote
4answers
144 views
Double conditional : Cleaner way to write a set of conditionals
Is there a cleaner way to write:
def b_fname
if mdes_version_is_after?(3.0)
result = c_fname
else
result = response_for("#{birth_baby_name_prefix}.BABY_FNAME")
end
if ...
4
votes
1answer
51 views
Writing A Better/Cleaner if statement
This piece of code I have written works, but I don't think its the best solution.
What I am doing is check if a certain radio is checked if it is show this div if else show this div if this radio is ...
4
votes
4answers
122 views
Nested condition subset of the top one
I've got this code
Double best = nodes.get(edge.getToNodeId());
if (best == null || (best > g+h)) {
nodes.put(edge.getToNodeId(), g+h);
if (best == null) {
open.add(new ...
3
votes
1answer
66 views
Which is a better style to write default return case in if-else
private String GetFacilityName(String facilityHexID) {
if (facilityHexID.equals(FACILITY_AIRCON_HEX_ID)) {
return FACILITY_AIRCON_NAME;
}
else if ...
1
vote
2answers
66 views
A program that mimics T9 cell messaging
Today I solved a question from Google Code Jam. A program that mimics a cell phone keypad messaging.
Here is the program, with goto statement, I know it is unstructured style to use goto, but I still ...
0
votes
2answers
52 views
Refactor if function in ruby/rails [closed]
working on a rails app that doesn't seem to want to run 'Until' or 'While' functions. Having to do the following:
<% if q == 2 or q == 5 or q == 8 or q == 11 or q == 14 or q == 17 or q ==20 %>
...
3
votes
2answers
340 views
Refactor deeply nested if-else
I found a code on my computer that i wrote a while ago. It is based on an exercise from O'Reilly book Programming the Semantic Web. There is a class, that stores RDF triples, that is data in the form ...
3
votes
3answers
116 views
Which one is more readable for mutually exclusive but mandatory command line parameters validation?
I have a small validation code that needs to make sure that exactly one of two parameter is
set
I consider two options, the "Naive" that has some repetitions and might look "clumsy" but is very ...
4
votes
2answers
134 views
Optimize IF condition block
this is my first post here!
How can i optimize the if condition on this snippet?
The only one difference is && [self isCurrentPosition:i].
How can made it on a single if, including the ...
2
votes
2answers
91 views
factoring out the common code in cases
In
switch (ch) {
case '0': Value v = compute(); v = invert(v); accumulate(v); break;
case '1': v = compute(); accumulate(v); break;
default:
}
the compute and accumulate code pieces ...
2
votes
1answer
286 views
Resource Pool implementation with ReentrantLock and Condition
The Resource Pool is similar to the implementation of a semaphore. It takes the class type of the resource and number of resource pools associated with it as the constructor parameters. There is a ...
3
votes
1answer
190 views
Is there better way of writing these IF checks?
This is what I have, But I think I can write it in a sohrter and more efficient way too, what do you suggest?
string actionString= string.Empty;
if (auditAction == ...
3
votes
2answers
117 views
Is this a right and effcient way of checking AND in a for loop
Just saw this at work in a code review and was wondering if this style of coding - for loop, doing && like that, seems fine to you or is there a better way of doing the same?
for (int j = 1; ...
3
votes
1answer
187 views
Expressing multiple AND conditions in ruby
In ruby, is there a more concise way of expressing multiple AND conditions in an if statement?
For example this is the code I have:
if first_name.blank? and last_name.blank? and email.blank? and ...
3
votes
2answers
104 views
How could I improve this code to make it shorter and more functional? (Python)
#!usr/bin/python
import time
integer = 0
print("The current integer is set at " + str(integer) + ".")
print("\n")
time.sleep(2)
prompt = raw_input("Would you like to change the integer? (Y/N) ")
...
2
votes
1answer
74 views
Python: improve in elegant way (code saving) a function in order to avoid several statements
I wrote this function to read Las file and save a shapefile. The function creates a shapefile with 8 fields. What I wish insert a parse element in the function in order to select the fields I wish to ...
3
votes
1answer
220 views
Connect Four: Bitboard checking algorithm
I'm rather new to Clojure and so I decided to program a Connect Four for fun and learning.
The code below is a Clojure implementation of this bitboard algorithm.
The whole code can be found here: ...
2
votes
2answers
96 views
Shorthand if conditional in php
(!empty($student->former_name) ? print $student->former_name : '');
I only want to print out the former name if it is not empty, nothing else.
I imagine I do not need the else part of it, but ...
2
votes
2answers
54 views
Validation default value
I want to check if all items of a list meet a set of criteria. Currently I'm doing it as below:
bool AreValid(list<string> vals)
{
bool allValid=true;
foreach(string val in vals)
{
...
2
votes
3answers
151 views
Trying to improve another if/else statement
Is there a way to optimize the following if/else statement?
if current_user
if current_user.id == @user.id
render 'item'
else
render 'item_friend'
end
else
render 'item'
end
2
votes
3answers
218 views
Refactoring if statement
Is there a better way to do this? im kinda ending up having a lot of if statements. Any hints would be great!
if params[:custom_url] || params[:user_id]
if !params[:user_id].blank?
@user = ...
0
votes
1answer
93 views
Usage of Conditions Variables with Pthreads
This is just a program to show the use of condition variable when two threads are involved. One thread wants a non zero value of count, and other thread is responsible for signaling it when the count ...
4
votes
2answers
1k views
Multiple nested If checks in VBA
The below code is real and in use, but I've modified it to simplify the process/make it easier to explain.
The purpose of this code is to combine data from multiple data sources. All sources are ...
3
votes
4answers
222 views
if and elseif vs if inside if
Is there any way to improve this code? It looks a bit ugly for me.
if($url_chunks_num == 1) {
$this->controller = $this->pageData[0];
} elseif($url_chunks_num == 2) {
...
2
votes
1answer
96 views
Inner select performance
I have the following code, but I don't know how to make it run faster.
SELECT lot.order as lot_order,
lot.title as lot_title,
lot.id as lot_id,
lot.id_auction,
IF (
...
11
votes
7answers
1k views
Any better way to write this “if” statement?
if( (scene==1) || (scene==2) || (scene==3) || (scene==4) || .... (scene==n) ) {
....
....
}
How can we optimize the condition in the if statement? Any better logic to get the same result?
...
2
votes
1answer
57 views
Generic pass_many(vals, pass_one) to unroll and call pass_one(val)?
I'm trying to create a generic function that takes a data-structure as input and then sends each non-empty item—e.g.: str, int—individually:
def print_this(v, delim1="", delim2=""):
print v, ...
4
votes
4answers
202 views
poh-tay-toh poh-tah-toh, does writing the same code a different way affect readability?
I have a particular if statement that could be written in different ways, and I'm curious as to whether there's any significant difference in readability that I should prefer one over the other:
The ...
9
votes
3answers
1k views
Is it better practice to have void method throw an exception or to have the method return a boolean?
This falls straight into the same category with the recent "Is it better practice to return if false or execute an if statement if true?" question.
Often, while writing code, I find myself presented ...
2
votes
2answers
238 views
Continues If statements
I have a program where it takes the students name and last name and score and saves it. When checking to see if a textbox has information in it, I use ALOT of If statements. Is there a cleaner way to ...
1
vote
4answers
173 views
Refactoring and condensing a Javascript function with many repeat conditions, strings, and variables
I have this function which is pretty easy to read and works just fine. I'm looking to advance my coding abilities by writing more concise code, though.
Does anyone know of an elegant way to condense ...
64
votes
8answers
3k views
Is it better practice to return if false or execute an if statement if true?
Often, while writing code, I find myself presented with two options. Either I can do this:
public void aThing(boolean cond) {
if (cond) {
// Some code
}
}
Or this:
public void ...
1
vote
3answers
368 views
PHP if, else, and return true/false
Just wondering if my
else {return false;}
statements are superfluous... do I just need one return true; here?
function has_access() {
if ( is_user_logged_in() ) {
$role = ...
8
votes
4answers
332 views
Reduce length of condition
Is there a way to reduce this lengthy if condition:
if(
statusCode.equals( NO_ERRORS_NO_WARNINGS ) ||
statusCode.equals( HAS_ERRORS_NO_WARNINGS ) ||
statusCode.equals( ...
18
votes
8answers
987 views
Is it bad to use a ternary inside of an if condition?
Let's suppose I have some sort of form that automatically performs a copy procedure when the user either focuses on a textbox, or highlights some text.
Let's suppose that the user has a preference of ...
3
votes
2answers
534 views
How to simplify nested conditional for loop in R?
I have this for loop for creating a list of variables:
vars <- c( 'beta.o', 'sd.y')
for (x in c('ghs', 'site', 'trt')) {
if(model.parms[[x]] == 1) {
data <- data[, which(names(data) != ...
