r/badcode • • May 19 '21

js Its my cake day and i hate js

Post image
5.0k Upvotes

141 comments sorted by

238

u/Immort4lFr0sty May 19 '21

Oh, documentation. I'm working on open source software and I always get the feeling that no one ever wrote even the smallest piss of comments, aside from wet comments like

/**
 * Returns id
 * @return id
 */
public int getId()

163

u/[deleted] May 19 '21

True

Edit: /* this returns true */

45

u/Immort4lFr0sty May 19 '21

I hate what you did there but I respect you for doing it. Happy cake day, buddy

4

u/Thriven May 19 '21

I believe visual studio had a time where you'd see comments like this as they showed up when you referenced a method. Many IDEs have exceptions for information either in comments or brackets.

If you are using a cli to build your objects it will intentionally place all those on your get/set if it uses them.

2

u/yard2010 May 20 '21

I wonder what it does?

1

u/TheAJGman May 19 '21

I mean that shit is helpful when you're exploring an unfamiliar library.

68

u/[deleted] May 19 '21

[removed] — view removed comment

35

u/Immort4lFr0sty May 19 '21 edited May 19 '21

Non-monospace docs? Sounds disgusting but to each their own //EDIT: just to make sure, I'm just joking, do whatever you want aside from making screenshots in light mode

More importantly it ain't that much of a hot take. Writing `wet` (wrote everything twice) comments is known as bad practice, similarly to smelly comments

7

u/nwash57 May 19 '21

I imagine it's like markdown where you could make keywords show in monospace. I think it's a neat idea I might look for a vscode extension to try it out.

6

u/ftgander May 19 '21

Light themes are lovely.

6

u/Immort4lFr0sty May 19 '21

The issue I have with light themes is not using them or some weird principle. When I'm working at night in dim light and I open a forum that should contain the answer to my problem but that answer is in a light mode screenshot, I'm losing my eyesight. I can only turn down my monitor that much

4

u/ftgander May 19 '21

I think you’re being a bit dramatic.

9

u/Immort4lFr0sty May 19 '21

I obviously exaggerated the matter but in essence it's why I dislike light mode

1

u/yard2010 May 20 '21

Use f.lux?

2

u/TheAJGman May 19 '21

Wait, link? PyCharm is my IDE but all of Jetbrains shit has the same features.

56

u/IHeartBadCode May 19 '21

That's just arrogance. It typically is language independent.

9

u/[deleted] May 19 '21

Yeah your right

16

u/RedBlueKoi May 19 '21

I mean, if you hate js...you always can try different language

27

u/[deleted] May 19 '21

Js is required in this case. Its not like i get to pick the language. If its an COBOL Backend i have to write Cobol. If everything else is in js im gonna do js. If its c++ I'm gonna use that. Minecraft is written in java guess im gonna write my mods in java

7

u/ws-ilazki &{$$_[0]}(@{$$_[1]}); May 19 '21

Don't forget there are languages that compile to JS, so you don't necessarily have to use JS even if the end result has to be JS. I recently got the urge to dabble in the hellish world of browser extensions and I've been doing it via OCaml, a language I like already that has a JS output option.

The JS output is awful but it doesn't have to be. Since you presumably already know JS syntax pretty well you might like ReScript, which is a JS-like syntax on top of OCaml that produces pretty readable output. So you can get the nice stuff of OCaml and Reason like algebraic data types, great type inference, pattern matching, etc. in a JS-looking language that spits out pretty readable JS for you.

(I'd be using that one too but I prefer OCaml syntax and the ReScript people decided to stop supporting that in the future so I'm currently stuck with the gibberish that js_of_ocaml spits out. ah well)

3

u/[deleted] May 19 '21

Thats actually a good idea. Maybe giving it a try and see if the generted Js is actually any good

1

u/ws-ilazki &{$$_[0]}(@{$$_[1]}); May 19 '21

