r/learnprogramming • • Mar 22 '22

Don't be that guy.

I just took over a project for a guy who left. Don't be this guy:

  • ZERO comments in your codebase.

  • One and two character variable names with no discernable pattern.

  • Comment characters used to comment out big chunks of code for no discernable reason.

  • No error checking.

  • Reliance on data files that you forgot to include or explain, going in to meaningless data frame names.

  • Making outrageous claims about how much your code improves things with zero baseline performance.

People like this think you're an idiot because you can't make their code work, and get to be all smug. We will track you down and find you. You will need references someday....oh my.

2.6k Upvotes

427 comments sorted by

View all comments

138

u/[deleted] Mar 22 '22

We use self-documenting code. Comments where needed of course, but if we need a lot of comments, the code needs to be rewritten to be clear and concise.

All of the other bullet points, I would have no qualms about taking that developer out back and beating them with a sack of oranges (if it was past their first offense).

33

u/Traches Mar 22 '22

Comments are for explaining why, not what, and only when it's not obvious.

"We're doing it this way because if we do it the obvious way it causes some subtle bug over here that you won't notice till the CI fails"

13

u/vi_sucks Mar 22 '22

I think even when it's semi-obvious,it should be explained.

Because what is obvious to you now may not be obvious to some new guy 3 years from now.

2

u/Traches Mar 22 '22

Sorry but I have to disagree. That's what commit messages, careful naming, and separation of concerns are for. Comments don't get tested, which means there's nothing to prevent them from going out of date. Bad information is worse than no information.

By "not obvious", I'm talking about weird situations or subtle problems with the first, cleanest solution that comes to mind. Things you want to know about before you waste a few hours trying to refactor only to rediscover the same problems.

11

u/vi_sucks Mar 22 '22

Cool so what happens when the code base gets ported from SVN to Git and the commit history disappears?

And trust me, that super neat naming you think is so obvious? Some dude who isn't you years and years removed will not agree.

I had a senior dev with this same attitude and it always led to method and object names that stretched halfway across screen like workerToRunPricingEngineBeforeSavingToDatabase.callPricerOneTimeAndThenBackupDataToCachePriorToSavingToDatabase()

Just straight eyebleach. And then someone decides to save to the Cache and to print to a log, so now you either need to change the method signature in every single line where it's called, or leave to become less and less accurate with every change.

5

u/sus-is-sus Mar 23 '22

your example is just a method that does too much. it should be broken down to methods that only do one thing. runPricing, Cache, Save, Log.

→ More replies (2)

2

u/psioniclizard Mar 23 '22

Those are not super neat naming and honestly sound like methods that are doing to much. Honestly not to be mean but it sounds like the structure of the codebase is at fault.

You should not have methods that do so much, instead the caller should be chaining these together. I'm pretty sure that is the fundermental issue and I don't see how comments will fix that.

Also how often do you move codebase from SVN to git? That would really be an edgecase and I'm guessing dealing with legacy software, which needs to be handled specially anyway.

2

u/vi_sucks Mar 23 '22

Ok. So let's say you now have:

worker.callPricer()

worker.backupData()

worker.saveToDatabase()

Cool, now you need to do these three lines in multiple places since it's an often repeated action that all needs to happen together.

Now you've created a bug waiting to happen when someone in one of those places decides to add a statement to print to the log but forgets to update a couple of the 67 other places where this 3 line code call is happening.

That wouldn't be a problem if you wrapped the three method calls in a single method. But then what do you name that method? You want it to accurate describe what it does, right?

Also how often do you move codebase from SVN to git?

I would generally expect a repository migration once a decade or so. What, you think Git is perfect and will never need replacement?

The thing is, the migration itself isn't an issue. The fact that it breaks commit continuity is an ongoing problem thaf means you will continually ever after that run into code where the commit that could have explained why it was written is now lost in the sands of time. It's not like you move from SVN to Git, deal with it once, and then you're good again. Once you do it, this issue of missing historical data will come up over and over and over again.

→ More replies (5)
→ More replies (2)

2

u/Traches Mar 22 '22 edited Mar 22 '22

