c++ - Program feedback: named loop index and reference to constant data -


i had class assignment teacher took off points lot of things agree with, couple of them i'm not sure about.

"i not fan of naming variables i, when in fact counter represents real thing, port number:"

"it better call portnum, or like."

in response following lines:

for(int = 1; <= boat.getlastport(); ++i){     boat.load();     boat.move(i);     boat.unload(); } 

my first question is, sound advice, or not worthwhile? see named loop variable. thought people instantly know what's happening in situation 1 (in context of rest of program).


second , last feedback:

"first, method names need start lowercase letters, not uppercase. strong convention in oop. worse, there no reason pass in range value reference. requested value returned, how methods work each other." --- "...no program wants call class method , pass in reference: never trust class program variables. use pass-by-value in methods."

this quote refers member function:

int ferryboat::rand(const int& range) {     return rand() % (range+1); } 

the member function required assignment, otherwise have made macro outside of class or something. instructions didn't specify name named after library function, guess should have named random wouldn't have had capital first letter.

anyway second question is:
if function takes reference const data (const variable sounds oxymoronic), why wouldn't trust it? can't not alter data because of constness?

i suck , move on :-) if teacher borderline psycho, job pass. once you're out of school, can follow own guidelines if they're better (subject company rules , regulations of course).

i have agree first sentiment since boat.move(portnum) conveys more information boat.move(i).

even better boat.moveto(portnum) or boat.movetoportnumber(i).

probably not too bad if 3 lines there doesn't cost "right" small sample. given how time people spend maintaining code, more readable is, better.

the second one, think teacher has concentrated on wrong thing. it's not whether can corrupted, it's there's absolutely no point , no advantage passing int reference unless want modify in function.

references in c++ avoid c problems encountered when having emulate pass-by-reference pointers , minimise copying of large structures. in fact, hope iso adds them next iteration. but, if it's variable that's cheap copy (like int) , don't want change variable , have reflected caller, references waste of time.

i disagree method names need start lower-case letters. while common convention (one use myself), it's not requirement of oop. code doesn't become less object-oriented because method called isteacherloony() rather isteacherloony().


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -