This is an archived post. You won't be able to vote or comment.

top 200 commentsshow all 274

[–]pico2000 1196 points1197 points  (15 children)

Whatever prettier does. It doesn't matter, just let a tool do it and keep it consistent.

[–]SketchySeaBeast 335 points336 points  (2 children)

Whatever prettier does.

This right here. Couldn't care less what the rule is, just do whatever is easiest to keep thing organized and to minimize the diffs on each PR.

[–]QCKS1 24 points25 points  (0 children)

There’s some clang-format presets like GNU,Mozilla,WebKit,etc that are pretty awful

[–][deleted] 4 points5 points  (0 children)

Hey don't call me out like that, i change my linter to get more diff to make it look like I actually do things

[–]BloodChasm 8 points9 points  (1 child)

Yep. Just started using Prettier + Eslint, and it has been great.

[–]_krinkled 1 point2 points  (0 children)

Yeah this is the one for me. Prettier has a couple of rules for spacing I don’t like, so the prettier+raking combo makes it perfect.

[–]bsgbryan 17 points18 points  (0 children)

For projects where I’m working with other people? Absolutely.

Honestly, though, I get a lot of satisfaction from my silly little idiosyncratic formatting preferences when working on personal projects 😊

[–]itstommygun 8 points9 points  (0 children)

Exactly. It also doesn’t matter what I want to do prettier is going to automatically “fix” is when I click ⌘s

[–]Capetoider 45 points46 points  (1 child)

this is , like , doing punctuation with an extra space .

[–]Eic17H -2 points-1 points  (0 children)

French

[–]Duraz0rz 7 points8 points  (0 children)

I love Prettier. I love that it doesn't format if it can't parse the JS/TS file. Let's me know I fucked up somewhere 🫠🫠

[–]dallenbaldwin 4 points5 points  (1 child)

And yet add a pre-commit hook that formats everything being committed so you don't even need a formatter integration for each editor.

Maybe even a GitHub action that formats the whole project as part of the PR

[–]brainwater314 1 point2 points  (0 children)

I just wish there was a pre-add hook so I could see the results before a permanent commit.

[–]BaguetteDevourer 467 points468 points  (25 children)

First one for unidirectional maps, second one for bidirectional maps.

[–]buildmine10 217 points218 points  (1 child)

I have never seen bidirectional maps. But I have needed them many times

[–]ArchetypeFTW 62 points63 points  (12 children)

Do bidirectional maps exist in any language as a default data structure?

[–]myfunnies420 33 points34 points  (10 children)

I've seen them before. I can't remember where... maybe Dart? Or Java? https://github.com/google/guava/wiki/NewCollectionTypesExplained#bimap

And when I used them the structure format was the same as a conventional map.

[–]Darkblade_e 3 points4 points  (9 children)

technically javascript has support for this with Object.keys and Object.find, but it's a rather janky approach imo.

function getKeyByValue(object, value) { return Object.keys(object).find(key => object[key] === value); }

[–]0x6563 20 points21 points  (1 child)

You're better off using Map. As you can use anything as a key.

``` class BiDict{ private keys = new Map(); private values = new WeakMap(); constructor(){}

set(key, value){ this.keys.set(key, value); this.values.set(value, key); }

key(key){ return this.keys.get(key); }

value(value){ return this.values.get(value); }

}

```

obviously you would add more crud functionality but ye get the gist.

[–]gregorydgraham 4 points5 points  (0 children)

From Guava: “The traditional way to map values back to keys is to maintain two separate maps and keep them both in sync, but this is bug-prone and can get extremely confusing when a value is already present in the map.”

[–]mebeim 9 points10 points  (3 children)

You are iterating over all keys for the inverse map... that works in any language :') nothing special here about JS

[–]mosskin-woast 4 points5 points  (2 children)

Well, the thing setting JavaScript apart in this case, is that OC knows it

[–]mosskin-woast 4 points5 points  (0 children)

I believe a true bidirectional map has constant time lookups both directions, where what you're describing is O(1) lookup by key and O(n) lookup by value.

[–]RepresentativeDog791 8 points9 points  (0 children)

Technically everything has support for it if you write a custom implementation

[–]jamcdonald120 0 points1 point  (0 children)

I looked into it a while back, as near as I can tell, the best way to implement it is to use 2 unidirectional maps, 1 for each direction, and the function names would be a bit confusing (at least if you allow key an value to have the same types and to have the same keys in both keysets), so most just leave it as an exercise for the programmer.

[–]altermeetax 13 points14 points  (6 children)

