Tumgik
Text
Bad Code
Last semester, we were assigned to write a few paragraphs on why programmers produce bad code. Our teacher gave us his first reason: The programmer failed to test their code, and provided with his commentary. The assignment was to write about three more reasons in a similar fashion.
I think what I wrote was interesting enough to post here. It's a reflection on what I've learned about programming in the past few years, and the pitfalls associated with writing code.
The programmer failed to include enough program comments.
Comments are most helpful when reviewing old code. The state of mind that one had while writing a piece of code is quickly lost after only a few months. Similarly, they are useful to other people reviewing the code, as they provide orientation to someone who has never seen that code before. Comments also help explain obscure or complex lines of code that do unexpected things. Finally, comments at the top of a file or subroutine help describe what a block of code should do, and define a contract describing how it interacts with other code.
Program comments should be written carefully. When there are too few comments, the reader may feel disoriented and even intimidated by a heaping mass of code. When there are too many, even the most trivial lines of code are commented; preferably, code should be self-documenting, so that its intent is obvious at a glance, and does not require a comment to explain it.
The programmer did not organize or compartmentalize their code well.
When dealing with larger and more complex codebases, a clear pattern for where code should be located is necessary, preventing it from being placed haphazardly throughout the project. A lack of organization means that the program will be difficult to comprehend and maintain. In addition, without compartmentalizing code, the programmer tends to create messy code that tries to do many things at once that will be difficult to fix if the program must later be changed.
Several different strategies for managing code exist, varying with the language and project type; but it is important to keep some semblance of organization within and between program components. One corollary is that global state is to be avoided, because it is difficult to keep track of the interactions of several pieces of code with one variable. Programs should be made of components with a specific purpose that have well-defined interfaces with other pieces of code, both at the function and module level. This makes them cleaner and makes it easier to find problems.
The programmer did not follow best practices.
One very disappointing experience is finding some convoluted approach to solving a problem, then realizing that there is a much cleaner way to solve it. Re-inventing the wheel is a waste of time. Not following best practices also means that others who are more familiar with the language may be confused by the intent of the programmer's code.
Oftentimes a problem has already been solved by people who have experienced it and deliberated it with other people. Usually, a simple Google search suffices to find these cases. Depending on the programming language, best practice guidelines often exist on how to approach certain types of problems in a clean and efficient manner, which are also worth considering.
0 notes
Link
this post was written a few weeks ago.
this is an incredibly fascinating discussion: what's the best language to teach to beginners? i'm a big fan of javascript, but it has far too many annoying quirks that would frustrate beginners. i feel that it's a lot easier to express ideas in python, which would also have been the language i would recommend to others to begin with. but the presence of javascript in every web browser is certainly a valuable motivator, even if unfortunately, it has nothing to do with the quality of the language itself. and then there's our school district, which uses java as an introductory programming language which is just. why. perhaps it's just the curriculum: students end up learning about inheritance and polymorphism before even learning about arrays. in my opinion, not the smoothest introduction!
0 notes
Text
hi! i'm going to try to start writing things about programming here. i had something similar on Blogger a while ago, but i deleted it eventually. mainly it's just because i want to write things, and programming is one of the only things i really feel comfortable writing about, even if no one ever finds this blog. shrug.
anyway, thanks for reading!
1 note · View note