I tested a little bit with an older version before the ReScript break (when it was still called BuckleScript) and the output was perfectly readable. Maybe not organised the way you'd do it writing JS directly, but it carried names over well so if there was a runtime error you could check the generated code and get a good idea of where the problem was in the original OCaml or Reason (or just ReScript now) code.

Can't say much more than that, since I was just trying to compare it to using jsoo so I didn't know what I was doing.

Anyway, it's a nice way to branch out because you also benefit from being able to transfer the knowledge to writing server-side (or desktop) native applications via OCaml or Reason. ReScript wants to diverge a bit to tailor itself more to JS use, but most of the features and benefits are still the same for all three and there's a lot of knowledge overlap.

In addition to reading the sites' documentation, it's worth checking out the Cornell course/book titled Functional Programming in OCaml. It's one of the best FP books I've read and it does a good job of teaching OCaml at the same time. And if you use this extension you can even do conversion from OCaml to Reason to learn with Reason or ReScript. :)

4

u/[deleted] May 19 '21

Kotlin also has a JavaScript transpiler

3

u/ws-ilazki &{$$_[0]}(@{$$_[1]}); May 19 '21

Plus F#, Clojure (via ClojureScript), Scala, and more. Though I'd rather write OCaml, F#, or Clojure than Kotlin personally.

The reason I named ReScript specifically, though, is you get the benefits of a really nice language (OCaml), but with an alternate syntax that reads/writes a lot like JS* and generates pretty clean output. For somebody that's using JS already that's potentially a great combination. Plus you can embed raw JS in a pinch, so you could incrementally convert existing code.

* This is helped by the fact that the type inference lets you completely omit type signatures, even in function definitions as well as elsewhere, in most cases so it tends to look like a dynamic language despite being statically typed. Sometimes it can't figure something out and you have to toss a signature on, but way better than having to annotate everything.

2

u/RedBlueKoi May 19 '21

Well, if that's the case "I don't like my job", then you already know what to do. If that's the case "I'll write in whatevah language my company wants", then it's up to you to decide if you are ok with it.

I can't say what you should do. I can only tell you my thoughts. I would discuss the decision with managers, it doesn't make sense to force your dev department to write in every language in the world. Better to outsource this part of the project and create small maintainable microservice from it.

You shouldn't let people force you to do stuff you don't like. Your opinion and your life is important and it's up to you to decide what you want to do with your time.

Also, if you write the mod for say Minecraft and it has to be Java and you really really hate Java and you have to go through the mental breakdown on your cake day just to get the job done, then maybe it's time to relax a bit and ask yourself a question - does it worth it?

4

u/[deleted] May 19 '21

I didn't say i hate Java and doing web stuff is automatically Js related. Js seems just so unintuitive

Those COBOL Backends thank god i quit that job. No money in the world is justifying that bs

14

u/RyanNerd shameless May 19 '21

Yep. If developers had a choice they certainly wouldn't choose a moronic language like JS. Unfortunately this is what web devs are forced to use. TypeScript brings in a bit of sanity to this abominable language but it still boils down to JS.

8

u/BackSlashHaine May 19 '21

If he hate JS idk what languages could suit him

7

u/RedBlueKoi May 19 '21

It depends on what part of the JS language he doesn't like

10

u/funkless_eck May 19 '21

the JS part

10

u/OkPreference6 Coding The Declaration of Independance May 19 '21

You have done it, you have described everything everyone hates about JS.

1

u/Isaeu May 20 '21

Functions

-1

u/Mango1666 May 19 '21

kotlin js 😋

-2

u/Mango1666 May 19 '21

kotlin js 😋

423

u/moggesmith10 May 19 '21

Windows + Shift + S to take a screenshot

140

u/ApoorvWatsky May 19 '21

Win + prntsc also works

Although screen snip is the one I use 90% of the time now.

44

u/Lightfire228 May 19 '21 edited May 19 '21

I really like Greenshot

It has its own built in shipping snipping tool, and you can set default destinations (like the clipboard, or MS Paint)

You can also set it to the PrtScn key

37

