To learn it, you gotta 'do' it is blank code acceptable?


#1

Hello, my code worked as asked, however I'd like to know if a code with ´{ }´ is acceptable and if is normal to use like this:

var getToDaChoppa = function(){
  // Write your do/while loop here!
  do {
      console.log("text");
  }while(getToDaChopa=false){
  };
};

getToDaChoppa();

#2

Are you asking about this part? while(getToDaChopa=false) { };


#3

Yes, Id like to know if leaving it empty is Ok or a bad practice.


#4

As far as performance is concerned - probably not, but for readability I would remove {} these if you don't need any code inside while block. It's more like an individual choice.


#5

Got it! So the piece of code below would be acceptable as well?

var getToDaChoppa = function(){
  // Write your do/while loop here!
  do {
      console.log("text");
  };
getToDaChoppa = false;
};

getToDaChoppa();

Thank you again!


#6

No :), not as quite as you expected. I mean you still need to have while block, but you can make it a little shorter by removing {} curly brackets:

var getToDaChoppa = function(){
  // Write your do/while loop here!
  do {
      console.log("text");
  }while(getToDaChopa=false);
};

getToDaChoppa();

If you use do while construct you can't get away without using both do and while. Of course you can rewrite your code so that it uses while block only, but not the other way around.


#7

If youre suggesting he keep the while statement merely for passing the exercise, yet remove the curly brackets i would disagree. Id suggest you keep the brackets so you dont throw any errors under any miscellaneous conditions, but keep them on the same line .

while () {};

But then again im also against leaving out semi-colons...so do what you want. I think bringing the brackets onto the same line is a fair compromise.

Heres a post I made showing how you can compromise on your typical formatting to make code more readable, while still maintaining proper syntax & consistency.


#8

Partially I agree with you but it's really a matter of preference. Both ways will work, of course I see your point here that keeping curly brackets can make it more understandable and if next time you need to put something inside while block then you would only need to put code in it without thinking anything further about syntax, logic and stuff.


#9

Precisely, im also considering any potential pitfalls down the road because of such a thing. Syntax errors are the most common & least complex. I understand were permitted some leniency with JavaScript, but the underlying principles are still in play, even if we get away without participating.


#10

Yep, JavaScript can be forgiving but that comes with greater responsibility if you know what you're doing then it's only a choice, but if you want to avoid any ambiguity then it's best to to keep consistent syntax in code and to avoid short syntax, but when you get a little more advanced you are more aware of what's happening inside you code.

So to finally end this topic: We are both right :slight_smile:


#11

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.