r/badcode • • Aug 21 '20

js moment.js: modern problems require modern solutions.

Post image
688 Upvotes

36 comments sorted by

157

u/lesleh Aug 21 '20

Must be there for locales to override as necessary, would be my guess?

69

u/[deleted] Aug 22 '20

Yep, this is good code.

I’m not familiar with JavaScript, or this library/framework, but it’s obviously a stub method meant to be overridden by a subclass. Open for extension, closed for modification.

What does this mean?

In layman’s terms you can modify the code in that method without changing it.

Huh?

You create a class based on this class. A subclass that extends/overrides it. Then in your new class you override the stub method with your own.

In the main logic this stub method will be called pre something, thus it’s name.

It’s open for extension, and closed for modification.

Had to write this because it’s getting upvotes in r/badcode, so people obviously don’t understand the basics. : (

2

u/Nameyo Sep 06 '20

If this is a stub method, it could be improved upon to make it more obvious it's a stub method. Add a console.log("Stub method called") or something along those lines, instead of just dropping a meaningless return. If I forget to override the method, the API should tell me.

1

u/[deleted] Sep 07 '20 edited Sep 07 '20

I don’t know the language, nor the framework, and yet it is clear to me it is a stub method. That’s because it doesn’t do anything.

In this case you don’t have to override the method, it’s optional.

As you correctly suggested if it were required it would be standard practice to throw an error like. “stubMethod not overriden” as the entire content of the method. As you correctly pointed out that would make it obvious it was missed.

But, in this case it can be overridden, optional, if requirements require it. If for example you wanted to do something with the string before it was parsed, as per the method name preParsePostFormat().

E.g if I saw a method that does nothing called fly() on a class called Animal I’d know it was optional, a stub that could be overwritten, and I’d expect to see walk() and swim() too, also as stubs.

29

u/wooptyd00 Aug 21 '20

Came here to post this.

61

u/davvblack Aug 21 '20

came here to pre parse post format this.

11

u/keaton_fu Aug 22 '20

Indeed. In its base functionality, a locale can implement two functions: preparse and postFormat (src)

Example of locale ar-SA implementing these functions: src

112

u/[deleted] Aug 21 '20

Probably a placeholder for the future funcionality.

51

u/geesuth Aug 21 '20

Yeah. That's

29

u/[deleted] Aug 21 '20

It?

24

u/glydy Aug 21 '20

Yeah. That's

11

u/sezirblue Aug 21 '20

What I was going to say?

35

u/[deleted] Aug 22 '20

Highly unlikely to be stupid — both of the options already presented here (a placeholder, and leaving something there to override in a locale) are likely candidates for what we are seeing here, and neither one is a stupid thing to do.

1

u/galexj9 Aug 22 '20

I'm a novice programmer so forgive the question. But don't languages usually have a way to denote default methods that are intended to be overridden? Like a Java abstract?

4

u/maxslayer44 Aug 22 '20

In JavaScript there's no abstract or interfaces unfortunately

11

u/haikusbot Aug 22 '20

In javascript there's no

Abstract or interfaces

Unfortunately

- maxslayer44


I detect haikus. Sometimes, successfully. | Learn more about me

Opt out of replies: "haikusbot opt out" | Delete my comment: "haikusbot delete"

5

u/lightswitchr Aug 22 '20

You know, I kinda like that one.

1

u/[deleted] Aug 22 '20

This provides default behavior so that you don't have to override it when you don't need to.

Also, JavaScript (to the best of my knowledge) has no built-in equivalent of an abstract method - in fact, in js you're not manipulating classes that inherit from each other but prototypes. You can achieve class-like behavior with them, but fundamentally they are a different approach.

16

u/malstank Aug 22 '20

This isn’t bad code. It’s an extension hook.

10

u/[deleted] Aug 22 '20

Another possibility besides the suggested options is that maybe this actually did something at some point, but the actual body of the function was removed because it was causing issues. Not good practice, but doubt that someone would do this just for shits and giggles. Especially in a library like moment.js

4

u/TheUnlocked Aug 22 '20

This is very reasonable code. They know that at some point in the future it may be necessary to do formatting on certain strings, so instead of just putting it off for later, they've made a function which will let them make any necessary changes in one place later.

4

u/timgfx Aug 22 '20

This is not bad at all. If you ever have to change the format you will only have to change one function.

It’s pretty much equivalent to making abstractions over third part modules; it allows you to change code very efficiently

6

u/[deleted] Aug 22 '20

OP you're an idiot.

1

u/lesleh Aug 25 '20

Why insult when you can educate?

5

u/DoobyMcFoosen Aug 21 '20

I found my new wallpaper.

3

u/[deleted] Aug 21 '20

Modern solutions demand modern problems.

-2

u/the_d3f4ult Aug 22 '20

well yes, all js is bad code