12 ms·
... and then people just "solve" the problem by not indenting at all. Seriously though, it's super hard to avoid deeply-indented code in some languages (cough
by dheera 10y ago
... and then people just "solve" the problem by not indenting at all.
Seriously though, it's super hard to avoid deeply-indented code in some languages (cough Java, JavaScript).
@Override
protected void onDestroy() {
super.onDestroy();
if(someCrap != null) {
PendingResult<MessageApi.SendMessageResult> pending = Wearable.MessageApi.sendMessage(mGoogleApiClient, mPhoneNode.getId(), path, data);
pending.setResultCallback(new ResultCallback<MessageApi.SendMessageResult>() {
@Override
public void onResult(MessageApi.SendMessageResult result) {
runOnUiThread(new Runnable() {
....
- lugg 10y agoNot if you auto format in CI. Though that could be dangerous. Although you could just have a build step that waits for a human if the formatted doesn't match submitted.
- Groxx 10y agoretrolambda will get rid of some of those :) But generally, if the code is too onerous to modify due to e.g. excessive use of closure-captured data, then the code usually deserves a long, hard ಠ_ಠ to make sure it doesn't deserve a (╯°□°)╯︵ ┻━┻. Hard to test, hard to fix, hard to control.
- malikNF 10y agoI like to think of plugins like this one playing a sort of "assistant" role rather than a "benevolent dictator" kind of role. So you don't have to end up making ugly hacks to just make it work.
- TheCoreh 10y agoasync/await has greatly reduced the need for indentation in JavaScript
- ismail-khan 10y agoAnd Promises too!
- colordrops 10y agoEven without async/await, if you properly organize code in bite size functional blocks and use other asynchronous constructs then the indentation should be manageable.
- programmarchy 10y agoSmall improvement, but this can help: if(someCrap == null) { return; } I like that Swift makes this more explicit with the guard statement, but I've found it useful to reduce the "mental stack" in other languages, too.
- adrianN 10y agoNot everybody is allowed to do that. MISRA for example requires a single exit point.
- rjzzleep 10y agoThat's 14.7, but if for some reason 14.7 doesn't apply 16.8 requires that all exit points return a value. So I'd argue MISRA lets you do that.
- programmarchy 10y agoReading up on MISRA, I found an interesting defense of a single return statement [1]. In the embedded world, I imagine it would be a good standard to force people to consider refactoring functions to their truth table. Generally, I strongly favor coding standards to combat bad coding habits. Curious how MISRA works out in practice. [1] https://spin.atomicobject.com/2011/07/26/in-defence-of-misra/ https://spin.atomicobject.com/2011/07/26/in-defence-of-misra...
- lmm 10y ago> That’s a lot simpler isn’t it? WTF?? No it isn't. You have this magic initial value and have to figure out which code paths have mutated it and which haven't. Mutable variables make code far far more complex to read. I'm all for single return but through the use of expressions, not variables; I would probably express the given function as: expression() { return a ? (b && c) ? 2 : 1 : 0 ; } (Ideally I'd use a language that allowed a sensible conditional expression rather than one that relies on bizarre symbols, but we work with what's available)
- platz 10y ago"Bulldozer code" - gives the appearance of refactoring by breaking out chunks into subroutines, but that are impossible to reuse in another context (very high cohesion)
- golergka 10y agoSubroutine that is only used in one place still improves readability: instead of a bunch of statements, you have a name, explicit parameter list and a retugn value.
- platz 10y agoit's not when applied once, it's when something that can be understood more easily "locally" is exploded into a billion such pieces - then you have to mentally find all the call sites and usages to mentally build the call graph back up — you still have to understand how the whole ensemble works and bulldozing can (but not always) make understanding the ensemble more difficult especially if done by a poor programmer has the effect of obfuscation rather than clarification.
- patmcguire 10y agoIt's good when it gives a name to what you're doing. connection = establish_connection_given_these_conditions(cond1, cond2) Vs the many lines of checking, creating the ConnectionFactory, passing in the the conditions to the ConnectionConditionFactory, and generally having a new statement every time you want to blow your nose. What were we doing again? I dunno. I've used it. I'll defend it. It's better than comments when done properly as above.
- vog 10y agoIndeed! More generally, functions are not only about abstraction, but also about decomposition. The only thing that may hurt readability is when the decomposition itself is too much nested. Usually, it's best to split a large task purely linearly in subtasks (functions), and have them called all from a single function. That main task function gives you not only an overview of all sub tasks, but also about the dataflow between them. In constrast, too much subtask nesting (subtasks of subtasks of subtasks) often hide the overall flow, which then makes it hard to get the overall view.
- golergka 10y agoPromises?
- logicallee 10y agoBut for example your indent if(someCrap != null) { followed by indentation on the following block, would be turned by the programmer into something like: if(someCrap == null) return; //nothing to do without an indent following. thereby saving the indent and having to have the reader keep track of another level. How many times have you seen: } } } } And is it really that great? Breaking it into line by line is actually easier to follow than nested conditions. (You don't have to worry about proper indentation or matching braces properly, it's simply easier to write.) I think it's easier to read as well.
- dheera 10y agoI agree with you if that's all it were. But sometimes it's if(someCrap == null) { try { CrapSurface mCrapSurface = CrapGenerator.generateSomeCrap(CrapGenerator.CRAPPY, new CrapFactory.CrapOptions(new CrapOptionsCallBack() { @Override int getHeight() { return 5; } @Override int getWidth() { return 9; } })); someCrap = CrapSurface.getCrap(); } catch (SomeException e) { try { someOtherFailSafeButInefficientWayToGenerateTheCrap(); } catch (WeirdExceptionThatWillSeriouslyNeverHappenButStupidJavaRequiresMeToHaveATryCatchClauseAnyway f) { // do nothing because this exception will never happen and if it happens the user deserves it } } } else { ... the other code I had before ... }
- rootlocus 10y agoif(someCrap == null) { someCrap = makeNewCrap(); } That's more readable and less indented. It's also self documenting. } catch (WeirdExceptionThatWillSeriouslyNeverHappenButStupidJavaRequiresMeToHaveATryCatchClauseAnyway f) { // do nothing because this exception will never happen and if it happens the user deserves it } Nice joke, don't do this. I don't believe it's the user's fault, and even so either print the stack trace or name the exception `ignored`. It's not that hard to pipe all ignored exceptions in a single catch. Also, somebody made the conscious decision of making that exception checked, which means it's the implementor's fault for forcing you to catch it. If you want, you can use lombok's `@SneakyThrows`.
- rootlocus 10y agoIt's only "super hard" if you insist on anonymous classes. Reversing the `someCrap != null` condition and using an inner class reduces the identation count down to 2 (and that's in only one place for the return statement in the inverted null condition). Also, consider using static imports for `Wearable.MessageApi.sendMessage` and `MessageApi.SendMessageResult`.
- tokenizerrr 10y agoI really don't like static imports for stuff like that. You always have to do a double take to recognize where the method comes from.
- rootlocus 10y agoPersonally I've never had this problem. The context was always sufficient, and a simple IDE shortcut can offer me any additional information. I would argue a double take is worth reducing the number of dots I have to follow when reading the code. Also, in the case of `Wearable.MessageApi.sendMessage`, you can statically import `MessageApi.sendMessage` which gives a little more context.
- barrkel 10y agoI really like being able to read code in source control historical diffs, pull requests, etc. IDE shortcuts aren't a full solution; not everyone has time to check out a branch at that commit, create a project (if necessary), fire up an IDE (if there's a UI available) just to understand a line of code. Similarly, static imports at the top of the file are usually remote from the point of use, so you lose that context when viewing a diff.
- chukye 10y agoNo, it's not hard to avoid, actually it's pretty easy to avoid if you understand what you are doing.
- bsder 10y agoTime for my apocryphal story: I needed to a fix a particular kernel protocol subsystem so I could use Linux at work. I sat down with the spec and a non-Linux computer that implemented it. I spent 8 weeks (I thought it would take a couple days--HAH!), and I wrote it and wrote the very nice test suite which included the single test case I couldn't handle (handling would have required major changes to the kernel). It's a protocol. The word protocol means "state machine". Especially when hardware dudes design something. However, state machines mean "indentation". One level for current state, one level (at least) for output variables, and a couple of levels for input variables. My code was well documented, tested, and easily changeable if you needed to add a new state. "But, that's more than two levels of indentation. Rewrite it.". After I pulled my jaw up off the floor, swore a couple of times, I replied: "No. This code is properly written, well-tested, and very amenable to changes when the spec gets updated. This code does what I need and what a bunch of people need. I will happily distribute it to them and you will have a bunch of users of this subsystem happily telling everybody to not use mainline because they are a bunch of tossers. If you wish to rewrite it to your indentation spec, feel free, but I will not sign off until it passes all of the tests." So, they did. They decided to rewrite it to remove the indentation. Lots of early returns (full of bugs). State cases that got "simplified" by sharing common code (full of uninitialized variable bugs). And, because of all the sharing, it became difficult to add cases when the spec changed (which it did--regularly). But, eventually they got their two levels of 8 character indent. After SIXTEEN WEEKS. My question, of course, was "Why not just pull each state into a function? It was the obvious way to get what you want. It would have been much easier." The response: "Well, we tried that. But there are so many variables per state, that the function calls were all exceeding an 80-character line length and we had to wrap them too much." I had a bruise on my forehead that day. Yeah, formatting rules exist and have good reasons. But please remember that the goal is "code quality and readability"--not formatting rules.
- weitzj 10y agoMy questions I ask myself every time to keep indentation to a minimum: - Is there a way to use an early return? (In your case early return if == null) - How can I get around using an `else` ? And I am happy this works so well. Of course there are sometimes if/elseif constructs but exclusive if/else is rare.