views:

2728

answers:

24

During a phone interview someone asked the question, "When looking at code, what's the first thing that stands out to let you know it's from a good/experienced developer?"

I believe my answer was sufficient, just thought it was a bit strange so I'm curious how some of you would answer?

EDIT: thank you for all the answers. One of the reasons why I asked this here is only because many of these answers are the same that I started saying but they interviewer would constantly interrupt me to reiterate, "YES, but what is the FIRST thing?" which lead me to believe that my answer was not what he was looking for.

+29  A: 

Each function does exactly one thing.

Otávio Décio
I agree with this, but it would be difficult to know this by 'looking' at code unless it has a clean and logic structure - you can write crap code which is functionally impeccable while impossible to maintain. In other words, I think the 'do one thing' thing is secondary
Arrieta
Or 'necessary but not sufficient'
Arrieta
Now tell me - what is sufficient? In my experience things are generally so bad that if you are lucky to find code where functions do a single thing, it is a very good sign that the author has a clue of what he/she is doing.
Otávio Décio
Actually, this is a pretty good indicator. Inexperienced developers tend to throw everything and the kitchen sink into functions with names that have nothing to do with the six or more things they do. My favorite is any function beginning with the word "Process".
Tom Cabanski
Agreed - we can go on an on about the "theoretically perfect" piece of code, but in reality one is happy if the code is at least somewhat logic and legible.
Arrieta
@Tom - I've seen my share of "Process" functions, and boy it is nauseating.
Otávio Décio
I would add that it also should be obvious what that "one thing" is (even without a detailed analysis of the function's internals).
bta
@Tom +1 Know exactly what you mean...I see plenty of "functions" with this word in them all the time. And yes, they are usually hundreds of lines of garbage. In fact, I'm hacking on one right now... :)
Adam Neal
The only problem with “does one thing” (a goal I strive for) is that that one thing has to be at a sensible level of abstraction.
Donal Fellows
A good principle to follow is the Composed Method pattern. "Divide your program into methods that perform one identifiable task. Keep all of the operations in a method at the same level of abstraction. This will naturally result in programs with many small methods, each a few lines long." *- Kent Beck, Smalltalk Best Practice Patterns*
Esko Luontola
+1 for writing an answer that follows its own advice.
Joachim Sauer
+9  A: 

Does it have unit tests? Unit tests have many benefits and are usually a good sign of code quality:

  • They help separate the code into reusable units that are independent of each other.
  • They provide up-to-date documentation for the code. Comments, on the other hand, can quickly become out of date.
  • They make the developer think more of failure modes and error handling.
  • They make refactoring easier and safer.
Ayman Hourieh
good code unit testing does not necessarily make.
Jason
It's a sign of good code. If the code is testable and tested, it's more likely to be of high quality for the reasons I outlined. Unit tests also make it much easier to improve the quality of existing code.
Ayman Hourieh
I don't think having unit tests is in itself a good thing. If the department head says "All code must be Unit Tested with 100% coverage" and the sheep follow then the code will not be good. It is still probably better than the same code without any tests though.I think a sign of good code that is Unit tested would be lack of copy and paste reuse in the tests. Any indication that the developer treated the Unit Tests as code and applied some thought to their design rather than just churned out masses of "compiled scripts" would increase my optimism that the underlying code was good though
Modan
+3  A: 

Three things:

  1. Comments
  2. Comments
  3. Comments

Did I mention "comments"?

Update: Seriously? This negative a reaction to the idea that well-commented code is a valid indicator of a good and experienced (I'd add professional) developer?

Did I accidentally log in to the Bizarro StackOverflow or something? I have a question for all the down-voters:

If a job candidate submitted a code sample that included no comments whatsoever, would you conclude a) the candidate is the most brillant programmer in history, or b) the candidate has never worked in a professional code shop?

Remember that the original question was (to paraphrase) "what single feature of code lets you best evaluate a developer's ability and experience?", not "do the most awesomest programmers need to use comments or not?"

Update 2: So far, my favorite comment on why comments (in code) are bad is this one from Tom Cabanski:

Inexperienced programmers read the comments and get fooled.

I never thought of it this way. I'm going to stop putting comments in my code, and I'm also going to stop giving my variables descriptive names, since the names (which are just a convention not enforced by the compiler) might fool inexperienced programmers. And no more documentation of any kind, since that might fool inexperienced programmers too.

It's weird, though, because I always thought that the whole point of comments/naming conventions/documentation etc. was to help other programmers understand what your code is doing and why (as well as why you're not doing it some other way). I guess the great ones just don't need crutches like this.

