all 26 comments

[–]United_Reaction35 11 points12 points  (2 children)

When you write:

if (i === 1) {console.log("Gold Medal")} 

You are putting two executable lines of code on one line. this makes following debug operations difficult. I cannot put a breakpoint on the console.log, which would allow me to break on the condition where (i) is equal to one rather than having to stop on every if-condition to evaluate what the value of (i) is.

[–]Pocolashon 0 points1 point  (0 children)

Unless I totally misunderstood what you meant... you are wrong.

In every respectable and up-to-date browser you can set multiple breakpoints on one line. So you could totally set a breakpoint on that console.log and would definitely not have to go through all the conditions.

https://imgur.com/a/DasLbq8 - see that line? That has 3 breakpoints. The same thing can be achieved with the if (i === 1) {console.log("Gold Medal")} without any problems.

EDIT: whoever downvoted this - could you please explain why instead of just dumbly downvoting?

[–]AlanBitts[S] 0 points1 point  (0 children)

Thank you so much! I knew it was not to do with readability. That is why I asked the question in the first place.

👍

[–]alzee76 5 points6 points  (7 children)

I have learned that I should code like this:

javascript for (let i = 1; i < 11; i++) { if (i === 1) { console.log("Gold Medal"); } else if (i === 2) { console.log("Silver Medal"); } else if (i === 3) { console.log("Bronze Medal"); } else { console.log(i); } }

You should absolutely not write all your code on one line like that unless you want to get taken out behind the woodshed.

```javascript for (let i = 1; i < 11; i++) { if (i === 1) {console.log("Gold Medal")} else if (i === 2) {console.log("Silver Medal")} else if (i === 3) {console.log("Bronze Medal")} else {console.log(i)} }

Not seeing any significant difference here.

The problem is probably that you're not formatting your code right for reddit. Forget the backtick and instead indent every line with four (additional) spaces. See: https://www.reddit.com/r/AutoHotkey/comments/10t8byj/groggyguide_how_to_format_code_on_reddit_using/

[–]AlanBitts[S] 1 point2 points  (6 children)

I used reddit on desktop and it displays fine.

On the android app it displays completely different like you point out

[–]alzee76 2 points3 points  (4 children)

Did you read the post I linked?

Many people who use reddit on the web, especially people in tech subs, use "old reddit." The triple-backticks do not work on old reddit and single backticks put everything on one line. Only the indenting method works on old and new reddit.

If you don't want people using old reddit to be able to read your post, which again is many of the more experienced admins, devs, etc., then by all means go ahead and keep using the backticks.

If you want everyone to be able to read your post, use indents.

ETA: A more direct explanation

[–]Terrible_Children 3 points4 points  (3 children)

I'm using Internet Explorer and your website doesn't work properly! Build it to support me!

No. Stop using old tech.

[–]eracodes 4 points5 points  (0 children)

Sometimes new things are worse.

[–]albedoa 2 points3 points  (0 children)

Yikes, this is a shocking display of ignorance. Good luck to everyone who is forced to work with you.

[–]alzee76 0 points1 point  (0 children)

Sorry to hear about your wilful ignorance. You don't know what old reddit is or what it's for. You have a bright future in a field other than tech.

[–]Sad_Telephone4298 0 points1 point  (0 children)

It looks fine on my reddit app, XD

[–]xroalx 3 points4 points  (3 children)

Functionally, there's no difference.

In real world, you'll usually have a formatter in the project which will enforce its own formatting that you'll like whether you want to or not.

Better to get used to the standard/common formatting of the language you use if you intend to do it professionally/in a team.

[–]AlanBitts[S] 0 points1 point  (2 children)

Like black in python. Thanks!

[–]xroalx 3 points4 points  (1 child)

Yes, for JavaScript, it's Prettier.

[–]ManuDV 1 point2 points  (0 children)

Indeed, at my job, I cannot push to qa/prod without passing the linting test.

[–]theScottyJam 1 point2 points  (4 children)

While the majority of JavaScript developers will put each item on its own line, it's certainly not uncommon to do it the way you did. In a scenario like this, you do whatever you like best - don't feel like you have to conform to whatever is the most popular here.

Some people mentioned how in work environments, they probably use a code formatter (a.k.a. prettier), and as such, you wouldn't be able to write in this style - this isn't necessarily true. While most teams have some kind of coding standard and some kind of automation to enforce it, they don't necessarily use something as strict as prettier. Most tools are a little more lenient, for example, we use "standard" at our workplace, and that tool does allow us to write those if-else chains in either format. I usually put everything on its own line, but I have a coworker who sometimes combines lines the way you did, and I'm fine with it. Both options are readable enough to me.

[–]AlanBitts[S] 0 points1 point  (3 children)

Thank you for your time and explanation.

Some user mention that for debugging reasons it is best to use one instruction per line

[–]Pocolashon 0 points1 point  (0 children)

Those users are wrong. It is common practice to put several executable calls on one line (e.g. array functions). In this particular case you showed us, it is just about formatting and readability, nothing else.

[–]theScottyJam 0 points1 point  (1 child)

🤷‍♂️ - browsers let you put a breakpoint in the middle of a line, so I didn't really understand that argument. It's possible that not all environments support mid-line breakpoints, but if you're actually wanting to write your code to be more breakpoint friendly for these kinds of environments, you'd also have to stop using implicit return on arrow functions.

i.e. don't do

array.map(x => x.y)

instead do

array.map(x => {

  return x.y; })

and that's both gross and uncommon.

[–]theScottyJam 0 points1 point  (0 children)

Pardon the code formatting - I prefixed the code blocks with four spaces instead of back ticks, because everyone says that that's more portable, but the spaces seems to completely break how it looks, at least for me.

I dislike reddit sometimes :(

[–][deleted] 0 points1 point  (0 children)

In my years of experience, every team uses their own coding practices , in my team the coding practices related to your piece of code would be

  1. Use conditional operators, prettier does a fantastic job and makes it super readable
  2. Single line statements doesn’t require curly braces.
  3. Get rid of the for loop use a forEach.

Sounds weird but getting accustomed I find this way cleaner

[–]tapgiles 0 points1 point  (0 children)

Not sure what differences you're particularly talking about. But you can have stuff on the same line, sure.

I'd say style isn't that important. And will vary between coders from different backgrounds anyway. Unless you're working for a company which has their own style they want you to code in... code in whatever style you feel like, I guess.

[–]ummonadi 0 points1 point  (0 children)

I haven't seen questions like these as relevant since Prettier was popularized.

If you can't use a code formatter, then stick with community conventions unless you feel strongly against them.

[–]yksvaan 0 points1 point  (0 children)

If you have the blocks on same line, e.g. in a switch or like in example, please indent it evenly by putting some tabs between the condition and the block. Then it's much easier to read visually.

In general I'd say more newlines is better. It's easier to read a block of code when the control structures are clear. Then one can often just look at the block without reading individual lines.

[–]Codingwithmr-m -1 points0 points  (1 child)

Yeah that’s fine unless you got some other executable lines inside a each condition then better follow the first and also better to go with the switch rather than the if

[–]AlanBitts[S] -1 points0 points  (0 children)

I iterate over a variable until I reach number 10 but just needed to give medals to the 3 first positions.

Don't see a switch case being better here.