The Elements of Programming Style, 2nd Edition
All
Stack Overflow 13
This Year
Stack Overflow 1
This Month
Stack Overflow 1
You don't need to understand the code; you don't need to step through the code. You follow the steps for "extract method" and you go from a working state to another working state with no worries.
The individual refactorings are more-or-less interesting, but as others have said, they're somewhat commonplace now.
Two books that every programmer should read: Software Tools[1] and The Elements of Programming Style[2] by Kernighan and Plauger. Bonus: The Unix Programming Environment[3] by Kernighan and Pike.
[1] https://www.amazon.com/Software-Tools-Pascal-Brian-Kernighan...
[2] https://www.amazon.com/Elements-Programming-Style-2nd/dp/007...
[3] https://www.amazon.com/Unix-Programming-Environment-Prentice...
Facts:
1. GIT and other version controls systems treat white-space differently
Based on my experience, we faced on our projects: GIT and other version controls systems treat invisible
spaces
+TABS
differently, and it leads to changes in lines, which actually haven't been affected. It's easy not to notice, when there will accidentally added onespace
+TAB
= indent looks the same in IDE, but GIT will make the difference when merging. It damages your ability to effectively compare revisions in source control, which is really scary. It never going to happen when you are havingspaces
only.2. Neutralize difference in collaborator's environment (Editor, OS, preferences, etc.)
The tab width (in spaces) depends on your environment (text editor, OS, preferences, etc.), but the space width is the same everywhere. IDEs are smart enough to treat white spaces up to you personal taste, but the output generated for collaboration should be up to standards.
3. Developers who use spaces make more money than those who use tabs
Using spaces instead of tabs is associated with an 8.6% higher salary. Using spaces instead of tabs is associated with as high a salary difference as an extra 2.4 years of experience. (source: Stack Overflow 2017 Developer Survey).
4. Numerous studies on coding style importance
If every collaborator on your project would keep the same standards on coding - it will be good in long run, collaboration is more efficient and professional, the same indent when you refactor or develop. Studies regarding that:
For example, Ben Shneiderman confirmed this in Exploratory experiments in programmer behavior:
An old 1984 study by Soloway and Ehrlich cited in Code Complete, and supported studies from The Elements of Programming Style:
In the example, the value 1.5F has an exact representation in IEEE 754 (and pretty much any other conceivable binary or decimal floating point representation), so the answer is almost certainly going to be yes. However, there is no guarantee, and there could be compilers which do not manage to achieve the result.
If you change the value to one without an exact binary representation, such as 5.1F, the result is far from guaranteed.
Way, way, way back in their excellent classic book "The Elements of Programming Style", Kernighan & Plauger said:
(It's one of two phrases in the book that I highlighted many years ago1.)
They also observe:
Those observations were made in 1978 (for the second edition), but are still fundamentally valid today.
If the question is viewed at its most extremely restricted scope, you may be OK. If the question is varied very much, you are more likely to be bitten than not, and you'll probably be bitten sooner rather later.
1 The other highlighted phrase is (minus bullets):
Basically, because the decimal number 0.01 does not have an exact representation in binary floating point, so over time, adding the best approximation to 0.01 deviates from the answer you'd like.
This is basic property of (binary) floating point arithmetic and not peculiar to Perl. What Every Computer Scientist Should Know About Floating-Point Arithmetic is the standard reference, and you can find it very easily with a Google search.
See also: C compiler bug (floating point arithmetic) and no doubt a myriad other questions.
Kernighan & Plauger say, in their old but classic book "The Elements of Programming Style", that:
They also say:
Both sayings point out that floating point arithmetic is not precise.
Note that some modern CPUs (IBM PowerPC) have IEEE 754:2008 decimal floating point arithmetic built-in. If Perl used the correct types (it probably doesn't), then your calculation would be exact.
Sometimes if you can't come up with a good function name it's an indication that the function doesn't have a nice, crisp focus and needs to be refactored. If it's a class method, perhaps the class needs refactoring too.
But it's well worth the trouble finding the best possible names, since it makes your code so much more understandable and usable.
Update: Many software engineering authors have talked about the importance of naming. Henry F. Ledgard's Programming Proverbs (1975) and Brian Kernighan and P.J. Plaugher's Elements of Programming Style (1978) were early ones and are still worth reading. Steve McConnell's wonderful Code Complete (2nd Edition, 2005) is a more recent example, devoting an entire chapter to the topic.
Elements of Programming Style was in part patterned on Strunk and White's Elements of Style, which actually has a surprising relevance. Their stress on making prose clear and extremely concise applies to our technical writing and comments (and naming), but I've always seen it as analogous to what we do when we refactor and improve our code.
Your code seems to prompt for the patron name on each iteration of the loop, which is an unusual way of organizing things. It should probably prompt for the names outside the loop. You should also error check the calls to
scanf()
:We might need to see how you create the list of patrons within the library. It could be that you are storing the names in the same space each time, or something similar.
Your function always returns 0; there is no point that. Either it should not return any value (
void
) so you don't have to check what it returns, or you should make it return astruct Patron *
for the found patron, or NULL (0) if there is no matching patron.Your structures seem to me unexpectedly deeply nested. This fragment of code compiles, but I've not spent the effort to populate the list. However, providing 5 structures is 2 or 3 levels deeper than I'd expect. It definitely complicates your life.
I note I had to invent two structure names since they were not shown in your source code. At some point, you should look up the Law of Demeter and work out how to avoid violating it quite so flagrantly in your code.
For example:
For the purpose at hand, it doesn't matter whether you compare first names first or last names first. In general, if you want to sort the data (for example), you would need to know which way to order them. I'm using the case-sensitive search you used; again, you might want to think about using a case-insensitive search instead. You can do that more easily when your comparisons are nicely separated and isolated as shown here.
Notice that this avoids repetition in your code. Kernighan and Plauger summarized it neatly in their book 'The Elements of Programming Style':
Then in your 'find_patron()` function:
This is better; not fully clean, but better. A more realistic function would take in the name of the patron to be found and would search for that name in the list; you don't mix I/O operations such as reading the patrons name with searching operations. That is mixing up two very different operations.