-3

I've been in an argument at work about writing readable code. It was mainly about variable and method names and the idea that comments are worthless if the code is readable enough. I would not like to emphasize only on no specific coding language.

1.) Descriptive vs minimalistic variable and method names Descriptive variable names are easier for a newcommer to understand while minimalistic makes it easier to navigate your code (No screen cluttered with long camelCase names) Lets say i have a method:

  • convertOneFormatToAnotherFormat() vs oneFormatToAnother()
  • checkIfTypeIsBoolean() vs isBoolean
  • rerenderNavigationInNavbar vs refreshNavbar()

What would you say a good rule is?

2.) Using abbrevations for variable naming vs using full names everytime Abbrevations makes your code less cluttered, while full names make it easier to understand. The point of the argument was that i said that some abbrevations are better then fullNames. Lets check the silly examples:

  • JSON vs javascriptObjectNotation
  • RGB vs redGreenBlue
  • VB vs visualBasic

Of couse abbrevations are better.

  • conv vs convert
  • dev vs developer
  • usr vs user

Full names are better here for sure. I would like to ask for oppinions here for some more unclear examples:

  • kvp vs keyValuePair
  • ddl vs dropDownList

What would you choose here? Also, how would you write a rule for naming with abbrevations?

3.) Comments not needed if code is readable enough There is a standard that says that documentation and comments are not needed if the code is readable enough. And yet i have found that i have made some specific choices in my code and wanted to explain my choices in the comments above.

  • Would you say comments in code that explain logic are needed?
  • Would you say that comments that only segmentize the code are needed?
  • Would you document HTML with comments? (End tags, beginning tags (, )

4.) Short names in short standalone function vs meaningfull names Lets say i have a method that finds returns a number and multiplies it by 2 Is it ok to:

multiplyByTwo(x):
    a = x*2
    return a

or is it a lot better to:

multiplyByTwo(factor):
    result = factor*2
    return result

I know the best way is to be smart about it and use the one that informs the reader and makes his job the easiest possible is the best one, but for teams this is difficult to do since each one has his own style.

Thank you for the response

IamSneaky
  • 1
  • 1
  • 3
    Possible duplicate of [How would you know if you've written readable and easily maintainable code?](https://softwareengineering.stackexchange.com/questions/141005/how-would-you-know-if-youve-written-readable-and-easily-maintainable-code) – Caleth Dec 05 '17 at 12:41
  • 2
    How can a response to this possibly be anything other than subjective opinion? – MickeyfAgain_BeforeExitOfSO Dec 05 '17 at 13:16
  • 3
    see [On discussions and why they don't make good questions](https://softwareengineering.meta.stackexchange.com/q/6742/31260) – gnat Dec 05 '17 at 13:23
  • 2
    1 or 2 ch nm is ok to rd no? –  Dec 05 '17 at 15:55
  • Just in my opinion, I've never encountered implementations written by someone else where the comments or identifiers helped that much in understanding what the code did. Either I was familiar with the design pattern or algorithm being used, at which point anything within reason is easy enough for me to immediately recognize and understand, or I wasn't at which point even very nicely commented code wasn't going to be so effective in teaching me new algorithms. Interfaces are a different case. Comments and clear names can really help there as long as they aren't too verbose... –  Dec 05 '17 at 23:54
  • ... I did once encounter some legacy code that had 400 lines of a preamble comment describing usage and explaining exotic concepts. I found the comments worthless because the design was so counter-intuitive (probably why the author felt the need to add a 400-line manual at the top of the header). If it's between intuitive interfaces which barely need documentation and counter-intuitive ones which demand lengthy documentation, I'm definitely going with the former. I actually found the lengthy manual harder to understand than the code (actually looking at code to make sense of the comments). –  Dec 05 '17 at 23:55
  • That old codebase was actually written using Literate Programming as popularized by Knuth: https://en.wikipedia.org/wiki/Literate_programming. And as nice as that might sound, I often find that comments are not a good source for learning how things work. Someone unfamiliar with a radix sort is probably not going to learn how it works by reading comments in code, and someone already familiar with how it works would probably immediately recognize what's going on even with the most poorly-documented code using the most terse identifiers. –  Dec 06 '17 at 00:04
  • Last but not least, any focus on readability should prioritize interfaces and usage above all else IMHO. There's no hope of understanding how something works if you can't even understand what it's supposed to do in all possible scenarios. If you can't understand how something works but can fully comprehend what it's supposed to do for all possible input cases, in a worst-case scenario you could create something which fulls identical requirements, potentially even better. If you can't even understand what it's supposed to do and there are 10,000 dependencies in the system to it, you're SOL. –  Dec 06 '17 at 00:10

2 Answers2

1

1.) Descriptive vs minimalistic variable and method names Descriptive variable names are easier for a newcommer to understand while minimalistic makes it easier to navigate your code (No screen cluttered with long camelCase names) Lets say i have a method:

convertOneFormatToAnotherFormat() vs oneFormatToAnother()
checkIfTypeIsBoolean() vs isBoolean
rerenderNavigationInNavbar vs refreshNavbar()

What would you say a good rule is?

At first they should follow naming conventions which (in Java) includes that method names always start with a verb.

Then you example read out a bit foolish because they are (intentionally) generic. But what about this?

convertXmlToJson() 

2.) Using abbrevations for variable naming vs using full names everytime

  • JSON vs javascriptObjectNotation
  • RGB vs redGreenBlue
  • VB vs visualBasic

