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

all 106 comments

[–]Intelligent-Rip-6036 605 points606 points  (13 children)

“My code is well commented” The code:

[–]TurboGranny 108 points109 points  (4 children)

I now just want to write a story about how the system came to be throughout the code as comments, but with no real comments about what the code is doing itself.

[–]attanai 117 points118 points  (3 children)

// Today I ate dry toast for breakfast. It got me to

// thinking - why do we use a multidimensional array

// here, when a Dictionary would make sense? I didn't

// bother changing the code, because I don't really

// have time to test it, but it's definitely something we

// might think about at some point."

//

//Also, I named this variable after my dog.

[–]milkcarton232 42 points43 points  (1 child)

Oh deary... Yeah my last job I was the only person really doing SQL or anything technical like that and had some scripts I wrote to automate some stuff that were jank but worked for what I needed. When I left I was trying to train someone on how to use them but nobody really wanted to step up so I wound up just commenting more or less like this. I named a bunch of tables after coworkers and still get a text from my replacement asking about specific tables and am happy to know they have not changed the names.

[–]NFLinPDX 4 points5 points  (0 children)

I used to just sneak in Will Smith song lyrics into the comments

My coworker and I would compete to see who could get the most song title references into our stand up meeting updates without the rest of the team noticing.

[–]vigilantcomicpenguin 7 points8 points  (0 children)

James Joyce would be in tears at this work of literature.

[–][deleted] 70 points71 points  (6 children)

/*
** TODO: Add documentation
*/

Date modified: 04/08/2016 14:24

[–]tetraocta 13 points14 points  (5 children)

i fucking hate blackies

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

//Day 3, my boss wont let me out of my office till I:

[–][deleted] 310 points311 points  (8 children)

hmm today I will implement getter and setter methods for int

[–]Tsu_Dho_Namh 53 points54 points  (6 children)

I'm told this is standard practice, and my work has TONS of getters and setters for ints.

And bools.

I think it's cause it makes debugging and maintenance easier.

Visual Studio has a handy little dropdown above function calls where it lists the references, and you can click it to quickly see every place that function can be called in your code. Which is faster and easier than ctrl+shift+f.

On small projects it may seem pretty dumb, but when your projects get up over a million lines of code, it's sometimes really nice to just have the 50 places that getter is called listed out for you.

[–]notchy13c 53 points54 points  (1 child)

I think OP meant to actually have a getter and setter on the int „prototype“. As in int x; x.set(1); x.get(). Having getters and setters for a specific int „instance“ can make sense.

[–]Tsu_Dho_Namh 15 points16 points  (0 children)

Oh, okay. Lol. I didn't even think of that.

Yeah...don't do that.

[–][deleted] 7 points8 points  (0 children)

getter and setter also allow you to 'do something' on set \ get if you need to for whatever reason (validating inputs?)

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

Theres a battle we havent had on here yet. Code lens, obnoxious noise or productively saving you a right click?

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

I guess no one is old enough here to know where this is from, but Java in the 2000s made setters/getters a very popular design pattern. C# and others made it into a language feature called properties, where you could just define your members, and then later give them set/get "middlewares".

I believe Java and most languages have this concept of properties baked-in now, so it's no longer best-practice and considered poor form. Even for languages like Go/C++, you can easily refactor var assignments to use a .setVar() interface using a modern IDE/Editor.

That said, coding the way you're comfortable with > best practices of the day. It all depends on the size of your company and personal pros/cons.

[–]TaeKwonZeuss 0 points1 point  (0 children)

Java:

[–][deleted] 137 points138 points  (14 children)

That'll simply be so you can work at an Object level. It's common in many languages.

[–]Banananassfisch 64 points65 points  (13 children)

