8 ms·
Pretty simple: you use spaces because you're aware that there is more than one IDE/editor in this world and who knows what your code will get opened with tomorr
by nottorp 9y ago
Pretty simple: you use spaces because you're aware that there is more than one IDE/editor in this world and who knows what your code will get opened with tomorrow.
This means you consider consequences beyond "but it works on my machine" so you're a better programmer. Ergo, higher salary.
- sambe 9y agoI think there's probably some truth to considering existing norms and thinking ahead, but find it unlikely to be the main confounder. How do tabs fail to work in other editors/IDEs?
- peterbonney 9y agoTabs fail when my IDE is set to convert tabs to spaces and yours isn't.
- seren 9y agoAdditionnaly diff/merge tool tend to be inconsistent. So it looks good on your editor, but when I am doing your code review, alignement is totally broken. In theory, it should be easy enough to ask people to have consistent settings across all tools, but it is a bit like herding cats. Sadly most people do not even realize it could look differently without their settings. (I could ignore whitespaces when doing reviews but this is an open invitation to break everything)
- creepydata 9y agoSeriously? When you start on a project you configure your IDE to match the project's conventions. If you are in a mentoring role you instruct your jr developers to do the same, first thing, before writing any code. This has never been an issue at any place I ever worked. If a PM can't get their developers to do something as simple as that then you have larger organization problems than code formatted weirdly. As well as totally incompetent developers.
- drblast 9y agoOn a very large codebase, developed over decades, not months or years, by thousands of individual teams, using multiple versions of multiple IDE's, some of which didn't exist when the project started, across different languages, also some of which didn't exist when the project started, this is not so simple. Unless the project's settings are to use spaces.
- creepydata 9y agoI've worked on projects that have been developed over decades and there has never been an issue. We have conventions, developers are expected to follow conventions, and if they don't they are instructed go back and fix their code.
- LanceH 9y agoSpaces when tabs are used. Your point?
- Shorel 9y agoIn that case your IDE fails. Respect .editorconfig files
- nottorp 9y agoThey might be configured differently, and you usually can't align stuff properly using just tabs, you have to use a space or three somewhere. Also, you never know if it's spaces, tabs or a combination of them in existing code - which you may have not written yourself. So in the long run, the indentation WILL get fucked up and you WILL miss something when fixing a bug in code that you or someone else wrote 6 months ago. And your 10 minute bug fix will take 3 hours. Multiply both values by a constant factor as necessary. It's simply not worth the trouble.
- SippinLean 9y ago>you usually can't align stuff properly using just tabs Huh? Things indented to 1 tab will align with everything else on 1 tab. Things aligned to 2 will align with everything else at 2 tabs. >you have to use a space or three somewhere Why? Tabs will indent with other tabs. >you never know if it's spaces, tabs or a combination of them in existing code Well they should never be combined for indentation. Unless you mean using tabs for indentation and spaces for alignment, but this wouldn't create any issues. Using tabs for spacing is objectively poor form. I open space- and tab-indented files in my IDE, they are all converted to tabs. It is always consistent.
- pvinis 9y agoOh god. I also prefer tabs, but if a file has a choice of tabs or spaces, never change the whole thing to what you prefer. Always keep it as is.
- SippinLean 9y agoGood point, in my case that's just how I work on it, git magic converts it to whatever matches the repo style before it's committed.
- creepydata 9y ago>you never know if it's spaces, tabs or a combination of them in existing code - which you may have not written yourself. Um... I always know because I always set my IDEs to show those characters. You don't?
- tomsmeding 9y agoIn fact, tabs are better for compatibility: other people might like other indentation sizes. If you keep your stuff consistently with tabs, and use spaces for alignment instead of having your IDE auto-replace tabs there as well, your code will look great with whatever tab size someone chooses. And extra pro: in dumb editors, you automatically get the right thing.
- joncampbelldev 9y agounless you have anything aligned in columns then all bets are off (something which many code formatters can do automatically for you e.g. lists of bindings)
- scrollaway 9y agoTabs for indentation; spaces for alignment; I don't know why you'd want to insert tabs in the middle of a line. Also, most people who do have several years of experience under their belt will tell you: don't align. Mid-line alignment is a waste of a maintainer's time to have to update alignment blocks every time you add/remove variables... and statement-alignment is unreadable[1]. [1] https://github.com/pennersr/django-allauth/blob/a44134688f5be0a26f5e93b607f8c4e3f5b0782b/allauth/account/views.py#L685-L688 https://github.com/pennersr/django-allauth/blob/a44134688f5b...
- hn_throwaway_99 9y ago> Tabs for indentation; spaces for alignment On a large code base with multiple committers, I've never seen this rule followed consistently. In fact, I think this shows a poor engineering decision: "Let's make a rule where everything looks OK 95% of the time, except when you change tab widths (which is, presumably, why we made the rule in the first place.)"
- tomsmeding 9y agoWell everything looks OK 100% of the time if everyone followed the rule. Sadly, human history tells us that's an invalid assumption to make.
- peterbonney 9y ago^^^ Exactly. Using tabs is a decent (albeit not perfect) indicator that you've never worked under source control on a large dev team with diverse working habits.
- scrollaway 9y agoAlmost everyone I know who uses spaces or tabs does so because that's either the default on their IDE, or for their language. So this holier-than-thou argument holds less water than the sun. On the other hand, I find 2-space-indented code unreadable; in fact, 4-space wide indentation is the only size I find to be readable. I know plenty of people for whom that isn't the case. Using tabs lets the reader set the indentation level. ¯\_(ツ)_/¯ Edit: All the anti-tab arguments I'm reading below boil down to alignment. Don't align folks; there's good reasons not to align regardless of your indent style.
- nottorp 9y agoActually they all boil down to 'someone else, or another IDE that you haven't configured just right, will mess your tabs and make the code hard to read' :) They're fine for solo projects i guess, if you only ever use one editor. Edit: Also, in the long run the best thing you can do to your code is to make it easily readable. You will forget what you did. Not to mention your colleagues that have never seen it. So time spent aligning parameters to make code more readable is well spent, if you ask me.
- scrollaway 9y ago> So time spent aligning parameters to make code more readable is well spent, if you ask me. How is this[1] more readable than this[2]? [1] https://github.com/HearthSim/python-hslog/blob/5a6ad9a29a7d46672d563915ecfae74f3bfda94f/hslog/packets.py#L44-L53 https://github.com/HearthSim/python-hslog/blob/5a6ad9a29a7d4... [2] https://gist.github.com/jleclanche/ef40058731061e26d218e2a4c99099b0 https://gist.github.com/jleclanche/ef40058731061e26d218e2a4c... Let the lines get long enough and the latter is actually quite a bit harder to read; try to follow the variable all the way to its value and you might actually jump and be one line off. I deeply care about readability and code style, that's why I'm replying here :) And in all the codebases I have read and written, alignment has consistently been harmful to readability and maintenance.
- swift 9y agoWhen I hear "aligning parameters", I think more of the difference between this: someFunction(firstParam, secondParam, thirdParam, fourthParam); And this: someFunction(firstParam, secondParam, thirdParam, fourthParam); I've always found the second much easier to read, especially when there are multiple levels of nested expressions, and it's a style that has been encouraged on every project I've worked on. I agree that aligning '=' is actively harmful in the example you give, and that it's harmful in most situations where it's used. I've encountered very rare instances where it's helpful, though. Sometimes you have a sequence of values defined by expressions that follow a pattern: enum someEnum { FIRST = BASE + MASK & (1 << 0), SECOND_VALUE = BASE + MASK & (1 << 1), TH = BASE + MASK & (1 << 2) } Aligning the expressions can make it more clear what the pattern is and how it differs from value to value: enum someEnum { FIRST = BASE + MASK & (1 << 0), SECOND_VALUE = BASE + MASK & (1 << 1), TH = BASE + MASK & (1 << 2) } Often there are other ways to handle the issue, such as hiding the expression behind a macro or a function call; when that's more readable, that's the way to go. But in cases where writing the expression explicitly ends up being more readable, I find alignment to generally be a win in this case.
- SippinLean 9y agoThat's the reason I use tabs for indentation? 4 spaces will render as 4 spaces, 2 as 2. 1 tab will render to whatever the next programmer prefers, it's only courteous.
- astrodust 9y agoIt's courteous except to those that don't like tabs, so you really can't win. Many languages have a coding style preference. Go is super opinionated, Python in the middle, with C being as conflicted as it gets. Whatever you use, follow the style the language or team is a proponent of.
- ioquatix 9y agoIf you don't like tabs, converting them to spaces is trivial. You could even add git hooks to do it automatically.
- astrodust 9y agoIf it's your own personal code-base, go nuts. If you're working on a team don't be the one who does this and wrecks every file you touch.
- d0lph 9y agoPicking one or the other will be slightly discourteous to the other side, in this case this is an advantage only tabs hold.
- yAnonymous 9y agoThat's exactly my experience, too. Developers who use default settings and tools are less interested in computers. Knowing the details of a system makes a developer infinitely more valuable.