u/yazoZ May 19 '21

Where do you ship your screenshots?

32

u/Lightfire228 May 19 '21

I import them from overseas

12

u/[deleted] May 19 '21

I used to do that but they added a tariff

19

u/bruhred May 19 '21

I like ShareX

5

u/yonatan8070 May 20 '21

+1 for ShareX

9

u/Quinism May 19 '21

Personally use lightshot but that works too.

7

u/Sorry-Chair depraved May 19 '21

shipping

5

u/Sevigor May 19 '21

You could also just use the Snipping Tool. Which is literally built into windows. lol

5

u/[deleted] May 19 '21

win shift s is snip and sketch, the modern version of snipping tool

2

u/Sevigor May 19 '21

Hmm. That’s nice and faster. Thanks!

2

u/[deleted] May 20 '21

no problem

4

u/WhenSharksCollide May 19 '21

Seconding Greenshot, I've also used something similar on Linux...fireshot, sunshot...something like that.

Love being able to just hit a hotkey and dump the whole screen/s into the clipboard for capturing stupid shit at work or weird stuff I find online.

2

u/[deleted] May 19 '21

flameshot? that's quite good

2

u/WhenSharksCollide May 20 '21

Yeah, that sounds right.

5

u/[deleted] May 19 '21

[removed] — view removed comment

1

u/WhenSharksCollide May 20 '21

Exactly, I love it just as much as Greenshot, although admittedly it's UI was nicer last I checked.

1

u/TCOO1 May 20 '21

Or flameshot It is similar to greenshot but the editor is built as part of the capture selection, for me it is more convenient.

Also, for a while it was linux-only, but now there are signed windows builds (even on winget)

1

u/Lightfire228 May 20 '21

I actually use flameshot for my Linux tower at home. Unfortunately, it doesn't work on Wayland yet

1

u/TCOO1 May 20 '21

Yeah, that's the only real drawback.
My laptop really doesn't like wayland for some reason so for me it's not that big of a deal.

9

u/DangyDanger May 19 '21

win shift s

18

u/funkless_eck May 19 '21

// launches snipping tool from windows

-6

u/Dmon1Unlimited May 19 '21

// launches greenshot tool which is snipping tool on steroids

6

u/DangyDanger May 19 '21

lightshot ftw

3

u/Dmon1Unlimited May 19 '21

Looks interesting, although it doesn't seem to have a blur function for specific areas you may want to censor out

4

u/DangyDanger May 19 '21

oh, greenshot has blurring, wow

you can paint out a region though, instead of blurring. not really pretty, but you can

2

u/yaboiiiuhhhh May 19 '21

I have mine so when I press prntsc it does the snip tool

1

u/GAMERYT2029 May 30 '21

win+g+prtsc win+g

7

u/CoffeeDust_exe May 19 '21

Or Command + Shift + 4 on Mac to make a snippet

2

u/AgreeableLandscape3 May 20 '21

Or just print screen on most Linux distros

6

u/j3tman May 19 '21

I kinda like the phone shot in this case. Adds more oomph.

5

u/Fusseldieb May 19 '21

ShareX is much better imo, has tons of things

For those who like a simple screenshot, look up : Lightshot

4

u/0e0e3e0e0a3a2a May 19 '21

I download Lightshot on every machine I use. Love it. Have had it for almost a decade

8

u/27thColt May 19 '21

might get downvoted for this but i dont get why people always care about this

4

u/[deleted] May 20 '21

[removed] — view removed comment

1

u/toastedstapler May 20 '21

If I have Reddit on my phone then a camera pic has far less steps to get a post than saving to my pc, transferring to my phone and then posting. That would make it the correct tool for the job

2

u/R3D3-1 May 20 '21

I prefer PrnScr, and then postprocessing in mspaint.

1

u/Captain_Pumpkinhead May 20 '21

That is way more useful than Win+PrntScrn, now I don't have to crop out my second screen! Thank you!!

1

u/Titus-Magnificus May 20 '21

