r/badcode • • Feb 22 '21

python Just started a virtual work experience, and the first task was to clone a git repo. I had a look through, and it turned out the 3 python files all used different indentation... Tabs, 4 Spaces and 2 Spaces.

Post image
740 Upvotes

46 comments sorted by

5

u/Wooden_Yesterday1718 Feb 22 '21

Is this someone else’s proprietary code??

5

u/SpicyElectrons Feb 22 '21

No it's open source. It's a public work experience program anyone can sign up to, so all the code used is available on GitHub

4

u/Wooden_Yesterday1718 Feb 22 '21

Ahh okay. Makes sense. That’s interesting. I just didn’t want you to find out the hard way not to post proprietary code that isn’t yours haha. I haven’t heard of this kind of thing, what country are you in?

4

u/SpicyElectrons Feb 22 '21

I'm in the UK but I think this company is running it worldwide (since it's virtual). It was through theforage.com, I think they have loads of things like this.

4

u/Wooden_Yesterday1718 Feb 22 '21

Sweet! Good luck with everything!

4

u/Querray Feb 22 '21

I've never heard of the term virtual work experience. Is that like an internship or smth?

2

u/SpicyElectrons Feb 22 '21

It's basically just a programming course lol.

153

u/redfournine Feb 22 '21

My place has it all. 1 tab, 2 tabs, 0, 1, 2, 3, 4, 5, 6... n spaces. The worst part is everyone uses Visual Studio, so you should get a consistent indentation for free..... unless you explicitly fights against the IDE's indentation formatting.

Why someone would do that... I have no freaking idea.

59

u/yabai90 Feb 22 '21

I'm kinda glad to have a strict eslint project rules. Might be annoying sometimes but it's just soooo much better seriously. And it just feels good

24

u/0xF013 Feb 22 '21

It’s less annoying with autofix and prettier on save, it rustles some jimmies for people that have strong opinions about some minutiae styles, though

5

u/TCOO1 Feb 22 '21

I have tried the stricter liners, and the readable code that fits so snugly in a single line gets expanded into 3 or 4 making it look messier (js)

Maybe it's just me and I need to get used to it

4

u/nikvasya Feb 22 '21

If that line is several screens wide, its not readable nor snugly.

Otherwise linter should not touch it in strict mode by default iirc.

1

u/TCOO1 Feb 22 '21

Ye, I also did not touch the config when testing it out, probably could have tweaked it a bit.

I will keep this in mind if I ever try and do it again

3

u/0xF013 Feb 22 '21

I had that too, but after two projects, one with semicolons, and another - without, I stopped caring

9

u/VoilaLaViola Feb 22 '21

Even if everyone goes with their VS setting, it can be different. You need to sync your VS settings (tools -> optioms -> text editor -> <your language> -> Tabs

9

u/Johanno1 Feb 22 '21

Your company should use a format check before pushing into the repository.

2

u/Franks2000inchTV Mar 09 '21

Black / blackd is great.

2

u/[deleted] Feb 22 '21

It amazes me how someone can program for so long to be considered skillful enough to get a job and still not learn proper indentation. How does the code even run?

3

u/recycle4science Feb 23 '21

"Proper" indentation is a matter of opinion, as long as it's consistent within a project. So, one person can get used to 2 spaces and another 4, when they're working alone. The problem is when they start working together and there's no style being applied consistently.

0

u/[deleted] Feb 23 '21

Oh I see now. I honestly didn't know that code even runs if you don't use spaces between lines using the tab key

1

u/[deleted] Feb 23 '21

Use linters that autofix in combination with prettier on precommit with husky. This is how we keep a really consistent way of writing code throughout our projects.

36

u/[deleted] Feb 22 '21

Also, not only using camelCase for methods. But combining it with snake_case

6

u/slaymaker1907 Feb 23 '21

I think python requires test_ for something to be considered a test case by default. Also, it sort of makes sense since test_ distinguishes that it is a test of some particular thing. Is testDentist a method which returns a special test Dentist object or is it a test of the Dentist object? If you always use test_* for tests, there is no ambiguity.

1

u/[deleted] Feb 23 '21

You should use snake_case for methods in python

4

u/slaymaker1907 Feb 23 '21

That's just a convention and your comment does nothing to address the arguments in my post.

Following conventions is important for readability, but it is certainly possible to follow different ones if that works for your org/project.

2

u/[deleted] Feb 23 '21

Jesus. He uses snake case after that and not following that convention is considered bad code in python

5

u/the-computer-guy Feb 22 '21

At my work we have files with both spaces and tabs in them lol

24

u/DietoKill Feb 22 '21

I understand the indentation annoyance when it's given attention, but I fail to see how it would be "bad code", not ideal, but definitely not bad code.

7

u/recycle4science Feb 23 '21

Inconsistent style can make it harder to spot logic errors, I've experienced it personally. Granted, it was a block that wasn't indented at all...

3

u/CaydendW Feb 22 '21

3 spaces is best.

2

u/jeetelongname Feb 22 '21

There is a program called editorconfig and it smooths out all of this. If you keep a file in the root of your project and install the plugin then your editor will always follow the rules. Its pretty great

1

u/nautilicdive Feb 22 '21

Not necessarily bad code, but more like a bad practice for not having a linter and not checking it as part of the PR approval criteria.

1

u/Shadow_Being Feb 23 '21

what is a vitual work experience?

1

u/RyanNerd shameless Feb 23 '21

In what sane universe would whitespace impact logic? Oh... Wait...

1

u/toastyghost Feb 25 '21

Different naming conventions, too. Blech.

1

u/[deleted] Feb 22 '21

Yikes. How would you fix this? As a beginner, my guess is to write another program that can take text and identify (with regex) new lines, delete any spaces after the new line, and then replace it with a tab.

5

u/nuuren Feb 22 '21

My approach would be to just use a linter. For instance you can set up “black”, and let it run throughout your code. It’ll fix everything for you, as opposed to others like flake8 which will just warn you (not sure if it has an option to also fix them, though) But in general, that other program you’d write is called a “linter”

1

u/[deleted] Feb 22 '21

Rad! Thanks

0

u/[deleted] Feb 22 '21

[deleted]

9

u/acidrain42 Feb 22 '21

We use black at work, with a pre-commit hook that will take care of the formatting, and a check in the CI that will fail if formatting is not ok.

Use whatever IDE you want, mix tabs and spaces while coding, it doesn't matter as it will all be formatted automatically. It also settles any argument, just use what black does.

In the end, it's the best of both worlds: consistent formatting and nobody wastes time applying formatting / arguing about it.

Edit: This tool is python specific, but the same applies to basically any language, ex: go fmt, cargo fmt, eslint...

2

u/sharju Feb 23 '21

Same here. It makes life so easy. With linters you still can slip in all kinds of stupid shit with #noqa or whatever your checker uses for skipping and then you have to bitch about those in pr reviews. Even without the commit hook it's so simple to use Black that everybody agreeds.

8

u/binarycat64 Feb 22 '21

I think that tabs are better than spaces, but much more important is consistency. Id rather work in a file that is all spaces than a file that is half tabs half spaces any day.

0

u/[deleted] Feb 22 '21

[deleted]

3

u/jarfil Feb 22 '21 edited Dec 02 '23

CENSORED

3

u/binarycat64 Feb 22 '21

things start to break when you write a multi-line code.

they shouldn't though? unless you try to do some fancy alignment, but you shouldn't do that.

3

u/[deleted] Feb 22 '21 edited Sep 27 '24

close books strong soup square cable cobweb axiomatic bedroom worthless

This post was mass deleted and anonymized with Redact