r/AskComputerScience • u/DatabaseEmbrace • Nov 17 '18
As a newbie with programming, what conventions for making larger scale projects should I know so I do them from the start?
32
u/icendoan Nov 17 '18
Always use source control (git, usually).
Always leave the master branch in a working state - change things in other branches, and merge them in.
5
u/DatabaseEmbrace Nov 17 '18
never really thought to use git even in smaller personal projects, but thinking about it, and the issues I usually hit, this is really a great suggestion, thank you :)
3
u/wischichr Nov 18 '18
Use git even if you are alone. You can use simpler "branching policies" when you work alone or the project is just a prototype.
It will help you get used to it and prevent fuck ups. Version control is you insurance for many things.
And as a side effect you can stop commenting out code - I know you do this because everyone without version control does that ;-)
2
u/DatabaseEmbrace Nov 18 '18
XD ya hit the nail on the head with that one, I don't think I have a single project that doesn't at least have one block of code fully commented out in case I need to revert to it lol
27
u/that-one-redditor Nov 17 '18
Get in the habit of starting every function with a description of every parameter and what is returned, minimum. This will force you to have a good understanding of what a function is supposed to do before you write it, and will make reading your code way easier.
4
u/DatabaseEmbrace Nov 17 '18
I take this one to heart a lot because of how often I look at other people's code and just see functions with a comment above that tells me nothing more about the function than the name of it. Thank you!
12
u/Zenai Nov 17 '18
could not disagree more with this as a general rule. functions should have descriptive names for arguments and descriptive names of the functions. so descriptive that comments should be redundant. this cannot always be the case but its what we should strive for. naturally readable code.
4
u/that-one-redditor Nov 17 '18
I agree that parameter names should be as descriptive as possible, but IMO self-documenting variable names can almost never contain as much information as even a short sentence describing its type and other assumptions made about ones input.
4
u/Zenai Nov 17 '18
it depends how complex your functions are and how many things they do. if they do one thing and that thing is in the function name, that is perfectly readable without comments.
1
u/Tynach Nov 18 '18
Something may seem to be simple and just do one thing, and that one thing be in the name, and thus self documenting, to you... But that doesn't mean it's that way for everyone else.
If I write a colorspace conversion library in C++, and I decide to create this function declaration:
vec3 LabToXYZ(vec3 labColor);This makes perfect sense to me. But I know that my goals are to treat all color types as the same (rather than having a class/struct for each colorspace), I know what the L*a*b* and XYZ colorspaces are, and I know that L*a*b* is a non-linear transformation of the XYZ colorspace that was an attempt at making a colorspace that was perceptually uniform.
You might not know what half of that even means, and might assume
Labmeans some sort of laboratory, or maybe color processing thing. You might think XYZ means some sort of plotting function.And these would be perfectly reasonable assumptions because those words indeed tend to mean something else in other contexts. So I should definitely have a comment explaining what it does, what
labColoractually is, and what it will be translated to (brief explanation that it is converted to the XYZ colorspace, which indirectly informs the user of the library that XYZ is a colorspace and not a 3D plotting function).1
u/Zenai Nov 18 '18
if the name needs to have context in order to understand it as its read, it is not readable and therefore deserves a comment. this was addressed in my original comment, sometimes it makes sense to add comments. in my view this is the exception, and the rule is that you should write readable code.
1
u/Tynach Nov 18 '18
Why not both? Especially in this day and age where code documentation is often generated from specially formatted comments above function, class, etc. declarations, I feel like it's important to get in the habit of both writing 'self-documented' code, and properly documenting it with comments anyway. If you do both, you cover all your bases from the start.
1
u/Zenai Nov 18 '18
because its a waste of time. if you wanted to write redundant comments then you could run some automation step in your linter that did it for you.
it also makes comments less meaningful since they are everywhere the reader will tend to read them until they realize most of them are useless, then they'll go overlooked.
0
u/Tynach Nov 18 '18
Why would it be a waste of time, if you have to write the documentation somewhere anyway? Documentation can include example scenarios for when a function would be useful, how it's intended to be used, what functions it might call, etc. Besides, when compared to writing the code and figuring out how to write it to begin with, writing the documentation really doesn't take that long.
1
u/Zenai Nov 18 '18
You will not have to write the documentation anywhere if the code is readable. That's my point.
→ More replies (0)0
5
Nov 17 '18
I really agree with this. Clear contracts (parameters and return values) help spell out what you "think" a function should be doing...and they are a snapshot of the other parts of your program that feed into and out of your function. If your codebase changes, it might be that your function stops behaving how you want it to, and at that point, you can see whether some of your assumptions from your contract are no longer valid.
I'm also a really big fan of unit testing.
8
u/LiverReich Nov 17 '18
Maybe look into Object Oriented Programming and some other design paradigms. Try to keep your code nice looking, use consistent naming, run away from magic numbers and global variables. And comment your code like the person reading it is a murderer and knows where you live.
4
u/DatabaseEmbrace Nov 17 '18
I know of OOP, but I'm not too familiar with the design paradigms that go with it, so I'll have to look into that for sure, thank you. Why run away from global variables though? From all of my self teaching, global variables seem like they're super important, I guess that's the danger of self teaching, you learn some things wrong.
Also I definitely agree with you about commenting, mostly because of how many times I was both the angry person looking at uncommented code AND the person who didn't comment it lol
2
u/LiverReich Nov 18 '18
Having too many global variables could make the code hard to read and debug. No one likes to search for variables that are 1000 lines away from where they're used. Also 10 pieces of code modifying the same value could make the program prone to bugs.
2
u/DatabaseEmbrace Nov 18 '18
ahh ok, thats reasonable, so less of a "run from global variables because they're bad" and more of a "use global variables with caution and try to be minimal with it" then?
1
4
u/Sqeaky Nov 17 '18
Never stop learning. Right now programming and computer science is moving much too fast for any one person to keep up.
For the tools you use professionally, buy books and learn design patterns. there are structures of code that are commonly written in ust about every language and makes sense there. What is a factory, and why does Java need them? What are iterator pairs and why does C++ have them all over the place? Why are there so many anonymous functions in JavaScript? In C# why are inversion of control containers such a big deal?
Learning any language way more than just learning it syntax, it's learning how to apply it most effectively.
I know this advice doesn't apply directly to building a large project, but once you learn about the common design patterns in your language you will have learned how to to solve a kind of problem the people who came before you experienced. Almost all the people who came before you had problems making their own large projects, and the common design-patterns solved enough problems that they became permanent.
2
u/DatabaseEmbrace Nov 18 '18
thank you, that's actually exactly what I am looking for, to learn as much as possible from other people's learning experiences
8
Nov 18 '18
design design design before writing a single line of code
commit often, because you'll do a lot before you realize something is wrong, and it's easier to go back in baby steps than big leaps...
learn to take criticism, and ask people for their opinion on your code often.
1
Nov 18 '18
Oftentimes the best advice is simple and right there in front of you, and you know exactly what to do, it's just the starting that's difficult.
Great advice!
6
u/FenPhen Nov 18 '18
You asked about larger scale, which means working with others.
Embrace code reviews before submitting code (not afterward). This is an opportunity to teach and to learn for all parties, and reduces bugs. If a reviewer asks a question about a line of code, at a minimum, consider documenting or refactoring so the question needn't be asked in the future.
Establish, follow, and maintain a style guide. The guide should say what is acceptable, not acceptable, and everything else is open for discussion. This saves time by avoiding arguments, prevents bugs and bad patterns, and ramps up new people faster. The team should amend the style guide as needed.
1
u/but_how_do_i_go_fast Nov 19 '18
Input/output: SQL, JSON, private servers, Ajax/jQuery.
Version control and unit testing: You should be able to have tests and benchmarks in place for your entire ecosystem. And tested via a single command.
Work on projects with other people: communication and collaboration is a skill they didn't teach in University for me as a mandate.
1
u/DatabaseEmbrace Nov 19 '18
Is there anywhere you know of that one can learn project communication and collaboration without it just being experience and learning by doing?
1
u/but_how_do_i_go_fast Nov 19 '18
I always am practicing git by writing/coding on branches and merging them in, with a long wind of commit messages. Unfortunately, learning the communication skills is something that takes time and usually starts at a younger age.
My best form of communication skills were taught through debate and a communication skills course ( high school).
To teach it with comp sci, put them in groups of four, have an assignment due, and 50% of the grade is peer review.
Or, ask them to fill in the commits for a project log, with noticed changes as they go. Then, ask them to amend and write a main README.txt. This should be like a quiz. When reviewing, make sure pronouns do not exist, make sure new functions and functionality are well defined, and make sure dependencies are listed, deprecated support is mentioned, etc.
A final idea would be to teach them JSDoc and emphasize:
Functions and class methods
Objects
Closures
Object properties
Requires
Events
File headers
2
u/shawnadelic Nov 17 '18 edited Nov 18 '18
Read source code for open source projects you are interested in, especially anything you’re actually using (frameworks, etc). Hopefully you’ll see some good examples from which to draw.
Most of the little stuff you’ll have to learn along the way. I could tell you “write lots of tests” or something, but generally stuff like that you need to do things the hard way before you see the real value.
2
Nov 18 '18
Automated testing. That means using an off-the-shelf CI like travis or the one that it built in to gitlab or often something on-site.
If I join a project and it doesn't have automated testing or even worse, has shitty tests, that's typically the first thing that I force upon people and generally it improves things a lot.
2
u/TomBob42369 Nov 18 '18
Comments lots of comments that are actual useful.
0
Nov 18 '18
[deleted]
1
u/TomBob42369 Nov 18 '18
Right but if you change code comments should be updated as well. If not that is just sloppy work. Proper naming of variables and functions is true it is nice to have comments when working with a team. You can not assume everyone knows what you are thinking.
0
Nov 18 '18
[deleted]
1
u/TomBob42369 Nov 18 '18
I mean if you get a shitty developer who dose not like to comment then you are right. If have any experience with working on an actual development team the you would know that your team mate really appreciate proper comments. If you are working by yourself then by all means do whatever you like. Plus many development teams have required conventions such as good commenting. It take time to read other teammates code. Time is money after all.
2
1
u/jirocket Nov 29 '18
Have coding standards, from syntactical standards to naming conventions, and enforce those standards with a tool like a linter. If there are already coding standards, then adhere to them.
0
Nov 18 '18
[deleted]
2
u/DatabaseEmbrace Nov 18 '18
I think that is a fantastic start, and probably some of the best learning advice anyone has given me. You sound like a great teacher. Thank you so much!
0
Nov 18 '18
[deleted]
1
u/DatabaseEmbrace Nov 18 '18
ever consider taking on students or doing some kind of apprenticeship thing?
34
u/[deleted] Nov 17 '18
Write tests. Be unlike your felloe CS students and learn about testing early.