MusiGenesis
I mean *good* comments of course, not `// I dunno why this works - it just duz`.
MusiGenesis
IMO too many comments actually means it's not good code, it's not broken up into well named functions and thus the only way to understand it is by reading comments. You should be able to read the code and understand it w/o comments.
Sam
I disagree strongly. Comments don't compile so they easily get out of sync with the code. Inexperienced programmers read the comments and get fooled. Good code should be readable on its own. I might use a comment once in a blue moon but not very often at all.
Tom Cabanski
Good code doesn't need many comments at all. I won't say it needs none, but 90% of code should be clearer to understand without the clutter of comments IMHO.
David M
I disagree. Good code shouldn't need comments. Comments should be there for corner/edge cases or important issues. Nothing worse than out of date comments.
Finglas
I use comments when the code can't explain itself. Which means, as rarely as possible.
Otávio Décio
good code only needs commenting when the reason is not clear and this should not be a common case.
Sky Sanders
I recently came across a comment that simply read "Bodge bodge BODGE!". Not sure that counts as good code...
Rowland Shaw
Comments are a two-edged sword. I hate it when people comment assignments (`\\set value of i` then `i=1`) but fail to document the "flow" or "logic" of the program. This is typical of people who "hack" their way though a problem without thinking it thoroughly.
Arrieta
@musigenesis, you sound like my first CS instructor.
Jason
@Sam I agree that too many comments is a bad sign. I've seen plenty of 500 line "functions" that are broken up into sections with flowerbox comments and should really be about 10 functions or even another object altogether.
Adam Neal
I'll use `Finglas`' comment as an exemplar of the comments here: "Comments should be there for corner/edge cases or important issues." to which I would reply something like "duh" or "no $#!+, Sherlock". Is a candidate going to submit a code sample that *doesn't* do anything important or difficult? If they do, is *that* somehow a positive indicator of their ability?
MusiGenesis
@Rowland: "Bodge bodge BODGE!" is an *excellent* comment (because it provides useful information to the poor bastard who has to maintain it) in the middle of no-doubt awful code.
MusiGenesis
@MusiGenesis: There's a difference between the code *doing* something complex, and the *code itself being* complex. Good code that does something important and difficult, does it in a very simple way so that it is *obvious* what it does.
Esko Luontola
MusiGenesis++. Code is not design, the languages we use do not make all important design aspects obvious, comments make up the difference. None of the contrary assertions above carry evidence to back them up. I suspect the anti-comment camp doesn't like to write comments and hasn't had to maintain lots of uncommented code.
Conrad Albrecht
@Rowland, i couldn't agree more with @MusiGenesis, at least the developer who wrote that is alerting you to the fact that you may not want to touch that particular section of code.
Ben
I feel there is a reverse correlation between good code and comments. The better the code is, the fewer comments it needs. If I saw a block of code with a lot of comments, the *last* thing I would think is "wow, great code." Indeed, I would probably think something more like "Holy cow, what's wrong with this code that it needs all these comments? And what can I do to get rid of them?"
Eric King
@MusiGenesis: To answer your question (if a job candidate submitted a code sample that included no comments whatsoever), I wouldn't draw any particular conclusions from the lack of comments. Comments do not make a program run faster or more accurately or bug-free. I would look at the *code*, and determine if I can understand what the *code* is doing. To the extent that I have to move my eyes off the code and onto the *comments* to understand what the code's doing, that's a bad thing. If the candidate gives me a code sample that I can understand and enjoy, with no comments, so much the better.
Eric King
Wow, I didn't think comments would necessarily be the first and best indicator but neither did I think the response would be voted down! +1 for perseverance.
T.Rob
+18  A: 

I look for good error handling. Inexperienced developers often write code that assume everything will go right. Experienced developers will check parameter/code contracts and add plenty of error handling to deal with what can go wrong.

Tom Cabanski
Is there by chance a good example of good amount of error handling? I have recently been trying to get better at this, but I find mostly code examples of stuff which doesn't have good error handling.
percent20
not sure how I feel about this. I write a lot of library code and tend to throw more than I catch.
Sky Sanders
@Sky, that sounds about right to me. Of course plenty of error handling doesn't necessarily mean good error handling. One of my peeves is catch/log/throw, which isn't any kind of error handling at all.
uncle brad
@uncle - yup, catch/log/throw is, in most cases, wrong on many levels.
Sky Sanders
I call the catch/log/throw pattern "error acknowledging".
MusiGenesis
+1  A: 

Does it smell good ?

Romain Hippeau
+24  A: 

The structure tells you a lot. Without even reading the code you can tell its quality by whether or not it has deep nesting.

Bill the Lizard
so true. code shape is a good measure.
Sky Sanders
Yes, and I remember reading somewhere that most people can't really process more than 3-4 levels of nesting anyway. Definitely true for me, although I have to wrangle code all the time that is nested about 10 levels...
Adam Neal
Whenever I take on responsibility for a new codebase, the first thing I do is factor out all the deeply nested loops and complicated if/else blocks.
Kristopher Johnson
+11  A: 