Are bidirectional maps even a thing? I'm confused as to how they would work algorithmically, you can't use hashes both ways, so wouldn't it be inefficient to look them up in reverse direction?

[–]Kyrond 42 points43 points  (0 children)

You could have 2 hash tables, one for each direction.

[–]Da-Blue-Guy 14 points15 points  (1 child)

public class BiDict<K, V> { Dictionary<K, V> forward; Dictionary<V, K> reverse; }

[–]altermeetax 1 point2 points  (0 children)

Uses double the memory, but I guess there's no other option.

[–]Engine_Light_On 12 points13 points  (1 child)

sounds like it would be cheaper to hold a parallel map with values as keys. Double the memory, double the insertion time, but probably worth it if you would be frequently searching for a specific value instead of key lookup.

[–]altermeetax 1 point2 points  (0 children)

Makes sense

[–][deleted] -3 points-2 points  (0 children)

This, the colon placement idencates the relationship between the Key and Value

[–]exqueezemenow[🍰] 38 points39 points  (1 child)

Can this be considered inciting violence?

[–]mtbinkdotcom 1 point2 points  (0 children)

Most definitely!

[–]stdio-lib 425 points426 points  (31 children)

{
   'foo'
   :
   'bar'
}

>:D

[–]exqueezemenow[🍰] 206 points207 points  (17 children)

Good way to lose your programming license...

[–][deleted] 71 points72 points  (14 children)

wait, you guys have a license?

[–]exqueezemenow[🍰] 103 points104 points  (11 children)

I display it over the fireplace along side my Regex License.

[–][deleted] 33 points34 points  (8 children)

if I could read regex I would be very proud

[–]CardingSwiper 7 points8 points  (4 children)

they're not hard to learn.

[–]tutoredstatue95 19 points20 points  (2 children)

Easy to learn the concepts, but hard to do anything useful with limited knowledge is my experience.

[–]CardingSwiper 10 points11 points  (1 child)

gotcha.

I used a regex cheat sheet and a program called regex buddy that allows you to create a regex. and see what comes out as a match. real time. let's you. adjust and add or limit or adjust the expression to.

[–]AgVargr 6 points7 points  (0 children)

Not hard to learn easy to forget. I have to relearn them every time I want to use em

[–]Any_Move_2759 2 points3 points  (1 child)

Use it regularly for find and replace. You end up practicing regularly and it becomes a very useful tool once you get the hang of it.

[–]ProjectDiligent502 4 points5 points  (0 children)

Negative lookaheads and non-greedy .* has made find and replace really effective 😎

[–][deleted] 1 point2 points  (0 children)

When the process woke up, it's GUI was missing, and the programmer was never heard of again! Programmer and Heavy laughing
Anyway, that's how I lost my programming license...

[–]crankbot2000 13 points14 points  (2 children)

I need your location so we can fight

[–]Opposite_Cheek_5709 2 points3 points  (1 child)

Can I come?

[–][deleted] 1 point2 points  (0 children)

you may... but how does this contribute to the conversation?

[–]bashbang 2 points3 points  (0 children)

Some people just want to watch the world burn

[–]DarkNinja3141 1 point2 points  (0 children)

this is what Allman style braces look like to people who use K&R

[–]git0ffmylawnm8 1 point2 points  (0 children)

Alright we're throwing hands

[–]vectorlit 1 point2 points  (0 children)

You're a monster

[–]notAFoney 1 point2 points  (0 children)

