Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you assign a variable with the result of a if..else block?

I had an argument with a colleague about the best way to assign a variable in an if..else block. His orignal code was :

@products = if params[:category]   Category.find(params[:category]).products else   Product.all end 

I rewrote it this way :

if params[:category]   @products = Category.find(params[:category]).products else   @products = Product.all end 

This could also be rewritten with a one-liner using a ternery operator (? :) but let's pretend that product assignment was longer than a 100 character and couldn't fit in one line.

Which of the two is clearer to you? The first solution takes a little less space but I thought that declaring a variable and assigning it three lines after can be more error prone. I also like to see my if and else aligned, makes it easier for my brain to parse it!

like image 914
Pierre Olivier Martel Avatar asked May 27 '10 21:05

Pierre Olivier Martel


People also ask

Can you assign a variable in an if statement?

Yes, you can assign the value of variable inside if.

Can you assign a variable in an if statement in Python?

For example, assignment expressions using the := syntax allow variables to be assigned inside of if statements, which can often produce shorter and more compact sections of Python code by eliminating variable assignments in lines preceding or following the if statement.

How do you assign the value of a variable to another variable?

After a value is assigned to a variable using the assignment operator, you can assign the value of that variable to another variable using the assignment operator. var myVar; myVar = 5; var myNum; myNum = myVar; The above declares a myVar variable with no value, then assigns it the value 5 .

Can you declare a variable in an if statement JS?

Should you define a variable inside IF statement? Honestly, there's no right or wrong answer to this question. JavaScript allows it, so you can make your decision from there.


Video Answer


1 Answers

As an alternative to the syntax in badp's answer, I'd like to propose:

@products =    if params[:category]     Category.find(params[:category]).products   else     Product.all   end 

I claim this has two advantages:

  1. Uniform indentation: each level of logical nesting is indented by exactly two spaces (OK, maybe this is just a matter of taste)
  2. Horizontal compactness: A longer variable name will not push the indented code past the 80 (or whatever) column mark

It does take an extra line of code, which I would normally dislike, but in this case it seems worthwhile to trade vertical minimalism for horizontal minimalism.

Disclaimer: this is my own idiosyncratic approach and I don't know to what extent it is used elsewhere in the Ruby community.

Edit: I should mention that matsadler's answer is also similar to this one. I do think having some indentation is helpful. I hope that's enough to justify making this a separate answer.

like image 157
antinome Avatar answered Oct 17 '22 08:10

antinome