Is it worth to use if-else statement as Java optional pattern?

I came to a point while refactoring using if {} else {} code with Java optionals. While optimizing code it turned to a result to this:

Optional.of(myBoolean).filter(b -> b).ifPresent(b -> {/*my code if true*/}); 

I recognized filter(b -> b) is not the way i wanted to use optionals thus i turned it to -> {/*my code if true*/}).orElse(b -> {/*my code if false*/}); 

The code for the If.class is

public class If {      private boolean isTrue;      public If(final boolean value) {         isTrue = value;     }      public If then(final Consumer s) {         if (isTrue) {             s.accept(null);         }         return this;     }      public void orElse(final Consumer s) {         if (!isTrue) {             s.accept(null);         }     }      public static If is(final boolean value) {         return new If(value);     } } 

Is this a good (whatever good is or should be) approach using the if {} else {} statement in Java?

What would be the best way to accomplish this If-else check?

I have an if-else statement in a php class, which works perfectly. I want to know if there could be an any better or faster way to accomplish this?

if( !empty($  value_1) && empty($  value_2)){     function_1(){ } } elseif( !empty($  value_2) && empty($  value_1) ){     function_2(){ } } elseif (!empty($  value_1) || !empty($  value_2)){     function_3(){ } } else {     function_default(){ } } 

how to assign value to a dataframe in ifelse query

I’m tring to assign dataframe value to a dataframe in ifelse query,but failed.I have no idea about that

df <- data.frame(x=c('a','g','s'),y=c(2,3,4)) df_1 <- ifelse(2>1,df,NULL) 

execute and get the following result:

> df_1 [[1]] [1] a g s Error in if (n <= 1L || lenl[n] <= width) n else max(1L, which.max(lenl >  :    missing value where TRUE/FALSE needed 

the actual results should be

> df   x y 1 a 2 2 g 3 3 s 4  

How to make a if-else condition in Kotlin using an Integer

I need to compare Objects with a lot of properties in Kotlin to determine if they are equals, and I want to know if I can do it in a non verbose way. (Cast Int -> Boolean) for example.

I have implemented this with When and another conditions but I don’t find how to cast Integer to Boolean in a Kotlin correct way

In Groovy I can make something like this

@Override int compareTo(Message message) {  int equalName = <=> message?.name  return equalName ?: this.type <=> message?.type } 

In Kotlin I have made this, but Integer is not casting to Boolean so I have to put more code to obtain the same result and it get worst with more properties…

override fun compareTo(other: Message): Int {     return if ( != 0) -1         else this.type.compareTo(other.type) } 

My Object is more verbose than that.

Is there a cleaner way to do this, of it’s considered correct the actual implementation.

`void` function to replace if-else with short circuit expression

It’s about the “code in question” in the following code:

const s = "waterrfetawx"; const del = 2; function palindromePossible(str, deleteAtMost) {   const set = new Set();   for (let i = 0, len = str.length; i < len; i++) {     const char = str[i];     void(set.delete(char) || set.add(char)); // <-- code in question   }   return set.size <= deleteAtMost + 1; } 

The code in question can also be written like this:

if (set.has(char)) {   set.delete(char) } else {   set.add(char) } 

I’m not too convinced myself whether void(...) is considered “clean”, i.e. readable, maintainable, and clear because it only wraps an expression into a statement. How would have you written it better?

avoid if-else block in favor of default assignment followed by if

A colleague, and frankly better software engineer that me, is telling me that this pattern

let variable = someDefaultVariable(); if (some_boolean) {     variable = someOtherValue(); } 

is better than this pattern

let variable if (some_boolean) {     variable = someOtherValue(); } else {     variable = someOtherValue(); } 

My doubt about this is that the initial instantiation is going to involved a waterfall of function calls before the if block is hit, although he seems convinced that the first appraoch is better.

Speaking specifically about Javascript, which of these patterns is more better and more secure. Is this the same for C?

is it possible to use if-else statements in mocha’s it() function individually?

I recently started with mocha and wanted to know if we could use if else statements for conditional testing. I want to be able to use conditions in ever it() function in order to put assertions on Json data received in response to that getprojection() function call.

I have posted a code as an example to illustrate what I am trying to achieve. Any help would be appreciated. Thanks!!

var assert = require('chai').assert; var expect = require('chai').expect; var should = require('chai').should(); // var projection = require(''); var projectionsTest = require('./getProjection')        describe("Annual Tax Projection", function () {         describe("Current Year Tax - #2018", function () {             it("should return correct taxable Income", function (done) {                 projectionsTest.getProjection('params', 'params', function (projections) {                     if (grossIncome <= deductions){                         assert.deepEqual(projections[2018].taxableIncome, 0)                     }else throw new Error("There is something wrong with tax calculation")                     done(Error);                 });             });             it("should check federal tax is calculated correctly", function (done) {                 projectionsTest.getProjection('params', 'params', function (projections) {                     if (taxableIncome === 0 || taxBracket ===10){                         assert.deepEqual(projections[2018]. )                     }else                     done(Error);                 });             });         });     })