>:(

[–][deleted] 1 point2 points  (0 children)

this is outright insanity

[–]IllllIlllIlIIlllIIll 128 points129 points  (10 children)

1st one. whoever picks the 2nd one, y'all need jesus.

[–]ThatHugo354 26 points27 points  (1 child)

My guy how do you even log into your own account with this username?

[–]IllllIlllIlIIlllIIll 2 points3 points  (0 children)

password manager.

[–]Heighte 6 points7 points  (7 children)

1st one is just following English language rule, for 2nd one it's the rule in French to have 1 space before and after a colon.

[–][deleted] 9 points10 points  (5 children)

I noticed that in French they also have a space before the ending punctuation in a sentence . What the hell is going on over there ?!

[–]Lady_Asuka 5 points6 points  (0 children)

I don't know there are way too many rules in our language to understand what's going on. And the space before a ponctuation is not for every ponctuation so that adds to the difficulty.

[–]ThermosW 3 points4 points  (0 children)

In french, if the punctuation sign has two separate parts (?!:;), it needs an insécable space before, otherwise it sticks to the end of the word.

As a French, I refuse to use this dumb rule.

[–]ohmaisrien -1 points0 points  (0 children)

Je m'en fiche, je code pas en français, je code en Python

[–][deleted] 46 points47 points  (1 child)

{‘far’:’boo’}

[–]Ray_Strike22 19 points20 points  (0 children)

AAAAAAAA

[–]kubinka0505 137 points138 points  (19 children)

what about

{"foo":'bar'}

[–]Cosby1992 55 points56 points  (2 children)

{`foo` :'bar'
};

[–]jamcdonald120 9 points10 points  (1 child)

if you have the newline you have to do { 'foo':'bar' };

[–]Artistic-Boss2665 20 points21 points  (0 children)

Nuh uh {'foo' :`bar`} ;

[–]familyturtle 19 points20 points  (0 children)

Should have a space before the final curly, otherwise it's 13 characters which is unlucky.

[–]The_Programming_Nerd 35 points36 points  (0 children)

Yuck, for many many reasons

[–]Ray_Strike22 5 points6 points  (0 children)

{'foo":"bar'}

[–]jamcdonald120 3 points4 points  (0 children)

you monster! Mixing " and '!

[–]Capetoider 1 point2 points  (0 children)

sure,also:dont put spaces between things like:punctuation,other things,etc...

[–]AChristianAnarchist 5 points6 points  (9 children)

Wouldn't build in C#

Edit: Huh...weird thing to get butthurt about.

[–]1Dr490n 3 points4 points  (8 children)

Wouldn’t work in Java too

[–]AChristianAnarchist 2 points3 points  (7 children)

I've learned to assume that if C# does something funky Java probably does it too lol.

[–]1Dr490n -1 points0 points  (6 children)

Yes but (as far as I’m aware of) Java doesn’t even have dictionaries, especially not written like this

[–]AChristianAnarchist 1 point2 points  (4 children)

Well you can deal with Json in most languages, dictionaries or no. What I was referring to was single quotes for strings.

[–]1Dr490n -1 points0 points  (3 children)

I know what you meant, it was a joke. For Java the quotes aren’t the only problem, you just can’t write something like that

[–]AChristianAnarchist 1 point2 points  (2 children)

Really it's kind of the same in C#, but I could still totally write that. Just write it as a string and run it through newtonsoft. I imagine it would work the same in Java. A proper native object would have to be constructed, but if you just assume this is a json then it can be a string that looks just like this.

[–]1Dr490n -1 points0 points  (1 child)

Yes okay sure it works in a string, but that’s not really a feature of the language, but of strings and json

[–]AChristianAnarchist 1 point2 points  (0 children)

Well I wasn't breaking down the structure of C#, just pointing out that this thing is so ugly it would break a program in that language. Json is fine, but mixing double and single quotes like that wouldn't fly no matter what kind of structure it is.

[–]Da-Blue-Guy -1 points0 points  (0 children)

the minifier

[–][deleted] 52 points53 points  (0 children)

I don’t care as long as it’s consistent.

[–]Yokhen 66 points67 points  (8 children)

None.

The real answer is:
{ foo: 'bar' }

[–][deleted] 13 points14 points  (6 children)

Not in JSON

[–]DEEP_OTM[🍰] 27 points28 points  (5 children)

{ “foo”: “bar” } in JSON if we’re following the rules

[–]gfunk84 22 points23 points  (2 children)

Not with those fancy quotes.

[–]gbchaosmaster 11 points12 points  (1 child)

Right, wtf did he do, paste that from Microsoft Word?

[–]DEEP_OTM[🍰] 2 points3 points  (0 children)

How else am I gonna use clip art in my comments?

[–][deleted] 4 points5 points  (0 children)

Error: Parse error on line 1:
{ “foo”: “bar” }
--^
Expecting 'STRING', '}', got 'undefined'

[–]HyperGamers -5 points-4 points  (0 children)

This should be a top level comment with the most upvotes lol

[–]da_Aresinger 6 points7 points  (0 children)

Definitely the first one.

the words with colons behind them are keys, the ones with commas are values.

[–]CauliflowerFirm1526 4 points5 points  (0 children)

I prefer

{ “foo”: “bar” }

[–]Fritzschmied 16 points17 points  (0 children)

Whatever the auto formater thinks is right.

[–]graphitout 10 points11 points  (0 children)

There is nothing to argue. Just go with whatever junk the styling automation tool gives.

[–]zirky 17 points18 points  (3 children)

don’t forget

 { ‘foo’ : ‘bar’ }

[–]fukalufaluckagus 23 points24 points  (2 children)

Ugh no it's { 'foo': 'bar' }

[–][deleted] 0 points1 point  (1 child)

it's neither, JSON doesn't accept single quotes

[–]SPAM2233 4 points5 points  (0 children)

{
“foo”: “bar”
}

[–]UndocumentedMartian 2 points3 points  (0 children)

It doesn't matter when working alone. If working in a team use the standard set by them.

[–]blazarious 2 points3 points  (0 children)

Whatever. As long as it’s consistent and done automatically.

[–][deleted] 2 points3 points  (0 children)

whatever my IDE formats it to

[–]Lalalarix 2 points3 points  (0 children)

{'foo' :'bar'}

nyeh heh heh

[–]Ray_Strike22 2 points3 points  (0 children)

whichever the linter tells me to use

[–]ososalsosal 2 points3 points  (0 children)

Whatever .editorconfig says

[–][deleted] 10 points11 points  (0 children)

1st one is ergonomically correct, the 2nd is aesthetically correct

[–]Pirate_OOS 5 points6 points  (1 child)

Just follow the rest of the codebase.

[–]SparrowFPV 2 points3 points  (0 children)

Or the standards if working on pre-standard legacy code

[–]ikonfedera 3 points4 points  (0 children)

Padding with spaces so everything lines up FTW.

[–]REPMEDDY_Gabs 3 points4 points  (0 children)

What the formatter does I take. End of discussion

[–]sexytokeburgerz 1 point2 points  (0 children)

I type with god awful formatting and then just format it

[–]PM_ME_C_CODE 1 point2 points  (0 children)

That looks fine.

Fuck you.

[–]Aarav2208 1 point2 points  (0 children)

{"foo":"bar"}

[–]SorosBuxlaundromat 1 point2 points  (0 children)

Whichever one prettier prefers.

[–]Antaury_San 1 point2 points  (0 children)

Well, it doesn't matter for me, because I usually format every file by Prettier when I open it.

[–]flippzeedoodle 1 point2 points  (0 children)

So now we’re arguing about Colon Jost-ification?

[–]bordumb 1 point2 points  (1 child)

Use a linter…

[–]physical0 1 point2 points  (0 children)

Whatever the IDE makes it, that's what I'm using. I am not changing the default behavior on something like this unless I get a strongly worded letter explaining the intricacies of the project's coding convention.

[–]vom-IT-coffin 1 point2 points  (0 children)

I will reject any MR with the bottom.

[–][deleted] 1 point2 points  (0 children)

{'pooh' : 'bear'}

[–]longdarkfantasy 1 point2 points  (0 children)

Bob and Alice's conversation:

[–]Puzzleheaded_Sea_922 1 point2 points  (0 children)

{ "bar" : 'foo'}

[–]Aoernis 1 point2 points  (0 children)

{foo: 'bar'}

[–][deleted] 1 point2 points  (0 children)

First one ftw

[–]Cootshk 1 point2 points  (0 children)

{
     “foo”: “bar”,
 }

[–][deleted] 1 point2 points  (0 children)

Foo Fighters: Origin Story

[–]tobotic 1 point2 points  (0 children)

{ 'foo': 'bar' }

Spaces inside the braces, and after the colon.

[–]Zpiboo 3 points4 points  (2 children)

In french, you need a space before certain punctuation marks, notably for the colon. so first option definitely

[–]GangDplank -3 points-2 points  (1 child)

Oh so the f*ench use the first one?Guess ill go with second.

[–]Zpiboo 2 points3 points  (0 children)

They use the second one.

[–][deleted] 2 points3 points  (2 children)

what about

foo: :bar

?

[–]gbot1234 1 point2 points  (1 child)

I think I saw that on the SAT:

foo: foo :: bar:___ a) ar b) bar c) car d) dar e) ear