Sounds like you're working in a different world than me, because porting from SVN to git is just not a thing I'll ever have to deal with. I'll concede that if you have a lot of technical debt, comments are the least bad way to document it. I also don't have a problem with a brief description at the top of a class, or method descriptions used to generate API docs and the like.

"Neat" sounds a lot like "clever", which is usually the opposite of obvious. Names should be simple. Also if your names are long, your namespace is too big. Long names suck.

Two hard problems in computer science...

Edit: just noticed your username. Guess we disagree on quite a few things!

→ More replies (2)

2

u/psioniclizard Mar 23 '22

Not sure why this was voted down. Developers are notorious bad ad updating comments. Its one reason wjy you don't comment everything. Also another goof reason for comments are when a customer requests something specific/odd. I would day commit messages are not full proof (a lot of devs also wrote bad commit messages) and certain languages do test comments (rust for example) but and out of date comment is often worse than no comment.

Code should be reasonable self documenting, this comes from good structure and naming. Both of which are way more important than comments.

59

u/imnos Mar 22 '22 edited Mar 22 '22

Agree. Our codebase has next to zero comments. Tips for writing clean and readable code:-

  • Naming. Name your variables, functions, classes descriptively. A variable named x tells me nothing about what it's there for, and likewise with a function called get. What are you getting? getAllUsers tells me exactly what that function should be doing. If it needs to be something longer like getAllExpiredOrganisations - nothing wrong with that. It's still super readable.

  • Try to break down your code by responsibility, following the Single Responsibility principle (look up SOLID principles if you don't know). For example, if you have a class or module that's 10000 lines long - chances are it can be broken into multiple logical classes. Then if you need to extend this to do another similar task - you can probably reuse some of the classes you've refactored.

  • Likewise with a function that's 100 lines long. Many language style guides enforce maximum lines per function (Ruby recommends no more than 5 for example). This keeps your code nice and modular, makes it easier to read, easier to reuse, and easier to unit test each piece individually.

  • Please don't try to be clever by doing fancy one liners that make the code unclear.

Lastly, a relevant quote from Jeff Atwood's blog which I think helps to enforce the above:-

"Always code as if the person who ends up maintaining your code is a violent psychopath who knows where you live."

https://blog.codinghorror.com/coding-for-violent-psychopaths

3

u/BeardSprite Mar 23 '22

To hopefully achieve this, here's some guidelines that I've been using with decent success:

  • If you absolutely can't find a name that is readable, descriptive and exactly captures what you're trying to do... chances are there is a problem in how the functionality is currently structured OR you haven't fully understood the problem yet

  • If you added a single line to the top of each class/module/function, describing its responsibility, stop and think whenever it gets too long or the word "and" occurs

  • If something is so long that you have to scroll multiple times, or even at all, and it isn't just data that just takes all the space, chances are the problem can be divided up further to make its solution easier to understand

  • Comments should explain the why, not the how, and have the goal of reducing the "WTF factor" of the code (number of times an unfamiliar reader will utter or think "What the f*ck" while reading it for the first time)

Not sure about self-documenting code, but surely a large number of comments can be eliminated by restructuring the code. The rest should have a purpose and not be sprinkled in redundantly.

1

u/vi_sucks Mar 22 '22

Our codebase has next to zero comments.

This is not a thing to be proud of.

The way I see it, comments should always exist.

Comments document what you think the code ought to be doing. Code documents what it actually does. Noticing a mismatch between those two things solves so many issues quickly and easily.

→ More replies (28)
→ More replies (4)

15

u/Meborg Mar 22 '22

yeah this. If you add comments that have to explain a line in our codebase, you basically gotta rewrite your code. Method names should be self explanatory, variables should make sense. If you need paragraphs, you basically need more methods. Very rarely that comments in between lines of code are needed.

WHILE building something, I add comments nonstop. It's all todo stuff, or outlines, or pseudocode.

15

u/Bluedragon_00 Mar 22 '22

I'm self teaching coding please don't be too harsh for asking a super 101 question. Method is a new term to me. Methodology on how you should be coding? Or is it a technical function of code I havnt learned yet?

Context: When I first started I had no idea what assigning a variable was. Im currently practicing For Loops.

22

u/[deleted] Mar 22 '22

Method basically reffers to a function that is defined inside of a class / object, you will get to it once you get to around to learning basics of OOP

13

u/Bluedragon_00 Mar 22 '22

Thank you for the reply. And for not judging too harshly.

3

u/Shit_Bananas Mar 23 '22

We've all been there lol

3

u/psioniclizard Mar 23 '22

Anyone who judges you for not knowing what a method is on a reddit for learning to program is not great. From one self taught programmer to another, nothing feels better than helping others understand!

3

u/[deleted] Mar 23 '22

No worries mate! Also try not to worry too much about people judging you on here, from my experience 99% of programmers are happy to help if they know the answer. And the few exceptions are people with a massive superiority complex. Try to just ignore those

→ More replies (1)

4

u/[deleted] Mar 22 '22

It's an OOP concept. A method is a function in an object, usually some sort of behavior.

5

u/Meborg Mar 22 '22

If you're practicing for loops, don't worry about methods too much, although they can be of great help to you!

What a method is, is just some code that does whatever it's supposed to, and can be called from other parts in your code. A good example of a method is the Print(input) method, which writes the "input" argument in the console. Input can be any string that you want, so when you do a Print("ladida123"), you get ladida123 printed in your console.

Anyway a good habit is that your methods do as little as possible, and have a name that describes what they do. So if you have a complicated method that does 20 things (like sort, the first thing most people do is to split up those 20 things into 20 methods, and have the complicated method run those 20 methods sequentially. And then see is you can make them simpler from there.

I hope this made any sense, if you have questions feel free to ask them! Also, what language are you programming in? :)

5

u/Bluedragon_00 Mar 22 '22 edited Mar 22 '22

I'll be honest, I'm not catching on 100%. However, I did a cursory Google search and have some things to read on when I get off work today. Thank you for going into detail.

I'm learning Python. Using Dataquest.io

Edit: I'm just about 100% lurker on this sub as I dont feel like I know enough to ask "good" questions yet. IE Trying to avoid asking stuff like "What language do I choose?" or "How long does it take to learn code?" Etc

10

u/Meborg Mar 22 '22 edited Mar 23 '22

(sorry for formatting, I'm on phone)

First of all, there are no stupid questions. The worst thing that can happen is that people find your question stupid and won't try to answer it. I wouldn't be bothered by what language to use. Everyone has a different opinion, and if you Google for the most used coding languages you get the most popular ones, which are fairly solid.

Python is perfectly fine as a language programming language, the reason I asked is so I can write code examples tailored to your needs. The structure and syntax (and conventions) in a c# application are different than in python, but at the end of the day it does the same: translate what you write into computer language that your pc can use.

Now for methods (can call them functions too), it's kinda like a codeblock that can be reused. If you write a big application that does a lot of stuff, your code monolithically goes from top to bottom. Let's say for any reason, we use a for loop to print numbers from 0 to 4 (that's what our customer pays us money for, so that's what we build).

What we would do initially is make a for loop:

for i in range(5):
    print(i)    

This bit should print 0 till 4 in our console, for a total of 10 printed numbers.

Now, I get the request to also print 0 till 6, and 0 till 2, so I add some new code:

for i in range(5):
    print(i)    

for i in range(7):
    print(i)

for i in range(3):
    print(i)

In these three bits, the only part that changes is the amount of iterations we do based on the number. As we're lazy, we want to combine this code so we can reuse it more often. One of the things we can do is to extract a function.

To do that we think of a function name that describes what we want to do. For example PrintNumbers. Then we write the function as follows

def PrintNumbers(inputName):
    for i in range(inputName):
        print(i)

PrintNumbers(5)
PrintNumbers(7)
PrintNumbers(3)

The def part defines the function. After it is defined, you can call it with the arguments.

Anyway, you can basically do that for everything you can come up with. Add numbers? Make a function that does it:

def AddTwoNumbers(num1, num2):
   return num1 + num2

result = AddTwoNumbers(1,2)

Then call it

→ More replies (1)
→ More replies (1)
→ More replies (8)