Wednesday, February 5, 2014

Coding style

    This slog http://slogslogslogslog.blogspot.ca/2014/01/week-4-inheritance-exceptions-and.html is quite intriguing to me. It talks mostly about the importance of writing syntactically correct programs, and I believe this plays a huge role in writing programs. Different programming languages have their own syntax so it is essential to learn the correct syntax at the very beginning and then apply the correct syntax. Otherwise, obviously, ‘Syntax Error’ will prevent any program from running and the logic behind it could never be implemented. In addition to syntax, I would like to talk more on coding style. A good coding style is something that makes codes easier to read, easier to follow, and easier to understand. Think about the following example.







    Both function return the power three of x and syntactically correct but the first function is, under most circumstances, prohibited. There are two reasons: First: the function header shows nothing about what the function will do. (Who knows what will ‘dosomething’ do). Second: Writing function body just after the function header does not show the block of the function which makes it harder to read when the function body became longer and longer. So the second function, clearly has a better coding style: clear header, apparent blocks and is taught as a standard way by almost all teachers. So like assignment1, when my group and I start working on that, the very first thing is to set up a consistent coding style, so that everyone could understand what others are writing more efficiently and can add codes without too much modification. Like the initialization of TOAHModel, we set the names of the attributes of the objects as ‘stools, cheeses, etc.’ So when other members of the group and TA wants to read it, it is obviously much more understandable than simply putting ‘a,b,c’ as attributes.
To sum up, a good coding style saves a lot of time and makes both programmer and debugger easier to understand and trace the program, so it is important to have correct syntax but more important to have a good coding style.

No comments:

Post a Comment