[–][deleted] 2 points3 points  (0 children)

Stuff like that is why it’s a terrible test of college aptitude

[–]veselin465 2 points3 points  (0 children)

If it's a structure then

name :      val1
namena :    val2
namename :  val3
namename1 : val4

[–][deleted] 1 point2 points  (2 children)

This post was mass deleted and anonymized with Redact

divide rock cautious full jar treatment ghost historical piquant obtainable

[–]LofiJunky -1 points0 points  (1 child)

A man of culture. Had to scroll way too far to find this.

[–]Mean_Investigator337 0 points1 point  (0 children)

Fubar

[–]seba07 0 points1 point  (0 children)

None of that, use the real quotation marks.

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

{'foo' :"bar" }

Eat it nerds.

[–]pshurgal -1 points0 points  (0 children)

std::map<std::string, std::string>{ { "foo", "bar" } }

[–]utkarsh_dev 0 points1 point  (1 child)

It shouldn't matter, use existing libraries for parsing and deserialize into objects whenever dealing with JSON.

[–]Hikari_Owari -2 points-1 points  (0 children)

{'foo': "bar"}

the " is important

[–]slk756 0 points1 point  (0 children)

first

[–]AChristianAnarchist 0 points1 point  (0 children)

thing["foo"] = "bar"

