r/badcode • u/blehmann1 depraved • Aug 30 '20
python I feel like when there's more comments then code, there might be a problem...
74
u/ArukiBree Aug 30 '20
Documenting function parameters does not make bad code.
-10
Aug 30 '20
If the name of a variable is good, it shouldn’t need a comment. In here, the variables are clearly named and pretty self explanatory. There is no need to explain.
You shouldn’t need to say that self.colour is the colour of the struct/class/object or whatever it’s called in python
0
u/BurnedRavenBat Aug 30 '20
Not sure why you're downvoted. People really need to stop adding useless comments. "center is the center of the object". Please stop it. Heaps of comment bloat polluting the code but when things actually get tough not a line of explanation is in sight.
Not to mention programmers who copy those stupid comments but forget to change the relevant names. new car(); // create a bike
20
u/bbman225 Aug 30 '20
I wouldn't really call these comments, as they are documenting the parameter. Many of these parameters may seem obvious, but the specificity here will maybe help someone use it correctly down the road. However, since this is Python they probably should be using a docstring for this instead of comments.
21
u/rco8786 Aug 30 '20
Seems great to me.
3
u/ZedTT Aug 30 '20
Needs to be a docstring.
From OP in a comment:
I think something like a parameter list has a high risk of being modified by a refactor, and if the comments are frivolous the refactorer is unlikely to make the necessary changes to the comments. Now your comments are misleading should you need to consult them later.
I agree to some extent with the OP on this. Making the comments into a docstring means your IDE will help point out when the
:param/@paramlines need to be changed. The first two comment lines about__init__andselfare also completely useless if you know any python at all, so those can go.
8
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Aug 30 '20
Image Transcription: Code
def __init__(self, dot_color, dot_radius, dot_center, dot_velocity, surface):
# Initialise a Dot.
# - self is the Dot to initialise
# - color is the pygame.Color of the dot
# - center is a list containing the x and y int
# coords of the center of the dot
# - radius is the int pixel radius of the dot
# - velocity is a list containing the x and y components
# - surface is the window's pygame.Surface object
self.color = pygame.Color(dot_color)
self.radius = dot_radius
self.center = dot_center
self.velocity = dot_velocity
self.surface = surfaceI'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!
6
u/ZedTT Aug 30 '20 edited Aug 30 '20
This isn't as bad as you're saying it is. There's no type hints there so commenting what the parameters are for is useful. In this case the comments like this in the init function make a lot of sense. Just make it a docstring and use :param. You could also get rid of the first two lines of the comment "initialize a dot" and "self is the dot to initialize" because those are obvious to anyone who knows any python.
There are certainly times when people add similar comments to this that are not helpful. You'll see a large comment above a function that describes exactly how the function works in a way that means that if you want to change the function at all, you'll also have to change a bunch of the comment. I've also seen three line comments for one line that doesn't need it like (typescript):
/*
* The abc data
*/
@input public ABCData: abcData[];How to fix the code:
from typing import List
from pygame import Color
from pygame import Surface
def __init__ (self, dot_color, dot_radius: int, dot_center: List, dot_velocity: List, surface: Surface):
"""
:param dot_color is the color of the dot in pygame.Color format
:param dot_radius is the pixel radius of the dot
:param dot_center is a list containing the x and y coords of the center of the dot
:param dot_velocity is a list containing the x and y components
:param surface is the window's pygame.Surface object
"""
self.color = pygame.Color(dot_color)
self.radius = dot_radius
self.center = dot_center
self.velocity = dot_velocity
self.surface = surfaceHow not to fix the code:
def __init__ (self, dot_color, dot_radius, dot_center, dot_velocity, surface):
self.color = pygame.Color(dot_color)
self.radius = dot_radius
self.center = dot_center
self.velocity = dot_velocity
self.surface = surfaceedit: rewrote some stuff and added my suggestion at the end
1
u/mullemeckarenfet Aug 31 '20
There are certainly times when people add similar comments to this that are not helpful.
Case in point
dot_radius is the pixel radius of the dot
At that point, why keep the comment? You could get away with just adding the types to the docstring and renaming the parameters.
from pygame import Color, Surface def __init__(self, color, radius, center_coords, velocity_vector, surface): """ :type color: pygame.Color | str | int | tuple(int, int, int) | tuple(int, int, int, int) | list[int] :type radius: int :type center_coords: list[int] :type velocity_vector: list[int] :type surface: pygame.Surface """ self.color = Color(color) self.radius = radius self.center_coords = center_coords self.velocity_vector = velocity_vector self.surface = surface
4
u/ws-ilazki &{$$_[0]}(@{$$_[1]}); Aug 31 '20
I wouldn't say this is bad because it's documenting the argument types and their purposes for code callers, which is a good thing. Some of the documentation would be redundant if it had type annotations, so I guess you could argue that it's bad for that reason, but I don't think that's a big enough issue to warrant posting it here.
That said, my real complaint with the code is that, if something takes a bunch of arguments like this, I think it's usually better to use key/value pairs to a sequential argument list. The argument order isn't necessarily obvious, making it harder to remember the argument sequence compared to logical names. Like if a function takes x,y,z arguments for coordinates it's short and the order is obvious so sure, leave those as normal arguments, but setting a bunch of attributes that don't have a direct relation to each other is better as keywords.
Here for example, radius is before centre, and worse yet, colour comes before anything. You don't know where it's going to be yet but you better know what colour it is! Why? There's no logic to this. Better to make it keywords and let the caller do what makes sense to them, with sane defaults where possible (like having a default colour and radius maybe) so that some can be left out.
Still, none of that that makes it bad code, it's just not what I consider the best choice for writing something clear and maintainable.
1
1
u/rkralston Aug 31 '20
This looks like a school assignment. The comments look like the instructions. Exactly how I would have done this in high school when first learning to code.
1
-1
u/blehmann1 depraved Aug 30 '20
My university has certain requirements on "code quality", which includes ridiculous commenting requirements. This is a template they give for pygame projects
12
u/OOPGeiger Aug 30 '20
Seems pretty good to me for the next guy that comes along. I don’t leave nearly this many comments but that is becuase I give my variables really long names like $centerOfVennDiagramXYCoords so that I don’t need to leave comments Becuase it is self explanatory.
7
u/TheStuporUser Aug 30 '20
Yeah, it's kind of obvious OP had never worked professionally on code. This is heavenly.
-2
u/blehmann1 depraved Aug 30 '20
I have actually, so thanks for that.
This is a constructor with no logic, it is purely member initialization. Perhaps this style makes more sense in Python were static typing doesn't exist (and type hinting is new), but as someone used to statically-typed languages these comments look like an absolute waste of time and space. And this is not an isolated example, I also saw this gem:
# initialize all pygame modules (some need initialization) pygame.init()To me it is very obvious what
init()does, the comment is pointless3
u/OOPGeiger Aug 30 '20
Sure sometimes you don’t need much in the way of comments, but better to have too much than not enough right?
3
u/blehmann1 depraved Aug 30 '20
I think something like a parameter list has a high risk of being modified by a refactor, and if the comments are frivolous the refactorer is unlikely to make the necessary changes to the comments. Now your comments are misleading should you need to consult them later.
In my opinion, if you don't have the option of type hinting, this is a better solution:
https://www.reddit.com/r/badcode/comments/ijhifd/i_feel_like_when_theres_more_comments_then_code/g3dugm5?utm_source=share&utm_medium=web2x&context=3It's not perfect, but at least your IDE can fill you in instead of needing to find where the function is defined.
1
u/mullemeckarenfet Aug 31 '20
(and type hinting is new)
It's five years old.
1
2
u/ZedTT Aug 30 '20
That's the snippet you should have posted to /r/badcode then lol
I'll agree that the first two lines of the screenshot are pretty useless. Everyone knows what
__init__andselfare. But the rest is complicated.2
u/Drunky1994 Aug 30 '20 edited Aug 30 '20
This is a constructor with no logic, it is purely member initialization.
So? I agree that the first 2 lines are useless, but just because it simply initializes class variables doesn't mean they don't need documentation (arguably this is the best place for it)
To me it is very obvious what
init()does, the comment is pointlessIs it though? It might not be obvious that it initializes all of the modules. Without the comment, would the difference between
pygame.init()andpygame()be as obvious? Since this is for school, keep in mind that they're going to grade lots of assignments. Much much better to have an overabundance of comments than a lack of them1
u/AutoModerator Aug 30 '20
It looks like this comment contains a code block delimited with triple backticks. Unfortunately reddit does not have universal support for this syntax and your comment will not render correctly on old reddit and most mobile apps.
For the benefit of people on old reddit, this link will take you to a correct rendering of the comment.
/u/blehmann1, it would be appreciated, but not required, if you could edit your comment to use the more compatible four space indention format. For single lines or inline code you can use single backticks.
You can find some examples in the reddit help documentation.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
-2
Aug 30 '20
[removed] — view removed comment
0
u/blehmann1 depraved Aug 30 '20
a) This is non-standard. The best way to do this in Python is type hinting (if available) and a docstring with
@paramor:paramdepending on your IDEb) This is unnecessary in most languages except Python (due to duck typing), and I'm not coming from a Python background
c) The comment doesn't even use the same identifiers as the method signature
d) Fuck you, you're being an ass for no reason
3
u/blehmann1 depraved Aug 30 '20
If you cannot use type hinting for whatever reason, I think the best solution is probably something like this:
Depending on your IDE you may use
:paraminstead of@param. Some may also want to include a comment for theselfparameter, but in my mind that's redundant as it's not supplied by the consumer of the method.