I'd say using (very) common acronyms is OK.

But

Of couse abbrevations are better.

  • conv vs convert
  • dev vs developer
  • usr vs user

Full names are better here for sure. I would like to ask for oppinions here for some more unclear examples:

  • kvp vs keyValuePair
  • ddl vs dropDownList

abbreviations are no good at all.

3.) Comments not needed if code is readable enough

Comments comments should explain why the code is like it is. E.g. why you preferred a certain syntax or structure over another, more common (within your codebase)...

Also interfaces usually need comments to help the implementer understanding the contract behind it.

  • Would you say comments in code that explain logic are needed?

No

  • Would you say that comments that only segmentize the code are needed?

No

  • Would you document HTML with comments? (End tags, beginning tags (, )

No (I have Tools with proper highlighting)

4.) Short names in short standalone function vs meaningfull names

Is a special case of the abbreviation issue...

Timothy Truckle
  • 2,336
  • 9
  • 12
1

Just like any language meaning is often given by context and history.

for example ddl is a common abbreviation for drop down list in some contexts but not others. If you come from a VB6 background you may use comboBox instead.

The clean coding philosophy would have you get rid of comments entirely, but in my view that is rather narrow minded. It depends on everyone having the same view of 'readablity'. If you have worked with developers who don't speak English or systems with odd terminology you will know that long names don't always explain what the code is doing.

Just as you have discovered with your colleague, what one person find readable another doesn't.

So in general. Yes, avoid myFunc(x) and //adding two to the number but don't go crazy. A combination of comments and longer names will see you though eg

///
///convert incoming JSON data into Data object so that we can blah blah
///
Data ParseJson(string json) {...}
Ewan
  • 70,664
  • 5
  • 76
  • 161
  • 1
    While this may be true for *new* code: what about *evolving* code? Comments like this tend *not to get updated* as the codes behavior/responsibility changes. Reasons may be time restrictions or careless developers.... You might say that is is easy to find out, but what purpose does a comment serve if I have to check with the code if it is still valid? – Timothy Truckle Dec 05 '17 at 13:17
  • The same argument could apply to the name. But! the idea with this kind of comment is you describe what the intention of the function is. what it SHOULD do, not what it does. This is usually more constant over the life time of an app and too long to condense to a function name. – Ewan Dec 05 '17 at 13:19
  • Adding comments for terse code just shifts the problem... – Robbie Dee Dec 05 '17 at 13:20
  • but adding comments to verbose code gives valuable information – Ewan Dec 05 '17 at 13:21
  • *"The same argument could apply to the name."* which makes two lies against one truth. Much better... *"But! the idea with this kind of comment is you describe what the intention of the function is. what it SHOULD do"* which is also true for the functions name! – Timothy Truckle Dec 05 '17 at 13:21
  • convertIncommingJSONDataIntoADataObjectSoThatWeCanBlahBlah as a function name doesn't really add to the readability of the code. You dont always need both, but thats not the same as NEVER needing a comment – Ewan Dec 05 '17 at 13:23
  • *"but adding comments to verbose code gives valuable information"* Does it? - The *extra information* that your example gives is a *wild guess* about what the functions user *might* what to do with it... – Timothy Truckle Dec 05 '17 at 13:23
  • @TimothyTruckle no it will be the intention of the code as relating to the requirements. – Ewan Dec 05 '17 at 13:24
  • *"... is a function name doesn't really add to the readability of the code."* Yes! Because it contains *unnessesarry* information. – Timothy Truckle Dec 05 '17 at 13:25
  • inf act in your answer you have "interfaces usually need comments to help the implementer understanding the contract" which is exactly what i mean – Ewan Dec 05 '17 at 13:26
  • unnecessary to you, maybe but you are not your reader – Ewan Dec 05 '17 at 13:26
  • *"inf act in your answer you have "interfaces usually need comments to help the implementer understanding the contract" which is exactly what i mean"* Yes, but only for interfaces. – Timothy Truckle Dec 05 '17 at 13:29
  • not all languages have interfaces, if the comment helps then you put it in the most suitable place to hand – Ewan Dec 05 '17 at 13:31
  • *"it will be the intention of the code as relating to the requirements"* Does that mean that the requirements must explicitly mention any possible context your function might be used? Am I not allowed to cut a rope with a kitchen knife because it is not mentioned in its specification? – Timothy Truckle Dec 05 '17 at 13:35
  • your arguements seem very specific. if I add interface or private to the example would you withdraw your objections? – Ewan Dec 05 '17 at 13:37
  • Imo this is a prime example of a bad comment. It’s only needed because of the meaningless name of the return type `Data`. Give that type a non-generic, descriptive name and the signature alone tells you the same as the current comment. That’s not to say that any comment is superfluous here, but it should be something that cannot easily put into code: pre and post conditions are an example in many languages. – besc Dec 05 '17 at 20:27
  • its really the "..so that we can blah blah.." which is the important bit, obvs without any context its hard to pick a real example – Ewan Dec 05 '17 at 21:38
  • "The clean coding philosophy would have you get rid of comments entirely" Nonsense. Comments clarify your code and that's what they are used for in clean code, too. Only, when do you need to clarify? When it isn't clear enough on it's own. **A comment is a failure to express yourself clearly enough in code**. Which totally happens, sometimes you just can't make the code that clear. However, you should at least try and not start every method thinking your code will be too convoluted to stand on it's own. – R. Schmitz Dec 20 '17 at 18:26