Is an else statement always needed? [duplicate] - tcl

This question already has answers here:
Is it necessary to write else part in every if condition?
(12 answers)
Closed 8 months ago.
I am writing a function that returns 1 if a pattern is matched. Else it should return 0. Should I avoid the else statement and just return 0 as the last line? It won't affect the logic of the function, but what is the recommended way? Is there any site/book I can follow for such tips?
proc is_bus_net { net } {
set net_name [perc::name $net -fromTop]
if { [regexp {(\S+\)[\d+\]$} $net_name match first_name ]} {
puts "matched $net_name => $first_name"
return 1
}
return 0
}

Is it necessary to write else part in every if condition?
Else statement is not always needed but it makes the code more readable.

Related

function returns a 0 after write host/return statement powershell [duplicate]

This question already has answers here:
How do I pass multiple parameters into a function in PowerShell?
(15 answers)
.GetType().FullName or subtraction doesn't work on a function call directly in powershell?
(1 answer)
Powershell function call changing passed string into int
(1 answer)
Closed 1 year ago.
Playing around with powershell and am getting funky print out of "0" at the end of both a write-host and or return statements. Unsure how to fix that. I am sure its a dumb mistake I am doing... anyone know why this happens?
Code:
function New-Test(){
param(
[String] $stringTest,
[Int] $intTest
)
Write-Host $stringTest, $intTest
}
# calling function
New-Test("Hello World", 26)
Output is: Hello World 23 0 --> notice the 0 output... why does this happen and how do I get rid of this?

How to use powershell function parameters [duplicate]

This question already has answers here:
How do I pass multiple parameters into a function in PowerShell?
(15 answers)
Closed 2 years ago.
This a very newbie powershell question, but I couldn't find what's wrong in my little script, I appreciate if anyone can help on this. So, my function is as basic as :
cls
function Matchstring ($source, $compare) {
write-host -ForegroundColor Green ("Comparing " + $compare +' and '+ $source)
}
Matchstring('Test1', 'Test2')
But the result I get is Comparing and Test1 Test2 instead of Comparing Test1 and Test2
No comma and no parenthesis in the call line.
Matchstring 'Test1' 'Test2'

What is the logic behind the "&&" operator here? [duplicate]

This question already has answers here:
What does '&&' operator indicate with { this.props.children && React.cloneElement(this.props.children, { foo:this.foo})
(5 answers)
Using &&'s short-circuiting as an if statement?
(6 answers)
Closed 2 years ago.
I know that when we use the "&&", the logic is like this:{ boolean && action } -> if the boolean is true then return the "action". In my code it happens like this: if the src property exists, then return the <Avatar />. My question is: why is it like this? src doesn't have any true or false value. Why it happens like this ?
function Sidebar({src}) {
return (
<div>
{src && <Avatar src={src} />}
</div>
)
}
src doesn't have any true or false value
It has a value (even if that value is undefined or null) that is truthy or falsy even if that value isn't a Boolean.
src can have undefined or null value. So to avoid this.
First we are checking if src contains valid data which is neither undefined nor null And if that condition is true then we are passing that source to Avatar as props.
Just operator of javascript && operator will return last one if others in ahead different flase (each others != flase)
//just operator of javascript // && operator will return last one if //others in ahead different flase (mean others != flase)
let object = {a:1};
console.log(object && 1);
console.log(object && 1 && object);
console.log(null && object);

Is it possible in Go to call a function with named arguments? [duplicate]

This question already has answers here:
Initialize function fields
(2 answers)
Closed 3 years ago.
I want to call a function in Go, and attach to the argument values the argument names
func sum(a int, b int) int {
return a + b
}
func main() {
result := sum(a=4, b=5) // result == 9
}
Is it possible?
There is no such thing like named arguments in go
At the moment Go does not have a way to use named argument in functions.
If you really need to use named arguments you can try this library go-named-params

Laravel 5.0 Join Sub Query With Request Paramters [duplicate]

This question already has answers here:
How to select from subquery using Laravel Query Builder?
(11 answers)
Laravel query builder General error 2031
(1 answer)
Closed 6 years ago.
I'm trying to create a very complex query, I've simplified it as much as possible (2 select statements, are concatenated to the main query), the code below, works only If there are no "date" parameters sent in the request, When parameters sent, I got an error "SQLSTATE[HY000]: General error: 2031"
I think the problem is in toSql() method, but I don't know what else could I use
to solve that?
Update: I need to keep leftJoin method so I can't use mergeBindings method
// 1st select statement
$earnQuery = DB::Table('san_charity_point_earn')->select('charity_id','earn_date',DB::raw('count(user_id) as users,sum(points) as earn_points,count(transaction_id) as earn_trans'))
->where(function($q) use($request){
// filter paramters
if($request->has("from")){
$q->where("earn_date",">=",$request->from);
}
if($request->has("to")){
$q->where("earn_date","<=",$request->to);
}
})
->groupBy('charity_id')
->toSql();
// 2nd select statement
$redeemQuery = DB::Table('san_charity_point_redeem')->select('charity_id','redeem_date',DB::raw('sum(points) as redeem_points,count(transaction_id) as redeem_trans'))
->where(function($q) use($request){
// filter paramters
if($request->has("from")){
$q->where("redeem_date",">=",$request->from);
}
if($request->has("to")){
$q->where("redeem_date","<=",$request->to);
}
})
->groupBy('charity_id')
->toSql();
//full Query
$charities = DB::table('san_charity')->select('san_charity.charity_id','title_ar','title_en',DB::raw('
COALESCE(users,0) as users,
COALESCE((earn_trans+redeem_trans),0) as transactions,
COALESCE(earn_points,0) as earn_points,
COALESCE(redeem_points,0) as redeem_points'))
->leftJoin(DB::raw('('.$earnQuery.') earn'),function($join){
$join->on('earn.charity_id','=','san_charity.charity_id');
})
->leftJoin(DB::raw('('.$redeemQuery.') redeem'),function($join){
$join->on('redeem.charity_id','=','san_charity.charity_id');
})
->get();