amongst other things...

  • predominantly short methods
  • relatively flat left margins - deep nesting a sign of poor design.
Sky Sanders
"deep nesting is a sign of poor design" - I will be plagiarizing that quote.
Arrieta
+1 for clues you can spot at first glance.
Amanda S
+55  A: 

A lot of good/experienced developers write code that I hate reading, so there is really no way to determine whether someone is good with a cursory glance.

My own preferences are:

  • short functions (generally less than ten lines long), each with a single clear task
  • avoid nesting too deep
  • good names for classes, functions, variables, etc.
  • has easy-to-find "hooks" for adding new functionality
  • idiomatic (That is, if it's C++, then it looks like C++ should look; if it's Java, then it looks like Java should look.)
  • makes good use of standard library/framework (I don't like seeing wheels reinvented.)
  • no unnecessary comments (which are the mark of someone who can't really think in code)
  • good use of whitespace

Adhering to these preferences doesn't guarantee the person is a good/experienced developer, but it does give me a good first impression. Someone who takes care to get these things right probably does a lot of other stuff right too.

You don't really know how good someone's code is until you try to change it. I've seen code that was really ugly which nevertheless was easily maintainable, and I've seen pretty code that had to be thrown away and rewritten from scratch. Reviewing code gives you a good feel for a programmer's tactical abilities, but not strategic abilities.

Kristopher Johnson
+1 (for the whitespaces at least)
Bozho
+1 for not really knowing how good it is, until you try to change it.
Esko Luontola
+4  A: 

I take into consideration the following when viewing code, if the following is true, it's 'good' code in my eyes.

  • Well formatted/consistent
  • Well factored
  • Nice use of spacing
  • Methods not too long - should fit on one page/no need for scrolling at worst
  • Modular/Testable - otherwise it's a nightmare to unit test
  • Is the code SOLID? - hard to tell from a quick glance, but you can see some principles being broken if a class is large, or there is a lot going on for example.

All code should look as if one developer produced it. There is nothing worse than seeing several styles of coding within one source file. Even worse when only one developer produced it! The other points are a given really, but you could add any other code smells to that list.

Finglas
+4  A: 

Good code should flow like a good conversation. While Literate Programming can be overkill in some cases, the philosophy should always be present.

My rules would contain:

  • Say what you mean
  • Don't try to be clever
  • Add comments, but don't exaggerate
  • Indent and space cleanly
  • Don't try to be clever
  • Don't try to be clever

The Zen of Python provides good advice on what to look for in good code (regardless of the language).

Arrieta
+11  A: 

Good variable names.

Concise.

Minimal comments (method and variable names should do most of the explanation).

As soon as anything non-obvious is done, then it should be extensively commented.

chris
One could argue that if it is not obviously expressed in the code without comment, it is probably not being done correctly.
Tom Cabanski
One could "claim" that; what's the "argument"? Sorry, I've had to maintain too much unmaintainably uncommented code written by coders who claim that "C [or whatever] is self-documenting".
Conrad Albrecht
@Tom Cabanski -- Not always. Often you will need to work around some issue out of your control, or need to do something that is performant but not readable. For example, One-line swaps are marginally more performant than the obvious 3-line swap. If for some reason, that marginal performance improvement became important, then the One-liner should be implemented, and then immediately commented as a swap
chris
+4  A: 

First thing? Squint (or imagine squinting). Does it look good?

Obviously good code is more than formatting. But in order to have good code, it is necessary for the code to be well formatted, obeying conventions and not a big incoherent mess. That will whittle away most really bad code.

Tom Hawtin - tackline
+1 for actually answering the question and not just providing a laundry list of what makes you great.
MusiGenesis
Just was I was thinking. Code that looks beautiful is almost always of superior quality; logically it shouldn't be, but it is.
Keith Williams
+1  A: 

if I understand what the code does at first look, the developer of the code rocks!

(because understanding is the reason why I look at the code and it includes the level of mess, the goodness of the structure, comments, indentation, the cohesion of the code and even exception handling)

rahmivolkan
+13  A: 

Interesting question. Reflecting on it, I think a lot of it boils down to clarity of purpose:

  • Is it clear what this class is meant to represent (I'm assuming OO code here)
  • Is that purpose sufficiently limited in scope (not trying to do too much)?
  • Is the purpose of each variable clear?
  • Is the purpose of each method clear?

If all of those are true, it's probably reasonable code to maintain - and that's the sign of a good developer, IMO.

I'd also look for testability... that's a slightly different set of criteria, but limiting the scope of each class (and keeping it clear) is certainly a good start.

Jon Skeet
A: 

if :

1) Every function does exactly the same thing 2) There is an ample description in comments especially about the function signature and what various elements in the signature mean with respect to the problem domain 3) Re-usable, abstracted code