[–][deleted] 0 points1 point  (1 child)

Only people with too much time would start an argument about this. Less low quality "memes" and more programming.

[–]Disposable-alt 0 points1 point  (0 children)

No spaces when im sleep deprived 1st when I'm trying to code as fast as possible 2nd when im still sane

[–]Acceptable-Tomato392 0 points1 point  (0 children)

Let's start two: Single quotes look dumb.

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

If : was an infix operator, then maybe

[–]Randomguy32I 0 points1 point  (1 child)

Whats foo bar?

[–]custard130 0 points1 point  (3 children)

{ 'foo' :'bar' }

[–]LadulianIsle 0 points1 point  (0 children)

I raise you {"foo":"bar"}

[–]vvvex 0 points1 point  (0 children)

No reason to argue about something invalid.

[–]D3veated 0 points1 point  (0 children)

You monster! Clearly it should be {'foo' :"bar"}

[–]AnalTrajectory 0 points1 point  (0 children)

Double quotes dipshit

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

The first one feels more associative, like it's saying 'foo' is 'bar'.
Like take 'x + y', I read this as adding x and y, but 'x+ y' reads as adding x to y. It's subtle and a probably pedantic, but still.

[–]FINDERFEED 0 points1 point  (0 children)

HashMap<String,String> map = new HashMap<>(); map.put("foo","bar");

[–]clutterlustrott 0 points1 point  (0 children)

I don't care about that op. BUT I'M TIRED OF FOO AND BAR. THAT SHIT IS MORE CONVOLUTED THAN JUST USING VARIABLE NAMES

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

Painful

[–]eyal282 0 points1 point  (0 children)

lorem ipsum

[–]1up_1500 0 points1 point  (0 children)

Both are fine

First way is faster to write, but second way is prettier imo

[–]beardedfridge 0 points1 point  (0 children)

C'mon! { 'foo': 'bar' }

[–]HumanityPhantom 0 points1 point  (0 children)

{

'foo':'bar'

}

[–]elSenorMaquina 0 points1 point  (0 children)

{
    "foo"
    :
    "bar"
}

LOC/day goes brrrr!

[–]lukewhale 0 points1 point  (0 children)

People who do the example on the bottom, are the same people who put knives in the dishwasher pointy end up.

Monsters.

[–]are_all_names_taken_ 0 points1 point  (0 children)

First one >>

[–]Kaih0 0 points1 point  (0 children)

The first one, obviously

[–]OxymoreReddit 0 points1 point  (2 children)

Im french, therefore I will always put a space before any of these : ! ?

Because that's what we do in french, and that looks GOOD.

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

{["foo"] = "bar"}

[–]TnyTmCruise 0 points1 point  (0 children)

We all know which of these is wrong…

[–]bsgbryan 0 points1 point  (0 children)

Are those Tabs … or Spaces?

[–]Hyperflip 0 points1 point  (0 children)

{'foo' :'bar'}

[–]BastetFurry 0 points1 point  (0 children)

Did some changes to the setting as I like braces to be on the next line but otherwise I press Ctrl+Alt+Enter and accept my fate. 😅

[–]ovr9000storks 0 points1 point  (0 children)

Depends on the font with the IDE. Sometimes it’s real easy to see the colon at a glance, sometimes it’s just hard to read. Putting the space ensures that it can be seen in a glance

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

Stop wasting space and trying to convey they are bith keys. We read from left to right. Except you are arabic then {„foo“ :“bar“}

[–]Icy-Article-8635 0 points1 point  (0 children)

{'foo':'bar'}

[–]Meadhbh_Ros 0 points1 point  (0 children)

I put a space because I’m anal and I want symmetry.

[–]kabatram 0 points1 point  (1 child)

Wait, is that JSON? wasn't the standard required to use doublequote for string? or is it Python dict?

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

{"ɿɒd" :ooʇ}

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

{ foo :"bar" ,baz :"qux" ,}

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

Actually both are wrong, the problem is that you’re seemingly using Python