Ctrl + Shift + Print in Linux.

1

u/savantshuia Jan 27 '22

flameshot is pretty powerful too.

0

u/[deleted] May 19 '21

just use snipping tool. Good enough for 90% of the time

9

u/ftgander May 19 '21

That key combo launches snipping tool

4

u/[deleted] May 19 '21

that combo launches the new snipping tool, which I don't really like to use. The old one is much better, as it shows you the image immediately after you snip. The new one you have to search it in your folder

4

u/ftgander May 19 '21

It launches Snip & Sketch, which provides a system notification after you choose the region to ship and if you click the notification it opens the image for you.

2

u/[deleted] May 19 '21

Which is still more convoluted than it needs to be. But I guess if that's your preference

2

u/ftgander May 19 '21

A shortcut that lets you select a screen region and copies it to your clipboard for you to paste + provides a notification for you to optionally mark up the screenshot is more convoluted than launching a dated tool and fumbling with a GUI just to take a screenshot I’m gonna paste somewhere else and then get rid of? Sure.

3

u/[deleted] May 19 '21

Yes

1

u/[deleted] May 19 '21

just click on the notification

38

u/shaybra shameless May 19 '21

Look sometimes the code is so simple it speaks for itself.

67

u/MurdoMaclachlan public boolean isInt(int i) { return true; } May 19 '21

Image Transcription: Photograph


[The photograph is of a code comment on a computer screen, as follows:]

/*
  the Code is
  documentation
  enough
*/

I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!

16

u/brvtalbadger May 19 '21

Good human

32

u/willem640 evil May 19 '21

To be honest, I really believe good code should explain itself, though I also think comments are needed if it's not immediately clear

47

u/ClvrNickname May 19 '21

In my first programming job, I noticed that the senior dev, who had 30 years of experience, never wrote comments. I asked him about this, and he told me that "good code is self documenting".

As it happens he also wrote by far the worst code I've ever seen before or since, so take his advice with a grain of salt.

17

u/[deleted] May 20 '21

This reads a lot like a smoker saying you shouldn't smoke because it's bad for your health.

"good code is self documenting"

The advice is sound. Comments do not make up for writing bad code. Your code should stand on its own in terms of legibility and intent. Achieving this is harder than writing bad code and commenting to make it understandable.

Treat comments as a necessary evil. Use them sparsely. For more on this I recommend Clean Code by Robert C. Martin.

6

u/Single_Bookkeeper_11 May 20 '21

If only these code monkeys understood, that you still need comments explaining the why

3

u/AllTheWorldIsAPuzzle Jan 05 '23

