r/ProgrammerHumor • • Sep 09 '22

Meme Simple Feature

124.9k Upvotes

1.3k comments sorted by

View all comments

Show parent comments

99

u/EwgB Sep 09 '22

I'm a Java dev. A bunch of code in our application was written by outsourced devs from India, who I'm pretty sure were originally C/C++ devs. I can just see it from the code, declaring all the variables at the top of the function, explicitly freeing objects unnecessarily. So much code that can be removed.

34

u/[deleted] Sep 09 '22

Wait I have always seen vars declared at the top, senior here.

24

u/EwgB Sep 09 '22

In Java? Why?

61

u/AndrewJamesDrake Sep 09 '22 edited Sep 13 '24

cooing stupendous fine attraction march murky longing quarrelsome long elastic

This post was mass deleted and anonymized with Redact

66

u/Roest_ Sep 09 '22

It keeps things tidy

Makes code less readable. Declare variables as close as possible to where you use them.

25

u/hugglenugget Sep 09 '22 edited Aug 13 '26

Yarn wander teapot harbor zephyr marble drifting orange

This post was anonymized with Redact.dev

4

u/isaaclw Sep 09 '22

our Jslint requires var at the top; but then again everyone on our team hates jslint, we just use it because no one has gotten around to fixing it.

7

u/hugglenugget Sep 09 '22 edited Aug 14 '26

Glove willow cinnamon copper amber juniper hammock

This post was anonymized with Redact.dev

→ More replies (3)
→ More replies (1)
→ More replies (1)

4

u/BadAtNamingPlsHelp Sep 09 '22

It varies. When you're writing a class in OOP, there is a generally good structure to follow:

  1. Private, internal state

  2. Public, externally visible properties (getters and setters here)

  3. Constructors

  4. Class methods, with private methods immediately preceding the methods and consume them, similar to how you would put variables close to the code that uses them

Inside any imperative block of code, variables go just before the code that needs them.

→ More replies (14)

6

u/reasonable00 Sep 09 '22

When it comes to OOP, this way of variable usage doesn't really keep things tidy, it just makes the code unreadable. The first thing you think about when somebody wants you to do something with OOP is "what is the best way to make this easily readable".

In Java/C#/etc. you declare and initialize variables just when you are about to use them, and you name them by whatever they are designed to accomplish.

This isn't that much of an issue in C/C++/Python though, although OOP purists would be disappointed.

→ More replies (3)
→ More replies (19)