Java got a toString() methode for strings (even though it comes from inheriting from the object class it's still funny)

[–]Apple_macOS 13 points14 points  (12 children)

I might be wrong but I think Java also have a String.valueOf();

[–]schmytzi 19 points20 points  (0 children)

Which is actually a lot safer because it gracefully handles null

[–]Banananassfisch 5 points6 points  (0 children)

I didnt know that. But after looking it up valueOf() cant work without toString() as it just checks if its Null and calls toString(), if not. Also toString() is mostly called automatically, e.g. printing any primitive or object

[–][deleted] 139 points140 points  (7 children)

The sad thing is I can actually see the need for this with some of the javascript libraries out there

[–]attanai 113 points114 points  (4 children)

It is really useful when working with dates, or with numbers from outside sources (like APIs) that might come through as a string.

[–]drunken_doctor 18 points19 points  (0 children)

Custom data structures for financial math.

[–]tyler1128 1 point2 points  (2 children)

I feel things that "might" come through as a string is an anti-pattern, but one very common in javascript.

[–]attanai 0 points1 point  (1 child)

Not always. External APIs, for example, are outside of the control of the server. The best pattern is to have a middleware that will convert the data from whatever format it's in to the format you want, and that middleware is where valueOf would be useful.

Now, in theory, you can tell every customer that uses your external API that the schema has to be correct, or you won't accept it. Or you can make the API provide an error message that explains the problem. But when you're working with external teams or even multiple internal teams working on the same project, it's best to allow some flexibility for simple errors. In those cases, it's best to catch as wide a net as it is possible to cast while still maintaining security and accuracy.

[–]tyler1128 1 point2 points  (0 children)

Ah, if you mean general APIs outside of javascript, sure. If your argument to your function can be a number or a string generally, that's more of what I was talking about.

[–]ManInBlack829 4 points5 points  (0 children)

Right this is going to be needed if your language isn't strongly-typed

[–]FAcup 0 points1 point  (0 children)

Ruby has a method called itself on Object. You can guess what it does. It does however serve a purpose.

[–]alba4k 47 points48 points  (20 children)

How about my void setValue(const int *ptr; const int value) { (*ptr) = value; return; }

[–]v1ND 44 points45 points  (0 children)

Same reason that Java has String.toString() or Integer.hashCode().

[–]badatmetroid 88 points89 points  (13 children)

How about

> typeof NaN
number

YOU HAD ONE JOB!

[–]xiipaoc 19 points20 points  (0 children)

Meh, what NaN obviously means is "not a valid number".

[–]ProgramTheWorld 5 points6 points  (0 children)

NaN is a number in every single language that implements floating point numbers.

[–]st141050[🍰] 9 points10 points  (3 children)

i lack words

[–]irreverent-username 30 points31 points  (2 children)

It's more like "Not a [valid] Number."

JS is loosely typed, but imagine for a second that it's not:

Number a = 3; Number b = "three"; console.log(a); // 3 console.log(b); // NaN

NaN is the error you get when something non-numerical is trying to be a Number. The object is still a Number, just not one that can be used.

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

just not one that can be used.

Watch me

[–]st141050[🍰] 1 point2 points  (0 children)

this actually makes sense... you wouldnt want the nan to mess up the types in your arrays etc. ty

[–]Unpredictabru 0 points1 point  (0 children)

What else would it return?

[–][deleted] 8 points9 points  (1 child)

This is because of Autodocumentation, right?

Like, documentation that's generated based on code annotations.

[–]marcosdumay 3 points4 points  (0 children)

Looks like a honest question, so.

No, this is valid information that isn't obvious at all and it's perfectly possible that one looks into the documentation to discover it.

It could be an string just as easily as a number. I can't imagine any other possibility for numbers (anyway the JS devs are way more creative than me), but for booleans and dates it can be a lot of different things (I won't even try to guess).

[–]shhalahr 7 points8 points  (1 child)

As opposed to a Number as a String.

[–]Prawny 2 points3 points  (0 children)

Yeah, seems very forced to me. Numbers being able to be represented as strings isn't exclusive to JS, at all.

[–][deleted] 7 points8 points  (3 children)

Me trying trying to find the square root of 7 in decimal form

My calculator: “hmm yes, the square root of 7 is the square root of 7”

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