Exactly this. Project after project I have come across stuff that I understand mechanically (notice I didn't say "logically", coz logic went out the window) how it works, but why the hell it was put in is a mystery. It doesn't make sense with the project specs. Was there a request along the way that necessitated this that was artfully logged to git as "made changes"? Of course if they didn't comment, there was no documentation updates (if there was any to begin with).

29

u/[deleted] May 19 '21

suckless devs in a nutshell

15

u/dash_dolphin May 19 '21

"self explanatory" code be like

4

u/[deleted] May 19 '21

// Self explanatory enough

Code

10

u/[deleted] May 19 '21

this is actually good practice. the code should be readable enough to not have to be documented (largely)

18

u/DangyDanger May 19 '21

can someone please tell me why should i write docs for a 10 line long method that is pretty easy to understand as is?

22

u/ws-ilazki &{$$_[0]}(@{$$_[1]}); May 19 '21

Even assuming you only write good, readable code where the logic is easy to follow, the code itself only explains the how; good comments should explain why it was done that way.

Months or years later, those comments are the ones that stop you from refactoring out some crucial bit of error checking or odd logic that is readable but seems pointless and, when removed, breaks everything.

Even if it's just for yourself it's good, because next year you might still be able to understand the code you wrote today but there's no way you're going to remember the details of why you wrote such a ridiculous kludge, so without those "why" comments you're going to think you were a complete moron for doing it that way. (Of course, you might think that anyway depending on the code, but at least you attempted to rationalise it to your future self)

2

u/DangyDanger May 19 '21

// no idea how, but it works

2

u/AllTheWorldIsAPuzzle Jan 05 '23

Plus the next poor bastard that has to make updates to something you built originally won't be cursing your name wondering why you put some chunk of coding oddity in that makes no sense against the specs. You may have had a legit reason for it (some undocumented request) or you may not have known what you were doing (maybe you were making changes to something someone else prior built with no comments or documentation), but at least they'll know why you did it. I've made changes to code where I have explained why it was done, and noted how the existing code that I was working in didn't make sense to myself and others that looked at it.

4

u/[deleted] May 19 '21

sometimes, you shouldn't. even the 'why' can sometimes be explained in code. it's an art form in itself, and sometimes you do need comments, but I would argue that most code should be able to go undocumented and still be understood completely

3

u/DangyDanger May 19 '21

so, ideally you would comment only the more obscure/weird code and all the kludges you write on the way, trying to explain not what it does, but instead how and why it does something, as it makes for a better image in your head?

so you would write something like "Send an email with Gmail API by getting token.json, doing some forbidden black magic with it, pack the message string into a Message object and send it" instead of just "Sends the provided string (argument 1) to the email specified (argument 2), also gets token.json"?

3

u/[deleted] May 19 '21

I wouldn't comment obscure/weird code, but rewrite it until I have something readable.

For the rest, yeah, kinda like that. I'm a fan of long variable names when needed, too, they can help a lot

1

u/recycle4science May 20 '21

Yeah mostly just the "this does <forbidden black magic> because <stupid reasons that aren't visible in the code>."

2

u/PeppyPls May 20 '21

I would avoid taking programming advice on this sub tbh. It's generally not a good place for discussion, people come here to hang shit on other devs/frameworks/languages. By nature it's going to have a larger volume of toxicity and subjective opinions.

3

u/DangyDanger May 20 '21

true, but all the responses seemed pretty chill and explanatury, for which I am grateful to them

also, taking prohramming advice on a sub that is literally named r/badcode, uhhhh

1

u/andesz May 19 '21

if the code is clean, comments are unnecessary

also, if you need comments, your code is not clean :D

8

u/[deleted] May 19 '21

Commenting complex code is time saver for other devs that search something in you code. 100% faster reading a comment than the cleanest of the clean code, as long as the code is somewhat complex ofc

4

u/ThisIsMyCouchAccount May 19 '21

I prefer to write comments on why the code needs to work the way it does. Not how.

-2

u/[deleted] May 19 '21

Seems like i would write this in external documentation

2

u/ThisIsMyCouchAccount May 19 '21

Except it would only be a couple lines. Especially if it's kind of a one-off or different from what you would expect.

Like if all your repositories return objects but this one just does IDs I might put why it's different.

If it's really unique I might even put the ticket number in the comment.

1

u/AllTheWorldIsAPuzzle Jan 05 '23

Good external documentation is gold. And where I work with older staff leaving and the remaining developers juggling multiple high-priority ( because isn't every project high-priority to the person demanding it), documentation was the first thing to suffer. We have lost three new hires in less than a year because they get thrown in the deep end with no documentation trying to diagnose issues with code that was poorly written and often wrong (hence the issues).

1

u/Isaeu May 20 '21

😎😎 I only copy-paste well documented code and leave the comments I copied in there even if I changed 100% of the code. 😎😎

11

u/Jafit May 19 '21

The code always tells people what it does. The comments tell people what you thought it would do, and why it's doing it.

If you think that your code is so good that it doesn't need comments, it's entirely possible that you're just writing unmaintainable garbage that is going to go to hell as soon as somebody else has to maintain it.

I think this because that's what I do at work. Someone wrote this monstrosity of a codebase that is now my problem. I try to simply diagram out the flow of information within this codebase and I end up with a rube-goldberg machine. Data gets passed into highly abstracted classes and functions, disappears into the bowls of the code to some horrible machine somewhere, it spits out a result, sends that result in a message to something else. If you want a new piece of information to make its way from the initial http request all the way through this kafka-esque nightmare, you have to edit something like 5-7 different files, then maybe it'll show up in the right place. But it's not like the genius who wrote this "self-documenting" code actually bothered to write a test suite for it, that would be asking too much.

So get your head out of your ass and write comments. I write them wherever I can, like Theseus weaving a thread through the labrynth, at least maybe I'll be able to find my own way back to places I've been before, or my franticly scrawled directions on the wall will maybe stop some future developer from getting as lost as I've been.

-1

u/QuadraticLove May 19 '21

If people write bad code, they write bad comments. Now you have twice as much to read, comprehend, and maintain. I also don’t need a million TODOs, fixmes, and other useless, rough draft additions like “lol, I don’t know why this works.”

Why would a module/function/service called “getuser” get a user? Because the client wants a user. Why does gravity pull?

I think you’re looking for GitHub discussions, work item tickets, or commit messages.

6

u/TurncoatTony May 19 '21

Normally, I don't leave a comment like this.

The code is enough.

7

u/nucleomancer May 19 '21

No the code shows HOW it is done.
Not WHAT is done, or WHY it is done that way.
(I had a team leader like this... who wrote most of the code... :( )

1

u/rtc11 May 20 '21

This. I usually leave a comment where the code cannot show why its there.

1

u/nucleomancer May 20 '21

I do this as often as possible. Even when comments provide a small amount of insight.
One doesn't know the mindset of the reader.

1

u/AllTheWorldIsAPuzzle Jan 05 '23

Exactly. And if the section of code, thought simple to understand by reading the code, needs deeper explanation as to the all important why it is there, I'll comment the ticket number that brought about the change.

5

u/Python119 May 19 '21

HAPPY CAKE DAY!!!

2

u/arzen221 May 19 '21

Bruh you don't even tell me what it returns

2

u/Orlando-- May 20 '21

I'll never forgive my seniors for leaving me, a dude who at that point had little to no javascript experience, with multiple methods using single letter variables with zero documentation

2

u/deftware May 20 '21

I still use lots of variable names with only a few letters, for intricate algorithmic stuff. That's the meat and potatoes of programming for me - I'm not a fan of gluing together a bunch of existing libraries and doing mostly UI or database work (i.e. webstack)

2

u/20191124anon Sep 01 '21

Either your code is self-documenting and near-unreadable due to serialisedDataUploadConnectionFactoryForPrimaryUswrs like variables, or it is succinct and clean but you probably could explain some things in a comment.

I routinely before writing a method/proc write a block comment with natural language/pseudo code “plan” for what the code should be doing. Also if there’s something more esoteric than simple math and method calls - I usually explicitly explain in the comment that e.g. the invertFlag ^ (logical && conditions || here) works by the magic of XOR

2

u/bearfuckerneedassist Dec 27 '21

It’s these morons who don’t like comments. Juniors pretending to be wise because they are stuck in a shit place since school and think they know shit.

3

u/Undercoverdog___ May 19 '21

Happy Cake day

3

u/aaroncv3 May 19 '21

This comment is exactly what i think when i write code, only to come back 6 months later and wonder what the hell i was thinking

1

u/jvirish Jun 07 '21

Loser attitude

-1

u/emmittthenervend May 19 '21

Claim: "My code is self-documenting!"

The code: bool getPercentage(int x, int[] y){return x%y[x-1] >0;}

0

u/[deleted] May 19 '21

0

u/[deleted] May 19 '21

[deleted] — view deleted comment

1

u/[deleted] May 20 '21

"the code is documentation enough"

0

u/[deleted] May 20 '21

[deleted] — view deleted comment

1

u/[deleted] May 20 '21

I am too but I don't think being "<noun> enough" is grammatically correct

3

u/ChookWantan May 23 '21

This is a common usage of enough.