Option 1:
className={
data.msg.length > 48
? `${classes.message} ${classes.longMessage}`
: `${classes.message}`
}
Option 2:
className={`${classes.message} ${
data.msg.length > 48 ? classes.longMessage : ""
}`}
Is there any performance difference too? Thanks.
They're pretty much the same, and choosing one over the other will have no noticeable effect on the performance of your app. The only thing you will want to consider in this circumstance is the readability - in which case I would argue that the first example would be preferable, but that's entirely subjective.
I totally agree with the readability what #noob mentioned in the answer earlier.
From data side let me configure the following below:
// I assume you are getting this from an API or somewhere else
const data = {
msg: 'Testing text for stackoverflow'
};
From that perspective my suggestion would be to go with the following option:
let messageClassName = `${classes.message}`;
if (data.msg.length > 48) {
messageClassName = `${classes.message} ${classes.longMessage}`;
}
return (
<div className={messageClassName}>
{data.msg}
</div>
)
Most of the time readability helps.
My guess is the top one is faster, here is my reasoning:
Both will have one ternary expression - Where you are using it does not change it's speed, but the commands executed after expression will, and this is what make the first faster, eg:
Case expression TRUE: ( no - difference )
Both will have to concatenate
Case expression FALSE: ( Top one faster )
Top one just evaluate
Botton one concatenate and evaluate
But to do this is too much preciosim.
Related
I am trying to filter some information with 2 conditionals (something is true and something else is >0)
Independently things work just fine:
=unique(filter(
indirect($A$1&"!$E$3:E"),
indirect($A$1&"!$C$3:C")=TRUE
))
gives me a list of things that are true, and
=unique(filter(
indirect($A$1&"!$E$3:E"),
indirect($A$1&"!$J$3:J")>0
))
gives me a list of things that are >0.
When i try to combine them, like this
=unique(filter(
indirect($A$1&"!$E$3:E"),
indirect($A$1&"!$C$3:C")=TRUE,
indirect($A$1&"!$J$3:J")>0
))
I get an error No matches are found in FILTER evaluation.
What am i missing please?
PS: It goes without saying that I do indeed have things that are both true and are > 0
for non-english locale it would be:
=unique(filter(
indirect($A$1&"!$E$3:E");
indirect($A$1&"!$C$3:C")=TRUE;
indirect($A$1&"!$J$3:J")>0))
Is it possible to do this in terraform?
for i in range(10):
if var != "" and i > 2:
# Something to do
elif var != "" and i < 2:
# Something to do
else:
# Something else to do
What i want to achieve is to create list but i need if/else statement in for loop. What i have achieved so far is:
for i in range(10):
if var != "":
# Something to do
It's hard to answer this question because it seems like you already decided on a solution to a problem but you haven't actually stated what the underlying problem is. However, I will try to answer directly what you asked, nonetheless.
When thinking about approaching problems in Terraform it's best to think from the perspective of constructing values from expressions using other values, rather than writing imperative statements to describe how to construct those. In this case, I would try to pick your problem into two parts:
Doing something different when var is set to the empty string vs. other cases.
Doing something different when constructing the first two elements of a list, but only in the non-empty string case.
The first of those sounds like a conditional expression, because you want to choose between two possible outcomes based on a condition:
locals {
example = (
var != "" ?
(expression for normal case) :
(expression for empty case)
)
}
You haven't included details about what ought to happen in the "empty case", but since you suggested you would use a loop in an imperative language I assume that your problem would best map to a for expression in Terraform.
locals {
example = (
var != "" ?
(expression for normal case) :
[ for i in range(10) : (something based on i) ]
)
}
The "normal case" has the extra detail of doing something different based on whether the index is less than two:
locals {
example = (
var != "" ?
[
for i in range(10) : (
i < 2 ?
(something based on i) :
(something else based on i)
)
]
[ for i in range(10) : (something based on i) ]
)
}
(Your original question used i < 2 and i > 2, and my answer above admittedly uses i < 2 and i >= 2 instead; I made an assumption that this interpretation was more likely, but this answer won't work if you really do need to do something special when i == 2 that's different to when it's less than or greater than.)
I can't take this answer any further without more detail about the underlying problem you're trying to solve, but since you seem to be coming from familiarity with Python I do have two general mappings for you that might help you translate from Python-ish approaches to Terraform-ish approaches:
Terraform's conditional operator c ? t : f is equivalent to Python's conditional expressions.
Terraform's for expressions are equivalent to Python's list comprehensions.
In both cases these constructs have similar capabilities but different syntax. If you feel more comfortable exploring in Python first then I'd suggest using these particular Python constructs to build your solution and then you should be able to translate the result to an equivalent Terraform expression.
Terraform is (sadly) not a programming language, so it's not easy (or possible) to convert any pseudo-code to HCL.
Anyway, for your specific case you need to combine two things:
the equivalent of i of your pseudo-code in Terraform would be: count.index (in case you use count)
the if-else-else is probably something like:
(local.a != "" && count.index > 2) ? "option 1" : ((local.a != "" && count.index < 2) ? "option 2" : "option 3" )
(not tested)
Also it might come across as nit-picking, but a tiny remark is "Something to do" in Terraform you rather not declare things to be done (as it's not a procedural programming language), but rather desired state of things.
I've started using Google Cloud Deployment recently, having come from AWS CloudFormation (and Terraform alongside) and I'm really struggling to use Jinja in dynamic ways that I do so simplistically in the others.
My current issue is that my deployment is entirely dynamic based on some user input, and so in AWS CF and Terraform (which points at both AWS and GCP) I use maps to get settings determined by a previous choice. The following is an example from Terraform:
variable "Cluster_Instance_Map" {
type = map
default = {
"C1" = "Single-Node : 0 : A : B"
"C2" = "Multi-Node : 2 : Q : R"
"C3" = "Multi-Node : 4 : X : Y"
"C4" = "Multi-Node : 8 : S : T"
...
}
}
And then I would, for example, grab the first value for the respective row by using the Cluster_Config_Choice variable chosen from 'C1, C2, C3, C4' by a user previously as follows:
split ( " : ", var.Cluster_Instance_Map[ var.Cluster_Config_Choice ] ) [0]
Thus far, I've really struggled to re-create this type of variable in Jinja for GCP. I'm new to GCP in general, but also Jinja, and what I find online is, for the most part, confusing me more than anything, and so any help with this is muchly appreciated!
--- Edit ---
As per request, I will give some detail into what I've done with Jinja thus far, although it sadly isn't too much. My initial idea is via the use of LIST and SPLIT. I figure I could do something like the following:
{% set list_example = ({ "A" : "1 ; 2 ; 3", "B" : "4 ; 5 ; 6" }) %}
{{ list_example [ user_input_variable ].split(';')[1] }}
And the second line would then return, "5" if the user selected B, for example. I did make that code up, though (second line) so it doesn't work for syntax errors (100% expected) but I don't know if it's even close.
Is a LIST and SPLIT the way to go? Or are there MAP-like functions available that I am missing out on..
I also don't know how to put my SET function across multiple lines without erroring, so sorry for the mess above. Though I assume Google can tell me that when I'm not busy! >.>
Hope this helps clarify things.
After going around in circles, and learning very minimal in the process, I've actually realised that my answer was what I had already tried... The code I posted above saying doesn't work, works - Though it turns out there were a multitude of other reasons it didn't, but not related directly to itself.
And so, my answer to re-create what I have as a 'map' in Terraform/AWS CF is as follows:
{% set Cluster_Instance_Map = ({
"C1" : "Single-Node : 0 : A : B",
"C2" : "Multi-Node : 2 : Q : R",
"C3" : "Multi-Node : 4 : X : Y",
"C4" : "Multi-Node : 8 : S : T",
...
}) %}
{% set user_input_variable = "C3" %}
{{ Cluster_Instance_Map [ user_input_variable ].split(':')[1] }}
And the final piece would return the number 4, in that case. My code is also across multiple lines, so it's much easier to read now as well - This also was an issue unrelated (as mentioned, I'm entirely new to Google and Jinja, so lessons learned).
I must say, though, the documentation for Google Cloud really is terrible in comparison to others. Very easy to loose yourself. So even though the above doesn't really need an answer, figure best to put this here just in case others have a similar question.
Is it possible to have multiple statements in an inline if statement in an AngularJS expression? For example, the following fail:
ng-change="someCondition() ? doA(); doB() : doC()"
ng-change="someCondition() ? doA(), doB() : doC()"
This can be done as follows:
ng-change="someCondition() ? doA() : doC(); someCondition() ? doB() : ''"
But that's ugly, calls someCondition() twice, and screams to be done in a controller. Is there any way to get the first examples to work?
Angular expressions are limited to simple expressions. Anything more complicated may be rejected by parser and error will be thrown. Read about differences between Angular Expressions vs. JavaScript Expressions in documentation. Namely:
No Control Flow Statements: You cannot use the following in an Angular expression: conditionals, loops, or exceptions.
No Comma And Void Operators: You cannot use , or void in an Angular expression.
etc.
The best thing you can do is to create a function in controller that will combine doA and doB. Not only it will make Angular parse happy, but the code itself will become cleaner and simper to read. After all this is template, it's better to keep it simple.
Your first example is not valid javascript.
Your second example contains , which cannot be in any angular expression (not sure if valid javascript or not).
Depending on your use case you can sometimes get it to work using && and ||.
However, code in templates is still code. Try to make it readable. If you can't make it readable, extract it into a function in your controller.
ng-change="onChange()"
Please try the below scenario:::
use the () in multiple statements
ng-change="someCondition ? (doA(), doB()) : doC()"
ng-change="someCondition ? (doA(), doB()) : doC()"
I tried something like this in Angular 8 with ternary operator
<input
maxlength="{{memberProofForm.proofTypeId === 1?16:(memberProofForm.proofTypeId === 2?10:(memberProofForm.proofTypeId === 3?15:77))}}"
name="proofIdNumber"
required
type="text"
[(ngModel)]="memberProofForm.proofIdNumber">
It will work like this
if(memberProofForm.proofTypeId === 1){
return 16;
}
}else{
if(memberProofForm.proofTypeId === 2){
return 10;
}else{
if(memberProofForm.proofTypeId === 3){
return 15
}else{
return 77;
}
}
}
Silverlight is case sensitive for query string parameters so the following code would return false with "callid=5"
string callId;
if (System.Windows.Browser.HtmlPage.Document.QueryString.TryGetValue("callId", out callId))
{
....
}
Microsoft defends the decision by citing the www.w3.org spec, but I think it leads to a less friendly experience for people trying to link to you, or give a URL over the phone.
Looks like Stackoverflow is case insensitive:
https://stackoverflow.com/search?q=silverlight+bug
https://stackoverflow.com/search?Q=silverlight+bug
I think you should focus on your naming conventions rather than the implementations of standards, making sure to avoid similar field names and mixed case. For example, you can use a convention of words that over the phone can be read out stating "all lowercase" or "all uppercase".
I did this. Don't know if it helps.
var keyName = "";
if (!string.IsNullOrEmpty(keyName = someDictionary.SomeKeys.FirstOrDefault(k => k.ToLowerInvariant() == "size")))
{
var someValue = someDictionary[keyName];
}
Yes, I'm used to it being case sensitive, and therefore have been programming to it for a long time. I know of some people that have implemented methods to do intermediate parsing to convert them all to lowercase, or other things server side, and it really depends on what you are working with specifically.
As for usability, yes it is harder to read. BUT, at the same time a URL over the phone that has a querystring is not easy to give out anyway.
This workaround will not use the power of dictionaries because it will iterate through all keys, but it is likely to be a sufficient work-around for most scenarios.
var keyName = HtmlPage.Document.QueryString.Keys.SingleOrDefault(key => key.Equals("callid", StringComparison.OrdinalIgnoreCase));
string callid;
HtmlPage.Document.QueryString.TryGetValue(keyName, out callid)
You could also transform the whole QueryString dictionary to a new dictionary with a case insensitive comparer if you are having many dictionary lookups.
var insensitiveQueryString = HtmlPage.Document.QueryString.ToDictionary(pair => pair.Key, pair => pair.Value, StringComparer.OrdinalIgnoreCase);