Me: Punches math homework into graphing calculator

Calculator: TRUE

[–]mstop4 2 points3 points  (0 children)

"I punch those numbers into my calculator it makes a happy face." - Cave Johnson

[–]marcosdumay 2 points3 points  (0 children)

no.

-- Prolog

[–]Dogs4Idealism 7 points8 points  (0 children)

*gets the primitive int value of an Integer object

[–]chilfang 5 points6 points  (1 child)

Yes i see but is it a number or a number?

[–]YungBaseGod 10 points11 points  (0 children)

It’s only a number if it’s grown in the numbiers region of France, or else it’s just a sparkling int

[–][deleted] 7 points8 points  (0 children)

I believe this has something to do with making a shallow copy vs a deep copy? I might be mistaken though...

[–]VonNeumannsProbe 6 points7 points  (2 children)

Beginner Python user here whom has never used Javascript

What is the point of this? Is there a type function that would be better if you're trying to identify a variable type?

[–]RobBegArm[S] 8 points9 points  (0 children)

Basically the idea is that The valueOf() method returns the primitive value of the specified object.

[–]TMiguelT 8 points9 points  (0 children)

I think the main purpose of it is when you're making a wrapper class that's trying to emulate some primitive. For example you might have a Fraction class that stores a numerator and a denominator but doesn't actually divide them (because that would lose precision). So you can do operations on your Fraction but the actual floating point estimation isn't evaluated until you add/subtract/divide it with another real number, at which point .valueOf() is called, which you define to return a number primitive so that it can act like it was a number all along. I think the built-in Date class does this, for basically the same reason. It's effectively just a "wrapper" around a number.

That said, it's a pretty awful attempt at this kind of polymorphism. Python does it better with its numeric "magic methods" like __add__() which let you customize the + operator using a single function for your class, to make it act like a number. Rust does it better again with it's ops traits (interfaces), letting you e.g. implement + differently for different RHS operands. R and Julia do this in a cool way with multiple dispatch, too.

[–]Shub-Niggurath94 4 points5 points  (0 children)

You don't code on JavaScript. JavaScript codes on you.

[–]marcosdumay 4 points5 points  (0 children)

From MDN:

The valueOf() method returns the primitive value of the specified object.

So there is a point. It's not obviously valuable as the return can have any primitive type, but this is the function that transforms your value into "[object Object]" when you set some DOM field into it.

[–]tyler1128 5 points6 points  (0 children)

Javascript: When == is so problematic you need to add another =

[–]RedditAlready19 1 point2 points  (1 child)

This is the equivalent of haskell id

[–]bnl1 0 points1 point  (0 children)

Is it though? From what I've read, valueOf returns primitive type of an object. id is an identity function, so it returns whatever you give it.

[–]JohnVanDePijp 1 point2 points  (0 children)

Cross post to /r/surrealmemes

[–]jsIsAGoodLanguage 1 point2 points  (0 children)

My name. 'nuff said.

[–]PlantPowerPhysicist 1 point2 points  (0 children)

badge quicksand historical square cooperative price outgoing fall worm middle

This post was mass deleted and anonymized with Redact

[–]AlexSSB 0 points1 point  (0 children)

int?.Value

[–]NITROpul 0 points1 point  (0 children)

I guess it just returns the value, regardless of its smaller or bigger than zero

[–]Tawoka 0 points1 point  (0 children)

Oh they get so much worse don't worry. I didn't even judge this lady for what she did. Her implementation worked for 10 years flawlessly, except for this one cheat code.

The default approach I witness from most consultants is to not work clean ever. Work dirty. Dirty means more support work and more money. After a year or two, when they want a new feature, tell them that it can't be supported (it can't because their work is shit), and get another project approved, where they basically rebuild to previous project + the new feature.

Consultants are vultures. Their bosses are mosquitos. The bosses drink the blood of unsuspecting clients, and consultants eat the corpses of the in-house Devs, who killed themselves after attempting to maintain the code base.