Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
I have a colleague who uses ternary operators this way (In javascript):
var genderLabel = '';
isMale? genderLabel = 'Man' : genderLabel = 'Woman';
In C#, I'd just do that .
var genderLabel = isMale? "Man" : "Woman";
My colleague says it's a javascript coding convention... is that true? I'm no javascript expert and I dislike that language... When I review code, I focus on my left hand-side to follow a variable initialization or assignment, that kind of style forces me to read the whole line.
I'm also maintaining a Java code of an ex-employee, he uses ternary operators the same way. Is that an anti-pattern? I think it should be disallowed by the compiler the same way it's disallowed in a if statement :
if(x = 2)
{
...
}
This won't compile in C#.
My colleague says it's a javascript coding convention... is that true?
No.
Is that an anti-pattern?
Usually a line by itself is a statement, however this
isMale ? genderLabel = 'Man' : genderLabel = 'Woman';
is an expression, with a side effect of setting the value of genderLabel. Is that a good practice? I don't know, however if you think that is a good practice, then you will also have to allow this:
var a = 1, b = 2;
b = [a][a = b, 0]; # swap a and b
Your colleague might as well do:
if(isMale) genderLabel = 'Man';
else genderLabel = 'Woman';
which is much clearer.
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
I am studying JS from freecodecamp and the instructor was making an app which counts people, and he had written the code like this:
let countEl = document.getElementById("count-el")
console.log(countEl)
function increment() {
count = count + 1
countEl.innerText = count
}
increment()
can i write it like this
let count = 0
function increment() {
count = count + 1
document.getElementById("count-el").innerText = count
}
increment()
where i dont have to define countEl separately .
The second snippet is definitely better. The first one is a no-go for several reasons:
the countEl variable is global, which makes the function fragile and impossible to test
resource acquisition is separated from the actual usage and there's no apparent connection between both
should the HTML element go away for some reason, the reference will be kept around, thus creating a memory leak
On a more general note, try to design your code to avoid globals altogether and to only request resources when they are actually needed and free them as soon as possible.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I need to make a function that compares two strings, no matter the language. Example:
let string1 = 'hola' //Spanish
let string2 = 'hi' //english
console.log (string1 === string2) //true as expected result
Is there a way to compare strings in different languages.
Regards!!
No, not really. Doesn't make much sense either considering that each word might have different meaning in each language depending on context.
Best you can do is try to translate the words and try to find matches in one of the many translations, or use an embedding model for those specific languages and see if the vectors are similar.
The only idea that I have right now is to declare 2 Maps for both languages and then get the key:
var spEn = new Map();
spEn.set('hola', 'hi');
var enSp = new Map();
spEn.set('hi', 'hola');
function spanishEnglish(spanish, en){
translation = spEn.get(spanish);
return translation === en;
}
function englishSpanish(en, spanish){
translation = enSp.get(en);
return translation === spanish;
}
console.log(spanishEnglish('hola', 'hi'))
console.log(englishSpanish('hi', 'hola'))
However this is a just a rudimentary example. There are tons of things you need to pay attention to :)
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I'm using jQuery and I'd like to know whether it is good practice to save $("#myVar") to a variable first like this
var jQvar = $("#myVar");
or directly using it like this:
var myVar = $("#myVar").val();
$("#myVar").removeProp("disabled");
..
and when is the best time to use the other over the other? (var and NonVar)
Each time you are accessing an element it takes (little but still) time. So saving it to a variable makes sense in terms of performance and efficiency if you access the element more then once in the current scope.
So writing it like this:
var myVar = $("#myVar");
var myVal = myVar.val();
myVar.removeProp("disabled");
will be faster.
In your specific case, this is the difference for me:
Try for your self
var jQvar = $("#myVar");
- jQvar.val();
- jQvar.onclick(function(){
// do something here
});
- jQvar.onSubmit(function(){
// do something here
});
... obviously this is more faster, you can reduce redundancy of variable declaration, much neater and less clutter you don't want to scroll and find every variable in file right?
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Is there a way to make &&& behave as an operator which can concat operations into one codeblock?
This is something that came up to my mind while coding and was wondering if I could implement it somehow.
What I'm trying to achieve is make code more concise.
Example:
for (i=0;i<n;++i)
arr[i][0]+=2; &&& arr[i][1]+=3; &&& arr[i][2]=45;
I do not want to hear about how I can use additional for loop or use { } instead.
I'm wondering if such operator is possible in c++, javascript or (if not in one of those 2) in ruby
EDIT: I was aware that this is a bad thing to do but I was wondering if it is possible to be done nevertheless.
I though this was the whole point of having fun while coding.
Well, &&& is lexed as && followed by &, so you could overload the && operator taking a pointer:
struct X
{
int x;
X& operator=(int i)
{
x = i;
return *this;
}
X& operator+=(int i)
{
x += i;
return *this;
}
X& operator&&(X*)
{
return *this;
}
};
Unfortunately, operator precedence gets in the way, so you have to parenthesize the assignments:
int main()
{
const int n = 10;
X arr[n][3];
for (int i = 0; i < n; ++i)
{
(arr[i][0]+=2) &&& (arr[i][1]+=3) &&& (arr[i][2]=45);
}
}
(But of course nobody in their right mind would actually use abominations like this in production code.)
You could use the , operator instead:
arr[i][0]+=2, arr[i][1]+=3, arr[i][2]=45;
But this is a very ugly way to code (and so is your proposal). Introducing a block and having each as a separate statement would be much easier to read.
It is not part of the language definition, so no - you cannot arbitrarily define new syntax - that would be a different language. You could create a preprocessor or translator to convert your new language to valid C++, but I hardly see what purpose this would serve on its own.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I'm trying to learn to shorthand my javascript but stuck with the following one.
if (windowwidth >= 960){
widthofwindow = 1;
yooucandoit()
} else {
widthofwindow = 0;
$('#topbar').remove();
}
That code looks fine. It's easily unterstandable and has very little duplication of code.
You can make it really short, if the yooucandoit function doesn't depend on the widthofwindow variable, but this uses side effects in the conditional operators, so it has a pretty bad code smell...
widthofwindow = windowwidth >= 960 ? yooucandoit(), 1 : $('#topbar').remove(), 0;
If all you wanted was to set the value of widthofwindow, you could do it with a ternary operator like so:
widthofwindow = (windowwidth >= 960) ? 1 : 0;
However, for the other statements in your code you still need to use an if statement.
you can use the ternary operator and then do the if on the widthofwidth
widthofwidth = (windowwidth>=960) ? 1 : 0;
if (widthofwidth) youcandoit() else $('#topbar').remove();
(widthofwindow = +(windowwidth >= 960)) ? yooucandoit() : $('#topbar').remove();
Using Ternary operator
condition ? ifTrue : ifFalse
and casting to number with +. Combining this with the fact
(foo = bar) === bar