Unindented Code Cannot Possibly Work!


Published on

Presented at the DC Agile Software Engineering Conference 2012

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • June 4, 1996 was the maiden flight of the unmanned Ariane 5 rocket built by the European Space Agency.Destined to propel Europe to the top of the commercial space business.It cost $7B to develop Ariane 5 and $500M for the rocket and cargo. The software was based on Ariane 4, although there were a few differences between the two rockets.
  • Ariane 5 had redundant Inertial Reference Systems which communicated with redundant On-Board Computers which controlled the boosters and the main engine.The Ariane 4 required a pre-flight alignment.Ariane 5 had no such requirement, but it was included in the software.The trajectory of Ariane 5 differed from Ariane 4; it required a 64-bit float instead of a 16-bit int. The alignment function produced an operand error.Both [redundant] inertial reference systems shut down and dumped diagnostic data.Diagnostic data was interpreted as flight data; the onboard computers deflected the engines to compensate.37 seconds later, aerodynamic loads separated the solid rocket boosters from the main stage.
  • Oh crap.Thankfully, something worked – Ariane 5 self-destructed when the rocket tore itself apart.Our software failures may not result in such a dramatic consequence. Maybe no one’s going to die, but it might cost some money… I still want everyone to write good software.3/3
  • I’m Ed Buhain, I’m a Microsoft developer. I held the Microsoft Certified Solutions Developer and Microsoft Certified Professional Developer Enterprise Applications Developer certifications and I’m working on my MCSD 2. I’m a Certified Scrum Professional and a PMP.And this is…
  • Jeff Prosise is a popular trainer for technologies on the Microsoft Windows platform. I first saw him present 10 years ago at a local user group event much like this one.Afew years ago Jeff presented at a national conference to a standing room-only, don't tell the fire marshal kind of crowd. He was showing off some new but probably now-obsolete technology on the Burton (snowboard) website and typing code into a text editor. And you've probably seen the results of hurried typing and this was in the days before our IDEs formatted everything perfectly for us. But he before he ran anything, he stopped and he said (maybe facetiously) -- "I'm going to clean this up real quick because we all know that unindented code cannot possibly work.”That was the inspiration for this presentation.(2/5)
  • How many of you have seen Hell's Kitchen?Have you seen Gordon Ramsay get mad? Sometimes he knows that his chefs are perfectly capable of cooking, but they get too caught up in the hectic environment to pay attention to quality. He’s looking for people that care enough about their work product that they won’t send up poor-quality food.I get a little upset when I see developers that aren’t doing their best work.Today we've been talking all about Agile engineering and how to make our processes better. We all share the same goal: we want to build great software.But just because we do TDD and we have checks and balances like CI servers doesn’t mean that we have good code. It’s up to us to be good developers.
  • When we talk about building good software, we sometimes talk about best practices, guidelines like the SOLID principles of object-oriented design. S - single responsibility - objects should do only one thingO - open/closed principle - entities should be open for extension, closed for modificationL - Liskov substitution - objects should be replaceable with subtypes without altering correctnessI - interface segregation - many client-specific interfaces are better than one general-purpose interfaceD - dependency inversion - depend upon abstractions, not concretions We also talk about code smells - symptoms that may indicate deeper problems. The Ariane 5 code was a copy-paste job. The code that failed wasn't even necessary code. This is why we follow best practices like TDD; this is why we avoid code smells.3/8
  • You hear a lot about software craftsmanship. It's interesting that we choose that word: we're being judged by much of the same criteria we'd use to judge other things, perhaps fine woodworking.  With woodworking, we might look at the joinery. Dovetail joints work well; they work much better than what I have on my Ikea furniture -- dowel-and-luck construction.It’s more expensive because it takes a lot more effort to make that joint, but I think you get a much higher quality product.We’re going to judge the materials, how well they’re put together. We're going to judge how good something is by how well it looks, how well it works.We’re also going to judge how something fits with its surroundings. We expect to see nice furniture in a nice house.1/9
  • What if we were in a really nice house in a nice neighborhood and we found this? What if we were in a really bad neighborhood and we found a really nice kitchen?Wouldn’t that be kind of weird? We want the rest of the house to match the quality of the kitchen. Otherwise, something just seems off.The same goes for our software.0.5/9.5
  • From this one moment, from this one quote -- what was I able to glean about how well Jeff Prosise's software works?How many of you have seen a presentation with a demo that didn't work?I measure presentation quality based on attendee retention.His presentation stood out, though, because it was so perfectly executed, so crisply delivered, that I try to see him present as often as I can. 0.5/10
  • The things we build are a reflection of our skills, our processes, ourselves. And it's a challenge because we won't always be there to explain how to use it; it's up to our designs, our code, our products to be self-explanatory. I'm here to remind you that we're not going to be measured solely by how well we implement Agile engineering processes. We're measured by our results. I'm going to talk about how you might take the things you've learned here today and apply them.People will judge our softwareby how well it looks, how well it works, andhow well we put it together.1/11
  • One of the partners at Excella Consulting compiled a list of observations about the work we do. One of the observations he noted (#17): Success is dependent on people, process, and technology. In that order.Today we've learned a lot of useful techniques to improve our process, but it doesn't matter how good our process is if we aren't good software engineers.I was recently helping Sahil learn how our interview process works when I realized that I've interviewed a few hundred people, but I’ve never really shared the techniques I use to interview. I hear often: how do I know if someone's a good software engineer?The process to inspect a home is far better defined. I can walk around the house making sure the switches work, turning on the faucets, flushing the toilets. That doesn’t really translate to software engineering.Here I’ll present three things I look for – to me, they’re signs that you’re a good software engineer.1/12
  • The first thing I look for is passion. You have to WANT to develop software and produce good work.The Cleaning Service –Does anyone own a home? Do you care about it?Their mission was to clean everything in the house, but they didn’t do it quite the way I’d do it. I have different preferences because I own the home!I prefer to leave my shower doors on their tracks and not leaning against the frame.I prefer to leave permanent fixtures attached to the house.I prefer to keep the fridge door closed.I prefer to keep the stove gas off.Seems like common sense, right? So why do the cleaners do that? They don’t care the same way I do.2/14
  • In the interview, I often ask a lot of questions to find out how much someone loves developing software or working with computers.How many computers do you have?Physical or virtual?What do you do to learn about new technologies?Do you play any MMORPGs?I want people who are passionate about computers and software engineering. I want someone who would choose to do the same thing in their free time.CS 201 TA – I was a student TA right when the .COM bubble was starting – It’s the thing to do.2/16
  • I also look for people with great communication skills. Verbal communications, for one.Gregg Boer, Program Manager, TFS – “The Greatest Estimate”We determined that the project would take 6 months – we’d be done sometime in June.The product owner runs off and thinks, “We could work just a bit harder and be done on May 31.”The CTO realizes it’s already mid-December, we could be done in April!The CEO asks, “Can we get it done in March?”The CTO sets up a demo on March 1, let’s get this thing ready for test by the end of January!What the heck just happened?2.5/18.5
  • Written communication’s important, too.Here’s a sample resume that I received – the name’s been changed, of course.I tried to make it somewhat anonymous, but if you’re in the room and it’s your resume, please correct it.I couldn’t read past MS-Sequel Server!Where else does written communication play a part in your job?1/19.5
  • Of course, the third well-known communication mechanism is… code.For many of us, our primary work product is code. We are often evaluated on the code we write.When I was a TA, I graded a lot of labs. 50% of your grade was dependent on producing the correct output.The other half of your grade came from choosing good identifiers, applying proper indentation and formatting.You could submit a working program and still get an F!Call me sadistic, but I'd probably still be doing that if it paid more than $8/hr.◊ CS 340: 10% off for every embedded literal.◊ <sample>: embedded literal◊ That's one letter grade for every embedded literal.I went through my archives to find the best examples of the worst code snippets ever. These snippets worked – but sometimes they weren’t exactly production-quality code.2/21.5
  • I really wish I was making this up.This came from a VB6 app. This developer didn’t know about control arrays.You need to develop a sense of how much you’re copying and pasting. When I find myself copying and pasting or typing the same thing over and over again, I start thinking, “There has to be an easier way to do this.”Consider refactoring into a parameterized method; inheritance; loops; any conveniences that your programming language provides.If you fail to refactor, you’re creating technical debt!1/22.5
  • Those of you that know me may know that this is one of my pet peeves.Bad Identifiers (T-SQL Aliases)1/23.5
  • Here’s a classic debate. Are code comments good or bad?Here’s a bad case.First, we don’t know whether the comment’s wrong or the code’s wrong!Second, I would hope that the next developer that works on this code would know what i++ means!I personally prefer to use comments sparingly – I want the code to be self-documenting through the use of readable identifiers with whitespace and indentation to provide emphasis.1/24.5
  • Unindented Code, Bad Identifiers, Inconsistent StandardsDoes this code work? Maybe it produces the right output, but that's only half of the evaluation criteria. This code fails because it doesn't properly communicate what it's supposed to do.When I’m interviewing, I’ve gotten into the habit of asking for code samples so I can get an idea of how easy it is to read someone’s code. Much like the resume, imagine if someone’s best work has some pretty bad code smells.Has anyone seen any other examples where code wasn’t communicating what it should? Just the other day we found an AND clause where the second half was always the same as the first half! Is anyone paid by the keystroke?The part that bothers me is that in every case, we usually know better! We’re just in such a rush that we lose all concept of high-quality, readable code.What annoys me even more is that we have lots of nifty little tools to make sure we select the right method. And if you misspell something the first time, the tool makes it easy to misspell the same thing over and over and over…1.5/26
  • The third thing I look for is how well candidates know their tools and techniques. You will be judged on your mastery of tools and patterns.Talking to the homeowners again, have you ever had to hire a contractor?I kick myself every time I hire a contractor that doesn't know how to use a bubble level. I've had to redo too many home projects that weren't level. The same should apply to software.1/27
  • Learn your tools!TDD works really well, but you still need to know how to write and run your tests before you dive into TDD!Learn what your tools do well; learn what your tools do not do well.0.5/27.5
  • Know what your tools do (and don’t do).Have you ever seen a big list of bug documentation on top of just a few lines (or even one line)?This is a pretty useful practice if your code is the only method of tracking bug history.The unfortunate thing is that I saw this when we had a source control system that kept track of all these details!0.5/28
  • Have you seen this? I swear, I’m not making these up, although I’m exaggerating a bit.Some people get really verbose when they’re filling in comments. This snippet was inspired by someone I worked with who had a habit of including random personal information in his comments.Speaking of tools, don't rely so much on them that it keeps you from writing good software.Don't confuse Intellisense with spell check. If you spell it wrong the first time, it'll help you spell it wrong every time.Same thing goes with ORM. If you spelled your column name like this <sample>, your class names are going to look like this.1/29
  • Your class names and properties are going to look like this!Tools are not a substitute for thinking.0/29
  • Speaking of techniques, you’re also going to be judged on your presentation.1.5/30.5
  • Here’s a door from Excella's main office in Arlington.Which way does the door open?It's a really nice office, but here, we sacrificed obvious cues for the sake of beauty and symmetry -- or at least conforming to the design standards of the building. We're used to cues that tell us whether to (gesture) push or pull.When we first moved in, I noticed one of Excella's partners looking up every time he approached a doorway. He was looking for the only visual cue we have…
  • … the door stop at the top of the door.Someone gave me a helpful mnemonic. The main doors in the office open away from the main space.Is that helpful? Maybe.I'll also tell you that to make room for that knowledge, I probably forgot something else. Was it a grocery list from July? Was I supposed to be somewhere else today? Should your users have to make up mnemonics because you forgot to think about how they'd use your software?
  • Now pick someone to represent your little group. We'll go around the room and just shout out some of the ideas.(Shout Out – 120s)
  • Here’s my opportunity to complain about consistency.
  • They put a lot of thought into the airport signage at Schiphol.Color coding: Color should make sense and clearly communicate an information category.Terminology: Assume that visitors know nothing about the facility and use terminology that is easily understandable to everyone. Fonts: Don’t use more than one font, and stick to such sans-serif typefaces as Frutiger, Clearview, Gill or Meta. Pictograms: Don’t rely too much on pictograms; supplement with text, especially with less familiar functions.
  • Competitive Cooking Shows – Iron Chef, Top Chef, ChoppedPresentation is a key element.It’s human nature.We assume that food that looks disgusting tastes disgusting.What if your code worked perfectly but the developer calling your classes sees all of your typos?The back-end code for your software could be amazing, but if your user interface looks like you rushed to put it together, people will assume that you rushed the whole thing.1/31.5
  • Let’s talk a bit about usability.Have you seen the Carelman Coffeepot?Does it look usable to you?Can we manufacture it? Sure. But it doesn’t follow a design pattern that’s easy to interact with.0.5/32
  • Have you ever built THE FORM? Have you ever thought it was a good idea?When this stuff was new, it was pretty neat. You could throw something like this together pretty quickly.Our needs have evolved. THE FORM might work, sure, but there are so many things wrong with the form.1/33
  • Let’s look at some unpopular UX patterns. Maybe we could call these UI smells.This one popped up on the console from a Windows Service.I didn’t have access to the server room, so we had to call an administrator to clear out this message box.1/34
  • I worked on one project (actually the same one as the last one) where I couldn’t figure out why my data didn’t save when I clicked OK. It turns out I was supposed to click Apply / Cancel. Aren’t those supposed to be equivalent?Have you ever run into software where they switch OK and Cancel? I’m so used to that order that I’ll click the wrong one if you switch it around.1.5/35.5
  • Here’s something from a site that completely blew me away. I’m used to seeing Login and Password – this site had them switched around.Usually this sort of thing happens to me when someone’s looking over my shoulder.1/36.5
  • Know Your Users’ Patterns (Expect the Unexpected!)The Therac-25 was a state-of-the-art cancer treatment system designed to deliver radiation with pinpoint accuracy.It had two treatment modes: a high-power x-ray mode and a low-power electron beam mode.X-ray mode fired 25,000 rads of radiation. To switch to x-ray mode, you typed an “X” into a field and a thick metal plate automatically deployed to transform the radiation beam into lower-intensity x-rays.Electron beam mode fired 200 rads. You entered an “E” to activate it.The operator pushed X accidentally, but used an unexpected key combination to change it – up arrow to select “edit” and change from “x-ray” to “electron beam” mode, then return to get back to the bottom of the screen.This sequence had never been tested. The screen showed “electron beam” mode – the metal plate was retracted, but the power setting remained at maximum.There was a video camera in the treatment room, but the control room monitor wasn’t plugged in.beam fired, blue flash, excruciating pain. control room displayed "Malfunction 54" -- operator fired beam again.Cherenkov radiation caused by acceleration of a stream of electrons."Captain Kirk forgot to put the machine on stun.“2/38.5
  • If we were to be agile when it comes to UI…We’re conditioned to look for structured data. Specific information vs. natural language prose.Prose vs. Tables - Airline ReservationCut the fluff!
  • Who’s a mobile app developer? Who’s gone through an app certification process? Polishing your app might take longer than the functionality!I recently went through the Windows 8 certification process and they’re cracking down on some of these patterns. I’ve shown you a few of the smells that could make your customers, your users, your bosses cringe – and I want you to recognize them, too, because that’s how you’ll deliver good software.1/39.5
  • So here are a few more references for your reading list.Set Phasers on Stun by Stephen CaseyClean Code by Robert C. MartinThe Design of Everyday Things by Donald A. Norman (they updated the cover on that one)Designing with the Mind in Mind by Jeff JohnsonUrsEnzler – Clean Code and Clean TDD Cheat Sheets
  • UrsEnzler – Clean Code and TDD Cheat SheetsSo… we’ve learned how to identify and how to avoid code smells, but what happens when you have to work with a code base filled with code smells?2/41.5
  • Roberto Hernandez is up next to tell you all about how we deal with legacy code.
  • And thank you guys. Get out there and write some good software.I’m Ed Buhain.
  • Unindented Code Cannot Possibly Work!

    1. 1. Unindented Code Cannot Possibly Work! Ed Buhain December 7, 2012PRIVILEGED AND CONFIDENTIAL. The information contained in this material is privileged and confidential, and is intended only for the use of the individual towhom it is addressed and others who have been specifically authorized to receive it. If you are not the intended recipient, you are hereby notified that anydissemination, distribution or copying of this material is strictly prohibited. If you have received this material in error, please destroy it immediately. #DCAEC12 #DCAEC12
    2. 2. The Ariane 5 $7B development $500M rocket & cargo Software Based on the Ariane 4 #DCAEC12 #DCAEC12
    3. 3. The Ariane 5 Inertial Reference Systems On-Board Computers Boosters & Main Engine #DCAEC12 #DCAEC12
    4. 4. The journey of a thousand milessometimes ends very, very badly.  - Despair, Inc. #DCAEC12 #DCAEC12
    5. 5. About Me NAME: Ed Buhain COMPANY: Excella Consulting EMAIL: ed.buhain@excella.com TWITTER: @buhain BLOG: blog.buhain.com #DCAEC12
    6. 6. Unindented Code Cannot Possibly Work! Ed Buhain December 7, 2012PRIVILEGED AND CONFIDENTIAL. The information contained in this material is privileged and confidential, and is intended only for the use of the individual towhom it is addressed and others who have been specifically authorized to receive it. If you are not the intended recipient, you are hereby notified that anydissemination, distribution or copying of this material is strictly prohibited. If you have received this material in error, please destroy it immediately. #DCAEC12 #DCAEC12
    7. 7. #DCAEC12 #DCAEC12
    8. 8. SOLID Principles S - Single Responsibility O - Open/Closed Principle L - Liskov Substitution I - Interface Segregation D - Dependency Inversion #DCAEC12
    9. 9. #DCAEC12 #DCAEC12
    10. 10. #DCAEC12 #DCAEC12
    11. 11. Unindented code cannot possibly work.  - Jeff Prosise #DCAEC12 #DCAEC12
    12. 12. #DCAEC12#DCAEC12
    13. 13. Success is dependent on people,process, technology – in that order.  - Jeff Gallimore #DCAEC12 #DCAEC12
    14. 14. I - Passion #DCAEC12 #DCAEC12
    15. 15. I - Passion #DCAEC12 #DCAEC12
    16. 16. II – Communication Verbal Written #DCAEC12 #DCAEC12
    17. 17. II – Communication #DCAEC12 #DCAEC12
    18. 18. Any fool can write code that acomputer can understand.Good programmers write code thathumans can understand.  - Martin Fowler #DCAEC12 #DCAEC12
    19. 19. Does This Code Work? If (productNames.Count > 0) Then lblProductName1.Text = productNames(0) lblProductName1.Visible = True End If If (productNames.Count > 1) Then lblProductName2.Text = productNames(1) lblProductName2.Visible = True End If . . . If (productNames.Count > 98) Then lblProductName99.Text = productNames(98) lblProductName99.Visible = True End If #DCAEC12
    20. 20. Does This Code Work? SELECT a.CustomerName AS a, b.Address1 AS b, b.City AS c, b.PostalCode AS d, c.OrderNumber AS e FROM Customers a INNER JOIN Addresses b ON b.AddressId = a.AddressId INNER JOIN Orders c ON c.CustomerId = a.CustomerId WHERE c.IsProcessed = 0 #DCAEC12
    21. 21. Does This Code Work? // increment j i++; #DCAEC12
    22. 22. Does This Code Work?string iCustnm;iCustnm = "644";if (Cust.Has_Orders(iCustnm))processOrders(iCustnm);else wiatforOrdersw(iCustnm);Display_Customer_Orders(iCustnm.ToString()); #DCAEC12
    23. 23. III – Tools & Techniques #DCAEC12
    24. 24. Skynet became self-aware.  - The Terminator #DCAEC12 #DCAEC12
    25. 25. Know Your Tools! // Doug H. - 12/11/2010 - Bug 2733 // Rachel G. - 11/17/2010 - Bug 2544 // Doug H. - 11/11/2010 - Bug 2211 // Doug H. - 10/29/2010 - Bug 2211 // Howard W. - 8/6/2010 - Bug 2099 // Norm P. - 4/11/2010 - Bug 1556 // Doug H. - 1/14/2010 - Bug 1444 ProcessPayment(payment); #DCAEC12
    26. 26. Know Your Tools!/* * * Edited by Doug H. * Thursday 3:45 p.m. * Outside Temperature 38°F * I live at 65 Main Street. * I wish I was on vacation. I have 6 days of vacation left. * */ #DCAEC12
    27. 27. CuStOmErADDRESSid #DCAEC12 #DCAEC12
    28. 28. III – Tools & Techniques Know Your Tools Think About Presentation #DCAEC12 #DCAEC12
    29. 29. #DCAEC12
    30. 30. #DCAEC12
    31. 31. You’re Surrounded! Think of something you think is poorly designed. Share with your group. #DCAEC12
    32. 32. We’re Surrounded! Pick one or two ideas. Share with the whole room. #DCAEC12
    33. 33. #DCAEC12
    34. 34. #DCAEC12#DCAEC12
    35. 35. Attractive things work better.  - Donald A. Norman #DCAEC12 #DCAEC12
    36. 36. Know Your Patterns #DCAEC12
    37. 37. Know Your Patterns #DCAEC12
    38. 38. Know Your Patterns #DCAEC12
    39. 39. Know Your Patterns #DCAEC12
    40. 40. Know Your Patterns #DCAEC12
    41. 41. Know Your Patterns #DCAEC12
    42. 42. Know Your Patterns You are confirmed on United Airlines flight 2049, departing from Washington-Dulles Airport at 10:00 a.m. on Friday, December 7, 2012 and arriving at Seattle-Tacoma International Airport at 3:00 p.m. United Flight 2049 Washington-Dulles to Seattle- Tacoma Departs: Fri 12/7 10:00 AM #DCAEC12
    43. 43. Putting It All Together… #DCAEC12
    44. 44. Reading List #DCAEC12
    45. 45. Reading List #DCAEC12
    46. 46. What do you think about when youhear the term legacy code?… the sort of code that you just wishwould die.  - Michael Feathers #DCAEC12 #DCAEC12
    47. 47. About Me NAME: Ed Buhain COMPANY: Excella Consulting EMAIL: ed.buhain@excella.com TWITTER: @buhain BLOG: blog.buhain.com #DCAEC12