Pranz
"Every function does exactly the same thing" ? - Do i miss something or does it need to be edited?
rahmivolkan
It's a true statement if "exactly the same thing" is "moving bits around".
MusiGenesis
A: 

It takes time to recognize good code. Bad code can be recognized immediately. Short code files, classes and methods are good signs and so is good formatting, but it is only superficial. Anyone can split up large methods into small ones, but if the program is unclear it will only become slightly clearer.

One 50-line method is usually better than 200 1-line methods, even if 25 short methods might have been the optimal solution.

I've seen a lot of superficially good code, which had a well thought-out architecture, but where the algorithms were bad and/or unclear. A good program should be well-designed in every respect, but it is hard to identify a good program without really working with it.

Jørgen Fogh
+2  A: 

Readability. [allows easy maintenance]

If the code is easy to read it doesn't need to looks ingenious (one long complicated line) and take the reader time to review to understand it.

The faster someone can look at it and understand it. The faster the application will move forward (feature and revenue).

Glennular
I couldn't agree more.
Sparky
+9  A: 

Quote from Ward Cunningham:

You know you are working on clean code when each routine you read turns out to be pretty much what you expected.

Which in turn was commented (in the above link) by Uncle Bob:

Statements like this are characteristic of Ward. You read it, nod your head, and then go on to the next topic. It sounds so reasonable, so obvious, that it barely registers as something profound. You might think it was pretty much what you expected. But let's take a closer look.

". . . pretty much what you expected." When was the last time you saw a module that was pretty much what you expected? Isn't it more likely that the modules you look at will be puzzling, complicated, tangled? Isn't misdirection the rule? Aren't you used to flailing about trying to grab and hold the threads of reasoning that spew forth from the whole system and weave their way through the module you are reading? When was the last time you read through some code and nodded your head the way you might have nodded your head at Ward's statement?

Ward expects that when you read clean code you won't be surprised at all. Indeed, you won't even expend much effort. You will read it, and it will be pretty much what you expected. It will be obvious, simple, and compelling. Each module will set the stage for the next. Each tells you how the next will be written. Programs that are that clean are so profoundly well written that you don't even notice it. The designer makes it look ridiculously simple like all exceptional designs.

Esko Luontola
That's the Principle of Least Astonishment, in a nutshell.
Carl Manaster
I would say that it's more than "Principle of Least Astonishment". It's more like "Principle of *No* Astonishment" or "Principle of Trivial Predictability".
Esko Luontola
A: 

For me:

  • Concise but readable code.
  • Functions have as few state-changing side-effects as practical.
  • The state-changing side-effects that do exist are unsurprising.
  • Code makes few assumptions about the objects it interacts with. The assumptions that are made are enforced by well-designed contracts or interfaces.
  • The code is idiomatic and follows community conventions for the language used.
  • The code closely reflects the language of the problem domain, at least where it's appropriate (as in a "domain layer").
  • Static variables, when used, don't introduce temporal coupling issues.
  • Use of "clever" features of the language improves, rather than detracts, from readability.
  • The behavior of objects in the system (presuming an object-oriented system) is unsurprising.

And finally, presuming this is code from a living, rather than dead, organization:

  • The inevitable compromises between the idealized code we'd like to be reading and the code that we must maintain should be defensible by reasonable people, and not simply a product of stagnation or ignorance.
  • The developers still working on the system have at least some ideas for improving the pain points in the design that won't completely undermine the existing design. (If the only way to improve things is to throw most of it away, it can't be good code).
JasonTrue
+2  A: 

Though I don't disagree with these answers, I would say they are more of an indication of wither a coder is "good" at working with others. Does how does he dress and what is his personal hygiene really have anything to do with code quality? What if the guy wrote an entire cms in 3 lines of undecipherable bash script or something like that, is his code good? I think the honest answer would be there really is no way that does include the context of the code submitted. Was this a prototype? Is this a modification of someone elses code? I have seen a function with code from two people and the only thing wrong with it is that it looked like two people worked on it. Who knows what kind of answer the interviewer was looking for in any case. Who really wants to hear the existential truth about things.

RandyMorris
A: 

The very first thing, before I even start reading the code would be code indentation.

Michał Piaskowski
@Michal other than no indents at all, what would you consider "bad"?
Farstucker
A: 

I would put him no hold and go do my laundry.

Jive Dadson
+1  A: 

The function and variables names sufficiently explain what is going on, the comments explain why the code is a certain way.

iterationx
A: 

I think that he was look for a single line answer, could be:

  • It looks elegant

Maybe he was trying to find out if you were a "software craftsman", then trully good code is a work of art, it is more something that you feel, than something that you analyse.

Shiraz Bhaiji